-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbuild-site.jl
More file actions
124 lines (112 loc) · 3.38 KB
/
build-site.jl
File metadata and controls
124 lines (112 loc) · 3.38 KB
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
const site_header = """
<!DOCTYPE html>
<html lang = "en">
<HEAD>
<meta charset="UTF-8"/>
<title>JuliaSmoothOptimizers Tutorials</title>
</HEAD>
<BODY>
<h1>JuliaSmoothOptimizers Tutorials</h1>
"""
const site_footer = """
</BODY>
"""
function generate_site()
pr = get(ENV, "TRAVIS_PULL_REQUEST", nothing)
if pr === nothing # Local build
# Removing "site" since this is a test
if isdir(joinpath(@__DIR__, "site"))
rm(joinpath(@__DIR__, "site"), recursive = true)
end
elseif pr != "false"
# This is a PR. Don't generate the site, as it can't be used
return
end
build_dir = joinpath(@__DIR__, "site")
mkpath(build_dir)
function titlename(name)
titlecase(join(split(name, "-"), " "))
end
cp(joinpath(@__DIR__, "jso.png"), joinpath(build_dir, "jso.png"))
cp(joinpath(@__DIR__, "jso-banner.png"), joinpath(build_dir, "jso-banner.png"))
open(joinpath(build_dir, "index.html"), "w") do io
println(io, site_header)
println(io, "<ul>")
dirs = filter(
x -> isdir(joinpath(@__DIR__, x)) && x[1] != '.' && !(x in ["site", "src", "test"]),
readdir(@__DIR__),
)
for dir in dirs
print(io, "<li>")
print(io, titlename(dir))
println(io, "</li>")
println(io, "<ul>")
subdirs = readdir(joinpath(@__DIR__, dir))
for subdir in subdirs
fullpath = joinpath(dir, subdir, subdir)
print(io, "<li><a href=\"$fullpath.html\">$(titlename(subdir))</a> - Download")
for ft in ("ipynb", "jl")
print(io, " <a href=\"$fullpath.$ft\">$ft</a>")
end
println(io, "</li>")
src = joinpath(@__DIR__, dir, subdir)
dst = joinpath(build_dir, dir, subdir)
mkpath(dst)
for file in readdir(src)
if !(split(file, ".")[end] in ["jmd", "toml"])
cp(joinpath(src, file), joinpath(dst, file))
end
end
end
println(io, "</ul>")
end
println(io, "</ul>")
println(io, site_footer)
end
end
function push_to_gh_pages()
pr = get(ENV, "TRAVIS_PULL_REQUEST", nothing)
if pr === nothing
println("Local build. Not pushing")
return
elseif pr != "false"
println("PR. Not pushing")
return
end
key = get(ENV, "GITHUB_AUTH", nothing)
if key === nothing
error("GITHUB_AUTH not found.")
end
repo = ENV["TRAVIS_REPO_SLUG"]
user = split(repo, "/")[1]
upstream = "https://$user:$key@github.com/$repo"
run(`git remote add upstream $upstream`)
run(`git fetch --all`)
if !success(`git checkout -f -b gh-pages upstream/gh-pages`)
run(`git checkout --orphan gh-pages`)
run(`git reset --hard`)
run(`git commit --allow-empty -m "Initial commit"`)
end
dst = ENV["TRAVIS_BRANCH"]
if dst == "main"
dst = "."
else
# When fixing a PR, the branch folder in gh-pages needs to be removed so it can be created again
isdir(dst) && rm(dst, recursive = true)
mkpath(dst)
end
for d in readdir("site")
mv("site/$d", "$dst/$d", force = true)
end
rm("site")
run(`git add $dst`)
if !success(`git diff --cached --exit-code`) # Don't commit if there are no changes
run(`git commit -m ":robot: Building tutorials pages"`)
run(`git push upstream gh-pages`)
end
site = "https://$user.github.io/JSOTutorials.jl/" * (dst == "." ? "" : "$dst/") * "index.html"
println("Here is your site:")
println(" \033[1;33m$site\033[0m")
end
generate_site()
push_to_gh_pages()