Skip to content

Commit

Permalink
Add formatting files and relevant githooks
Browse files Browse the repository at this point in the history
  • Loading branch information
ReubenJ committed Apr 11, 2024
1 parent 507a847 commit 1abf9c4
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
style = "sciml"
3 changes: 3 additions & 0 deletions .dev/Project.toml
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"
75 changes: 75 additions & 0 deletions .dev/herb_format.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""
Formatting script inspired by that of the `Flux.jl` package, which
can be found at:
https://github.com/FluxML/Flux.jl/blob/caa1ceef9cf59bd817b7bf5c94d0ffbec5a0f32c/dev/flux_format.jl
"""

using Pkg
Pkg.activate(@__DIR__)
Pkg.instantiate()

using JuliaFormatter

help = """
Usage: herb_format [flags] [FILE/PATH]...
Formats the given julia files using the Herb 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.
--overwrite
Overwrite the files with the formatted content.
"""

options = Dict{Symbol, Bool}()
indices_to_remove = [] # used to delete options once processed

options[:overwrite] = false # default to not overwrite files

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)
elseif arg == "--overwrite"
opt = :overwrite
write(stdout, "Overwriting files.\n")
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 returns true if the files were already formatted
# and false if they were not (had to be formatted)
exit(format(filenames; options...) ? 0 : 1)
21 changes: 21 additions & 0 deletions .dev/setup.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Dev environment setup script inspired by that of the `Flux.jl` package, which
can be found at:
https://github.com/FluxML/Flux.jl/blob/caa1ceef9cf59bd817b7bf5c94d0ffbec5a0f32c/dev/setup.jl
"""

# 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)
19 changes: 19 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

# Get the list of files that are about to be committed and filter out only the .jl files
files=$(git diff --cached --name-only --diff-filter=ACM | grep "\.jl$")

# If no files are found, exit
if [ -z "$files" ]; then
exit 0
fi

# Run the herb formatter on the list of files
julia --startup-file=no -O1 --color=yes .dev/herb_format.jl $files

# If the formatter exited with an error, abort the commit
if [ $? -ne 0 ]; then
echo "Error: formatter must be run on the files before committing."
echo "Please run julia .dev/herb_format.jl YOUR_CHANGED_FILES.jl"
exit 1
fi

0 comments on commit 1abf9c4

Please sign in to comment.