From 2d910d1321e3763905afe7e471e1bdbbc5ee15ea Mon Sep 17 00:00:00 2001 From: Siddhant Chaudhary Date: Mon, 28 Aug 2023 15:11:56 +0530 Subject: [PATCH 01/12] Adding new environment for tooling. --- dev/Project.toml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 dev/Project.toml diff --git a/dev/Project.toml b/dev/Project.toml new file mode 100644 index 0000000000..f3aab8b8bf --- /dev/null +++ b/dev/Project.toml @@ -0,0 +1,2 @@ +[deps] +JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899" From da04ae7457cbd6e891c337a8c9f1335e12850241 Mon Sep 17 00:00:00 2001 From: Siddhant Chaudhary Date: Mon, 28 Aug 2023 15:15:46 +0530 Subject: [PATCH 02/12] Adding `JuliaFormatter` config. --- .JuliaFormatter.toml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .JuliaFormatter.toml diff --git a/.JuliaFormatter.toml b/.JuliaFormatter.toml new file mode 100644 index 0000000000..b9af0ebafa --- /dev/null +++ b/.JuliaFormatter.toml @@ -0,0 +1,6 @@ +indent = 4 +margin = 80 +always_for_in = true +whitespace_typedefs = true +whitespace_ops_in_indices = true +remove_extra_newlines = false From 0a99bc5c7eb848bde637877825519dfe23f2362c Mon Sep 17 00:00:00 2001 From: Siddhant Chaudhary Date: Mon, 28 Aug 2023 15:54:22 +0530 Subject: [PATCH 03/12] Added file for formatting. --- dev/flux_format.jl | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 dev/flux_format.jl diff --git a/dev/flux_format.jl b/dev/flux_format.jl new file mode 100644 index 0000000000..ade0553c5e --- /dev/null +++ b/dev/flux_format.jl @@ -0,0 +1,56 @@ +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) + 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 + +format(filenames; options...) From 5b39a345b918603cd46a8b9c1c6ffb9a86b13570 Mon Sep 17 00:00:00 2001 From: Siddhant Chaudhary Date: Mon, 28 Aug 2023 16:01:30 +0530 Subject: [PATCH 04/12] Adding CI for JuliaFormatter. --- .github/workflows/JuliaFormatter.yml | 47 ++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 .github/workflows/JuliaFormatter.yml diff --git a/.github/workflows/JuliaFormatter.yml b/.github/workflows/JuliaFormatter.yml new file mode 100644 index 0000000000..d66b0b70cb --- /dev/null +++ b/.github/workflows/JuliaFormatter.yml @@ -0,0 +1,47 @@ +on: + push: + branches: + - main + - trying + - staging + tags: '*' + pull_request: + types: + - opened + - reopened + - synchronize + - ready_for_review + +jobs: + format: + runs-on: ubuntu-20.04 + timeout-minutes: 30 + steps: + - name: Cancel Previous Runs + uses: styfle/cancel-workflow-action@0.4.0 + with: + access_token: ${{ github.token }} + + - uses: actions/checkout@v2.2.0 + + - uses: dorny/paths-filter@v2.9.1 + 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 From 1327c1cbb2dc0b3068a56119df7838d76ac640d1 Mon Sep 17 00:00:00 2001 From: Siddhant Chaudhary Date: Sat, 2 Sep 2023 13:52:12 +0530 Subject: [PATCH 05/12] Adding a custom pre-commit hook, and a new script which sets up the hook. --- .githooks/pre-commit | 1 + dev/Project.toml | 2 ++ dev/setup.jl | 9 +++++++++ 3 files changed, 12 insertions(+) create mode 100644 .githooks/pre-commit create mode 100644 dev/setup.jl diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100644 index 0000000000..8752801247 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1 @@ +julia --color=yes dev/flux_format.jl --verbose . diff --git a/dev/Project.toml b/dev/Project.toml index f3aab8b8bf..24d6711c07 100644 --- a/dev/Project.toml +++ b/dev/Project.toml @@ -1,2 +1,4 @@ [deps] +Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c" +Git = "d7ba0133-e1db-5d97-8f8c-041e4b3a1eb2" JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899" diff --git a/dev/setup.jl b/dev/setup.jl new file mode 100644 index 0000000000..baf0efdb15 --- /dev/null +++ b/dev/setup.jl @@ -0,0 +1,9 @@ +# 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) From 01786527bd3f56f4531e2e86f4bede0d8663c455 Mon Sep 17 00:00:00 2001 From: Siddhant Chaudhary Date: Sun, 3 Sep 2023 18:37:49 +0530 Subject: [PATCH 06/12] Removing Flux from tooling deps, and instantiating environment from `setup.jl`. --- dev/Project.toml | 1 - dev/flux_format.jl | 3 +-- dev/setup.jl | 5 +++++ 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/dev/Project.toml b/dev/Project.toml index 24d6711c07..2922960e2c 100644 --- a/dev/Project.toml +++ b/dev/Project.toml @@ -1,4 +1,3 @@ [deps] -Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c" Git = "d7ba0133-e1db-5d97-8f8c-041e4b3a1eb2" JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899" diff --git a/dev/flux_format.jl b/dev/flux_format.jl index ade0553c5e..ee6386e00d 100644 --- a/dev/flux_format.jl +++ b/dev/flux_format.jl @@ -1,6 +1,5 @@ using Pkg -Pkg.activate(@__DIR__) -Pkg.instantiate() +Pkg.activate(@__DIR__) # no need to instantiate here using JuliaFormatter diff --git a/dev/setup.jl b/dev/setup.jl index baf0efdb15..a119426eac 100644 --- a/dev/setup.jl +++ b/dev/setup.jl @@ -1,3 +1,8 @@ +# instantiate the environment +using Pkg +Pkg.activate(@__DIR__) +Pkg.instantiate() + # setup the custom git hook using Git From 13a1358e7421298662e6bbf8cf118c4907d723db Mon Sep 17 00:00:00 2001 From: Siddhant Chaudhary Date: Sun, 3 Sep 2023 18:59:06 +0530 Subject: [PATCH 07/12] Adding shebang for hook to work on windows. --- .githooks/pre-commit | 1 + 1 file changed, 1 insertion(+) diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 8752801247..d6cb927f2e 100644 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -1 +1,2 @@ +#!/bin/sh julia --color=yes dev/flux_format.jl --verbose . From 20d22db8b9349d7b573f3d4403ba8067451ad458 Mon Sep 17 00:00:00 2001 From: Siddhant Chaudhary Date: Sun, 3 Sep 2023 22:24:31 +0530 Subject: [PATCH 08/12] Instantiating from `flux_format.jl` too. --- dev/flux_format.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dev/flux_format.jl b/dev/flux_format.jl index ee6386e00d..ade0553c5e 100644 --- a/dev/flux_format.jl +++ b/dev/flux_format.jl @@ -1,5 +1,6 @@ using Pkg -Pkg.activate(@__DIR__) # no need to instantiate here +Pkg.activate(@__DIR__) +Pkg.instantiate() using JuliaFormatter From 5428b8f1f7cb58fd2908fba3b680dd0a7c3f4855 Mon Sep 17 00:00:00 2001 From: Siddhant Chaudhary Date: Mon, 4 Sep 2023 14:09:00 +0530 Subject: [PATCH 09/12] Minor fixes in JuliaFormatter CI. --- .github/workflows/JuliaFormatter.yml | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/.github/workflows/JuliaFormatter.yml b/.github/workflows/JuliaFormatter.yml index d66b0b70cb..3782df82f9 100644 --- a/.github/workflows/JuliaFormatter.yml +++ b/.github/workflows/JuliaFormatter.yml @@ -1,9 +1,7 @@ on: push: branches: - - main - - trying - - staging + - master tags: '*' pull_request: types: @@ -17,11 +15,6 @@ jobs: runs-on: ubuntu-20.04 timeout-minutes: 30 steps: - - name: Cancel Previous Runs - uses: styfle/cancel-workflow-action@0.4.0 - with: - access_token: ${{ github.token }} - - uses: actions/checkout@v2.2.0 - uses: dorny/paths-filter@v2.9.1 From aade59d69f93f9a3b525c5747d8e42b8b17e41d6 Mon Sep 17 00:00:00 2001 From: Siddhant Chaudhary Date: Mon, 4 Sep 2023 19:14:51 +0530 Subject: [PATCH 10/12] Adding formatting steps to `CONTRIBUTING.md`. --- CONTRIBUTING.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ce008e459b..e5f6321637 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -154,4 +154,20 @@ The following table shows how the Flux code is organized: | docs | Documentation site | | paper | Paper that describes Flux | | src | Source for Flux | -| test | Test suites | \ No newline at end of file +| test | Test suites | + +### Julia Formatter + +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: + +```julia +julia dev/setup.jl +``` + +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 + +```julia +julia dev/flux_format.jl --verbose . +``` + +Flux's CI will also test whether any changes you make conform to this formatting style whenever any pull request is made. From 779018ffb24494659a308a07adcc25c1609ed014 Mon Sep 17 00:00:00 2001 From: Siddhant Chaudhary Date: Tue, 5 Sep 2023 10:12:36 +0530 Subject: [PATCH 11/12] Removing the `--verbose` option from the hook, and printing some useful text during formatting. --- .githooks/pre-commit | 2 +- dev/flux_format.jl | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) mode change 100644 => 100755 .githooks/pre-commit diff --git a/.githooks/pre-commit b/.githooks/pre-commit old mode 100644 new mode 100755 index d6cb927f2e..2f8429e4f3 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -1,2 +1,2 @@ #!/bin/sh -julia --color=yes dev/flux_format.jl --verbose . +julia --color=yes dev/flux_format.jl . diff --git a/dev/flux_format.jl b/dev/flux_format.jl index ade0553c5e..dba5d5763c 100644 --- a/dev/flux_format.jl +++ b/dev/flux_format.jl @@ -53,4 +53,5 @@ else filenames = ARGS end +write(stdout, "Formatting in progress.\n") format(filenames; options...) From 5fdd72a823735bb8b0141867a43ac70dbeb7a673 Mon Sep 17 00:00:00 2001 From: Siddhant Chaudhary Date: Fri, 8 Sep 2023 00:08:43 +0530 Subject: [PATCH 12/12] Minor fix in formatting script. --- dev/flux_format.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/flux_format.jl b/dev/flux_format.jl index dba5d5763c..699a3eeaad 100644 --- a/dev/flux_format.jl +++ b/dev/flux_format.jl @@ -30,7 +30,7 @@ for (index, arg) in enumerate(ARGS) push!(indices_to_remove, index) elseif arg in ["-h", "--help"] opt = :help - push!(indices_to_remove) + push!(indices_to_remove, index) else error("Option $arg is not supported.") end