Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial pass at docs for formatters #466

Merged
merged 3 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/bin/sort-dependencies
Git LFS file not shown
18 changes: 17 additions & 1 deletion config/git/hooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,23 @@ function partially_staged_chksum() {
original_partially_staged_chksum=$(partially_staged_chksum)
original_partially_staged_filenames=$(echo "$original_partially_staged_chksum" | grep -oE '[^ ]+$')

# TODO eventually support detekt here too once we can wire baseline and config fils
#### 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 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)?$")"
Expand Down
34 changes: 33 additions & 1 deletion docs/formatters-and-analysis.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
Formatters and Static Analysis
==============================

TODO
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<tool name>` tasks that you can run to download and install them, by default to `config/bin/<tool name>`. You should re-run these any time you update a tool to re-run them.