Skip to content

Commit

Permalink
Format shell scripts using shfmt
Browse files Browse the repository at this point in the history
Shell scripts are now formatted using shfmt:
https://github.com/mvdan/sh/blob/master/cmd/shfmt/shfmt.1.scd

This is enforced in CI, and formatting can be applied locally
using a new `make format` target.

Notably, this also reformats the scripts to use tabs instead of spaces,
since tabs are sadly the only way to be able to properly indent bash
here documents (which is something the scripts here will be using a
lot of soon) without having to resort to mixed tabs and spaces
indentation in the same file. (Plus tabs is also the shfmt default style.)

GUS-W-16808198.
  • Loading branch information
edmorley committed Sep 23, 2024
1 parent b9f7f3f commit 2eabbcc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
26 changes: 26 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# https://editorconfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.sh]
# We have to use tabs in shell scripts otherwise we can't indent here documents:
# https://www.gnu.org/software/bash/manual/html_node/Redirections.html#Here-Documents
indent_style = tab
shell_variant = bash

# Catches scripts that we can't give a .sh file extension, such as the Buildpack API scripts.
[**/bin/**]
indent_style = tab
shell_variant = bash

[.hatchet/repos/**]
ignore = true

[Makefile]
indent_style = tab
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ on:
permissions:
contents: read

env:
# Used by shfmt and more.
FORCE_COLOR: 1

jobs:
lint:
runs-on: ubuntu-24.04
Expand All @@ -23,6 +27,10 @@ jobs:
ruby-version: "3.3"
- name: Run ShellCheck
run: make lint-scripts
- name: Run shfmt
uses: docker://mvdan/shfmt:latest
with:
args: "--diff ."
- name: Run Rubocop
run: bundle exec rubocop

Expand Down
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
# These targets are not files
.PHONY: lint lint-scripts lint-ruby run publish
.PHONY: lint lint-scripts lint-ruby check-format format run publish

STACK ?= heroku-24
FIXTURE ?= spec/fixtures/python_version_unspecified

# Converts a stack name of `heroku-NN` to its build Docker image tag of `heroku/heroku:NN-build`.
STACK_IMAGE_TAG := heroku/$(subst -,:,$(STACK))-build

lint: lint-scripts lint-ruby
lint: lint-scripts check-format lint-ruby

lint-scripts:
@git ls-files -z --cached --others --exclude-standard 'bin/*' '*/bin/*' '*.sh' | xargs -0 shellcheck --check-sourced --color=always

lint-ruby:
@bundle exec rubocop

check-format:
@shfmt --diff .

format:
@shfmt --write --list .

run:
@echo "Running buildpack using: STACK=$(STACK) FIXTURE=$(FIXTURE)"
@docker run --rm -it -v $(PWD):/src:ro --tmpfs /app -e "HOME=/app" -e "STACK=$(STACK)" "$(STACK_IMAGE_TAG)" \
Expand Down

0 comments on commit 2eabbcc

Please sign in to comment.