This repository has been archived by the owner on Jun 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
skeleton.jl
executable file
·99 lines (81 loc) · 2.89 KB
/
skeleton.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env julia
using Pkg
# print some version information for debugging
githash =
try
skeletondir = @__DIR__
chomp(read(`git -C $skeletondir log -n 1 --pretty=format:"%H"`, String))
catch
"(not a repo)"
end
@info "skeleton.jl" Julia_VERSION = VERSION githash = githash
# sanity checks and parsing input arguments
minver = v"0.7"
err(msg) = (printstyled(stderr, msg * '\n'; bold = true, color = :blue); exit(1))
VERSION ≥ minver || err("need at least version $(minver)")
1 ≤ length(ARGS) ≤ 2 || err("Usage: [julia] skeleton.jl destination [template]")
destdir = ARGS[1]
isfile(destdir) && err("destination $(destdir) is a file")
srcdir = length(ARGS) == 2 ? ARGS[2] : joinpath(@__DIR__, "template")
pkgname = basename(destdir)
if isempty(pkgname)
pkgname = basename(dirname(destdir)) # add trailing /
end
# utility functions
"Get an option from `git`."
function getgitopt(opt)
try
chomp(read(`git config --get $(opt)`, String))
catch
err("couldn't get option $(opt)")
end
end
"Replace multiple pairs in `str`, using `replace` iteratively."
function replace_multiple(str, replacements)
for pair in replacements
str = replace(str, pair)
end
str
end
"""
Copy from `srcdir` to `destdir` recursively, making the substitutions of contents and
filenames using `replacements`.
"""
function copy_and_substitute(srcdir, destdir, replacements)
for (root, dirs, files) in walkdir(srcdir)
relroot = relpath(root, srcdir)
mkpath(normpath(destdir, relroot))
for file in files
srcfile = joinpath(root, file)
destfile = normpath(joinpath(destdir, relroot,
replace_multiple(file, replacements)))
if isfile(destfile)
println("$(destfile) exists, skipping")
else
println("$(srcfile) => $(destfile)")
srcstring = read(srcfile, String)
deststring = replace_multiple(srcstring, replacements)
write(destfile, deststring)
end
end
end
end
# generate
if isdir(destdir)
@info "destination directory exists, skipping package generation"
exit(0)
end
replacements = ["{UUID}" => Pkg.METADATA_compatible_uuid(pkgname),
"{PKGNAME}" => pkgname,
"{GHUSER}" => getgitopt("github.user"),
"{USERNAME}" => getgitopt("user.name"),
"{USEREMAIL}" => getgitopt("user.email")]
@info "copy and substitute" srcdir destdir replacements
copy_and_substitute(srcdir, destdir, replacements)
@info "git init"
run(`git init $destdir`)
@info "adding documenter (completing the Manifest.toml for docs)"
run(`$(Base.julia_cmd()) --project=$(joinpath(destdir, "docs")) -e 'import Pkg; Pkg.add("Documenter")'`)
# cd(destdir)
# run(`git add --all`)
# run(`git commit -am "Initial commit (skeleton.jl)."`)