Skip to content

Commit 2eabbcc

Browse files
committed
Format shell scripts using shfmt
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.
1 parent b9f7f3f commit 2eabbcc

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

.editorconfig

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# https://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
end_of_line = lf
7+
indent_style = space
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.sh]
12+
# We have to use tabs in shell scripts otherwise we can't indent here documents:
13+
# https://www.gnu.org/software/bash/manual/html_node/Redirections.html#Here-Documents
14+
indent_style = tab
15+
shell_variant = bash
16+
17+
# Catches scripts that we can't give a .sh file extension, such as the Buildpack API scripts.
18+
[**/bin/**]
19+
indent_style = tab
20+
shell_variant = bash
21+
22+
[.hatchet/repos/**]
23+
ignore = true
24+
25+
[Makefile]
26+
indent_style = tab

.github/workflows/ci.yml

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ on:
1010
permissions:
1111
contents: read
1212

13+
env:
14+
# Used by shfmt and more.
15+
FORCE_COLOR: 1
16+
1317
jobs:
1418
lint:
1519
runs-on: ubuntu-24.04
@@ -23,6 +27,10 @@ jobs:
2327
ruby-version: "3.3"
2428
- name: Run ShellCheck
2529
run: make lint-scripts
30+
- name: Run shfmt
31+
uses: docker://mvdan/shfmt:latest
32+
with:
33+
args: "--diff ."
2634
- name: Run Rubocop
2735
run: bundle exec rubocop
2836

Makefile

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
# These targets are not files
2-
.PHONY: lint lint-scripts lint-ruby run publish
2+
.PHONY: lint lint-scripts lint-ruby check-format format run publish
33

44
STACK ?= heroku-24
55
FIXTURE ?= spec/fixtures/python_version_unspecified
66

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

10-
lint: lint-scripts lint-ruby
10+
lint: lint-scripts check-format lint-ruby
1111

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

1515
lint-ruby:
1616
@bundle exec rubocop
1717

18+
check-format:
19+
@shfmt --diff .
20+
21+
format:
22+
@shfmt --write --list .
23+
1824
run:
1925
@echo "Running buildpack using: STACK=$(STACK) FIXTURE=$(FIXTURE)"
2026
@docker run --rm -it -v $(PWD):/src:ro --tmpfs /app -e "HOME=/app" -e "STACK=$(STACK)" "$(STACK_IMAGE_TAG)" \

0 commit comments

Comments
 (0)