Skip to content

Commit d97cd0e

Browse files
committed
Introduce code formatting + git hooks
Adds the spotless gradle plugin for a formatting abstraction; prettier for formatting Java, Java properties, YAML, and Markdown; and pre-commit to tie the aforementioned into a pre-commit hook.
1 parent 0b44a67 commit d97cd0e

12 files changed

+997
-744
lines changed

.github/workflows/test.yml

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
name: Continuous Testing
22

33
on:
4-
push:
5-
branches: [ "main" ]
6-
pull_request:
7-
branches: [ "main" ]
4+
push:
5+
branches: ["main"]
6+
pull_request:
7+
branches: ["main"]
88

99
permissions:
10-
contents: read
10+
contents: read
1111

1212
jobs:
13-
test:
14-
runs-on: ubuntu-latest
15-
steps:
16-
- uses: actions/checkout@v4
17-
- uses: actions/setup-java@v3
18-
with:
19-
java-version: '8'
20-
distribution: 'temurin'
21-
- uses: gradle/gradle-build-action@v2
22-
- run: gradle wrapper
23-
- run: ./gradlew test
13+
test:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
- uses: actions/setup-java@v3
18+
with:
19+
java-version: "11"
20+
distribution: "temurin"
21+
- uses: gradle/gradle-build-action@v2
22+
- run: gradle wrapper
23+
- run: ./gradlew check

.greclipse.properties

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
groovy.formatter.remove.unnecessary.semicolons = true
2+
org.eclipse.jdt.core.formatter.indent_empty_lines = false
3+
org.eclipse.jdt.core.formatter.tabulation.char = space
4+
org.eclipse.jdt.core.formatter.tabulation.size = 4

.pre-commit-config.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
repos:
2+
- repo: local
3+
hooks:
4+
- id: spotless
5+
name: spotless
6+
language: system
7+
entry: ./gradlew spotlessApply
8+
pass_filenames: false
9+
always_run: true
10+
- id: git-diff
11+
name: git-diff
12+
language: system
13+
entry: git --no-pager diff --color --exit-code
14+
always_run: true

.prettierrc.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
plugins:
2+
- prettier-plugin-java
3+
- prettier-plugin-properties
4+
tabWidth: 4

CHANGELOG.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# CHANGELOG
22

33
**v0.1.0:**
4-
- Initial implementation of [the spec](https://github.com/sqids/sqids-spec)
5-
- Packaging & cleanup
4+
5+
- Initial implementation of [the spec](https://github.com/sqids/sqids-spec)
6+
- Packaging & cleanup

README.md

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
# [Sqids Java](https://sqids.org/java)
22

3-
[Sqids](https://sqids.org/java) (*pronounced "squids"*) is a small library that lets you **generate unique IDs from
3+
[Sqids](https://sqids.org/java) (_pronounced "squids"_) is a small library that lets you **generate unique IDs from
44
numbers**. It's good for link shortening, fast & URL-safe ID generation and decoding back into numbers for quicker
55
database lookups.
66

77
Features:
88

9-
- **Encode multiple numbers** - generate short IDs from one or several non-negative numbers
10-
- **Quick decoding** - easily decode IDs back into numbers
11-
- **Unique IDs** - generate unique IDs by shuffling the alphabet once
12-
- **ID padding** - provide minimum length to make IDs more uniform
13-
- **URL safe** - auto-generated IDs do not contain common profanity
14-
- **Randomized output** - Sequential input provides nonconsecutive IDs
15-
- **Many implementations** - Support for [40+ programming languages](https://sqids.org/)
9+
- **Encode multiple numbers** - generate short IDs from one or several non-negative numbers
10+
- **Quick decoding** - easily decode IDs back into numbers
11+
- **Unique IDs** - generate unique IDs by shuffling the alphabet once
12+
- **ID padding** - provide minimum length to make IDs more uniform
13+
- **URL safe** - auto-generated IDs do not contain common profanity
14+
- **Randomized output** - Sequential input provides nonconsecutive IDs
15+
- **Many implementations** - Support for [40+ programming languages](https://sqids.org/)
1616

1717
## 🧰 Use-cases
1818

1919
Good for:
2020

21-
- Generating IDs for public URLs (eg: link shortening)
22-
- Generating IDs for internal systems (eg: event tracking)
23-
- Decoding for quicker database lookups (eg: by primary keys)
21+
- Generating IDs for public URLs (eg: link shortening)
22+
- Generating IDs for internal systems (eg: event tracking)
23+
- Decoding for quicker database lookups (eg: by primary keys)
2424

2525
Not good for:
2626

27-
- Sensitive data (this is not an encryption library)
28-
- User IDs (can be decoded revealing user count)
29-
27+
- Sensitive data (this is not an encryption library)
28+
- User IDs (can be decoded revealing user count)
3029

3130
## System Requirements
32-
Java 8 or higher is required.
3331

32+
Java 8 or higher is required.
3433

3534
## 🚀 Getting started
3635

3736
Download and Import Sqids via:
3837

3938
```java
4039
import org.sqids.Sqids;
40+
4141
```
4242

4343
## 👩‍💻 Examples
@@ -55,7 +55,7 @@ List<Long> numbers=sqids.decode(id); // [1, 2, 3]
5555
> important to your design that IDs are canonical, you have to manually re-encode decoded numbers and check that the
5656
> generated ID matches.
5757
58-
Enforce a *minimum* length for IDs:
58+
Enforce a _minimum_ length for IDs:
5959

6060
```java
6161
Sqids sqids=Sqids.builder()
@@ -87,4 +87,4 @@ List<Long> numbers=sqids.decode(id); // [1, 2, 3]
8787

8888
## 📝 License
8989

90-
[MIT](LICENSE)
90+
[MIT](LICENSE)

build.gradle

+44-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
plugins {
2+
id 'com.diffplug.spotless' version '6.23.1'
3+
id 'com.github.node-gradle.node' version '7.0.1'
24
id 'java-library'
35
id 'maven-publish'
46
id 'signing'
@@ -7,7 +9,7 @@ plugins {
79
group = 'org.sqids'
810
version = '0.1.0-SNAPSHOT'
911

10-
String rootArtifactiId = 'sqids'
12+
String rootArtifactId = 'sqids'
1113
String projectUrl = 'https://sqids.org/java'
1214

1315
repositories {
@@ -17,7 +19,7 @@ repositories {
1719
dependencies {
1820
testImplementation platform('org.junit:junit-bom:5.10.0')
1921
testImplementation 'org.junit.jupiter:junit-jupiter'
20-
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
22+
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
2123
}
2224

2325
test {
@@ -34,20 +36,19 @@ java {
3436

3537
publishing {
3638
publications {
37-
3839
mavenJava(MavenPublication) {
3940
groupId = group
40-
artifactId = rootArtifactiId
41+
artifactId = rootArtifactId
4142
version = version
4243
from components.java
4344
pom {
4445
name = 'Sqids'
4546
description = 'Generate short YouTube-looking IDs from numbers.'
4647
url = projectUrl
4748
properties = [
48-
"parent.groupId": "org.sonatype.oss",
49-
"parent.artifactId": "oss-parent",
50-
"parent.version": "7"
49+
'parent.groupId': 'org.sonatype.oss',
50+
'parent.artifactId': 'oss-parent',
51+
'parent.version': '7'
5152
]
5253
licenses {
5354
license {
@@ -72,12 +73,12 @@ publishing {
7273
}
7374
repositories {
7475
maven {
75-
def releasesRepoUrl = 'https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/'
76-
def snapshotsRepoUrl = 'https://s01.oss.sonatype.org/content/repositories/snapshots/'
76+
String releasesRepoUrl = 'https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/'
77+
String snapshotsRepoUrl = 'https://s01.oss.sonatype.org/content/repositories/snapshots/'
7778
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
7879
credentials {
79-
username "${System.getenv("SONATYPE_USERNAME")}"
80-
password "${System.getenv("SONATYPE_PASSWORD")}"
80+
username System.getenv('SONATYPE_USERNAME')
81+
password System.getenv('SONATYPE_PASSWORD')
8182
}
8283
}
8384
}
@@ -86,3 +87,35 @@ publishing {
8687
sign publishing.publications.mavenJava
8788
}
8889
}
90+
91+
node {
92+
download = true
93+
}
94+
95+
String nodeSetup = 'nodeSetup'
96+
String npmSetup = 'npmSetup'
97+
98+
spotless {
99+
format 'prettier', {
100+
target '**/*.java', '**/*.properties', '**/*.md', '**/*.yml', '**/*.yaml'
101+
targetExclude 'gradle/**/*'
102+
103+
boolean isWindows = System.getProperty('os.name').toLowerCase().contains('windows')
104+
String nodeExec = isWindows ? '/node.exe' : '/bin/node'
105+
String npmExec = isWindows ? '/npm.cmd' : '/bin/npm'
106+
107+
prettier(['prettier': '3.1.0', 'prettier-plugin-java': '2.2.0', 'prettier-plugin-properties': '0.3.0'])
108+
.npmInstallCache()
109+
.npmExecutable("${tasks.named(npmSetup).get().npmDir.get()}${npmExec}")
110+
.nodeExecutable("${tasks.named(nodeSetup).get().nodeDir.get()}${nodeExec}")
111+
.configFile('.prettierrc.yaml')
112+
}
113+
114+
groovyGradle {
115+
greclipse().configFile('.greclipse.properties')
116+
}
117+
}
118+
119+
tasks.named('spotlessPrettier').configure { task ->
120+
task.dependsOn(nodeSetup, npmSetup)
121+
}

0 commit comments

Comments
 (0)