-
Notifications
You must be signed in to change notification settings - Fork 24
/
utils.jl
291 lines (249 loc) · 8.58 KB
/
utils.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using LiveServer
export
build_templates,
serve_templates
"""
newsite(topdir; template="basic", cd=true)
Generate a new folder (an error is thrown if it already exists) that contains
the skeleton of a website that can be processed by Franklin. The user can
specify a `template` out of the list of available templates.
If `topdir` is specified as `"."` then the current directory is used.
* `template="basic"`: name of the template to use,
* `cd=true`: whether to change the current directory to the newly
created folder or not.
* `verbose=true`: whether to display information or not.
### Example
```julia
newsite("MyNewWebsite", template="pure-sm")
```
"""
function newsite(topdir::String="TestWebsite";
template::String="basic", changedir::Bool=true,
verbose::Bool=true)
template = lowercase(template)
template ∈ LIST_OF_TEMPLATES ||
throw(ArgumentError("Template $template doesn't exist."))
# create the top-directory
if topdir == "."
topdir = pwd()
changedir = true
else
topdir = mkdir(topdir)
end
common_dir = joinpath(TEMPL_PATH, "common")
template_dir = joinpath(TEMPL_PATH, template)
mergefolders(common_dir, topdir)
mergefolders(template_dir, topdir)
# for sandbox, remove aux pages menu1,menu2,menu3
if template in ("sandbox", "sandbox-extended")
for i in 1:3
rm(joinpath(topdir, "menu$i.md"))
end
end
# For 'added' packages, Pkg.jl makes some files read-only, so here we
# restore to 644 to guarantee that they are r/w
for (root, _, files) ∈ walkdir(topdir)
for file in files
chmod(joinpath(root, file), 0o644)
end
end
# check if the template has a pre-specified index.html, if so rename
# index.md to avoid confusion
if isfile(joinpath(topdir, "index.html"))
mv(joinpath(topdir, "index.md"), joinpath(topdir, "franklin.md"))
end
# move to the directory if relevant
changedir && cd(topdir)
# display information as adequate
verbose && begin
print("✓ Website folder generated at \"$(topdir)\"")
println(ifelse(changedir, " (now the current directory)." , "."))
println("→ Use serve() from Franklin to see the website in your browser.")
end
return nothing
end
"""
modify(template_name)
Convenience function to quickly instantiate a directory with a given template
once satisfied with the changes, they can be added to the repo by doing
`addtemplate(path)`.
"""
function modify(name)
@assert name in LIST_OF_TEMPLATES "unknown template"
newsite(name, template=name)
return nothing
end
"""
addtemplate(path)
Assuming you're working with FranklinTemplates on development mode (i.e.
`] dev FranklinTemplates` this function takes a path where you would have
worked on a template and incorporates it into FranklinTemplates, putting files
at the right place; this makes iterations and PRs easier.
Note: if you use this without FranklinTemplates on dev mode, bad things will
likely occur.
"""
function addtemplate(path::String)
path = abspath(path)
@assert isdir(path) "'$path' does not seem to be a valid directory."
# check whether it looks like a sensible dir
rd = readdir(path)
if !all(p->p in rd, ("_css", "_layout", "_libs"))
error("The directory $path does not contain a `_css`, `_layout` and " *
"a `_libs` folder and so will be ignored.")
end
# Add the folder at the right place in FranklinTemplates
#
# does it exist?
d, tname = splitdir(path)
isempty(tname) && (tname = splitdir(d)[2]) # it ended with separator
dest = joinpath(TEMPL_PATH, tname, "")
common = joinpath(TEMPL_PATH, "common", "")
if tname ∉ readdir(TEMPL_PATH)
cp(path, dest)
else
# it exists already --> merge the folders
mergefolders(path, dest)
end
# postprocess
if isdir(joinpath(dest, "__site"))
rm(joinpath(dest, "__site"), recursive=true, force=true)
end
# remove duplicates
deduplicate(common, dest)
return nothing
end
"""
mergefolders(src, dst)
Internal function to looks at what's inside `src/` and put it in `dst/`. If
there are paths that match, the files are merged. It is assumed that files will
not clash, if they clash files in `dst` are replaced.
See also [`newsite`](@ref).
"""
function mergefolders(src, dst)
for (root, _, files) ∈ walkdir(src)
for file ∈ files
newpath = replace(root, Regex("^$(escape_string(src))")=>"$dst")
isdir(newpath) || mkpath(newpath)
newpathfile = joinpath(newpath, file)
cp(joinpath(root, file), newpathfile; force=true)
end
end
end
"""
deduplicate(src, dest)
Consider the files provided by `src` and remove all identical files from `dest`
to avoid duplication.
"""
function deduplicate(src, dest)
# Post processs by removing files which are already provided by src
dest = joinpath(dest, "") # ensure it has a separator at the end
for (root, _, files) in walkdir(dest)
for file in files
fpath = joinpath(root, file)
rpath = replace(fpath, dest => "")
cpath = joinpath(src, rpath)
isfile(cpath) || continue
if filecmp(fpath, cpath)
rm(fpath)
end
end
end
# there may be empty folders (these would be ignored by GitHub anyway)
emptydirs = []
for (root, dirs, _) in walkdir(dest)
for dir in dirs
dpath = joinpath(root, dir)
if isdir(dpath) && isempty(readdir(dpath))
push!(emptydirs, dpath)
end
end
end
for dpath in emptydirs
try; rm(dpath, recursive=true); catch; end
end
return nothing
end
"""
filecmp(path1, path2)
Take 2 absolute paths and check if the files are different (return false if
different and true otherwise).
This code was suggested by Steven J. Johnson on discourse:
https://discourse.julialang.org/t/how-to-obtain-the-result-of-a-diff-between-2-files-in-a-loop/23784/4
"""
function filecmp(path1::AbstractString, path2::AbstractString)
stat1, stat2 = stat(path1), stat(path2)
if !(isfile(stat1) && isfile(stat2)) || filesize(stat1) != filesize(stat2)
return false
end
compare_buffers(b1, b2, n) = begin
if VERSION > v"1.10.0-"
Base.memcmp(
Base.unsafe_convert(Ptr{UInt8}, b1),
Base.unsafe_convert(Ptr{UInt8}, b2),
n
)
else
Base._memcmp(b1, b2, n)
end
end
stat1 == stat2 && return true # same fileq
open(path1, "r") do file1
open(path2, "r") do file2
buf1 = Vector{UInt8}(undef, 32768)
buf2 = similar(buf1)
while !eof(file1) && !eof(file2)
n1 = readbytes!(file1, buf1)
n2 = readbytes!(file2, buf2)
n1 != n2 && return false
0 != compare_buffers(buf1, buf2, n1) && (@show "hello"; return false)
end
return eof(file1) == eof(file2)
end
end
end
"""
build_templates()
Build all the templates.
"""
function build_templates()
root = dirname(dirname(@__FILE__))
make_path = joinpath(root, "docs", "make.jl")
include(make_path)
for (root, _, files) ∈ walkdir(joinpath(root, "docs", "build"))
for file ∈ files
endswith(file, ".html") || continue;
path = joinpath(root, file);
htmls = read(path, String);
if get(ENV, "CI", nothing) == "true"
htmls = replace(htmls, "href=\"/" => "href=\"/FranklinTemplates.jl/");
htmls = replace(htmls, "src=\"/" => "src=\"/FranklinTemplates.jl/");
end
write(path, htmls);
end
end
end
"""
serve_templates()
Rebuild all templates upon detecting changes.
"""
function serve_templates()
build_templates()
root = dirname(dirname(@__FILE__))
function custom_callback(fp::AbstractString)
if contains(fp, "src/")
build_templates()
cd(joinpath(root, "docs", "build"))
LiveServer.file_changed_callback(fp)
end
end
sw = LiveServer.SimpleWatcher(custom_callback)
# Source files to watch for changes.
for (root, dirs, files) in walkdir(joinpath(root, "src"))
for file in files
path = joinpath(root, file)
LiveServer.watch_file!(sw, path)
end
end
cd(joinpath(root, "docs", "build"))
LiveServer.serve(sw; verbose=true)
end