From 5e223e539b3f0ae1eff45ad2035833c274e50763 Mon Sep 17 00:00:00 2001 From: Danny Robinson Date: Tue, 6 Apr 2021 16:00:46 +0100 Subject: [PATCH] Add project_version macro --- docs/src/settings.md | 1 + src/ArgParse.jl | 3 ++- src/settings.jl | 37 +++++++++++++++++++++++++++++++++++++ test/Project.toml | 2 ++ test/argparse_test08.jl | 23 ++++++++++++++++++++++- 5 files changed, 64 insertions(+), 2 deletions(-) diff --git a/docs/src/settings.md b/docs/src/settings.md index b238c8e..58c8d25 100644 --- a/docs/src/settings.md +++ b/docs/src/settings.md @@ -2,4 +2,5 @@ ```@docs ArgParseSettings +@project_version ``` diff --git a/src/ArgParse.jl b/src/ArgParse.jl index fc63603..cc061c0 100644 --- a/src/ArgParse.jl +++ b/src/ArgParse.jl @@ -29,7 +29,8 @@ export set_default_arg_group!, import_settings!, usage_string, - parse_args + parse_args, + @project_version import Base: show, getindex, setindex!, haskey diff --git a/src/settings.jl b/src/settings.jl index 7206747..c234032 100644 --- a/src/settings.jl +++ b/src/settings.jl @@ -1465,3 +1465,40 @@ function import_settings!(settings::ArgParseSettings, end return settings end + +""" + @project_version + @project_version(filename::AbstractString) + +Reads the version from the Project.toml file at the given filename, at compile time. +If no filename is given, defaults to `Base.current_project()`. +Intended for use with the [`ArgParseSettings`](@ref) constructor, +to keep the settings version in sync with the project version. + +## Example + +```jl +ArgParseSettings(add_version = true, version = @project_version) +``` +""" +macro project_version(filename::String=Base.current_project()) + project_version(filename) +end + +macro project_version(expr::Expr) + project_version(eval(expr)) +end + +function project_version(filename::AbstractString)::String + re = r"^version\s*=\s*\"(.*)\"\s*$" + for line in eachline(filename) + if startswith(line, "[") + break + end + if !occursin(re, line) + continue + end + return match(re, line)[1] + end + throw(ArgumentError("Could not find a version in the file at $(filename)")) +end diff --git a/test/Project.toml b/test/Project.toml index 0c36332..b5813e5 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,2 +1,4 @@ +version = "1.0.0" + [deps] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/test/argparse_test08.jl b/test/argparse_test08.jl index 47e57c9..b84b5c3 100644 --- a/test/argparse_test08.jl +++ b/test/argparse_test08.jl @@ -1,4 +1,4 @@ -# test 08: read args from file +# test 08: read args from file, read version from project @testset "test 08" begin @@ -63,4 +63,25 @@ end @ap_test_throws ArgParseSettings(fromfile_prefix_chars=['Å']) @ap_test_throws ArgParseSettings(fromfile_prefix_chars=['8']) +# default project version +@test stringversion(ArgParseSettings( + add_version = true, + version = @project_version +)) == "1.0.0\n" + +# project version from filepath +@test stringversion(ArgParseSettings( + add_version = true, + version = @project_version "Project.toml" +)) == "1.0.0\n" + +# project version from expression that returns a filepath +@test stringversion(ArgParseSettings( + add_version = true, + version = @project_version(joinpath(@__DIR__, "Project.toml")) +)) == "1.0.0\n" + +# throws an error if the file doesn't contain a version +@test_throws ArgumentError ArgParse.project_version("args-file1") + end