Skip to content

Commit

Permalink
Merge 22862f4 into 4ded59d
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger-luo committed Sep 19, 2020
2 parents 4ded59d + 22862f4 commit 79c8f0a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 52 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -15,4 +15,5 @@ Manifest.toml
/test/bin/
/test/completions/
/test/Foo/deps/
/test/Foo/Manifest.toml
/test/Foo/Manifest.toml
!/test/Foo/deps/precompile.jl
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -13,7 +13,7 @@ Roger's magic book for command line interfaces.
<p>
Comonicon is a &nbsp;
<a href="https://julialang.org">
<img src="https://julialang.org/favicon.ico" width="16em">
<img src="https://julialang.org/assets/infra/julia.ico" width="16em">
Julia Language
</a>
&nbsp; package. To install Comonicon,
Expand Down
67 changes: 27 additions & 40 deletions src/tools/build.jl
Expand Up @@ -30,6 +30,10 @@ const COMONICON_URL = "https://github.com/Roger-luo/Comonicon.jl"
# filter_stdlibs=true
# cpu_target="native"

# [sysimg.precompile]
# execution_file = ["deps/precopmile.jl"]
# statements_file = ["deps/statements.jl"]

# [download]
# host="github.com"
# user="Roger-luo"
Expand All @@ -47,6 +51,10 @@ const DEFAULT_SYSIMG_CONFIG = Dict(
"incremental" => true,
"filter_stdlibs" => false,
"cpu_target" => "native",
"precompile" => Dict{String, Vector{String}}(
"statements_file" => String[],
"execution_file" => String[],
)
)

const COMONICON_TOML = ["Comonicon.toml", "JuliaComonicon.toml"]
Expand Down Expand Up @@ -86,9 +94,19 @@ function read_configs(mod; kwargs...)
install_configs[string(k)] = v
end

if k in [:path, :incremental, :filter_stdlibs, :cpu_target]
if k in [
:path, :incremental, :filter_stdlibs, :cpu_target, :precompile,
# NOTE: we handle these two kwargs in a more flatten way
:precompile_execution_file, :precompile_statements_file
]
sysimg_configs = get!(configs, "sysimg", Dict{String,Any}())
sysimg_configs[string(k)] = v

if k in [:precompile_execution_file, :precompile_statements_file]
precompile_configs = get!(sysimg_configs, "precompile", Dict{String, Vector{String}}())
precompile_configs[string(k)[12:end]] = v
else
sysimg_configs[string(k)] = v
end
end

if k in [:host, :repo, :user]
Expand Down Expand Up @@ -117,6 +135,11 @@ end
function merge_defaults(mod, configs)
if haskey(configs, "sysimg")
configs["sysimg"] = merge(DEFAULT_SYSIMG_CONFIG, configs["sysimg"])

if haskey(configs["sysimg"], "precompile")
pr_cfg = configs["sysimg"]["precompile"]
configs["sysimg"]["precompile"] = merge(DEFAULT_SYSIMG_CONFIG["precompile"], pr_cfg)
end
end

configs["install"] = merge(DEFAULT_INSTALL_CONFIG, configs["install"])
Expand Down Expand Up @@ -331,13 +354,6 @@ function build_sysimg(mod::Module, configs::Dict)
mkpath(lib_path)
end

# install precompile script
precompile_jl = PATH.project(mod, "deps", "precompile.jl")
@info "generating precompile execution file: $precompile_jl"
open(precompile_jl, "w+") do f
print(f, precompile_script(mod))
end

project = PATH.project(mod)
incremental = sysimg_configs["incremental"]
filter_stdlibs = sysimg_configs["filter_stdlibs"]
Expand All @@ -354,7 +370,8 @@ function build_sysimg(mod::Module, configs::Dict)
sysimage_path = image_path,
incremental = incremental,
project = project,
precompile_execution_file = [precompile_jl, PATH.project(mod, "test", "runtests.jl")],
precompile_statements_file = sysimg_configs["precompile"]["statements_file"],
precompile_execution_file = sysimg_configs["precompile"]["execution_file"],
cpu_target = cpu_target,
filter_stdlibs = filter_stdlibs,
)
Expand Down Expand Up @@ -446,36 +463,6 @@ function cmd_script(
return join(script, " \\\n ")
end

"""
precompile_script(mod)
Generates a script to execute as `precompile_execution_file` for all the commands.
"""
function precompile_script(mod::Module)
script = "using $mod;\n$mod.command_main([\"-h\"]);\n"

if isdefined(mod, :CASTED_COMMANDS)
for (name, cmd) in mod.CASTED_COMMANDS
if name != "main" # skip main command
script *= "$mod.command_main([$(precompile_script(mod, cmd))]);\n"
end
end
end
return script
end

function precompile_script(mod::Module, cmd::EntryCommand)
return precompile_script(mod, cmd.root)
end

function precompile_script(mod::Module, cmd::LeafCommand)
return "\"$(cmd_name(cmd))\", \"-h\""
end

function precompile_script(mod::Module, cmd::NodeCommand)
return join(map(x -> "\"$(cmd_name(cmd))\", " * precompile_script(mod, x), cmd.subcmds))
end

Base.write(x::EntryCommand) = write(cachefile(), x)

"""
Expand Down
3 changes: 3 additions & 0 deletions test/Foo/Comonicon.toml
Expand Up @@ -12,6 +12,9 @@ incremental=true
filter_stdlibs=false
cpu_target="native"

[sysimg.precompile]
execution_file = ["deps/precopmile.jl"]

[download]
host="github.com"
user="Roger-luo"
Expand Down
3 changes: 3 additions & 0 deletions test/build.jl
Expand Up @@ -52,6 +52,9 @@ d = Dict(
"cpu_target" => "native",
"incremental" => true,
"path" => "deps/lib",
"precompile" => Dict(
"execution_file" => ["deps/precopmile.jl"],
)
),
)

Expand Down
11 changes: 1 addition & 10 deletions test/parse.jl
Expand Up @@ -2,7 +2,7 @@ using Comonicon
using Markdown
using Comonicon.Types
using Comonicon.Parse
using Comonicon.BuildTools: precompile_script, install
using Comonicon.BuildTools: install
using Test

module Dummy
Expand Down Expand Up @@ -111,15 +111,6 @@ end
@test Dummy.command_main(String["tick", "1.0", "2.0"]) == 0
@test Dummy.command_main(String["tick", "1.0"]) == 0


@test precompile_script(Dummy) == """
using Main.Dummy;
Main.Dummy.command_main(["-h"]);
Main.Dummy.command_main(["goo", "-h"]);
Main.Dummy.command_main(["tick", "-h"]);
Main.Dummy.command_main(["foo", "-h"]);
"""

empty!(ARGS)
append!(ARGS, ["2", "--opt1", "3"])

Expand Down

0 comments on commit 79c8f0a

Please sign in to comment.