From 4ae6d4c915e53108ba7ba651dc4a552035f5cec4 Mon Sep 17 00:00:00 2001 From: Andrew Winters Date: Fri, 5 Apr 2024 10:29:29 +0200 Subject: [PATCH] Add formatter functions (#37) * add formatting helper functions * small update to help command --- utils/trixisw-format-file.jl | 43 ++++++++++++++++++++++++++++++++++++ utils/trixisw-format.jl | 31 ++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100755 utils/trixisw-format-file.jl create mode 100755 utils/trixisw-format.jl diff --git a/utils/trixisw-format-file.jl b/utils/trixisw-format-file.jl new file mode 100755 index 0000000..b6a1105 --- /dev/null +++ b/utils/trixisw-format-file.jl @@ -0,0 +1,43 @@ +#!/usr/bin/env julia + +using Pkg +Pkg.activate(; temp = true, io = devnull) +Pkg.add(PackageSpec(name = "JuliaFormatter", version = "1.0.45"); preserve = PRESERVE_ALL, + io = devnull) + +using JuliaFormatter: format_file + +function main() + # Show help + if "-h" in ARGS || "--help" in ARGS + println("usage: trixisw-format.jl PATH [PATH...]") + println() + println("positional arguments:") + println() + println(" PATH One or more paths (directories or files) to format. Default: '.'") + return nothing + end + + file_list = ARGS + if isempty(ARGS) + exit(0) + end + non_formatted_files = Vector{String}() + for file in file_list + println("Checking file " * file) + if !format_file(file) + push!(non_formatted_files, file) + end + end + if isempty(non_formatted_files) + exit(0) + else + @error "Some files have not been formatted! Formatting has been applied, run 'git add -p' to update changes." + for file in non_formatted_files + println(file) + end + exit(1) + end +end + +main() diff --git a/utils/trixisw-format.jl b/utils/trixisw-format.jl new file mode 100755 index 0000000..ce3bf14 --- /dev/null +++ b/utils/trixisw-format.jl @@ -0,0 +1,31 @@ +#!/usr/bin/env julia + +using Pkg +Pkg.activate(; temp = true, io = devnull) +Pkg.add(PackageSpec(name = "JuliaFormatter", version = "1.0.45"); preserve = PRESERVE_ALL, + io = devnull) + +using JuliaFormatter: format + +function main() + # Show help + if "-h" in ARGS || "--help" in ARGS + println("usage: trixisw-format.jl PATH [PATH...]") + println() + println("positional arguments:") + println() + println(" PATH One or more paths (directories or files) to format. Default: '.'") + return nothing + end + + # Set default path if none is given on command line + if isempty(ARGS) + paths = String["."] + else + paths = ARGS + end + + return format(paths) +end + +main()