|
| 1 | +using Base64 |
| 2 | + |
| 3 | +const BUILDROOT = get(ENV, "BUILDROOT", pwd()) |
| 4 | +const JULIA_SOURCE = get(ENV, "JULIA_SOURCE", "$(BUILDROOT)/julia") |
| 5 | +const JULIA_DOCS = get(ENV, "JULIA_DOCS", "$(BUILDROOT)/docs.julialang.org") |
| 6 | + |
| 7 | +# download and extract binary for a given version, return path to executable |
| 8 | +function download_release(v::VersionNumber) |
| 9 | + x, y, z = v.major, v.minor, v.patch |
| 10 | + julia_exec = cd(BUILDROOT) do |
| 11 | + julia = "julia-$(x).$(y).$(z)-linux-x86_64" |
| 12 | + tarball = "$(julia).tar.gz" |
| 13 | + sha256 = "julia-$(x).$(y).$(z).sha256" |
| 14 | + run(`curl -o $(tarball) -L https://julialang-s3.julialang.org/bin/linux/x64/$(x).$(y)/$(tarball)`) |
| 15 | + run(`curl -o $(sha256) -L https://julialang-s3.julialang.org/bin/checksums/$(sha256)`) |
| 16 | + run(pipeline(`grep $(tarball) $(sha256)`, `sha256sum -c`)) |
| 17 | + mkpath(julia) |
| 18 | + run(`tar -xzf $(tarball) -C $(julia) --strip-components 1`) |
| 19 | + return abspath(julia, "bin", "julia") |
| 20 | + end |
| 21 | + return julia_exec |
| 22 | +end |
| 23 | +# download and extract nightly binary, return path to executable and commit |
| 24 | +function download_nightly() |
| 25 | + julia_exec, commit = cd(BUILDROOT) do |
| 26 | + julia = "julia-latest-linux64" |
| 27 | + tarball = "$(julia).tar.gz" |
| 28 | + run(`curl -o $(tarball) -L https://julialangnightlies-s3.julialang.org/bin/linux/x64/$(tarball)`) |
| 29 | + # find the commit from the extracted folder |
| 30 | + folder = first(readlines(`tar -tf $(tarball)`)) |
| 31 | + _, commit = split(folder, '-'); commit = chop(commit) |
| 32 | + mkpath(julia) |
| 33 | + run(`tar -xzf $(tarball) -C $(julia) --strip-components 1`) |
| 34 | + return abspath(julia, "bin", "julia"), commit |
| 35 | + end |
| 36 | + return julia_exec, commit |
| 37 | +end |
| 38 | + |
| 39 | +function build_release_pdf(v::VersionNumber) |
| 40 | + x, y, z = v.major, v.minor, v.patch |
| 41 | + @info "building PDF for Julia v$(x).$(y).$(z)." |
| 42 | + |
| 43 | + file = "julia-$(x).$(y).$(z).pdf" |
| 44 | + path = "$(JULIA_DOCS)/$(file)" |
| 45 | + |
| 46 | + # early return if file exists |
| 47 | + if isfile(path) |
| 48 | + @info "PDF for Julia v$(x).$(y).$(z) already exists, skipping." |
| 49 | + return |
| 50 | + end |
| 51 | + |
| 52 | + # download julia binary |
| 53 | + @info "downloading release tarball." |
| 54 | + julia_exec = download_release(v) |
| 55 | + |
| 56 | + # checkout relevant tag and build the PDF |
| 57 | + run(`git -C $(JULIA_SOURCE) checkout v$(x).$(y).$(z)`) |
| 58 | + run(`git -C $(JULIA_SOURCE) clean -fdx`) |
| 59 | + withenv("DOCUMENTER_VERBOSE" => "true", |
| 60 | + "TRAVIS_REPO_SLUG" => nothing, # workaround Documenter bugs and julia#26314 |
| 61 | + "BUILDROOT" => nothing) do |
| 62 | + run(`make -C $(JULIA_SOURCE)/doc pdf texplatform=docker JULIA_EXECUTABLE=$(julia_exec)`) |
| 63 | + end |
| 64 | + |
| 65 | + # copy built PDF to JULIA_DOCS |
| 66 | + output = "$(JULIA_SOURCE)/doc/_build/pdf/en" |
| 67 | + for f in readdir(output) |
| 68 | + if startswith(f, "TheJuliaLanguage") && endswith(f, ".pdf") |
| 69 | + cp("$(output)/$(f)", path) |
| 70 | + @info "finished, output file copied to $(path)." |
| 71 | + break |
| 72 | + end |
| 73 | + end |
| 74 | +end |
| 75 | + |
| 76 | +function build_nightly_pdf() |
| 77 | + @info "downloading nightly tarball" |
| 78 | + julia_exec, commit = download_nightly() |
| 79 | + # output is "julia version 1.1.0-DEV" |
| 80 | + _, _, v = split(readchomp(`$(julia_exec) --version`)) |
| 81 | + @info "commit determined to $(commit) and version determined to $(v)." |
| 82 | + file = "julia-$(v).pdf" |
| 83 | + path = "$JULIA_DOCS/$file" |
| 84 | + run(`git -C $(JULIA_SOURCE) checkout $(commit)`) |
| 85 | + run(`git -C $(JULIA_SOURCE) clean -fdx`) |
| 86 | + withenv("DOCUMENTER_VERBOSE" => "true", |
| 87 | + "TRAVIS_REPO_SLUG" => nothing, # workaround Documenter bugs and julia#26314 |
| 88 | + "BUILDROOT" => nothing) do |
| 89 | + run(`make -C $(JULIA_SOURCE)/doc pdf texplatform=docker JULIA_EXECUTABLE=$(julia_exec)`) |
| 90 | + end |
| 91 | + |
| 92 | + # copy the built PDF to JULIA_DOCS |
| 93 | + output = "$(JULIA_SOURCE)/doc/_build/pdf/en" |
| 94 | + for f in readdir(output) |
| 95 | + if startswith(f, "TheJuliaLanguage") && endswith(f, ".pdf") |
| 96 | + cp("$(output)/$(f)", path) |
| 97 | + @info "finished, output file copied to $(path)." |
| 98 | + break |
| 99 | + end |
| 100 | + end |
| 101 | +end |
| 102 | + |
| 103 | +# find all tags in the julia repo |
| 104 | +function collect_versions() |
| 105 | + str = read(`git -C $(JULIA_SOURCE) ls-remote --tags origin`, String) |
| 106 | + versions = VersionNumber[] |
| 107 | + for line in eachline(IOBuffer(str)) |
| 108 | + # lines are in the form 'COMMITSHA\trefs/tags/TAG' |
| 109 | + _, ref = split(line, '\t') |
| 110 | + _, _, tag = split(ref, '/') |
| 111 | + if occursin(Base.VERSION_REGEX, tag) |
| 112 | + v = VersionNumber(tag) |
| 113 | + # pdf doc only possible for 1.0.3 and above |
| 114 | + v >= v"1.0.4" && push!(versions, v) |
| 115 | + end |
| 116 | + end |
| 117 | + return versions |
| 118 | +end |
| 119 | + |
| 120 | +function withfile(f, file, contents) |
| 121 | + hasfile = isfile(file) |
| 122 | + original = hasfile ? read(file) : nothing |
| 123 | + write(file, contents) |
| 124 | + try |
| 125 | + f() |
| 126 | + finally |
| 127 | + hasfile ? write(file, original) : rm(file) |
| 128 | + end |
| 129 | +end |
| 130 | + |
| 131 | +# similar to Documenter.deploydocs |
| 132 | +function commit() |
| 133 | + if get(ENV, "TRAVIS_PULL_REQUEST", "true") != "false" |
| 134 | + @info "skipping commit from pull requests." |
| 135 | + return |
| 136 | + end |
| 137 | + @info "committing built PDF files." |
| 138 | + |
| 139 | + # initialize git |
| 140 | + run(`git config user.name "zeptodoctor"`) |
| 141 | + run( `git config user.email "[email protected]"`) |
| 142 | + # committing all .pdf files |
| 143 | + run(`git add '*.pdf'`) |
| 144 | + run(`git commit --amend --date=now -m "PDF versions of Julia's manual."`) |
| 145 | + |
| 146 | + # setting up ssh key and force push |
| 147 | + keyfile = abspath(".documenter") |
| 148 | + try |
| 149 | + write(keyfile, String(base64decode(get(ENV, "DOCUMENTER_KEY_PDF", "")))) |
| 150 | + chmod(keyfile, 0o600) |
| 151 | + withfile("$(homedir())/.ssh/config", |
| 152 | + """ |
| 153 | + Host github.com |
| 154 | + StrictHostKeyChecking no |
| 155 | + HostName github.com |
| 156 | + IdentityFile $keyfile |
| 157 | + BatchMode yes |
| 158 | + """) do |
| 159 | + run( `git remote set-url origin [email protected]:JuliaLang/docs.julialang.org.git`) |
| 160 | + run(`git push -f origin assets`) |
| 161 | + end |
| 162 | + finally |
| 163 | + rm(keyfile; force=true) |
| 164 | + end |
| 165 | +end |
| 166 | + |
| 167 | +function main() |
| 168 | + if "releases" in ARGS |
| 169 | + @info "building PDFs for all applicable Julia releases." |
| 170 | + foreach(build_release_pdf, collect_versions()) |
| 171 | + elseif "nightly" in ARGS |
| 172 | + @info "building PDF for Julia nightly." |
| 173 | + build_nightly_pdf() |
| 174 | + elseif "commit" in ARGS |
| 175 | + @info "deploying to JuliaLang/docs.julialang.org" |
| 176 | + cd(() -> commit(), JULIA_DOCS) |
| 177 | + end |
| 178 | +end |
| 179 | + |
| 180 | +if abspath(PROGRAM_FILE) == @__FILE__ |
| 181 | + main() |
| 182 | +end |
0 commit comments