-
-
Notifications
You must be signed in to change notification settings - Fork 612
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
7511cbe
commit 0841ea4
Showing
7 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
indent = 4 | ||
margin = 80 | ||
always_for_in = true | ||
whitespace_typedefs = true | ||
whitespace_ops_in_indices = true | ||
remove_extra_newlines = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/sh | ||
julia --color=yes dev/flux_format.jl . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
on: | ||
push: | ||
branches: | ||
- master | ||
tags: '*' | ||
pull_request: | ||
types: | ||
- opened | ||
- reopened | ||
- synchronize | ||
- ready_for_review | ||
|
||
jobs: | ||
format: | ||
runs-on: ubuntu-20.04 | ||
timeout-minutes: 30 | ||
steps: | ||
- uses: actions/[email protected] | ||
|
||
- uses: dorny/[email protected] | ||
id: filter | ||
with: | ||
filters: | | ||
julia_file_change: | ||
- added|modified: '**.jl' | ||
- uses: julia-actions/setup-julia@latest | ||
if: steps.filter.outputs.julia_file_change == 'true' | ||
with: | ||
version: 1.9 | ||
|
||
- name: Apply JuliaFormatter | ||
if: steps.filter.outputs.julia_file_change == 'true' | ||
run: | | ||
julia --color=yes dev/flux_format.jl --verbose . | ||
- name: Check formatting diff | ||
if: steps.filter.outputs.julia_file_change == 'true' | ||
run: | | ||
git diff --color=always --exit-code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[deps] | ||
Git = "d7ba0133-e1db-5d97-8f8c-041e4b3a1eb2" | ||
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Pkg | ||
Pkg.activate(@__DIR__) | ||
Pkg.instantiate() | ||
|
||
using JuliaFormatter | ||
|
||
help = """ | ||
Usage: flux_format.jl [flags] [FILE/PATH]... | ||
Formats the given julia files using the Flux formatting options. | ||
If paths are given instead, it will format all *.jl files under | ||
the paths. If nothing is given, all changed julia files are formatted. | ||
-v, --verbose | ||
Print the name of the files being formatted with relevant details. | ||
-h, --help | ||
Print this help message. | ||
""" | ||
|
||
options = Dict{Symbol, Bool}() | ||
indices_to_remove = [] # used to delete options once processed | ||
|
||
for (index, arg) in enumerate(ARGS) | ||
if arg[1] != '-' | ||
continue | ||
end | ||
if arg in ["-v", "--verbose"] | ||
opt = :verbose | ||
push!(indices_to_remove, index) | ||
elseif arg in ["-h", "--help"] | ||
opt = :help | ||
push!(indices_to_remove, index) | ||
else | ||
error("Option $arg is not supported.") | ||
end | ||
options[opt] = true | ||
end | ||
|
||
# remove options from args | ||
deleteat!(ARGS, indices_to_remove) | ||
|
||
# print help message if asked | ||
if haskey(options, :help) | ||
write(stdout, help) | ||
exit(0) | ||
end | ||
|
||
# otherwise format files | ||
if isempty(ARGS) | ||
filenames = readlines(`git ls-files "*.jl"`) | ||
else | ||
filenames = ARGS | ||
end | ||
|
||
write(stdout, "Formatting in progress.\n") | ||
format(filenames; options...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# instantiate the environment | ||
using Pkg | ||
Pkg.activate(@__DIR__) | ||
Pkg.instantiate() | ||
|
||
# setup the custom git hook | ||
using Git | ||
|
||
# set the local hooks path | ||
const git = Git.git() | ||
run(`$git config --local core.hooksPath .githooks/`) | ||
|
||
# set file permission for hook | ||
Base.Filesystem.chmod(".githooks", 0o777; recursive = true) |