From c245bcd556071726c84408299f33e519d4ea4a4f Mon Sep 17 00:00:00 2001 From: Chris de Graaf Date: Fri, 18 Aug 2017 02:08:03 -0500 Subject: [PATCH] Add requirements keyword to Template --- src/generate.jl | 1 + src/template.jl | 27 +++++++++++++++++++++++---- test/tests.jl | 18 ++++++++++++++++-- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/generate.jl b/src/generate.jl index 467d301..d17401f 100644 --- a/src/generate.jl +++ b/src/generate.jl @@ -144,6 +144,7 @@ Returns an array of generated file/directory names. """ function gen_require(pkg_name::AbstractString, template::Template) text = "julia $(version_floor(template.julia_version))\n" + text *= join(template.requirements, "\n") gen_file(joinpath(template.temp_dir, pkg_name, "REQUIRE"), text) return ["REQUIRE"] diff --git a/src/template.jl b/src/template.jl index 1b3a249..0ce7a71 100644 --- a/src/template.jl +++ b/src/template.jl @@ -21,10 +21,16 @@ Records common information used to generate a package. on the license. Can be supplied by a number, or a string such as "2016 - 2017". * `dir::AbstractString=Pkg.dir()`: Directory in which the package will go. * `julia_version::VersionNumber=VERSION`: Minimum allowed Julia version. +* `requirements::Vector{String}=String[]`: Package requirements. If there are duplicate + requirements with different versions, i.e. ["PkgTemplates", "PkgTemplates 0.1"], + an `ArgumentError` is thrown. + Each entry in this array will be copied into the `REQUIRE` file of packages generated + with this template. * `git_config::Dict{String, String}=Dict{String, String}()`: Git configuration options. -* `plugins::Vector{Plugin}`: A list of `Plugin`s that the package will include. +* `plugins::Plugin[]`: A list of `Plugin`s that the package will include. -**Note**: When you create a `Template`, a temporary directory is created with +# Notes +When you create a `Template`, a temporary directory is created with `mktempdir()`. This directory will be removed after you call [`generate`](@ref). Creating multiple packages in succession with the same instance of a template will still work, but there is a miniscule chance of another process sharing the temporary directory, @@ -40,6 +46,7 @@ don't belong. dir::AbstractString temp_dir::AbstractString julia_version::VersionNumber + requirements::Vector{AbstractString} git_config::Dict plugins::Dict{DataType, Plugin} @@ -51,8 +58,9 @@ don't belong. years::Union{Int, AbstractString}=string(Dates.year(Dates.today())), dir::AbstractString=Pkg.dir(), julia_version::VersionNumber=VERSION, + requirements::Vector{String}=String[], git_config::Dict=Dict(), - plugins::Vector{P}=Vector{Plugin}(), + plugins::Vector{P}=Plugin[], ) where P <: Plugin # If no username was set, look for one in a supplied git config, # and then in the global git config. @@ -84,6 +92,17 @@ don't belong. temp_dir = mktempdir() + requirements_dedup = collect(Set(requirements)) + diff = length(requirements) - length(requirements_dedup) + names = [tokens[1] for tokens in split.(requirements_dedup)] + if length(names) > length(Set(names)) + throw(ArgumentError( + "requirements contains duplicate packages with conflicting versions" + )) + elseif diff > 0 + warn("Removed $(diff) duplicate$(diff == 1 ? "" : "s") from requirements") + end + plugin_dict = Dict{DataType, Plugin}(typeof(p) => p for p in plugins) if (length(plugins) != length(plugin_dict)) warn("Plugin list contained duplicates, only the last of each type was kept") @@ -91,7 +110,7 @@ don't belong. new( user, host, license, authors, years, dir, temp_dir, - julia_version, git_config, plugin_dict + julia_version, requirements_dedup, git_config, plugin_dict ) end end diff --git a/test/tests.jl b/test/tests.jl index 5c1499b..5232c7b 100644 --- a/test/tests.jl +++ b/test/tests.jl @@ -67,6 +67,18 @@ write(test_file, template_text) rm(t.temp_dir; recursive=true) @test t.julia_version == v"0.1.2" + t = Template(; user="invenia", requirements=["$test_pkg 0.1"]) + rm(t.temp_dir; recursive=true) + @test t.requirements == ["$test_pkg 0.1"] + @test_warn r".+" t = Template(; user="invenia", requirements=[test_pkg, test_pkg]) + rm(t.temp_dir; recursive=true) + @test t.requirements == [test_pkg] + @test_throws ArgumentError t = Template(; + user="invenia", + requirements=[test_pkg, "$test_pkg 0.1"] + ) + rm(t.temp_dir; force=true, recursive=true) + t = Template(; user="invenia", git_config=git_config) rm(t.temp_dir; recursive=true) @test t.git_config == git_config @@ -102,10 +114,11 @@ write(test_file, template_text) @test_throws ArgumentError t = Template() else t = Template() - rm(t.temp_dir; recursive=true) @test t.user == LibGit2.getconfig("github.username", "") end + rm(t.temp_dir; force=true, recursive=true) @test_throws ArgumentError t = Template(; user="invenia", license="FakeLicense") + rm(t.temp_dir; force=true, recursive=true) end @testset "Plugin creation" begin @@ -226,6 +239,7 @@ end t = Template(; user="invenia", license="MPL", + requirements=[test_pkg], git_config=git_config, plugins=[Coveralls(), TravisCI(), CodeCov(), GitHubPages(), AppVeyor()], ) @@ -287,7 +301,7 @@ end @test gen_require(test_pkg, t) == ["REQUIRE"] @test isfile(joinpath(pkg_dir, "REQUIRE")) vf = version_floor(t.julia_version) - @test readchomp(joinpath(pkg_dir, "REQUIRE")) == "julia $vf" + @test readchomp(joinpath(pkg_dir, "REQUIRE")) == "julia $vf\n$test_pkg" rm(joinpath(pkg_dir, "REQUIRE")) @test gen_tests(test_pkg, t) == ["test/"]