0 | module Pack.Runner.Install
5 | import Data.SortedMap
6 | import Idris.Package.Types
10 | import Pack.Runner.Database
11 | import System.Escape
22 | -> {auto _ : PackDirs}
25 | -> (pth : PkgName -> Hash -> Package -> Path Abs)
26 | -> EitherT PackErr io DirList
27 | pathDirs pre pth = do
28 | rs <- traverse resolveLib (keys e.all)
29 | ps <- filterM (\r => exists $
pth r.name r.hash r.pkg) rs
30 | let ps' := filter (not . isCorePkg . value . name) ps
31 | pure $
(pre ::) $
map (\r => pth r.name r.hash r.pkg) ps'
36 | packagePathDirs : HasIO io => Env -> EitherT PackErr io DirList
37 | packagePathDirs _ = pathDirs idrisInstallDir pkgPathDir
42 | packageLibDirs : HasIO io => Env -> EitherT PackErr io DirList
43 | packageLibDirs _ = pathDirs idrisLibDir pkgLibDir
48 | packageDataDirs : HasIO io => Env -> EitherT PackErr io DirList
49 | packageDataDirs _ = pathDirs idrisDataDir pkgDataDir
54 | packagePath : HasIO io => Env => EitherT PackErr io EnvVar
55 | packagePath = IdrisPackagePathVar <$> packagePathDirs %search
60 | libPath : HasIO io => Env => EitherT PackErr io EnvVar
61 | libPath = IdrisLibsVar <$> packageLibDirs %search
66 | dataPath : HasIO io => Env => EitherT PackErr io EnvVar
67 | dataPath = IdrisDataVar <$> packageDataDirs %search
73 | buildEnv : HasIO io => Env => EitherT PackErr io (List EnvVar)
75 | let pre := if useRacket then [IdrisCodegenVar Racket] else []
76 | in (pre ++ ) <$> sequence [packagePath, libPath, dataPath]
83 | -> {auto _ : IdrisEnv}
85 | -> EitherT PackErr io (CmdArgList, List EnvVar)
87 | (idrisWithCG ++ ["-p", name rl],) <$> buildEnv
94 | -> {auto _ : IdrisEnv}
95 | -> List (ResolvedLib t)
96 | -> EitherT PackErr io (CmdArgList, List EnvVar)
97 | idrisWithPkgs [] = pure (idrisWithCG, [])
98 | idrisWithPkgs pkgs =
99 | let ps = concatMap (\p => ["-p", name p]) pkgs
100 | in (idrisWithCG ++ ps,) <$> buildEnv
107 | ipkgCodeGen : (e : Env) => PkgDesc -> Codegen
108 | ipkgCodeGen desc = case e.config.codegen of
109 | Default => getCG (maybe [] (filter (not . null) . words . snd) desc.options)
113 | getCG : List String -> Codegen
114 | getCG ("--cg" :: cg :: _) = fromString cg
115 | getCG ("--codegen" :: cg :: _) = fromString cg
116 | getCG [_] = Default
118 | getCG (h :: t) = getCG t
120 | coreGitDir : (e : Env) => Path Abs
121 | coreGitDir = gitTmpDir compiler
123 | copyApp : HasIO io => IdrisEnv => SafeApp -> EitherT PackErr io ()
125 | let dir := pkgBinDir ra.name ra.hash ra.pkg
127 | debug "Copying application to \{dir}"
129 | sys ["cp", "-r", Escapable "\{buildPath ra.desc}/exec/" ++ NoEscape "*", dir]
131 | noAppError : (app : PkgName) -> List String
132 | noAppError app = lines $
"""
133 | [ fatal ] Package `\{app}` is not built or not installed in the current
134 | environment. Maybe, it was installed with an older compiler version
135 | or using a local `pack.toml` which is not available in the current
136 | directory. Try to reinstall it with `pack install-app \{app}`.
139 | pthStr : (c : Config) => PackDirs => Bool -> (packBinaryLoc : String) -> String
140 | pthStr False _ = ""
141 | pthStr True packBinaryLoc =
142 | let racket := if useRacket then "export \{schemeVar}" else ""
144 | export IDRIS2_PACKAGE_PATH="$(\{packBinaryLoc} package-path)"
145 | export IDRIS2_LIBS="$(\{packBinaryLoc} libs-path)"
146 | export IDRIS2_DATA="$(\{packBinaryLoc} data-path)"
158 | {auto _ : HasIO io}
160 | -> {auto _ : PackDirs}
163 | -> (withPkgPath : Bool)
164 | -> (codeGen : Codegen)
165 | -> EitherT PackErr io ()
166 | appLink exec app withPkgPath cg =
168 | interp := case cg of
171 | target := MkF packBinDir exec
176 | if [ -f "\{packExec}" ] && [ -x "\{packExec}" ]; then
180 | if ! APPLICATION="$(${PACK} app-path \{app})" || [ ! -r "$APPLICATION" ]; then {
181 | \{unlines $ noAppError app <&> \s => " echo '\{s}'"}
184 | \{pthStr withPkgPath "${PACK}"}
186 | \{interp}$APPLICATION "$@"
188 | in write target content >> sys ["chmod", "+x", target]
190 | installCmd : (withSrc : Bool) -> CmdArgList
191 | installCmd True = ["--install-with-src"]
192 | installCmd False = ["--install"]
199 | checkBuildDir : HasIO io => (e : IdrisEnv) => Desc Safe -> EitherT PackErr io ()
201 | let buildDir := buildPath d
202 | version := the (File Abs) (buildDir /> ".idrisCommit")
203 | commit := e.env.db.idrisCommit.value
205 | str <- readIfExists version ""
206 | when (str /= commit) $
do
208 | write version commit
210 | dependsMsg : Path Abs -> String
212 | Found local package directory at \{p}.
213 | Using local package directories together with pack is highly discouraged,
214 | as they might interfere with the packages managed by pack in an unpredictable
222 | {auto _ : HasIO io}
223 | -> {auto e : IdrisEnv}
224 | -> (env : List EnvVar)
225 | -> (logLevel : LogLevel)
226 | -> (cleanBuild : Bool)
227 | -> (cmd : CmdArgList)
228 | -> (desc : Desc Safe)
229 | -> EitherT PackErr io ()
230 | libPkg env lvl cleanBuild cmd desc =
231 | let exe := idrisWithCG
232 | s := exe ++ cmd ++ [desc.path.file]
234 | pre <- (env ++) <$> buildEnv
235 | debug "About to run: \{escapeCmd s}"
236 | when cleanBuild (checkBuildDir desc)
239 | let dependsDir := desc.path.parent /> "depends"
240 | when !(exists dependsDir) $
241 | when e.env.config.warnDepends $
warn (dependsMsg dependsDir)
243 | inDir (desc.path.parent) (\_ => sysWithEnvAndLog lvl s pre)
246 | toPlatform : String -> MachineType
247 | toPlatform "unix" = Ubuntu
248 | toPlatform _ = MacOS
251 | platformOf : MachineType -> Bool
252 | platformOf mt = mt == toPlatform os
256 | {auto _ : HasIO io}
258 | -> Maybe (List MachineType)
259 | -> EitherT PackErr io ()
260 | verifyPlatform _ Nothing = pure ()
261 | verifyPlatform name (Just mts) =
262 | if any platformOf mts
264 | else throwE $
InstallOnIncompatibleMachinePkg
272 | hasTTC : String -> Bool
273 | hasTTC = any (("--ttc-version" `isPrefixOf`) . trim) . lines
276 | getTTCVersion : HasIO io => Env => EitherT PackErr io TTCVersion
278 | hlp <- sysRun [idrisExec, "--help"]
281 | str <- sysRun [idrisExec, "--ttc-version"]
282 | case Body.parse (trim str) of
283 | Just v => debug "Using TTC version \{v}" $> TTCV (Just v)
284 | Nothing => warn "Failed to parse TTC version \{str}" $> TTCV Nothing
285 | False => debug "No TTC version given by Idris" $> TTCV Nothing
288 | tryDirectBuild : HasIO io => Env => io (Either PackErr ())
291 | sysAndLog Build ["make", "support"]
292 | sysAndLog Build ["make", "idris2-exec", prefixVar, schemeVar]
294 | idrisCleanup : HasIO io => Env => io ()
297 | sysAndLog Build ["make", "clean-libs"]
298 | sysAndLog Build ["rm", "-r", "build/ttc", "build/exec"]
300 | idrisBootstrapWithStage3 : HasIO io => (e : Env) => Path Abs -> EitherT PackErr io ()
301 | idrisBootstrapWithStage3 dir = do
302 | let bootstrappedPrefixVar = PrefixVar dir
303 | sysAndLog Build ["make", bootstrapCmd, bootstrappedPrefixVar, schemeVar]
304 | debug "Install bootstrapped Idris..."
305 | sysAndLog Build ["make", "bootstrap-install", bootstrappedPrefixVar, schemeVar]
308 | debug "Stage 3: Rebuilding Idris..."
309 | let idrisBootVar = IdrisBootVar $
dir /> "bin" /> "idris2"
310 | let idrisDataVar = IdrisDataVar [dir /> idrisDir /> "support"]
311 | sysAndLog Build ["make", "idris2-exec", prefixVar, idrisBootVar, idrisDataVar, schemeVar]
313 | ignoreError $
sysAndLog Build ["rm", "-rf", dir]
315 | idrisBootstrap : HasIO io => (e : Env) => Path Abs -> EitherT PackErr io ()
316 | idrisBootstrap dir = do
317 | debug "Bootstrapping Idris..."
318 | if e.config.bootstrapStage3
319 | then idrisBootstrapWithStage3 $
dir </> "bootstrapped"
320 | else sysAndLog Build ["make", bootstrapCmd, prefixVar, schemeVar]
321 | ignoreError $
sysAndLog Build ["make", "bootstrap-clean"]
325 | mkIdris : HasIO io => (e : Env) => EitherT PackErr io IdrisEnv
327 | debug "Checking Idris installation"
328 | when !(missing idrisInstallDir) $
do
329 | debug "No Idris compiler found. Installing..."
330 | withCoreGit $
\dir => do
331 | case e.config.bootstrap of
332 | True => idrisBootstrap dir
336 | tryDirectBuild >>= \case
338 | warn "Building Idris failed. Trying to bootstrap now."
340 | Right () => pure ()
342 | sysAndLog Build ["make", "install-support", prefixVar]
343 | sysAndLog Build ["make", "install-idris2", prefixVar]
345 | cacheCoreIpkgFiles dir
347 | appLink "idris2" "idris2" True Default
348 | ttc <- getTTCVersion
349 | pure $
MkIdrisEnv %search ttc ItHasIdris
355 | withSrcStr : (c : Config) => String
356 | withSrcStr = case c.withSrc of
357 | True => " (with sources)"
360 | maybeGiveNotice : HasIO io => Config => SafeLib -> io ()
361 | maybeGiveNotice (RL (Git _ _ _ _ _ (Just notice) _) _ _ _ _ _) = warn notice
362 | maybeGiveNotice _ = pure ()
365 | {auto _ : HasIO io}
366 | -> {auto e : IdrisEnv}
367 | -> (dir : Path Abs)
369 | -> EitherT PackErr io ()
370 | installImpl dir rl =
371 | let pre := libInstallPrefix rl
372 | instCmd := installCmd e.env.config.withSrc
373 | libDir := rl.desc.path.parent </> "lib"
375 | info "Installing library\{withSrcStr}: \{name rl}"
377 | when (isInstalled rl) $
do
378 | info "Removing currently installed version of \{name rl}"
379 | rmDir (pkgInstallDir rl.name rl.hash rl.pkg rl.desc)
380 | rmDir (pkgLibDir rl.name rl.hash rl.pkg)
381 | libPkg pre Build True ["--build"] rl.desc
382 | libPkg pre Debug False instCmd rl.desc
383 | debug "checking if libdir at \{libDir} exists"
384 | when !(exists libDir) $
do
385 | debug "copying lib dir"
386 | copyDir libDir (pkgLibDir rl.name rl.hash rl.pkg)
389 | {auto _ : HasIO io}
390 | -> {auto e : IdrisEnv}
392 | -> EitherT PackErr io ()
393 | preInstall rl = withPkgEnv rl.name rl.pkg $
\dir =>
394 | let ipkgAbs := ipkg dir rl.pkg
396 | Git u c ipkg _ _ _ ro => do
397 | verifyPlatform rl.name ro
398 | let cache := ipkgCachePath rl.name c ipkg
399 | copyFile cache ipkgAbs
400 | Local _ _ _ _ => pure ()
402 | let cache := coreCachePath c
403 | copyFile cache ipkgAbs
406 | sysAndLog Build ["make", "src/IdrisPaths.idr", prefixVar]
411 | {auto _ : HasIO io}
412 | -> {auto e : IdrisEnv}
414 | -> EitherT PackErr io ()
415 | installLib rl = case rl.status of
416 | Installed _ _ => pure ()
419 | withPkgEnv rl.name rl.pkg $
\dir => do
423 | when (not $
isInstalled rl) $
do
424 | debug "writing \{nanoString} to \{libTimestamp rl.name}"
425 | write (libTimestamp rl.name) nanoString
428 | uncacheLib (name rl)
438 | {auto _ : HasIO io}
439 | -> {auto e : IdrisEnv}
440 | -> (withWrapperScript : Bool)
442 | -> EitherT PackErr io ()
444 | let cg := ipkgCodeGen ra.desc.desc
445 | in case ra.status of
446 | BinInstalled _ => pure ()
447 | Installed _ => case b of
449 | True => appLink ra.exec ra.name (usePackagePath ra) cg
450 | _ => withPkgEnv ra.name ra.pkg $
\dir =>
451 | let ipkgAbs := ipkg dir ra.pkg
454 | Git u c ipkg pp _ _ ro => do
455 | verifyPlatform ra.name ro
456 | let cache := ipkgCachePath ra.name c ipkg
457 | copyFile cache ipkgAbs
458 | libPkg [] Build True ["--build"] (notPackIsSafe ra.desc)
460 | when b $
appLink ra.exec ra.name pp cg
461 | Local _ _ pp _ => do
462 | libPkg [] Build True ["--build"] (notPackIsSafe ra.desc)
464 | when b $
appLink ra.exec ra.name pp cg
465 | write (libTimestamp ra.name) nanoString
474 | {auto _ : HasIO io}
475 | -> {auto e : IdrisEnv}
477 | -> EitherT PackErr io ()
478 | installDocs rl = case rl.status of
479 | Installed _ True => pure ()
480 | _ => withPkgEnv rl.name rl.pkg $
\dir => do
481 | let docsDir : Path Abs
482 | docsDir = buildPath rl.desc /> "docs"
485 | pre = libInstallPrefix rl
488 | htmlDir = docsDir /> "docs"
490 | info "Building source docs for: \{name rl}"
492 | libPkg pre Build False ["--mkdoc"] rl.desc
494 | when e.env.config.useKatla $
do
495 | info "Building highlighted sources for: \{name rl}"
497 | rp <- resolveApp "katla"
498 | let katla := pkgExec rp.name rp.hash rp.pkg rp.exec
499 | fs <- map (MkF htmlDir) <$> htmlFiles htmlDir
500 | for_ fs $
\htmlFile =>
501 | let Just ds@(MkDS _ src ttm srcHtml) := sourceForDoc rl.desc htmlFile
502 | | Nothing => pure ()
503 | in when !(srcExists ds) $
do
504 | sysAndLog Build [katla, "html", src, ttm, NoEscape ">", srcHtml]
507 | let docs := pkgDocs rl.name rl.hash rl.pkg rl.desc
508 | when !(exists docs) (rmDir docs)
509 | copyDir docsDir docs
510 | uncacheLib (name rl)
512 | katla : (c : Config) => List (InstallType, PkgName)
513 | katla = if c.withDocs && c.useKatla then [(App False, "katla")] else []
515 | autoPairs : (c : Config) => List (InstallType, PkgName)
517 | map ((Library,) . corePkgName) [ Prelude, Base, Network ]
518 | ++ map (Library,) c.autoLibs
519 | ++ map (App True,) c.autoApps
521 | libInfo : List SafePkg -> List String
522 | libInfo = mapMaybe $
\case Lib rl => Just "\{rl.name}"
525 | appInfo : List SafePkg -> List String
526 | appInfo = mapMaybe $
\case App _ ra => Just "\{ra.name}"
534 | {auto _ : HasIO io}
535 | -> {auto e : IdrisEnv}
536 | -> List (InstallType, PkgName)
537 | -> EitherT PackErr io ()
539 | all <- plan $
katla <+> autoPairs <+> ps
540 | logMany Info "Installing libraries:" (libInfo all)
541 | logMany Info "Installing apps:" (appInfo all)
542 | for_ all $
\case Lib rl => installLib rl
543 | App b rl => installApp b rl
545 | when e.env.config.withDocs $
546 | for_ all $
\case Lib rl => installDocs rl
552 | export covering %inline
553 | installLibs : HasIO io => IdrisEnv => List PkgName -> EitherT PackErr io ()
554 | installLibs = install . map (Library,)
559 | export covering %inline
560 | installApps : HasIO io => IdrisEnv => List PkgName -> EitherT PackErr io ()
561 | installApps = install . map (App True,)
567 | {auto _ : HasIO io}
568 | -> {auto _ : IdrisEnv}
570 | -> EitherT PackErr io ()
571 | installDeps = install . map (Library,) . dependencies
576 | {auto _ : HasIO io}
577 | -> {auto _ : PackDirs}
578 | -> {auto _ : TmpDir}
579 | -> {auto _ : LibCache}
580 | -> {auto _ : LineBufferingCmd}
582 | -> (fetch : FetchMethod)
583 | -> EitherT PackErr io IdrisEnv
584 | idrisEnv mc fetch = env mc fetch >>= (\e => mkIdris)
588 | update : HasIO io => IdrisEnv -> EitherT PackErr io ()
590 | let bin := packBinDir
592 | info "Updating pack. If this fails, try switching to the latest package collection."
593 | commit <- maybe (gitLatest packRepo "main") pure packCommit
594 | info "Using commit \{commit}"
596 | withGit "pack" packRepo commit True $
\dir => do
597 | let ipkg := MkF dir "pack.ipkg"
598 | d <- parseLibIpkg ipkg ipkg
600 | let installDir := packInstallDir commit
601 | installedExec := installDir /> "pack"
602 | ex <- exists installedExec
604 | True => link installedExec packExec
606 | libPkg [] Build True ["--build"] d
608 | sys ["cp", "-r", NoEscape "build/exec/*", installDir]
609 | link installedExec packExec
616 | removeApp : HasIO io => Env => PkgName -> EitherT PackErr io ()
618 | info "Removing application \{n}"
620 | rmFile (pathExec ra.exec)
621 | rmDir (pkgBinDir ra.name ra.hash ra.pkg)
624 | removeLib : HasIO io => Env => PkgName -> EitherT PackErr io ()
627 | case isInstalled rl of
629 | info "Removing library \{n}"
630 | rmDir (pkgInstallDir rl.name rl.hash rl.pkg rl.desc)
631 | rmDir (pkgLibDir rl.name rl.hash rl.pkg)
632 | False => warn "Package \{n} is not installed. Ignoring."
636 | remove : HasIO io => Env => List (PkgType,PkgName) -> EitherT PackErr io ()
639 | for_ ps $
\case (PLib,n) => removeLib n
640 | (PApp,n) => removeApp n
644 | removeLibs : HasIO io => Env => List PkgName -> EitherT PackErr io ()
647 | remove $
map (PLib,) ns
651 | removeApps : HasIO io => Env => List PkgName -> EitherT PackErr io ()
652 | removeApps = remove . map (PApp,)