From 28c18ba204099ab973be50d572a3a79f55ec4853 Mon Sep 17 00:00:00 2001 From: Kevin Bonham Date: Thu, 19 Oct 2023 10:56:21 -0400 Subject: [PATCH 1/5] add Preferences --- Project.toml | 1 + src/Scratch.jl | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 642abd2..654c4bf 100644 --- a/Project.toml +++ b/Project.toml @@ -5,6 +5,7 @@ version = "1.2.0" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" +Preferences = "21216c6a-2e73-6563-6e65-726566657250" [compat] julia = "1" diff --git a/src/Scratch.jl b/src/Scratch.jl index 2c1ef21..98211c9 100644 --- a/src/Scratch.jl +++ b/src/Scratch.jl @@ -2,6 +2,16 @@ module Scratch import Base: UUID using Dates +@static if VERSION >= v"1.6" + using Preferences +end + +@static if VERSION >= v"1.6" + const scratch_base_path = @load_preference("scratch_dir", joinpath(first(Base.DEPOT_PATH), "scratchspaces")) +else + const scratch_base_path = joinpath(first(Base.DEPOT_PATH), "scratchspaces") +end + export with_scratch_directory, scratch_dir, get_scratch!, delete_scratch!, clear_scratchspaces!, @get_scratch! const SCRATCH_DIR_OVERRIDE = Ref{Union{String,Nothing}}(nothing) @@ -30,7 +40,7 @@ be overridden via `with_scratch_directory()`. """ function scratch_dir(args...) if SCRATCH_DIR_OVERRIDE[] === nothing - return abspath(first(Base.DEPOT_PATH), "scratchspaces", args...) + return abspath(scratch_base_path, args...) else # If we've been given an override, use _only_ that directory. return abspath(SCRATCH_DIR_OVERRIDE[], args...) From a0df6a81b3c1b8b319283c54cfadd61a23e1d3fb Mon Sep 17 00:00:00 2001 From: Kevin Bonham Date: Fri, 20 Oct 2023 09:06:46 -0400 Subject: [PATCH 2/5] add preferences and environmental variable option --- src/Scratch.jl | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Scratch.jl b/src/Scratch.jl index 98211c9..8feb30a 100644 --- a/src/Scratch.jl +++ b/src/Scratch.jl @@ -6,12 +6,6 @@ using Dates using Preferences end -@static if VERSION >= v"1.6" - const scratch_base_path = @load_preference("scratch_dir", joinpath(first(Base.DEPOT_PATH), "scratchspaces")) -else - const scratch_base_path = joinpath(first(Base.DEPOT_PATH), "scratchspaces") -end - export with_scratch_directory, scratch_dir, get_scratch!, delete_scratch!, clear_scratchspaces!, @get_scratch! const SCRATCH_DIR_OVERRIDE = Ref{Union{String,Nothing}}(nothing) @@ -40,6 +34,17 @@ be overridden via `with_scratch_directory()`. """ function scratch_dir(args...) if SCRATCH_DIR_OVERRIDE[] === nothing + if VERSION >= v"1.6" + scratch_base_path = @load_preference("scratch_dir", + get(ENV, "JULIA_SCRATCH_DIR", + joinpath(first(Base.DEPOT_PATH), "scratchspaces") + ) + ) + else + scratch_base_path = get(ENV, "JULIA_SCRATCH_DIR", + joinpath(first(Base.DEPOT_PATH), "scratchspaces") + ) + end return abspath(scratch_base_path, args...) else # If we've been given an override, use _only_ that directory. From 27ce23b9e9ef20b5d28b3642e97d2eafc440f18f Mon Sep 17 00:00:00 2001 From: Kevin Bonham Date: Fri, 20 Oct 2023 09:19:30 -0400 Subject: [PATCH 3/5] can I do @static inside functions? --- src/Scratch.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Scratch.jl b/src/Scratch.jl index 8feb30a..ac14db4 100644 --- a/src/Scratch.jl +++ b/src/Scratch.jl @@ -34,7 +34,7 @@ be overridden via `with_scratch_directory()`. """ function scratch_dir(args...) if SCRATCH_DIR_OVERRIDE[] === nothing - if VERSION >= v"1.6" + @static if VERSION >= v"1.6" scratch_base_path = @load_preference("scratch_dir", get(ENV, "JULIA_SCRATCH_DIR", joinpath(first(Base.DEPOT_PATH), "scratchspaces") From 5db55ce47bb4e8b632a37d86738ea07f25f979c2 Mon Sep 17 00:00:00 2001 From: Kevin Bonham Date: Fri, 20 Oct 2023 09:29:09 -0400 Subject: [PATCH 4/5] Remove @static from top level expr MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mosè Giordano --- src/Scratch.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Scratch.jl b/src/Scratch.jl index ac14db4..b4e78bb 100644 --- a/src/Scratch.jl +++ b/src/Scratch.jl @@ -2,7 +2,7 @@ module Scratch import Base: UUID using Dates -@static if VERSION >= v"1.6" +if VERSION >= v"1.6" using Preferences end From df6f59e630fbaebc6124cfe69a47e72937d41027 Mon Sep 17 00:00:00 2001 From: Kevin Bonham Date: Fri, 20 Oct 2023 10:08:30 -0400 Subject: [PATCH 5/5] add compat --- Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Project.toml b/Project.toml index 654c4bf..7f10a25 100644 --- a/Project.toml +++ b/Project.toml @@ -9,6 +9,7 @@ Preferences = "21216c6a-2e73-6563-6e65-726566657250" [compat] julia = "1" +Preferences = "1" [extras] Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"