Skip to content

Commit

Permalink
Introduce code formatting + git hooks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
robhanlon22 committed Nov 30, 2023
1 parent 0b44a67 commit 1c34510
Show file tree
Hide file tree
Showing 12 changed files with 997 additions and 744 deletions.
32 changes: 16 additions & 16 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
name: Continuous Testing

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
push:
branches: ["main"]
pull_request:
branches: ["main"]

permissions:
contents: read
contents: read

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v3
with:
java-version: '8'
distribution: 'temurin'
- uses: gradle/gradle-build-action@v2
- run: gradle wrapper
- run: ./gradlew test
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v3
with:
java-version: "11"
distribution: "temurin"
- uses: gradle/gradle-build-action@v2
- run: gradle wrapper
- run: ./gradlew check
4 changes: 4 additions & 0 deletions .greclipse.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
groovy.formatter.remove.unnecessary.semicolons = true
org.eclipse.jdt.core.formatter.indent_empty_lines = false
org.eclipse.jdt.core.formatter.tabulation.char = space
org.eclipse.jdt.core.formatter.tabulation.size = 4
14 changes: 14 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
repos:
- repo: local
hooks:
- id: spotless
name: spotless
language: system
entry: ./gradlew spotlessApply
pass_filenames: false
always_run: true
- id: git-diff
name: git-diff
language: system
entry: git --no-pager diff --color --exit-code
always_run: true
4 changes: 4 additions & 0 deletions .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
plugins:
- prettier-plugin-java
- prettier-plugin-properties
tabWidth: 4
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# CHANGELOG

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

- Initial implementation of [the spec](https://github.com/sqids/sqids-spec)
- Packaging & cleanup
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
# [Sqids Java](https://sqids.org/java)

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

Features:

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

## 🧰 Use-cases

Good for:

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

Not good for:

- Sensitive data (this is not an encryption library)
- User IDs (can be decoded revealing user count)

- Sensitive data (this is not an encryption library)
- User IDs (can be decoded revealing user count)

## System Requirements
Java 8 or higher is required.

Java 8 or higher is required.

## 🚀 Getting started

Download and Import Sqids via:

```java
import org.sqids.Sqids;

```

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

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

## 📝 License

[MIT](LICENSE)
[MIT](LICENSE)
55 changes: 44 additions & 11 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
plugins {
id 'com.diffplug.spotless' version '6.23.1'
id 'com.github.node-gradle.node' version '7.0.1'
id 'java-library'
id 'maven-publish'
id 'signing'
Expand All @@ -7,7 +9,7 @@ plugins {
group = 'org.sqids'
version = '0.1.0-SNAPSHOT'

String rootArtifactiId = 'sqids'
String rootArtifactId = 'sqids'
String projectUrl = 'https://sqids.org/java'

repositories {
Expand All @@ -17,7 +19,7 @@ repositories {
dependencies {
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

test {
Expand All @@ -34,20 +36,19 @@ java {

publishing {
publications {

mavenJava(MavenPublication) {
groupId = group
artifactId = rootArtifactiId
artifactId = rootArtifactId
version = version
from components.java
pom {
name = 'Sqids'
description = 'Generate short YouTube-looking IDs from numbers.'
url = projectUrl
properties = [
"parent.groupId": "org.sonatype.oss",
"parent.artifactId": "oss-parent",
"parent.version": "7"
'parent.groupId': 'org.sonatype.oss',
'parent.artifactId': 'oss-parent',
'parent.version': '7'
]
licenses {
license {
Expand All @@ -72,12 +73,12 @@ publishing {
}
repositories {
maven {
def releasesRepoUrl = 'https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/'
def snapshotsRepoUrl = 'https://s01.oss.sonatype.org/content/repositories/snapshots/'
String releasesRepoUrl = 'https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/'
String snapshotsRepoUrl = 'https://s01.oss.sonatype.org/content/repositories/snapshots/'
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
credentials {
username "${System.getenv("SONATYPE_USERNAME")}"
password "${System.getenv("SONATYPE_PASSWORD")}"
username System.getenv('SONATYPE_USERNAME')
password System.getenv('SONATYPE_PASSWORD')
}
}
}
Expand All @@ -86,3 +87,35 @@ publishing {
sign publishing.publications.mavenJava
}
}

node {
download = true
}

String nodeSetup = 'nodeSetup'
String npmSetup = 'npmSetup'

spotless {
format 'prettier', {
target '**/*.java', '**/*.properties', '**/*.md', '**/*.yml', '**/*.yaml'
targetExclude 'bin', 'gradle'

boolean isWindows = System.getProperty('os.name').toLowerCase().contains('windows')
String nodeExec = isWindows ? '/node.exe' : '/bin/node'
String npmExec = isWindows ? '/npm.cmd' : '/bin/npm'

prettier(['prettier': '3.1.0', 'prettier-plugin-java': '2.2.0', 'prettier-plugin-properties': '0.3.0'])
.npmInstallCache()
.npmExecutable("${tasks.named(npmSetup).get().npmDir.get()}${npmExec}")
.nodeExecutable("${tasks.named(nodeSetup).get().nodeDir.get()}${nodeExec}")
.configFile('.prettierrc.yaml')
}

groovyGradle {
greclipse().configFile('.greclipse.properties')
}
}

tasks.named('spotlessPrettier').configure { task ->
task.dependsOn(nodeSetup, npmSetup)
}
Loading

0 comments on commit 1c34510

Please sign in to comment.