Skip to content

Commit 8b3cce9

Browse files
stellartuxcarlobaldassi
authored andcommitted
Add project_version macro
1 parent 3963105 commit 8b3cce9

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

docs/src/settings.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
```@docs
44
ArgParseSettings
5+
@project_version
56
```

src/ArgParse.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export
2929
set_default_arg_group!,
3030
import_settings!,
3131
usage_string,
32-
parse_args
32+
parse_args,
33+
@project_version
3334

3435
import Base: show, getindex, setindex!, haskey
3536

src/settings.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,3 +1465,40 @@ function import_settings!(settings::ArgParseSettings,
14651465
end
14661466
return settings
14671467
end
1468+
1469+
"""
1470+
@project_version
1471+
@project_version(filename::AbstractString)
1472+
1473+
Reads the version from the Project.toml file at the given filename, at compile time.
1474+
If no filename is given, defaults to `Base.current_project()`.
1475+
Intended for use with the [`ArgParseSettings`](@ref) constructor,
1476+
to keep the settings version in sync with the project version.
1477+
1478+
## Example
1479+
1480+
```jl
1481+
ArgParseSettings(add_version = true, version = @project_version)
1482+
```
1483+
"""
1484+
macro project_version(filename::String=Base.current_project())
1485+
project_version(filename)
1486+
end
1487+
1488+
macro project_version(expr::Expr)
1489+
project_version(eval(expr))
1490+
end
1491+
1492+
function project_version(filename::AbstractString)::String
1493+
re = r"^version\s*=\s*\"(.*)\"\s*$"
1494+
for line in eachline(filename)
1495+
if startswith(line, "[")
1496+
break
1497+
end
1498+
if !occursin(re, line)
1499+
continue
1500+
end
1501+
return match(re, line)[1]
1502+
end
1503+
throw(ArgumentError("Could not find a version in the file at $(filename)"))
1504+
end

test/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
version = "1.0.0"
2+
13
[deps]
24
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

test/argparse_test08.jl

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# test 08: read args from file
1+
# test 08: read args from file, read version from project
22

33
@testset "test 08" begin
44

@@ -63,4 +63,25 @@ end
6363
@ap_test_throws ArgParseSettings(fromfile_prefix_chars=['Å'])
6464
@ap_test_throws ArgParseSettings(fromfile_prefix_chars=['8'])
6565

66+
# default project version
67+
@test stringversion(ArgParseSettings(
68+
add_version = true,
69+
version = @project_version
70+
)) == "1.0.0\n"
71+
72+
# project version from filepath
73+
@test stringversion(ArgParseSettings(
74+
add_version = true,
75+
version = @project_version "Project.toml"
76+
)) == "1.0.0\n"
77+
78+
# project version from expression that returns a filepath
79+
@test stringversion(ArgParseSettings(
80+
add_version = true,
81+
version = @project_version(joinpath(@__DIR__, "Project.toml"))
82+
)) == "1.0.0\n"
83+
84+
# throws an error if the file doesn't contain a version
85+
@test_throws ArgumentError ArgParse.project_version("args-file1")
86+
6687
end

0 commit comments

Comments
 (0)