From efb5816bc1e70fdd7b928f97384104a40afd5082 Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Fri, 2 Oct 2020 22:10:53 +0200 Subject: [PATCH] replace build.jl by Artifacts.toml (#3023) * replace build.jl by Artifacts.toml * check ENV * fix it --- Artifacts.toml | 7 +++++++ Project.toml | 1 + deps/build.jl | 18 ------------------ src/Plots.jl | 11 ----------- src/backends/plotly.jl | 2 +- src/backends/web.jl | 2 +- src/ijulia.jl | 2 +- src/init.jl | 22 +++++++++++++++------- 8 files changed, 26 insertions(+), 39 deletions(-) create mode 100644 Artifacts.toml delete mode 100644 deps/build.jl diff --git a/Artifacts.toml b/Artifacts.toml new file mode 100644 index 00000000..a14162cd --- /dev/null +++ b/Artifacts.toml @@ -0,0 +1,7 @@ +[plotly] +git-tree-sha1 = "f03dd0451a05a5fdbbcb3e548998a9d7a1ab6368" +lazy = true + + [[plotly.download]] + url = "https://cdn.plot.ly/plotly-1.54.2.min.js" + sha256 = "487f1da6b7a1f127de59af8a65bb3fe2c63d709f011afea82896bdce28ecc0f8" diff --git a/Project.toml b/Project.toml index 371c606b..55fefa4c 100644 --- a/Project.toml +++ b/Project.toml @@ -16,6 +16,7 @@ JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Measures = "442fdcdd-2543-5da2-b0f3-8c86c306513e" NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3" +Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" PlotThemes = "ccf2f8ad-2431-5c83-bf29-c5338b663b6a" PlotUtils = "995b91a9-d308-5afd-9ec6-746e21dbc043" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" diff --git a/deps/build.jl b/deps/build.jl deleted file mode 100644 index b174ab93..00000000 --- a/deps/build.jl +++ /dev/null @@ -1,18 +0,0 @@ - -#TODO: download https://cdn.plot.ly/plotly-1.54.2.min.js to deps/ if it doesn't exist -file_path = "" -if get(ENV, "PLOTS_HOST_DEPENDENCY_LOCAL", "false") == "true" - global file_path - local_fn = joinpath(dirname(@__FILE__), "plotly-1.54.2.min.js") - if !isfile(local_fn) - @info("Cannot find deps/plotly-1.54.2.min.js... downloading latest version.") - download("https://cdn.plot.ly/plotly-1.54.2.min.js", local_fn) - isfile(local_fn) && (file_path = local_fn) - else - file_path = local_fn - end -end - -open("deps.jl", "w") do io - println(io, "const plotly_local_file_path = $(repr(file_path))") -end diff --git a/src/Plots.jl b/src/Plots.jl index 0fee5a51..b67abe3f 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -25,17 +25,6 @@ import JSON using Requires -if isfile(joinpath(@__DIR__, "..", "deps", "deps.jl")) - include(joinpath(@__DIR__, "..", "deps", "deps.jl")) -else - # This is a bit dirty, but I don't really see why anyone should be forced - # to build Plots, while it will just include exactly the below line - # as long as `ENV["PLOTS_HOST_DEPENDENCY_LOCAL"] = "true"` is not set. - # If the above env is set + `plotly_local_file_path == ""``, - # it will warn in the __init__ function to run build - const plotly_local_file_path = "" -end - export grid, bbox, diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index 063bf05b..97710631 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -875,7 +875,7 @@ html_body(plt::Plot{PlotlyBackend}) = plotly_html_body(plt) const ijulia_initialized = Ref(false) function plotly_html_head(plt::Plot) - local_file = ("file:///" * plotly_local_file_path) + local_file = ("file:///" * plotly_local_file_path[]) plotly = use_local_dependencies[] ? local_file : "https://cdn.plot.ly/plotly-1.54.2.min.js" diff --git a/src/backends/web.jl b/src/backends/web.jl index b1663435..d73fa067 100644 --- a/src/backends/web.jl +++ b/src/backends/web.jl @@ -45,7 +45,7 @@ function standalone_html_window(plt::AbstractPlot) old = use_local_dependencies[] # save state to restore afterwards # if we open a browser ourself, we can host local files, so # when we have a local plotly downloaded this is the way to go! - use_local_dependencies[] = isfile(plotly_local_file_path) + use_local_dependencies[] = isfile(plotly_local_file_path[]) filename = write_temp_html(plt) open_browser_window(filename) # restore for other backends diff --git a/src/ijulia.jl b/src/ijulia.jl index 1f0ef219..4e543891 100644 --- a/src/ijulia.jl +++ b/src/ijulia.jl @@ -4,7 +4,7 @@ const use_local_plotlyjs = Ref(false) function _init_ijulia_plotting() # IJulia is more stable with local file - use_local_plotlyjs[] = isfile(plotly_local_file_path) + use_local_plotlyjs[] = isfile(plotly_local_file_path[]) ENV["MPLBACKEND"] = "Agg" end diff --git a/src/init.jl b/src/init.jl index b46e5ddc..54bac3b2 100644 --- a/src/init.jl +++ b/src/init.jl @@ -1,5 +1,7 @@ using REPL +using Pkg.Artifacts +const plotly_local_file_path = Ref("") function _plots_defaults() if isdefined(Main, :PLOTS_DEFAULTS) @@ -74,16 +76,22 @@ function __init__() end end - if haskey(ENV, "PLOTS_HOST_DEPENDENCY_LOCAL") - use_local_plotlyjs[] = ENV["PLOTS_HOST_DEPENDENCY_LOCAL"] == "true" - use_local_dependencies[] = isfile(plotly_local_file_path) && use_local_plotlyjs[] - if use_local_plotlyjs[] && !isfile(plotly_local_file_path) - @warn("PLOTS_HOST_DEPENDENCY_LOCAL is set to true, but no local plotly file found. run Pkg.build(\"Plots\") and make sure PLOTS_HOST_DEPENDENCY_LOCAL is set to true") + if get(ENV, "PLOTS_HOST_DEPENDENCY_LOCAL", "false") == true + artifact_toml = joinpath(@__DIR__, "Artifacts.toml") + + plotly_sha = artifact_hash("plotly", artifact_toml) + if plotly_sha == nothing || !artifact_exists(plotly_sha) + plotly_sha = create_artifact() do artifact_dir + download("https://cdn.plot.ly/plotly-1.54.2.min.js", joinpath(artifact_dir, "plotly-1.54.2.min.js")) + end end - else - use_local_dependencies[] = use_local_plotlyjs[] + + plotly_local_file_path[] = joinpath(artifact_path(plotly_sha), "plotly-1.54.2.min.js") + use_local_plotlyjs[] = true end + use_local_dependencies[] = use_local_plotlyjs[] + @require FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549" begin _show(io::IO, mime::MIME"image/png", plt::Plot{<:PDFBackends}) = _show_pdfbackends(io, mime, plt)