Skip to content

Commit

Permalink
Add pre-commit hooks (#17)
Browse files Browse the repository at this point in the history
* Setup pre-commit

* Run precommit hook across all files

* Update README.md
  • Loading branch information
rcoh authored Nov 5, 2020
1 parent 1d30ad0 commit 86dc5db
Show file tree
Hide file tree
Showing 32 changed files with 232 additions and 188 deletions.
178 changes: 89 additions & 89 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,105 +7,105 @@ jobs:
name: Kotlin style checks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK
uses: actions/setup-java@v1
with:
java-version: 8
- uses: actions/cache@v2
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: ktlint
run: ./gradlew ktlint
- uses: actions/checkout@v2
- name: Set up JDK
uses: actions/setup-java@v1
with:
java-version: 8
- uses: actions/cache@v2
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: ktlint
run: ./gradlew ktlint
unit-tests:
name: Codegen unit tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
restore-keys: |
${{ runner.os }}-gradle-
- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: Set up JDK
uses: actions/setup-java@v1
with:
java-version: 8
- name: test
run: ./gradlew :codegen:test
- uses: actions/checkout@v2
- uses: actions/cache@v2
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
restore-keys: |
${{ runner.os }}-gradle-
- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: Set up JDK
uses: actions/setup-java@v1
with:
java-version: 8
- name: test
run: ./gradlew :codegen:test
integration-tests:
name: Codegen integration tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
name: Gradle Cache
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
restore-keys: |
${{ runner.os }}-gradle-
- uses: actions/cache@v2
name: Cargo Cache
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: Set up JDK
uses: actions/setup-java@v1
with:
java-version: 8
- name: integration-tests
run: ./gradlew :codegen-test:test
- uses: actions/upload-artifact@v2
name: Upload Codegen Output for inspection
- uses: actions/checkout@v2
- uses: actions/cache@v2
name: Gradle Cache
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
restore-keys: |
${{ runner.os }}-gradle-
- uses: actions/cache@v2
name: Cargo Cache
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: Set up JDK
uses: actions/setup-java@v1
with:
java-version: 8
- name: integration-tests
run: ./gradlew :codegen-test:test
- uses: actions/upload-artifact@v2
name: Upload Codegen Output for inspection
# Always upload the output even if the tests failed
if: ${{ always() }}
with:
name: codegen-output
path: |
codegen-test/build/smithyprojections/codegen-test/*/rust-codegen/
codegen-test/build/smithyprojections/codegen-test/Cargo.toml
!**/target
if: ${{ always() }}
with:
name: codegen-output
path: |
codegen-test/build/smithyprojections/codegen-test/*/rust-codegen/
codegen-test/build/smithyprojections/codegen-test/Cargo.toml
!**/target
runtime-tests:
name: Rust runtime tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: execute runtime tests
run: ./rust-runtime/test.sh
- uses: actions/checkout@v2
- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: execute runtime tests
run: ./rust-runtime/test.sh
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: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: v1.6.1
hooks:
- id: pretty-format-kotlin
args: [--autofix, --ktlint-version, 0.39.0]
- id: pretty-format-yaml
args: [--autofix, --indent, '2']
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ Smithy code generators for Rust

## Setup
1. `./gradlew` will setup gradle for you
2. Running tests requires a working Rust installation. See [Rust docs](https://www.rust-lang.org/learn/get-started) for
2. Running tests requires a working Rust installation. See [Rust docs](https://www.rust-lang.org/learn/get-started) for
installation instructions on your platform.

## Run tests
## Run tests
```./tesh.sh```

This will run all the unit tests, codegen an example model end-to-end and validates that the generated code compiles.
This will run all the unit tests, codegen an example model end-to-end and validates that the generated code compiles.

## Development
For development, pre-commit hooks may be useful. Setup:
```
brew install pre-commit # (or appropriate for your platform: https://pre-commit.com/)
pre-commit install
```
6 changes: 3 additions & 3 deletions codegen-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ fun generateSmithyBuild(tests: List<CodegenTest>): String {
val projections = tests.joinToString(",\n") {
"""
"${it.module}": {
"plugins": {
"rust-codegen": {
"plugins": {
"rust-codegen": {
"runtimeConfig": {
"relativePath": "${rootProject.projectDir.absolutePath}/rust-runtime"
},
Expand All @@ -44,7 +44,7 @@ fun generateSmithyBuild(tests: List<CodegenTest>): String {
"build": {
"rootProject": true
}
}
}
}
}
""".trimIndent()
Expand Down
2 changes: 1 addition & 1 deletion codegen-test/model/dynamodb.json
Original file line number Diff line number Diff line change
Expand Up @@ -7799,4 +7799,4 @@
}
}
}
}
}
2 changes: 1 addition & 1 deletion codegen-test/model/ebs.json
Original file line number Diff line number Diff line change
Expand Up @@ -1347,4 +1347,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ sealed class RustType {
}

fun RustType.render(): String = when (this) {
is RustType.Bool -> this.name
is RustType.Float -> this.name
is RustType.Integer -> this.name
is RustType.String -> this.name
is RustType.Vec -> "${this.name}<${this.member.render()}>"
is RustType.HashMap -> "${this.name}<${this.key.render()}, ${this.value.render()}>"
is RustType.HashSet -> "${this.name}<${this.member.render()}>"
is RustType.Reference -> "&${this.lifetime?.let { "'$it" } ?: ""} ${this.value.render()}"
is RustType.Option -> "${this.name}<${this.value.render()}>"
is RustType.Box -> "${this.name}<${this.value.render()}>"
is RustType.Opaque -> this.name
is RustType.Bool -> this.name
is RustType.Float -> this.name
is RustType.Integer -> this.name
is RustType.String -> this.name
is RustType.Vec -> "${this.name}<${this.member.render()}>"
is RustType.HashMap -> "${this.name}<${this.key.render()}, ${this.value.render()}>"
is RustType.HashSet -> "${this.name}<${this.member.render()}>"
is RustType.Reference -> "&${this.lifetime?.let { "'$it" } ?: ""} ${this.value.render()}"
is RustType.Option -> "${this.name}<${this.value.render()}>"
is RustType.Box -> "${this.name}<${this.value.render()}>"
is RustType.Opaque -> this.name
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package software.amazon.smithy.rust.codegen.lang

import java.util.function.BiFunction
import software.amazon.smithy.codegen.core.CodegenException
import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.codegen.core.writer.CodegenWriter
Expand All @@ -17,6 +16,7 @@ import software.amazon.smithy.rust.codegen.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.smithy.isOptional
import software.amazon.smithy.rust.codegen.smithy.rustType
import software.amazon.smithy.utils.CodeWriter
import java.util.function.BiFunction

fun <T : CodeWriter> T.withBlock(
textBeforeNewLine: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class UseDeclarations(private val filename: String, private val namespace: Strin

private data class UseStatement(val moduleName: String, val symbolName: String, val alias: String) {
val rendered: String
get() {
val alias = alias.let { if (it == symbolName) "" else " as $it" }
return "use $moduleName::$symbolName$alias;"
}
get() {
val alias = alias.let { if (it == symbolName) "" else " as $it" }
return "use $moduleName::$symbolName$alias;"
}

override fun toString(): String = rendered
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package software.amazon.smithy.rust.codegen.smithy

import java.util.logging.Logger
import software.amazon.smithy.build.PluginContext
import software.amazon.smithy.codegen.core.SymbolProvider
import software.amazon.smithy.codegen.core.writer.CodegenWriterDelegator
Expand All @@ -29,6 +28,7 @@ import software.amazon.smithy.rust.codegen.smithy.generators.UnionGenerator
import software.amazon.smithy.rust.codegen.smithy.transformers.OperationNormalizer
import software.amazon.smithy.rust.codegen.util.CommandFailed
import software.amazon.smithy.rust.codegen.util.runCommand
import java.util.logging.Logger

private val PublicModules = listOf("error", "operation", "model")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

package software.amazon.smithy.rust.codegen.smithy

import java.io.File
import java.util.Optional
import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.model.node.ObjectNode
import software.amazon.smithy.model.traits.TimestampFormatTrait
import software.amazon.smithy.rust.codegen.lang.RustDependency
import software.amazon.smithy.rust.codegen.lang.RustType
import java.io.File
import java.util.Optional

data class RuntimeConfig(val cratePrefix: String = "smithy", val relativePath: String = "../") {
companion object {
Expand Down
Loading

0 comments on commit 86dc5db

Please sign in to comment.