Skip to content

Commit

Permalink
Merge #61
Browse files Browse the repository at this point in the history
61: The great formatting. r=ilopezgp a=ilopezgp

Let's use the formatting that makes other CliMA packages look so nice.

Co-authored-by: Ignacio Lopez Gomez <[email protected]>
  • Loading branch information
bors[bot] and ilopezgp committed Oct 2, 2021
2 parents c32683b + 461a6b3 commit 0151a84
Show file tree
Hide file tree
Showing 29 changed files with 1,503 additions and 1,191 deletions.
5 changes: 5 additions & 0 deletions .dev/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[deps]
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"

[compat]
JuliaFormatter = "0.3"
8 changes: 8 additions & 0 deletions .dev/clima_formatter_options.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
clima_formatter_options = (
indent = 4,
margin = 120,
always_for_in = true,
whitespace_typedefs = true,
whitespace_ops_in_indices = true,
remove_extra_newlines = false,
)
85 changes: 85 additions & 0 deletions .dev/climaformat.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env julia
#
# This is an adapted version of format.jl from JuliaFormatter with the
# following license:
#
# MIT License
# Copyright (c) 2019 Dominique Luna
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the
# following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
# NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
# USE OR OTHER DEALINGS IN THE SOFTWARE.
#
using Pkg
Pkg.activate(@__DIR__)
Pkg.instantiate()

using JuliaFormatter

include("clima_formatter_options.jl")

help = """
Usage: climaformat.jl [flags] [FILE/PATH]...
Formats the given julia files using the CLIMA formatting options. If paths
are given it will format the julia files in the paths. Otherwise, it will
format all changed julia files.
-v, --verbose
Print the name of the files being formatted with relevant details.
-h, --help
Print this message.
"""

function parse_opts!(args::Vector{String})
i = 1
opts = Dict{Symbol, Union{Int, Bool}}()
while i length(args)
arg = args[i]
if arg[1] != '-'
i += 1
continue
end
if arg == "-v" || arg == "--verbose"
opt = :verbose
elseif arg == "-h" || arg == "--help"
opt = :help
else
error("invalid option $arg")
end
if opt in (:verbose, :help)
opts[opt] = true
deleteat!(args, i)
end
end
return opts
end

opts = parse_opts!(ARGS)
if haskey(opts, :help)
write(stdout, help)
exit(0)
end
if isempty(ARGS)
filenames = readlines(`git ls-files "*.jl"`)
else
filenames = ARGS
end

format(filenames; clima_formatter_options..., opts...)
41 changes: 41 additions & 0 deletions .dev/hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env julia
#
# Called by git-commit with no arguments. This checks to make sure that all
# .jl files are indented correctly before a commit is made.
#
# To enable this hook, make this file executable and copy it in
# $GIT_DIR/hooks.

toplevel_directory = chomp(read(`git rev-parse --show-toplevel`, String))

using Pkg
Pkg.activate(joinpath(toplevel_directory, ".dev"))
Pkg.instantiate()

using JuliaFormatter

include(joinpath(toplevel_directory, ".dev", "clima_formatter_options.jl"))

needs_format = false

for diffoutput in split.(readlines(`git diff --name-status --cached`))
status = diffoutput[1]
filename = diffoutput[end]
(!startswith(status, "D") && endswith(filename, ".jl")) || continue

a = read(`git show :$filename`, String)
b = format_text(a; clima_formatter_options...)

if a != b
fullfilename = joinpath(toplevel_directory, filename)

@error """File $filename needs to be indented with:
julia $(joinpath(toplevel_directory, ".dev", "climaformat.jl")) $fullfilename
and added to the git index via
git add $fullfilename
"""
global needs_format = true
end
end

exit(needs_format ? 1 : 0)
44 changes: 44 additions & 0 deletions .github/workflows/JuliaFormatter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: JuliaFormatter

on:
push:
branches:
- main
- trying
- staging
tags: '*'
pull_request:

jobs:
format:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Cancel Previous Runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}

- 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.6.0

- name: Apply JuliaFormatter
if: steps.filter.outputs.julia_file_change == 'true'
run: |
julia --project=.dev .dev/climaformat.jl .
- name: Check formatting diff
if: steps.filter.outputs.julia_file_change == 'true'
run: |
git diff --color=always --exit-code
1 change: 1 addition & 0 deletions bors.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
status = [
'docs-build',
'test_success',
'format',
]
delete_merged_branches = true
timeout_sec = 3600
Expand Down
54 changes: 24 additions & 30 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# reference in tree version of CalibrateEmulateSample
prepend!(LOAD_PATH, [joinpath(@__DIR__, "..")])

using
EnsembleKalmanProcesses,
using EnsembleKalmanProcesses,
Documenter,
Plots, # so that Literate.jl does not capture precompilation output
Literate
Expand All @@ -11,13 +10,11 @@ using
# This happens as examples will use Plots.jl to make plots and movies.
# See: https://github.com/jheinen/GR.jl/issues/278
ENV["GKSwstype"] = "100"

const EXAMPLES_DIR = joinpath(@__DIR__, "..", "examples")
const OUTPUT_DIR = joinpath(@__DIR__, "src/literated")
const OUTPUT_DIR = joinpath(@__DIR__, "src/literated")

examples_for_literation = [
"LossMinimization/loss_minimization.jl"
]
examples_for_literation = ["LossMinimization/loss_minimization.jl"]

for example in examples_for_literation
example_filepath = joinpath(EXAMPLES_DIR, example)
Expand All @@ -28,17 +25,17 @@ end

api = [
"EnsembleKalmanProcessModule" => "API/EnsembleKalmanProcessModule.md",
"ParameterDistribution" => "API/ParameterDistribution.md",
"Observations" => "API/Observations.md",
"DataStorage" => "API/DataStorage.md",
]
"ParameterDistribution" => "API/ParameterDistribution.md",
"Observations" => "API/Observations.md",
"DataStorage" => "API/DataStorage.md",
]

examples = [
"Template" => "examples/template_example.md",
"Cloudy" => "examples/Cloudy_example.md",
"Lorenz" => "examples/lorenz_example.md",
"Template" => "examples/template_example.md",
"Cloudy" => "examples/Cloudy_example.md",
"Lorenz" => "examples/lorenz_example.md",
"Minimization Loss" => "literated/loss_minimization.md",
"HPC interfacing example: ClimateMachine" => "examples/ClimateMachine_example.md"
"HPC interfacing example: ClimateMachine" => "examples/ClimateMachine_example.md",
]

pages = [
Expand All @@ -56,28 +53,25 @@ pages = [

#----------

format = Documenter.HTML(
collapselevel = 1,
prettyurls = !isempty(get(ENV, "CI", ""))
)
format = Documenter.HTML(collapselevel = 1, prettyurls = !isempty(get(ENV, "CI", "")))

makedocs(
sitename = "EnsembleKalmanProcesses.jl",
sitename = "EnsembleKalmanProcesses.jl",
authors = "CliMA Contributors",
format = format,
pages = pages,
format = format,
pages = pages,
modules = [EnsembleKalmanProcesses],
doctest = true,
strict = true,
clean = true,
checkdocs = :none,
strict = true,
clean = true,
checkdocs = :none,
)

if !isempty(get(ENV, "CI", ""))
deploydocs(
repo = "github.com/CliMA/EnsembleKalmanProcesses.jl.git",
deploydocs(
repo = "github.com/CliMA/EnsembleKalmanProcesses.jl.git",
versions = ["stable" => "v^", "v#.#.#", "dev" => "dev"],
push_preview = true,
devbranch = "main",
)
push_preview = true,
devbranch = "main",
)
end
Loading

2 comments on commit 0151a84

@ilopezgp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register

Release notes:

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/46285

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.1 -m "<description of version>" 0151a847d65cc2ce7a7b35afe570255716e45cc5
git push origin v0.1.1

Please sign in to comment.