Skip to content

Commit 2a1cee4

Browse files
committed
[ refactor ] Typed environment variables
1 parent a62f0d7 commit 2a1cee4

File tree

7 files changed

+304
-286
lines changed

7 files changed

+304
-286
lines changed

micropack/micropack.rkt

Lines changed: 113 additions & 112 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

micropack/micropack.ss

Lines changed: 113 additions & 112 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Pack/Config/Environment.idr

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Idris.Package.Types
77
import Pack.CmdLn
88
import Pack.Config.TOML
99
import Pack.Config.Types
10+
import public Pack.Config.Environment.Variable
1011
import Pack.Core
1112
import Pack.Database
1213
import System
@@ -267,43 +268,27 @@ bootstrapCmd = if useRacket then "bootstrap-racket" else "bootstrap"
267268
-- Environment Variables
268269
--------------------------------------------------------------------------------
269270

270-
%inline
271-
mkEnvVar : Interpolation a => String -> a -> String
272-
mkEnvVar var val = "\{var}=\{val}"
273-
274-
export %inline
275-
mkPrefixVar : Path Abs -> String
276-
mkPrefixVar = mkEnvVar "PREFIX"
277-
278-
export %inline
279-
mkIdrisBootVar : File Abs -> String
280-
mkIdrisBootVar = mkEnvVar "IDRIS2_BOOT"
281-
282-
export %inline
283-
mkIdrisDataVar : Path Abs -> String
284-
mkIdrisDataVar = mkEnvVar "IDRIS2_DATA"
285271

286272
||| `$PREFIX` variable during Idris2 installation, unquoted
287273
export
288-
prefixVar : PackDirs => DB => String
289-
prefixVar = mkPrefixVar idrisPrefixDir
274+
prefixVar : PackDirs => DB => EnvVar
275+
prefixVar = PrefixVar idrisPrefixDir
290276

291277
||| `$IDRIS2_BOOT` variable during Idris2 installation, unquoted
292278
export
293-
idrisBootVar : PackDirs => DB => String
294-
idrisBootVar = mkIdrisBootVar idrisExec
279+
idrisBootVar : PackDirs => DB => EnvVar
280+
idrisBootVar = IdrisBootVar idrisExec
295281

296282
||| `$SCHEME` variable during Idris2 installation, unquoted
297283
export
298-
schemeVar : (c : Config) => String
299-
schemeVar = if useRacket then mkEnvVar "IDRIS2_CG" "racket" else mkEnvVar "SCHEME" c.scheme
284+
schemeVar : (c : Config) => EnvVar
285+
schemeVar = if useRacket then IdrisCGVar Racket else SchemeVar c.scheme
300286

301287
||| `IDRIS2_PREFIX` to be used with Idris when installing a library
302288
||| to a custom location.
303289
export
304-
libInstallPrefix : PackDirs => DB => ResolvedLib t -> List (String,String)
305-
libInstallPrefix rl =
306-
[("IDRIS2_PREFIX", "\{pkgPrefixDir rl.name rl.hash rl.pkg}")]
290+
libInstallPrefix : PackDirs => DB => ResolvedLib t -> List EnvVar
291+
libInstallPrefix rl = [IdrisPrefixVar $ pkgPrefixDir rl.name rl.hash rl.pkg]
307292

308293
||| Idris executable with extra arguments, if they are present in the config.
309294
export
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module Pack.Config.Environment.Variable
2+
3+
import public Data.FilePath.File
4+
import Pack.Config.Types
5+
6+
public export
7+
data EnvVar : Type where
8+
PrefixVar : Path Abs -> EnvVar
9+
IdrisPrefixVar : Path Abs -> EnvVar
10+
IdrisBootVar : File Abs -> EnvVar
11+
IdrisDataVar : List (Path Abs) -> EnvVar
12+
IdrisCGVar : Codegen -> EnvVar
13+
IdrisPackagePathVar : List (Path Abs) -> EnvVar
14+
IdrisLibsVar : List (Path Abs) -> EnvVar
15+
SchemeVar : FilePath -> EnvVar
16+
17+
%inline
18+
interpolateEnvVar : Interpolation a => String -> a -> String
19+
interpolateEnvVar var val = "\{var}=\{val}"
20+
21+
export %inline
22+
Interpolation (List (Path Abs)) where
23+
interpolate = fastConcat . intersperse ":" . map interpolate
24+
25+
export %inline
26+
Interpolation EnvVar where
27+
interpolate (PrefixVar p) = interpolateEnvVar "PREFIX" p
28+
interpolate (IdrisPrefixVar p) = interpolateEnvVar "IDRIS2_PREFIX" p
29+
interpolate (IdrisBootVar p) = interpolateEnvVar "IDRIS2_BOOT" p
30+
interpolate (IdrisDataVar p) = interpolateEnvVar "IDRIS2_DATA" p
31+
interpolate (IdrisCGVar p) = interpolateEnvVar "IDRIS2_CG" p
32+
interpolate (IdrisPackagePathVar p) = interpolateEnvVar "IDRIS2_PACKAGE_PATH" p
33+
interpolate (IdrisLibsVar p) = interpolateEnvVar "IDRIS2_LIBS" p
34+
interpolate (SchemeVar p) = interpolateEnvVar "SCHEME" p
35+

src/Pack/Core/IO.idr

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Pack.Core.IO
22

33
import public Control.Monad.Either
4+
import Pack.Config.Environment.Variable
45
import Pack.Config.Types
56
import Pack.Core.Logging
67
import Pack.Core.Types
@@ -67,8 +68,8 @@ run (MkEitherT io) = io >>= either fatal pure
6768
||| Display a list of variable-value pairs in the format
6869
||| `VAR1="val1" VAR2="val2"`.
6970
export
70-
dispEnv : List (String,String) -> String
71-
dispEnv = unwords . map (\(e,v) => "\{e}=\{quote v}")
71+
dispEnv : List EnvVar -> String
72+
dispEnv = unwords . map interpolate
7273

7374
||| Tries to run a system command.
7475
export
@@ -104,7 +105,7 @@ sysAndLog lvl cmd = do
104105
| n => throwE (Sys cmd n)
105106
pure ()
106107

107-
cmdWithEnv : CmdArgList -> List (String,String) -> String
108+
cmdWithEnv : CmdArgList -> List EnvVar -> String
108109
cmdWithEnv cmd [] = escapeCmd cmd
109110
cmdWithEnv cmd env = "\{dispEnv env} \{escapeCmd cmd}"
110111

@@ -120,7 +121,7 @@ export
120121
sysWithEnv :
121122
{auto _ : HasIO io}
122123
-> (cmd : CmdArgList)
123-
-> (env : List (String,String))
124+
-> (env : List EnvVar)
124125
-> EitherT PackErr io ()
125126
sysWithEnv cmd env = do
126127
0 <- system (cmdWithEnv cmd env) | n => throwE (Sys cmd n)
@@ -132,7 +133,7 @@ sysWithEnvAndLog :
132133
-> {auto _ : Env}
133134
-> (lvl : LogLevel)
134135
-> (cmd : CmdArgList)
135-
-> (env : List (String,String))
136+
-> (env : List EnvVar)
136137
-> EitherT PackErr io ()
137138
sysWithEnvAndLog lvl cmd env = do
138139
0 <- runProcessingOutput
@@ -160,7 +161,7 @@ export covering
160161
sysRunWithEnv :
161162
{auto _ : HasIO io}
162163
-> (cmd : CmdArgList)
163-
-> (env : List (String,String))
164+
-> (env : List EnvVar)
164165
-> EitherT PackErr io String
165166
sysRunWithEnv cmd env = do
166167
(res,0) <- System.run (cmdWithEnv cmd env) | (_,n) => throwE (Sys cmd n)

src/Pack/Runner.idr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ runCmd = do
173173
(RemoveApp ** [ps]) => idrisEnv mc fetch >>= \e => removeApps ps
174174
(Update ** []) => idrisEnv mc fetch >>= update
175175
(Fetch ** []) => idrisEnv mc fetch >>= \e => install []
176-
(PackagePath ** []) => env mc fetch >>= packagePathDirs >>= putStrLn
177-
(LibsPath ** []) => env mc fetch >>= packageLibDirs >>= putStrLn
178-
(DataPath ** []) => env mc fetch >>= packageDataDirs >>= putStrLn
176+
(PackagePath ** []) => env mc fetch >>= packagePathDirs >>= putStrLn . interpolate
177+
(LibsPath ** []) => env mc fetch >>= packageLibDirs >>= putStrLn . interpolate
178+
(DataPath ** []) => env mc fetch >>= packageDataDirs >>= putStrLn . interpolate
179179
(AppPath ** [n]) => env mc fetch >>= appPath n
180180
(Info ** []) => env mc fetch >>= printInfo
181181
(New ** [pty,p]) => idrisEnv mc fetch >>= new cd pty p

src/Pack/Runner/Install.idr

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,63 +21,58 @@ pathDirs :
2121
{auto _ : HasIO io}
2222
-> {auto _ : PackDirs}
2323
-> {auto e : Env}
24-
-> (pre : String)
24+
-> (pre : Path Abs)
2525
-> (pth : PkgName -> Hash -> Package -> Path Abs)
26-
-> EitherT PackErr io String
26+
-> EitherT PackErr io (List (Path Abs))
2727
pathDirs pre pth = do
2828
rs <- traverse resolveLib (keys e.all)
2929
ps <- filterM (\r => exists $ pth r.name r.hash r.pkg) rs
3030
let ps' := filter (not . isCorePkg . value . name) ps
31-
pure $
32-
fastConcat
33-
. intersperse ":"
34-
. (pre ::)
35-
$ map (\r => "\{pth r.name r.hash r.pkg}") ps'
31+
pure $ (pre ::) $ map (\r => pth r.name r.hash r.pkg) ps'
3632

3733
||| Directories to be listed in the `IDRIS2_PACKAGE_PATH` variable, so
3834
||| that Idris finds all libraries installed by pack in custom locations.
3935
export
40-
packagePathDirs : HasIO io => Env -> EitherT PackErr io String
41-
packagePathDirs _ = pathDirs "\{idrisInstallDir}" pkgPathDir
36+
packagePathDirs : HasIO io => Env -> EitherT PackErr io (List (Path Abs))
37+
packagePathDirs _ = pathDirs idrisInstallDir pkgPathDir
4238

4339
||| Directories to be listed in the `IDRIS2_LIBS` variable, so
4440
||| that Idris finds all `.so` files installed by pack in custom locations.
4541
export
46-
packageLibDirs : HasIO io => Env -> EitherT PackErr io String
47-
packageLibDirs _ = pathDirs "\{idrisLibDir}" pkgLibDir
42+
packageLibDirs : HasIO io => Env -> EitherT PackErr io (List (Path Abs))
43+
packageLibDirs _ = pathDirs idrisLibDir pkgLibDir
4844

4945
||| Directories to be listed in the `IDRIS2_DATA` variable, so
5046
||| that Idris finds all support files installed by pack in custom locations.
5147
export
52-
packageDataDirs : HasIO io => Env -> EitherT PackErr io String
53-
packageDataDirs _ = pathDirs "\{idrisDataDir}" pkgDataDir
48+
packageDataDirs : HasIO io => Env -> EitherT PackErr io (List (Path Abs))
49+
packageDataDirs _ = pathDirs idrisDataDir pkgDataDir
5450

5551
||| `IDRIS2_PACKAGE_PATH` variable to be used with Idris, so
5652
||| that it finds all libraries installed by pack in custom locations.
5753
export
58-
packagePath : HasIO io => Env => EitherT PackErr io (String, String)
59-
packagePath =
60-
("IDRIS2_PACKAGE_PATH",) <$> packagePathDirs %search
54+
packagePath : HasIO io => Env => EitherT PackErr io EnvVar
55+
packagePath = IdrisPackagePathVar <$> packagePathDirs %search
6156

6257
||| `IDRIS2_LIBS` variable to be used with Idris, so
6358
||| that it finds all `.so` files installed by pack in custom locations.
6459
export
65-
libPath : HasIO io => Env => EitherT PackErr io (String, String)
66-
libPath = ("IDRIS2_LIBS",) <$> packageLibDirs %search
60+
libPath : HasIO io => Env => EitherT PackErr io EnvVar
61+
libPath = IdrisLibsVar <$> packageLibDirs %search
6762

6863
||| `IDRIS2_DATA` variable to be used with Idris, so
6964
||| that it finds all support files installed by pack in custom locations.
7065
export
71-
dataPath : HasIO io => Env => EitherT PackErr io (String, String)
72-
dataPath = ("IDRIS2_DATA",) <$> packageDataDirs %search
66+
dataPath : HasIO io => Env => EitherT PackErr io EnvVar
67+
dataPath = IdrisDataVar <$> packageDataDirs %search
7368

7469
||| This unifies `packagePath`, `libPath` and `dataPath`,
7570
||| to generate an environment necessary to build packages with Idris
7671
||| the dependencies of which are handled by pack.
7772
export
78-
buildEnv : HasIO io => Env => EitherT PackErr io (List (String,String))
73+
buildEnv : HasIO io => Env => EitherT PackErr io (List EnvVar)
7974
buildEnv =
80-
let pre := if useRacket then [("IDRIS2_CG", "racket")] else []
75+
let pre := if useRacket then [IdrisCGVar Racket] else []
8176
in (pre ++ ) <$> sequence [packagePath, libPath, dataPath]
8277

8378
||| Idris executable loading the given package plus the
@@ -87,7 +82,7 @@ idrisWithPkg :
8782
{auto _ : HasIO io}
8883
-> {auto _ : IdrisEnv}
8984
-> ResolvedLib t
90-
-> EitherT PackErr io (CmdArgList, List (String,String))
85+
-> EitherT PackErr io (CmdArgList, List EnvVar)
9186
idrisWithPkg rl =
9287
(idrisWithCG ++ ["-p", name rl],) <$> buildEnv
9388

@@ -98,7 +93,7 @@ idrisWithPkgs :
9893
{auto _ : HasIO io}
9994
-> {auto _ : IdrisEnv}
10095
-> List (ResolvedLib t)
101-
-> EitherT PackErr io (CmdArgList, List (String,String))
96+
-> EitherT PackErr io (CmdArgList, List EnvVar)
10297
idrisWithPkgs [] = pure (idrisWithCG, [])
10398
idrisWithPkgs pkgs =
10499
let ps = concatMap (\p => ["-p", name p]) pkgs
@@ -221,7 +216,7 @@ export covering
221216
libPkg :
222217
{auto _ : HasIO io}
223218
-> {auto e : IdrisEnv}
224-
-> (env : List (String,String))
219+
-> (env : List EnvVar)
225220
-> (logLevel : LogLevel)
226221
-> (cleanBuild : Bool)
227222
-> (cmd : CmdArgList)
@@ -276,15 +271,15 @@ idrisCleanup =
276271

277272
idrisBootstrapWithStage3 : HasIO io => (e : Env) => Path Abs -> EitherT PackErr io ()
278273
idrisBootstrapWithStage3 dir = do
279-
let bootstrappedPrefixVar = mkPrefixVar dir
274+
let bootstrappedPrefixVar = PrefixVar dir
280275
sysAndLog Build ["make", bootstrapCmd, bootstrappedPrefixVar, schemeVar]
281276
debug "Install bootstrapped Idris..."
282277
sysAndLog Build ["make", "bootstrap-install", bootstrappedPrefixVar, schemeVar]
283278
idrisCleanup
284279

285280
debug "Stage 3: Rebuilding Idris..."
286-
let idrisBootVar = mkIdrisBootVar $ dir /> "bin" /> "idris2"
287-
let idrisDataVar = mkIdrisDataVar $ dir /> idrisDir /> "support"
281+
let idrisBootVar = IdrisBootVar $ dir /> "bin" /> "idris2"
282+
let idrisDataVar = IdrisDataVar [dir /> idrisDir /> "support"]
288283
sysAndLog Build ["make", "idris2-exec", prefixVar, idrisBootVar, idrisDataVar, schemeVar]
289284

290285
ignoreError $ sysAndLog Build ["rm", "-rf", dir]
@@ -461,7 +456,7 @@ installDocs rl = case rl.status of
461456
let docsDir : Path Abs
462457
docsDir = buildPath rl.desc /> "docs"
463458

464-
pre : List (String,String)
459+
pre : List EnvVar
465460
pre = libInstallPrefix rl
466461

467462
htmlDir : Path Abs

0 commit comments

Comments
 (0)