From 3baacfd18e6cf9faa285eeaa36c5795863502258 Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Mon, 17 Jul 2023 14:43:28 -0400 Subject: [PATCH 1/2] Initial pass at docs for formatters --- config/git/hooks/pre-commit | 16 ++++++++++++++++ docs/formatters-and-analysis.md | 34 ++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/config/git/hooks/pre-commit b/config/git/hooks/pre-commit index aadb941c3..68c5e86ff 100755 --- a/config/git/hooks/pre-commit +++ b/config/git/hooks/pre-commit @@ -63,6 +63,22 @@ function partially_staged_chksum() { original_partially_staged_chksum=$(partially_staged_chksum) original_partially_staged_filenames=$(echo "$original_partially_staged_chksum" | grep -oE '[^ ]+$') +#### Sort Dependencies section +# Get a list of staged build.gradle.kts files +kts_files_list="$(git diff --cached --name-only --diff-filter=ACMR "-G.*" | grep -Ei "build\.gradle\.kts$" | grep -v -x -F "tooling/slack-platform/build.gradle.kts")" +if [[ ! -z ${kts_files_list} ]]; then + # We have some build files! Join them to a string delimited on space. We'll pass these to sortDependencies + kts_files_list="$(echo ${kts_files_list} | paste -sd " " -)" + "${REPO_ROOT_DIR}/config/bin/sort-dependencies" ${kts_files_list} &> /dev/null + if [[ $? != 0 ]]; then + # Something it couldn't handle came up. Show the command and run verbosely for local debugging. + echo -e "${RED}sort-dependencies failed, re-running verbosely. Alternatively, you can run the command locally" >&2 + echo -e "Running format command: './config/bin/sort-dependencies --verbose ${kts_files_list}'${NC}" >&2 + "${REPO_ROOT_DIR}/config/bin/sort-dependencies --verbose" ${kts_files_list} >&2 + exit $? + fi +fi + # TODO eventually support detekt here too once we can wire baseline and config fils #### ktfmt # Get a list of staged files, parsing .kt or .kts files diff --git a/docs/formatters-and-analysis.md b/docs/formatters-and-analysis.md index 9d45e61e1..5ac7b5327 100644 --- a/docs/formatters-and-analysis.md +++ b/docs/formatters-and-analysis.md @@ -1,4 +1,36 @@ Formatters and Static Analysis ============================== -TODO \ No newline at end of file +SGP supports running a number of formatters and static analysis tools. + +Individual tools are usually gated on whether they have a version specified in `libs.versions.toml`. If they do not have a version specified, they are deemed not enabled. + +## Formatting + +The core set of formatters are: + +- ktfmt (Kotlin) +- google-java-format (Java) +- gson (JSON) +- gradle-dependency-sorter (Gradle build file dependencies) +- Spotless (general purpose Gradle plugin that runs most of the above) + +## Static Analysis + +The core set of analysis tools supported in SGP are: + +- Android Lint (Kotlin, Java, XML resources, build files, etc.) +- Detekt (Kotlin) +- Error Prone (Java) + +## Git Hooks + +SGP ships with a standard set of git hooks (pre-commit, etc) that it can bootstrap in projects by running `./gradlew installCommitHooks`. These hooks rely on checking in relevant binaries for each formatter/checker, it's strongly recommended to use git-lfs for these. These files should be edited as needed to best serve the project they're running in. + +SGP can configure these hooks in the project automatically during bootstrap if you add the `slack.git.hooksPath` gradle property and point it at the hooks directory that the above command output to, or wherever the host project opts to store them. + +Note that Detekt is not yet supported in git hooks as these require extra parameters for baselines. + +### Downloading binaries + +Each tool (ktfmt, gjf, etc) has corresponding `./gradlew update` tasks that you can run to download and install them, by default to `config/bin/`. You should re-run these any time you update a tool to re-run them. \ No newline at end of file From 8dd4814fbabaf56b644ec303631fcd741ebd963d Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Wed, 20 Sep 2023 13:39:41 -0400 Subject: [PATCH 2/2] Add sort-dependencies binary --- config/bin/sort-dependencies | 3 +++ config/git/hooks/pre-commit | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100755 config/bin/sort-dependencies diff --git a/config/bin/sort-dependencies b/config/bin/sort-dependencies new file mode 100755 index 000000000..1f916c229 --- /dev/null +++ b/config/bin/sort-dependencies @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e0ae4f7a5923cc4cf4e529d7045b516d28006dfb2d1b4ee0954c0814d8c32e85 +size 19227573 diff --git a/config/git/hooks/pre-commit b/config/git/hooks/pre-commit index 68c5e86ff..df36c6c76 100755 --- a/config/git/hooks/pre-commit +++ b/config/git/hooks/pre-commit @@ -79,7 +79,7 @@ if [[ ! -z ${kts_files_list} ]]; then fi fi -# TODO eventually support detekt here too once we can wire baseline and config fils +# TODO eventually support detekt here too once we can wire baseline and config files #### ktfmt # Get a list of staged files, parsing .kt or .kts files kotlin_files_list="$(git diff --cached --name-only --diff-filter=ACMR "-G.*" | grep -Ei "\.kt(s)?$")"