Skip to content

Commit 0841ea4

Browse files
authored
Adding tooling for JuliaFormatter. (#2323)
* Adding new environment for tooling. * Adding `JuliaFormatter` config. * Added file for formatting. * Adding CI for JuliaFormatter. * Adding a custom pre-commit hook, and a new script which sets up the hook. * Removing Flux from tooling deps, and instantiating environment from `setup.jl`. * Adding shebang for hook to work on windows. * Instantiating from `flux_format.jl` too. * Minor fixes in JuliaFormatter CI. * Adding formatting steps to `CONTRIBUTING.md`. * Removing the `--verbose` option from the hook, and printing some useful text during formatting. * Minor fix in formatting script.
1 parent 7511cbe commit 0841ea4

File tree

7 files changed

+139
-1
lines changed

7 files changed

+139
-1
lines changed

.JuliaFormatter.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
indent = 4
2+
margin = 80
3+
always_for_in = true
4+
whitespace_typedefs = true
5+
whitespace_ops_in_indices = true
6+
remove_extra_newlines = false

.githooks/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
julia --color=yes dev/flux_format.jl .
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
on:
2+
push:
3+
branches:
4+
- master
5+
tags: '*'
6+
pull_request:
7+
types:
8+
- opened
9+
- reopened
10+
- synchronize
11+
- ready_for_review
12+
13+
jobs:
14+
format:
15+
runs-on: ubuntu-20.04
16+
timeout-minutes: 30
17+
steps:
18+
- uses: actions/checkout@v2.2.0
19+
20+
- uses: dorny/paths-filter@v2.9.1
21+
id: filter
22+
with:
23+
filters: |
24+
julia_file_change:
25+
- added|modified: '**.jl'
26+
27+
- uses: julia-actions/setup-julia@latest
28+
if: steps.filter.outputs.julia_file_change == 'true'
29+
with:
30+
version: 1.9
31+
32+
- name: Apply JuliaFormatter
33+
if: steps.filter.outputs.julia_file_change == 'true'
34+
run: |
35+
julia --color=yes dev/flux_format.jl --verbose .
36+
37+
- name: Check formatting diff
38+
if: steps.filter.outputs.julia_file_change == 'true'
39+
run: |
40+
git diff --color=always --exit-code

CONTRIBUTING.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,20 @@ The following table shows how the Flux code is organized:
154154
| docs | Documentation site |
155155
| paper | Paper that describes Flux |
156156
| src | Source for Flux |
157-
| test | Test suites |
157+
| test | Test suites |
158+
159+
### Julia Formatter
160+
161+
Flux also uses it's own formatting style (see `dev/flux_format.jl`), with the style defined in the `.JuliaFormatter.toml` config file. All contributors must make sure to conform to this formatting style. To do this, run the following setup file on your local repository:
162+
163+
```julia
164+
julia dev/setup.jl
165+
```
166+
167+
This will setup the tooling environment (defined in the `dev/` directory), and will set up a hook to run the formatter before any changes are committed. You can also manually format the codebase by running
168+
169+
```julia
170+
julia dev/flux_format.jl --verbose .
171+
```
172+
173+
Flux's CI will also test whether any changes you make conform to this formatting style whenever any pull request is made.

dev/Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[deps]
2+
Git = "d7ba0133-e1db-5d97-8f8c-041e4b3a1eb2"
3+
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"

dev/flux_format.jl

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using Pkg
2+
Pkg.activate(@__DIR__)
3+
Pkg.instantiate()
4+
5+
using JuliaFormatter
6+
7+
help = """
8+
Usage: flux_format.jl [flags] [FILE/PATH]...
9+
10+
Formats the given julia files using the Flux formatting options.
11+
If paths are given instead, it will format all *.jl files under
12+
the paths. If nothing is given, all changed julia files are formatted.
13+
14+
-v, --verbose
15+
Print the name of the files being formatted with relevant details.
16+
17+
-h, --help
18+
Print this help message.
19+
"""
20+
21+
options = Dict{Symbol, Bool}()
22+
indices_to_remove = [] # used to delete options once processed
23+
24+
for (index, arg) in enumerate(ARGS)
25+
if arg[1] != '-'
26+
continue
27+
end
28+
if arg in ["-v", "--verbose"]
29+
opt = :verbose
30+
push!(indices_to_remove, index)
31+
elseif arg in ["-h", "--help"]
32+
opt = :help
33+
push!(indices_to_remove, index)
34+
else
35+
error("Option $arg is not supported.")
36+
end
37+
options[opt] = true
38+
end
39+
40+
# remove options from args
41+
deleteat!(ARGS, indices_to_remove)
42+
43+
# print help message if asked
44+
if haskey(options, :help)
45+
write(stdout, help)
46+
exit(0)
47+
end
48+
49+
# otherwise format files
50+
if isempty(ARGS)
51+
filenames = readlines(`git ls-files "*.jl"`)
52+
else
53+
filenames = ARGS
54+
end
55+
56+
write(stdout, "Formatting in progress.\n")
57+
format(filenames; options...)

dev/setup.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# instantiate the environment
2+
using Pkg
3+
Pkg.activate(@__DIR__)
4+
Pkg.instantiate()
5+
6+
# setup the custom git hook
7+
using Git
8+
9+
# set the local hooks path
10+
const git = Git.git()
11+
run(`$git config --local core.hooksPath .githooks/`)
12+
13+
# set file permission for hook
14+
Base.Filesystem.chmod(".githooks", 0o777; recursive = true)

0 commit comments

Comments
 (0)