0 | module Pack.Runner.Install
  1 |
  2 | import Core.FC
  3 | import Data.IORef
  4 | import Data.Maybe
  5 | import Data.SortedMap
  6 | import Idris.Package.Types
  7 | import Pack.Config
  8 | import Pack.Core
  9 | import Pack.Database
 10 | import Pack.Runner.Database
 11 | import System.Escape
 12 | import System.Info
 13 |
 14 | %default total
 15 |
 16 | --------------------------------------------------------------------------------
 17 | --          Paths
 18 | --------------------------------------------------------------------------------
 19 |
 20 | pathDirs :
 21 |      {auto _ : HasIO io}
 22 |   -> {auto _ : PackDirs}
 23 |   -> {auto e : Env}
 24 |   -> (pre : Path Abs)
 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'
 32 |
 33 | ||| Directories to be listed in the `IDRIS2_PACKAGE_PATH` variable, so
 34 | ||| that Idris finds all libraries installed by pack in custom locations.
 35 | export
 36 | packagePathDirs : HasIO io => Env -> EitherT PackErr io DirList
 37 | packagePathDirs _ = pathDirs idrisInstallDir pkgPathDir
 38 |
 39 | ||| Directories to be listed in the `IDRIS2_LIBS` variable, so
 40 | ||| that Idris finds all `.so` files installed by pack in custom locations.
 41 | export
 42 | packageLibDirs : HasIO io => Env -> EitherT PackErr io DirList
 43 | packageLibDirs _ = pathDirs idrisLibDir pkgLibDir
 44 |
 45 | ||| Directories to be listed in the `IDRIS2_DATA` variable, so
 46 | ||| that Idris finds all support files installed by pack in custom locations.
 47 | export
 48 | packageDataDirs : HasIO io => Env -> EitherT PackErr io DirList
 49 | packageDataDirs _ = pathDirs idrisDataDir pkgDataDir
 50 |
 51 | ||| `IDRIS2_PACKAGE_PATH` variable to be used with Idris, so
 52 | ||| that it finds all libraries installed by pack in custom locations.
 53 | export
 54 | packagePath : HasIO io => Env => EitherT PackErr io EnvVar
 55 | packagePath = IdrisPackagePathVar <$> packagePathDirs %search
 56 |
 57 | ||| `IDRIS2_LIBS` variable to be used with Idris, so
 58 | ||| that it finds all `.so` files installed by pack in custom locations.
 59 | export
 60 | libPath : HasIO io => Env => EitherT PackErr io EnvVar
 61 | libPath = IdrisLibsVar <$> packageLibDirs %search
 62 |
 63 | ||| `IDRIS2_DATA` variable to be used with Idris, so
 64 | ||| that it finds all support files installed by pack in custom locations.
 65 | export
 66 | dataPath : HasIO io => Env => EitherT PackErr io EnvVar
 67 | dataPath = IdrisDataVar <$> packageDataDirs %search
 68 |
 69 | ||| This unifies `packagePath`, `libPath` and `dataPath`,
 70 | ||| to generate an environment necessary to build packages with Idris
 71 | ||| the dependencies of which are handled by pack.
 72 | export
 73 | buildEnv : HasIO io => Env => EitherT PackErr io (List EnvVar)
 74 | buildEnv =
 75 |   let pre := if useRacket then [IdrisCodegenVar Racket] else []
 76 |    in (pre ++ ) <$> sequence [packagePath, libPath, dataPath]
 77 |
 78 | ||| Idris executable loading the given package plus the
 79 | ||| environment variables needed to run it.
 80 | export
 81 | idrisWithPkg :
 82 |      {auto _ : HasIO io}
 83 |   -> {auto _ : IdrisEnv}
 84 |   -> ResolvedLib t
 85 |   -> EitherT PackErr io (CmdArgList, List EnvVar)
 86 | idrisWithPkg rl =
 87 |   (idrisWithCG ++ ["-p", name rl],) <$> buildEnv
 88 |
 89 | ||| Idris executable loading the given packages plus the
 90 | ||| environment variables needed to run it.
 91 | export
 92 | idrisWithPkgs :
 93 |      {auto _ : HasIO io}
 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
101 |
102 | --------------------------------------------------------------------------------
103 | --          Utilities
104 | --------------------------------------------------------------------------------
105 |
106 | export
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)
110 |   cg      => cg
111 |
112 |   where
113 |     getCG : List String -> Codegen
114 |     getCG ("--cg"      :: cg :: _) = fromString cg
115 |     getCG ("--codegen" :: cg :: _) = fromString cg
116 |     getCG [_]                      = Default
117 |     getCG []                       = Default
118 |     getCG (h :: t)                 = getCG t
119 |
120 | coreGitDir : (e : Env) => Path Abs
121 | coreGitDir = gitTmpDir compiler
122 |
123 | copyApp : HasIO io => IdrisEnv => SafeApp -> EitherT PackErr io ()
124 | copyApp ra =
125 |   let dir := pkgBinDir ra.name ra.hash ra.pkg
126 |    in do
127 |      debug "Copying application to \{dir}"
128 |      mkDir dir
129 |      sys ["cp", "-r", Escapable "\{buildPath ra.desc}/exec/" ++ NoEscape "*", dir]
130 |
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}`.
137 |   """
138 |
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 ""
143 |    in """
144 |    export IDRIS2_PACKAGE_PATH="$(\{packBinaryLoc} package-path)"
145 |    export IDRIS2_LIBS="$(\{packBinaryLoc} libs-path)"
146 |    export IDRIS2_DATA="$(\{packBinaryLoc} data-path)"
147 |    \{racket}
148 |    """
149 |
150 | -- When linking to a binary from pack's `bin` directory,
151 | -- we distinguish between applications,
152 | -- which need acceess to the Idris package path and those,
153 | -- which don't. For the former, we create a wrapper script
154 | -- where we first set the `IDRIS2_PACKAGE_PATH` variable
155 | -- before invoking the binary. For both cases, we let pack
156 | -- decide which version to use.
157 | appLink :
158 |      {auto _ : HasIO io}
159 |   -> {auto e : Env}
160 |   -> {auto _ : PackDirs}
161 |   -> (exec        : Body)
162 |   -> (app         : PkgName)
163 |   -> (withPkgPath : Bool)
164 |   -> (codeGen     : Codegen)
165 |   -> EitherT PackErr io ()
166 | appLink exec app withPkgPath cg =
167 |   let
168 |       interp  := case cg of
169 |         Node => "node "
170 |         _ => ""
171 |       target  := MkF packBinDir exec
172 |       content := """
173 |       #!/bin/sh
174 |
175 |       PACK=pack
176 |       if [ -f "\{packExec}" ] && [ -x "\{packExec}" ]; then
177 |         PACK="\{packExec}"
178 |       fi
179 |
180 |       if ! APPLICATION="$(${PACK} app-path \{app})" || [ ! -r "$APPLICATION" ]; then {
181 |       \{unlines $ noAppError app <&> \s => "  echo '\{s}'"}
182 |         } >&2; exit 2
183 |       fi
184 |       \{pthStr withPkgPath "${PACK}"}
185 |
186 |       \{interp}$APPLICATION "$@"
187 |       """
188 |    in write target content >> sys ["chmod", "+x", target]
189 |
190 | installCmd : (withSrc : Bool) -> CmdArgList
191 | installCmd True  = ["--install-with-src"]
192 | installCmd False = ["--install"]
193 |
194 | ||| Check if the build directory associated with the given `.ipkg` file
195 | ||| is outdated, either because it does not contain an `.idrisVersion` file,
196 | ||| or because this file has a different commit tag than the Idris version
197 | ||| we use currently.
198 | export covering
199 | checkBuildDir :  HasIO io => (e : IdrisEnv) => Desc Safe -> EitherT PackErr io ()
200 | checkBuildDir d =
201 |   let buildDir := buildPath d
202 |       version  := the (File Abs) (buildDir /> ".idrisCommit")
203 |       commit   := e.env.db.idrisCommit.value
204 |    in do
205 |      str <- readIfExists version ""
206 |      when (str /= commit) $ do
207 |        rmDir buildDir
208 |        write version commit
209 |
210 | dependsMsg : Path Abs -> String
211 | dependsMsg p = """
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
215 |   manner.
216 |   """
217 |
218 | ||| Use the installed Idris to run an operation on
219 | ||| a library `.ipkg` file.
220 | export covering
221 | libPkg :
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]
233 |    in do
234 |      pre <- (env ++) <$> buildEnv
235 |      debug "About to run: \{escapeCmd s}"
236 |      when cleanBuild (checkBuildDir desc)
237 |
238 |      -- warn if we find a `depends` directory in a local package
239 |      let dependsDir := desc.path.parent /> "depends"
240 |      when !(exists dependsDir) $
241 |        when e.env.config.warnDepends $ warn (dependsMsg dependsDir)
242 |
243 |      inDir (desc.path.parent) (\_ => sysWithEnvAndLog lvl s pre)
244 |
245 | ||| Mapping from OS to platform.
246 | toPlatform : String -> MachineType
247 | toPlatform "unix" = Ubuntu
248 | toPlatform _      = MacOS
249 |
250 | ||| Check whether a machine type represents the current platform.
251 | platformOf : MachineType -> Bool
252 | platformOf mt = mt == toPlatform os
253 |
254 | ||| Verify that the current platform is supported by a package.
255 | verifyPlatform :
256 |      {auto _ : HasIO io}
257 |   -> PkgName
258 |   -> Maybe (List MachineType)
259 |   -> EitherT PackErr io ()
260 | verifyPlatform _    Nothing    = pure ()
261 | verifyPlatform name (Just mts) =
262 |   if any platformOf mts
263 |      then pure ()
264 |      else throwE $ InstallOnIncompatibleMachinePkg
265 |                      (toPlatform os)
266 |                      name
267 |
268 | --------------------------------------------------------------------------------
269 | --          Installing Idris
270 | --------------------------------------------------------------------------------
271 |
272 | hasTTC : String -> Bool
273 | hasTTC = any (("--ttc-version" `isPrefixOf`) . trim) . lines
274 |
275 | covering
276 | getTTCVersion : HasIO io => Env => EitherT PackErr io TTCVersion
277 | getTTCVersion = do
278 |   hlp <- sysRun [idrisExec, "--help"]
279 |   case hasTTC hlp of
280 |     True  => do
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
286 |
287 | -- Tries to build Idris from an existing version of the compiler.
288 | tryDirectBuild : HasIO io => Env => io (Either PackErr ())
289 | tryDirectBuild =
290 |   runEitherT $ do
291 |     sysAndLog Build ["make", "support"]
292 |     sysAndLog Build ["make", "idris2-exec", prefixVar, schemeVar]
293 |
294 | idrisCleanup : HasIO io => Env => io ()
295 | idrisCleanup =
296 |   ignoreError $ do
297 |     sysAndLog Build ["make", "clean-libs"]
298 |     sysAndLog Build ["rm", "-r", "build/ttc", "build/exec"]
299 |
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]
306 |   idrisCleanup
307 |
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]
312 |
313 |   ignoreError $ sysAndLog Build ["rm", "-rf", dir]
314 |
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"]
322 |
323 | ||| Builds and installs the Idris commit given in the environment.
324 | export covering
325 | mkIdris : HasIO io => (e : Env) => EitherT PackErr io IdrisEnv
326 | mkIdris = do
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
333 |         False =>
334 |           -- if building with an existing installation fails for whatever reason
335 |           -- we revert to bootstrapping
336 |           tryDirectBuild >>= \case
337 |             Left x => do
338 |               warn "Building Idris failed. Trying to bootstrap now."
339 |               idrisBootstrap dir
340 |             Right () => pure ()
341 |
342 |       sysAndLog Build ["make", "install-support", prefixVar]
343 |       sysAndLog Build ["make", "install-idris2", prefixVar]
344 |       idrisCleanup
345 |       cacheCoreIpkgFiles dir
346 |
347 |   appLink "idris2" "idris2" True Default
348 |   ttc <- getTTCVersion
349 |   pure $ MkIdrisEnv %search ttc ItHasIdris
350 |
351 | --------------------------------------------------------------------------------
352 | --          Installing Libs
353 | --------------------------------------------------------------------------------
354 |
355 | withSrcStr : (c : Config) => String
356 | withSrcStr = case c.withSrc of
357 |   True  => " (with sources)"
358 |   False => ""
359 |
360 | maybeGiveNotice : HasIO io => Config => SafeLib -> io ()
361 | maybeGiveNotice (RL (Git _ _ _ _ _ (Just notice) _) _ _ _ _ _) = warn notice
362 | maybeGiveNotice _ = pure ()
363 |
364 | installImpl :
365 |      {auto _ : HasIO io}
366 |   -> {auto e : IdrisEnv}
367 |   -> (dir : Path Abs)
368 |   -> SafeLib
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"
374 |    in do
375 |      info "Installing library\{withSrcStr}: \{name rl}"
376 |      maybeGiveNotice 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)
387 |
388 | preInstall :
389 |      {auto _ : HasIO io}
390 |   -> {auto e : IdrisEnv}
391 |   -> SafeLib
392 |   -> EitherT PackErr io ()
393 | preInstall rl = withPkgEnv rl.name rl.pkg $ \dir =>
394 |   let ipkgAbs := ipkg dir rl.pkg
395 |    in case rl.pkg of
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 ()
401 |         Core c => do
402 |           let cache   := coreCachePath c
403 |           copyFile cache ipkgAbs
404 |           case c of
405 |             IdrisApi =>
406 |               sysAndLog Build ["make", "src/IdrisPaths.idr", prefixVar]
407 |             _        => pure ()
408 |
409 | -- Install the given resolved library.
410 | installLib :
411 |      {auto _ : HasIO io}
412 |   -> {auto e : IdrisEnv}
413 |   -> SafeLib
414 |   -> EitherT PackErr io ()
415 | installLib rl = case rl.status of
416 |   Installed _ _ => pure ()
417 |   _             => do
418 |     preInstall rl
419 |     withPkgEnv rl.name rl.pkg $ \dir => do
420 |       installImpl dir rl
421 |       case rl.pkg of
422 |         Local _ _ _ _ =>
423 |           when (not $ isInstalled rl) $ do
424 |             debug "writing \{nanoString} to \{libTimestamp rl.name}"
425 |             write (libTimestamp rl.name) nanoString
426 |         _             => pure ()
427 |
428 |     uncacheLib (name rl)
429 |
430 | --------------------------------------------------------------------------------
431 | --          Installing Apps
432 | --------------------------------------------------------------------------------
433 |
434 | -- Install an Idris application given as a package name
435 | -- TODO: Install wrapper script only if necessary
436 | covering
437 | installApp :
438 |      {auto _ : HasIO io}
439 |   -> {auto e : IdrisEnv}
440 |   -> (withWrapperScript : Bool)
441 |   -> SafeApp
442 |   -> EitherT PackErr io ()
443 | installApp b ra =
444 |   let cg := ipkgCodeGen ra.desc.desc
445 |   in case ra.status of
446 |     BinInstalled _ => pure ()
447 |     Installed    _ => case b of
448 |       False => pure ()
449 |       True  => appLink ra.exec ra.name (usePackagePath ra) cg
450 |     _            => withPkgEnv ra.name ra.pkg $ \dir =>
451 |       let ipkgAbs := ipkg dir ra.pkg
452 |        in case ra.pkg of
453 |             Core _            => pure ()
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)
459 |               copyApp ra
460 |               when b $ appLink ra.exec ra.name pp cg
461 |             Local _ _ pp _    => do
462 |               libPkg [] Build True ["--build"] (notPackIsSafe ra.desc)
463 |               copyApp ra
464 |               when b $ appLink ra.exec ra.name pp cg
465 |               write (libTimestamp ra.name) nanoString
466 |
467 | --------------------------------------------------------------------------------
468 | --          Generating API Docs
469 | --------------------------------------------------------------------------------
470 |
471 | covering
472 | -- Install the API docs for the given resolved library.
473 | installDocs :
474 |      {auto _ : HasIO io}
475 |   -> {auto e : IdrisEnv}
476 |   -> SafeLib
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"
483 |
484 |         pre : List EnvVar
485 |         pre = libInstallPrefix rl
486 |
487 |         htmlDir : Path Abs
488 |         htmlDir = docsDir /> "docs"
489 |
490 |     info "Building source docs for: \{name rl}"
491 |     preInstall rl
492 |     libPkg pre Build False ["--mkdoc"] rl.desc
493 |
494 |     when e.env.config.useKatla $ do
495 |       info "Building highlighted sources for: \{name rl}"
496 |       mkDir htmlDir
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]
505 |               insertSources ds
506 |
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)
511 |
512 | katla : (c : Config) => List (InstallType, PkgName)
513 | katla = if c.withDocs && c.useKatla then [(App False, "katla")] else []
514 |
515 | autoPairs : (c : Config) => List (InstallType, PkgName)
516 | autoPairs =
517 |      map ((Library,) . corePkgName) [ Prelude, Base, Network ]
518 |   ++ map (Library,) c.autoLibs
519 |   ++ map (App True,) c.autoApps
520 |
521 | libInfo : List SafePkg -> List String
522 | libInfo = mapMaybe $ \case Lib rl  => Just "\{rl.name}"
523 |                            App _ _ => Nothing
524 |
525 | appInfo : List SafePkg -> List String
526 | appInfo = mapMaybe $ \case App _ ra => Just "\{ra.name}"
527 |                            Lib _    => Nothing
528 |
529 | ||| Install the given list of libraries or applications, by first
530 | ||| resolving each of them and then creating a build plan including
531 | ||| all dependencies of the lot.
532 | export covering
533 | install :
534 |      {auto _ : HasIO io}
535 |   -> {auto e : IdrisEnv}
536 |   -> List (InstallType, PkgName)
537 |   -> EitherT PackErr io ()
538 | install ps = do
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
544 |
545 |   when e.env.config.withDocs $
546 |     for_ all $ \case Lib rl  => installDocs rl
547 |                      App _ _ => pure ()
548 |
549 | ||| Install the given list of libraries, by first
550 | ||| resolving each of them and then creating a build plan including
551 | ||| all dependencies of the lot.
552 | export covering %inline
553 | installLibs : HasIO io => IdrisEnv => List PkgName -> EitherT PackErr io ()
554 | installLibs = install . map (Library,)
555 |
556 | ||| Install the given list of applications, by first
557 | ||| resolving each of them and then creating a build plan including
558 | ||| all dependencies of the lot.
559 | export covering %inline
560 | installApps : HasIO io => IdrisEnv => List PkgName -> EitherT PackErr io ()
561 | installApps = install . map (App True,)
562 |
563 | ||| Install the (possibly transitive) dependencies of the given
564 | ||| loaded `.ipkg` file.
565 | export covering
566 | installDeps :
567 |      {auto _ : HasIO io}
568 |   -> {auto _ : IdrisEnv}
569 |   -> Desc Safe
570 |   -> EitherT PackErr io ()
571 | installDeps = install . map (Library,) . dependencies
572 |
573 | ||| Creates a packaging environment with Idris2 installed.
574 | export covering
575 | idrisEnv :
576 |      {auto _ : HasIO io}
577 |   -> {auto _ : PackDirs}
578 |   -> {auto _ : TmpDir}
579 |   -> {auto _ : LibCache}
580 |   -> {auto _ : LineBufferingCmd}
581 |   -> MetaConfig
582 |   -> (fetch : FetchMethod)
583 |   -> EitherT PackErr io IdrisEnv
584 | idrisEnv mc fetch = env mc fetch >>= (\e => mkIdris)
585 |
586 | ||| Update the pack installation
587 | export covering
588 | update : HasIO io => IdrisEnv -> EitherT PackErr io ()
589 | update e =
590 |   let bin  := packBinDir
591 |    in do
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}"
595 |
596 |      withGit "pack" packRepo commit True $ \dir => do
597 |        let ipkg := MkF dir "pack.ipkg"
598 |        d <- parseLibIpkg ipkg ipkg
599 |        installDeps d
600 |        let installDir    := packInstallDir commit
601 |            installedExec := installDir /> "pack"
602 |        ex <- exists installedExec
603 |        case ex of
604 |          True  => link installedExec packExec
605 |          False => do
606 |            libPkg [] Build True ["--build"] d
607 |            mkDir installDir
608 |            sys ["cp", "-r", NoEscape "build/exec/*", installDir]
609 |            link installedExec packExec
610 |
611 | --------------------------------------------------------------------------------
612 | --          Removing Libs
613 | --------------------------------------------------------------------------------
614 |
615 | covering
616 | removeApp : HasIO io => Env => PkgName -> EitherT PackErr io ()
617 | removeApp n = do
618 |   info "Removing application \{n}"
619 |   ra <- resolveApp n
620 |   rmFile (pathExec ra.exec)
621 |   rmDir (pkgBinDir ra.name ra.hash ra.pkg)
622 |
623 | covering
624 | removeLib : HasIO io => Env => PkgName -> EitherT PackErr io ()
625 | removeLib n = do
626 |   rl <- resolveLib n
627 |   case isInstalled rl of
628 |     True  => do
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."
633 |
634 | ||| Remove the given libs or apps
635 | export covering
636 | remove : HasIO io => Env => List (PkgType,PkgName) -> EitherT PackErr io ()
637 | remove ps = do
638 |   ref <- emptyCache
639 |   for_ ps  $ \case (PLib,n) => removeLib n
640 |                    (PApp,n) => removeApp n
641 |
642 | ||| Remove the given libs
643 | export covering
644 | removeLibs : HasIO io => Env => List PkgName -> EitherT PackErr io ()
645 | removeLibs ns = do
646 |   checkDeletable ns
647 |   remove $ map (PLib,) ns
648 |
649 | ||| Remove the given apps
650 | export covering
651 | removeApps : HasIO io => Env => List PkgName -> EitherT PackErr io ()
652 | removeApps = remove . map (PApp,)
653 |