@@ -5,38 +5,38 @@ import Base: PkgId, PkgOrigin, UUID
55const RUNTIME_MODE_OPTIONS = [" MIN" , " INTERACTIVE" , " COMPILATION" , " SANDBOX" ]
66
77# Compilation can be done in one mode
8- const DEFAULT_RUNTIME_MODE = get (ENV , " DEFAULT_RUNTIME_MODE" , get (ENV , " RUNTIME_MODE" , " INTERACTIVE" ))
8+ # const DEFAULT_RUNTIME_MODE = get(ENV, "DEFAULT_RUNTIME_MODE", get(ENV, "RUNTIME_MODE", "INTERACTIVE"))
99
1010# get(ENV, "JULIA_RUNTIME_MODE", "INTERACTIVE") # We set it at compilation
1111
12- const DEFAULT_MODULE_NAME = get (ENV , " MODULE_NAME" , " MainEnv" )
13- const DEFAULT_APP_NAME = get (ENV , " APP_NAME" , " " )
14- const DEFAULT_BUNDLE_IDENTIFIER = get (ENV , " BUNDLE_IDENTIFIER" , " " )
12+ # const DEFAULT_MODULE_NAME = get(ENV, "MODULE_NAME", "MainEnv")
13+ # const DEFAULT_APP_NAME = get(ENV, "APP_NAME", "")
14+ # const DEFAULT_BUNDLE_IDENTIFIER = get(ENV, "BUNDLE_IDENTIFIER", "")
1515
16- const DEFAULT_STDLIB = get (ENV , " STDLIB" , relpath (Sys. STDLIB, dirname (Sys. BINDIR)))
16+ # const DEFAULT_STDLIB = get(ENV, "STDLIB", relpath(Sys.STDLIB, dirname(Sys.BINDIR)))
1717
18- println (" Compilation is about to happen with the following parameters:" )
19- println (" \t DEFAULT_RUNTIME_MODE=" * DEFAULT_RUNTIME_MODE)
20- println (" \t DEFAULT_MODULE_NAME=" * DEFAULT_MODULE_NAME)
21- println (" \t DEFAULT_APP_NAME=" * DEFAULT_APP_NAME)
22- println (" \t DEFAULT_BUNDLE_IDENTIFIER=" * DEFAULT_BUNDLE_IDENTIFIER)
18+ # println("Compilation is about to happen with the following parameters:")
19+ # println("\tDEFAULT_RUNTIME_MODE=" * DEFAULT_RUNTIME_MODE)
20+ # println("\tDEFAULT_MODULE_NAME=" * DEFAULT_MODULE_NAME)
21+ # println("\tDEFAULT_APP_NAME=" * DEFAULT_APP_NAME)
22+ # println("\tDEFAULT_BUNDLE_IDENTIFIER=" * DEFAULT_BUNDLE_IDENTIFIER)
2323
2424global USER_DATA:: String # This is set by the startup macro itself
2525
2626
2727# Theese constants must be present at compilation time as otherwise it does not make sense
2828
29- function save_pkgorigins (path , pkgorigins:: Dict{PkgId, PkgOrigin} ; stdlib_dir = Sys . STDLIB )
29+ function save_pkgorigins (index_path , pkgorigins:: Dict{PkgId, PkgOrigin} ; root_dir = nothing )
3030
31- file = open (path , " w" )
31+ file = open (index_path , " w" )
3232
3333 try
3434 for (pkgid, pkgorigin) in pkgorigins
3535
3636 (; name, uuid) = pkgid
3737 (; path, version) = pkgorigin
3838
39- rpath = relpath (path, stdlib_dir )
39+ rpath = isnothing (root_dir) ? path : relpath (path, root_dir )
4040
4141 println (file, " $uuid \t $name \t $version \t $rpath " )
4242
@@ -70,7 +70,7 @@ function parse_version(version_str::AbstractString)
7070 return VersionNumber (major, minor, patch)
7171end
7272
73- function load_pkgorigins! (pkgorigins, path; stdlib_dir = Sys . STDLIB )
73+ function load_pkgorigins! (pkgorigins, path; root_dir = dirname (path) )
7474 # pkgorigins = Dict{PkgId, PkgOrigin}()
7575
7676 file = open (path, " r" )
@@ -99,11 +99,11 @@ function load_pkgorigins!(pkgorigins, path; stdlib_dir = Sys.STDLIB)
9999 end
100100
101101 # Reconstruct absolute path
102- abspath = joinpath (stdlib_dir , rpath)
102+ _abspath = abspath ( joinpath (root_dir , rpath) )
103103
104104 # Create PkgId and PkgOrigin
105105 pkg_id = PkgId (uuid, name)
106- pkg_origin = PkgOrigin (abspath , nothing , version)
106+ pkg_origin = PkgOrigin (_abspath , nothing , version)
107107
108108 # Add to dictionary
109109 pkgorigins[pkg_id] = pkg_origin
@@ -116,17 +116,17 @@ function load_pkgorigins!(pkgorigins, path; stdlib_dir = Sys.STDLIB)
116116end
117117
118118
119- function collect_pkgorigins! (pkgorigins:: Dict{PkgId, PkgOrigin} ; stdlib_dir = Sys. STDLIB)
119+ function collect_pkgorigins! (pkgorigins:: Dict{PkgId, PkgOrigin} ; root_dir = Sys. STDLIB)
120120
121121 uuid_regex = r" ^uuid\s *=\s *\" ([a-f0-9\- ]+)\" " mi
122122 version_regex = r" ^version\s *=\s *\" ([^\" ]+)\" " mi
123123
124- if ! isdir (stdlib_dir )
124+ if ! isdir (root_dir )
125125 return pkgorigins
126126 end
127127
128- for name in readdir (stdlib_dir )
129- pkg_path = joinpath (stdlib_dir , name)
128+ for name in readdir (root_dir )
129+ pkg_path = joinpath (root_dir , name)
130130
131131 # Skip if not a directory
132132 isdir (pkg_path) || continue
@@ -174,18 +174,18 @@ function collect_pkgorigins!(pkgorigins::Dict{PkgId, PkgOrigin}; stdlib_dir = Sy
174174 return pkgorigins
175175end
176176
177- collect_pkgorigins (; stdlib_dir = Sys. STDLIB) = collect_pkgorigins! (Dict {PkgId, PkgOrigin} (); stdlib_dir )
177+ collect_pkgorigins (; root_dir = Sys. STDLIB) = collect_pkgorigins! (Dict {PkgId, PkgOrigin} (); root_dir )
178178
179179
180- function set_load_path! (LOAD_PATH ; module_name = " " )
180+ function set_load_path! (LOAD_PATH ; stdlib_project_name )
181181
182182 empty! (LOAD_PATH )
183183 push! (LOAD_PATH , " @" , " @stdlib" )
184- push! (LOAD_PATH , joinpath (Sys. STDLIB, module_name ))
184+ push! (LOAD_PATH , joinpath (Sys. STDLIB, stdlib_project_name ))
185185
186186end
187187
188- function set_depot_path! (DEPOT_PATH ; bundle_identifier, app_name, runtime_mode)
188+ function set_depot_path! (DEPOT_PATH ; bundle_identifier = " " , app_name = " " , runtime_mode = " MIN " )
189189
190190 if runtime_mode == " SANDBOX"
191191
@@ -194,24 +194,15 @@ function set_depot_path!(DEPOT_PATH; bundle_identifier, app_name, runtime_mode)
194194 elseif Sys. isapple ()
195195 set_depot_path_macos! (DEPOT_PATH ; app_name)
196196 elseif Sys. islinux ()
197- set_depot_path_snap! (DEPOT_PATH ; app_name )
197+ set_depot_path_snap! (DEPOT_PATH )
198198 else
199199 error (" Sandbox runtime mode is only supported for windows, macos and linux" )
200200 end
201201
202- elseif runtime_mode == " COMPILATION"
203-
204- set_depot_path_min! (DEPOT_PATH )
205- popfirst! (DEPOT_PATH )
206-
207202 elseif runtime_mode == " MIN"
208203
209204 set_depot_path_min! (DEPOT_PATH )
210205
211- elseif runtime_mode == " INTERACTIVE"
212-
213- global USER_DATA = get (ENV , " USER_DATA" , mktempdir ())
214-
215206 else
216207 error (" Sandbox mode RUNTIME_MODE=$runtime_mode not supported" )
217208 end
@@ -247,7 +238,7 @@ function set_depot_path_macos!(DEPOT_PATH::Vector{String}; app_name)
247238 return
248239end
249240
250- function set_depot_path_snap! (DEPOT_PATH :: Vector{String} )
241+ function set_depot_path_snap! (DEPOT_PATH )
251242
252243 empty! (DEPOT_PATH )
253244
@@ -272,7 +263,7 @@ function set_depot_path_snap!(DEPOT_PATH::Vector{String})
272263
273264 end
274265
275- push! (DEPOT_PATH , dirname (dirname (Sys. STDLIB) ))
266+ push! (DEPOT_PATH , joinpath (dirname (Sys. BINDIR), " share/julia " ))
276267
277268 return
278269end
@@ -326,29 +317,24 @@ function set_depot_path_msix!(DEPOT_PATH; bundle_identifier)
326317 return
327318end
328319
329-
330- function init (;
331- runtime_mode:: String = get (ENV , " RUNTIME_MODE" , DEFAULT_RUNTIME_MODE),
332- module_name:: String = get (ENV , " MODULE_NAME" , DEFAULT_MODULE_NAME),
333- app_name:: String = get (ENV , " APP_NAME" , DEFAULT_APP_NAME),
334- bundle_identifier:: String = get (ENV , " BUNDLE_IDENTIFIER" , DEFAULT_BUNDLE_IDENTIFIER)
335- )
336-
337-
338- @assert runtime_mode in RUNTIME_MODE_OPTIONS " runtime_mode=$runtime_mode not recognized. Available options are $(join (RUNTIME_MODE_OPTIONS, ' |' )) "
339-
340- if runtime_mode == " INTERACTIVE"
341- global USER_DATA = get (ENV , " USER_DATA" , mktempdir ())
342- return
320+ function load_config (config_path)
321+ config_dict = Dict {String, String} ()
322+
323+ for line in eachline (config_path)
324+ # Skip empty lines
325+ isempty (strip (line)) && continue
326+
327+ # Split on the first '=' character
328+ key, value = split (line, ' =' , limit= 2 )
329+ config_dict[strip (key)] = strip (value)
343330 end
344331
345- Sys. STDLIB = joinpath (dirname (Sys. BINDIR), DEFAULT_STDLIB)
332+ runtime_mode = config_dict[" RUNTIME_MODE" ]
333+ stdlib_project_name = config_dict[" STDLIB_PROJECT_NAME" ]
334+ app_name = get (config_dict, " APP_NAME" , " " )
335+ bundle_identifier = get (config_dict, " BUNDLE_IDENTIFIER" , " " )
346336
347- # This is a bit of a hack
348- if isempty (module_name)
349- # error("MODULE_NAME not set")
350- module_name = get (ENV , " MODULE_NAME" , DEFAULT_MODULE_NAME)
351- end
337+ @assert runtime_mode in RUNTIME_MODE_OPTIONS
352338
353339 if runtime_mode == " SANDBOX"
354340
@@ -361,37 +347,77 @@ function init(;
361347 end
362348 end
363349
364- set_load_path! (Base . LOAD_PATH ; module_name )
365- set_depot_path! (Base . DEPOT_PATH ; app_name, bundle_identifier, runtime_mode)
350+ return (; runtime_mode, stdlib_project_name, app_name, bundle_identifier )
351+ end
366352
367- if runtime_mode == " COMPILATION"
368- popfirst! (Base. LOAD_PATH )
369- else
370353
371- index_path = joinpath (Sys. STDLIB, " index" )
372- if isfile (index_path)
373- load_pkgorigins! (Base. pkgorigins, index_path)
374- else
375- collect_pkgorigins! (Base. pkgorigins)
376- end
354+ function save_config (config_path; stdlib_project_name, app_name = " " , bundle_identifier = " " , runtime_mode = " MIN" )
377355
378- end
356+ @assert runtime_mode in RUNTIME_MODE_OPTIONS
357+
358+ config = """
359+ RUNTIME_MODE=$runtime_mode
360+ STDLIB_PROJECT_NAME=$stdlib_project_name
361+ APP_NAME=$app_name
362+ BUNDLE_IDENTIFIER=$bundle_identifier
363+ """
364+
365+ rm (config_path; force= true )
366+ write (config_path, config)
379367
380368 return
381369end
382370
383371
384- function reset_cache ()
372+ # function init(;
373+ # runtime_mode::String = get(ENV, "RUNTIME_MODE", DEFAULT_RUNTIME_MODE),
374+ # module_name::String = get(ENV, "MODULE_NAME", DEFAULT_MODULE_NAME),
375+ # app_name::String = get(ENV, "APP_NAME", DEFAULT_APP_NAME),
376+ # bundle_identifier::String = get(ENV, "BUNDLE_IDENTIFIER", DEFAULT_BUNDLE_IDENTIFIER)
377+ # )
378+
385379
386- pkg = Base . PkgId (Base . UUID ( " 9f11263e-cf0d-4932-bae6-807953dbea74 " ), " AppEnv " )
387- cache_dir = Base . compilecache_path (pkg )
380+ function init (; config_path = joinpath ( dirname (Sys . BINDIR ), " config " ),
381+ index_path = joinpath ( dirname (Sys . BINDIR), " index " ) )
388382
389- if ! isnothing (cache_dir)
390- @info " Removing the cache "
391- rm (cache_dir; recursive = true )
383+ # Prevents reinitialization
384+ if isdefined ( @__MODULE__ , :USER_DATA )
385+ return
392386 end
393-
387+
388+ # If config file is not present we assume it is an interactive mode
389+ if ! isfile (config_path)
390+ global USER_DATA = get (ENV , " USER_DATA" , mktempdir ())
391+ return
392+ end
393+
394+ (; runtime_mode, stdlib_project_name, app_name, bundle_identifier) = load_config (config_path)
395+
396+ set_load_path! (Base. LOAD_PATH ; stdlib_project_name)
397+ set_depot_path! (Base. DEPOT_PATH ; app_name, bundle_identifier, runtime_mode)
398+
399+ if isfile (index_path)
400+ load_pkgorigins! (Base. pkgorigins, index_path)
401+ else
402+ @warn " Can't find pkgorigin index at $index_path "
403+ # collect_pkgorigins!(Base.pkgorigins)
404+ end
405+
406+ return
394407end
395408
396409
410+ # function reset_cache()
411+
412+ # pkg = Base.PkgId(Base.UUID("9f11263e-cf0d-4932-bae6-807953dbea74"), "AppEnv")
413+ # cache_dir = Base.compilecache_path(pkg)
414+
415+ # if !isnothing(cache_dir)
416+ # @info "Removing the cache"
417+ # rm(cache_dir; recursive=true)
418+ # end
419+
420+ # end
421+
422+
397423end # module AppEnv
0 commit comments