0 | module Pack.Database.Types
  1 |
  2 | import Core.Name.Namespace
  3 | import Data.Either
  4 | import Data.List1
  5 | import Data.List.Elem
  6 | import Data.SortedMap
  7 | import Data.String
  8 | import Derive.Prelude
  9 | import Idris.Package.Types
 10 | import Pack.Core.Types
 11 |
 12 | %default total
 13 | %language ElabReflection
 14 |
 15 | --------------------------------------------------------------------------------
 16 | --          MetaCommits
 17 | --------------------------------------------------------------------------------
 18 |
 19 | ||| Type-level identity
 20 | public export
 21 | 0 I : Type -> Type
 22 | I t = t
 23 |
 24 | ||| A git commit hash or tag, or a meta commit: The latest commit of a branch.
 25 | public export
 26 | data MetaCommit : Type where
 27 |   MC     : Commit -> MetaCommit
 28 |   Latest : Branch -> MetaCommit
 29 |   Fetch  : Branch -> MetaCommit
 30 |
 31 | %runElab derive "MetaCommit" [Show,Eq]
 32 |
 33 | public export
 34 | FromString MetaCommit where
 35 |   fromString s = case forget $ split (':' ==) s of
 36 |     ["latest",branch]       => Latest $ MkBranch branch
 37 |     ["fetch-latest",branch] => Fetch $ MkBranch branch
 38 |     _                       => MC $ MkCommit s
 39 |
 40 | export
 41 | Interpolation MetaCommit where
 42 |   interpolate (Latest b) = "latest:\{b}"
 43 |   interpolate (Fetch b)  = "fetch-latest:\{b}"
 44 |   interpolate (MC c)     = "\{c}"
 45 |
 46 | export
 47 | toLatest : MetaCommit -> MetaCommit
 48 | toLatest (MC x) = Latest (MkBranch x.value)
 49 | toLatest x      = x
 50 |
 51 | ||| Strategy for resolving `MetaCommit`s
 52 | public export
 53 | data FetchMethod : Type where
 54 |   ||| Update only meta commits, which don't have a cached entry in the
 55 |   ||| "commits" directory. This is the default.
 56 |   MissingOnly  : FetchMethod
 57 |
 58 |   ||| Update all custom commits. This is the strategy used with the `fetch`
 59 |   ||| command.
 60 |   All          : FetchMethod
 61 |
 62 |   ||| Remove the "commits" directory, thus forcing all meta commits to
 63 |   ||| be resolved again. This is the strategy used with the `switch latest`
 64 |   ||| command
 65 |   ClearCommits : FetchMethod
 66 |
 67 | %runElab derive "FetchMethod" [Show,Eq,Ord]
 68 |
 69 | --------------------------------------------------------------------------------
 70 | --          Core Packages
 71 | --------------------------------------------------------------------------------
 72 |
 73 | ||| Core packages bundled with the Idris compiler
 74 | public export
 75 | data CorePkg =
 76 |     Prelude
 77 |   | Base
 78 |   | Contrib
 79 |   | Linear
 80 |   | Network
 81 |   | Test
 82 |   | Papers
 83 |   | IdrisApi
 84 |
 85 | ||| The list of core packages.
 86 | public export
 87 | corePkgs : List CorePkg
 88 | corePkgs = [Prelude, Base, Contrib, Linear, Network, Test, Papers, IdrisApi]
 89 |
 90 | export
 91 | Interpolation CorePkg where
 92 |   interpolate Prelude  = "prelude"
 93 |   interpolate Base     = "base"
 94 |   interpolate Contrib  = "contrib"
 95 |   interpolate Linear   = "linear"
 96 |   interpolate Network  = "network"
 97 |   interpolate Test     = "test"
 98 |   interpolate Papers   = "papers"
 99 |   interpolate IdrisApi = "idris2"
100 |
101 | export
102 | Cast CorePkg Body where
103 |   cast Prelude  = "prelude"
104 |   cast Base     = "base"
105 |   cast Contrib  = "contrib"
106 |   cast Linear   = "linear"
107 |   cast Network  = "network"
108 |   cast Test     = "test"
109 |   cast Papers   = "papers"
110 |   cast IdrisApi = "idris2"
111 |
112 | export %inline
113 | Cast CorePkg (Path Rel) where
114 |   cast c = PRel [< cast c]
115 |
116 | ||| Package name of a core package.
117 | export
118 | corePkgName : CorePkg -> PkgName
119 | corePkgName = MkPkgName . interpolate
120 |
121 | ||| `.ipkg` file name corrsponding to a core package.
122 | export
123 | coreIpkgFile : CorePkg -> Body
124 | coreIpkgFile IdrisApi = "idris2api.ipkg"
125 | coreIpkgFile c        = cast c <+> ".ipkg"
126 |
127 | ||| Relative path to the `.ipkg` file corrsponding to a core package
128 | ||| (in the Idris2 project).
129 | export
130 | coreIpkgPath : CorePkg -> File Rel
131 | coreIpkgPath IdrisApi = MkF neutral "idris2api.ipkg"
132 | coreIpkgPath c        = MkF (neutral /> "libs" //> c) (coreIpkgFile c)
133 |
134 | ||| Try to convert a string to a core package.
135 | export
136 | readCorePkg : String -> Maybe CorePkg
137 | readCorePkg "prelude" = Just Prelude
138 | readCorePkg "base"    = Just Base
139 | readCorePkg "contrib" = Just Contrib
140 | readCorePkg "linear"  = Just Linear
141 | readCorePkg "network" = Just Network
142 | readCorePkg "test"    = Just Test
143 | readCorePkg "papers"  = Just Papers
144 | readCorePkg "idris2"  = Just IdrisApi
145 | readCorePkg _         = Nothing
146 |
147 | ||| True, if the given string corresponds to one of the core packges.
148 | export
149 | isCorePkg : String -> Bool
150 | isCorePkg = isJust . readCorePkg
151 |
152 | --------------------------------------------------------------------------------
153 | --          Packages
154 | --------------------------------------------------------------------------------
155 |
156 | ||| Description of a Git or local Idris package in the
157 | ||| package database.
158 | |||
159 | ||| Note: This does not contain the package name, as it
160 | ||| will be paired with its name in a `SortedMap`.
161 | public export
162 | data Package_ : (f : Type -> Type) -> (c : Type) -> Type where
163 |   ||| A Git repository, given as the package's URL,
164 |   ||| commit (hash or tag), and name of `.ipkg` file to use.
165 |   ||| `pkgPath` should be set to `True` for executables which need
166 |   ||| access to the `IDRIS2_PACKAGE_PATH`: The list of directories
167 |   ||| where Idris packages are installed.
168 |   Git :  (url      : f URL)
169 |       -> (commit   : f c)
170 |       -> (ipkg     : f $ File Rel)
171 |       -> (pkgPath  : f $ Bool)
172 |       -> (testIpkg : Maybe (File Rel))
173 |       -> (notice   : Maybe String)
174 |       -> (runsOn   : Maybe (List MachineType))
175 |       -> Package_ f c
176 |
177 |   ||| A local Idris project given as an absolute path to a local
178 |   ||| directory, and `.ipkg` file to use.
179 |   ||| `pkgPath` should be set to `True` for executable which need
180 |   ||| access to the `IDRIS2_PACKAGE_PATH`: The list of directories
181 |   ||| where Idris packages are installed.
182 |   Local  :  (dir      : Path Abs)
183 |          -> (ipkg     : File Rel)
184 |          -> (pkgPath  : Bool)
185 |          -> (testIpkg : Maybe (File Rel))
186 |          -> Package_ f c
187 |
188 |   ||| A core package of the Idris2 project
189 |   Core   : (core : CorePkg) -> Package_ f c
190 |
191 | export
192 | Functor (Package_ I) where
193 |   map f (Git u c i p t n ro) = Git u (f c) i p t n ro
194 |   map f (Local d i p t) = Local d i p t
195 |   map f (Core c)        = Core c
196 |
197 | export
198 | traverse : Applicative f => (URL -> a -> f b) -> Package_ I a -> f (Package_ I b)
199 | traverse g (Git u c i p t n ro) = (\c' => Git u c' i p t n ro) <$> g u c
200 | traverse _ (Local d i p t)    = pure $ Local d i p t
201 | traverse _ (Core c)           = pure $ Core c
202 |
203 | ||| An alias for `Package_ Commit`: A package description with
204 | ||| meta commits already resolved.
205 | public export
206 | 0 Package : Type
207 | Package = Package_ I Commit
208 |
209 | ||| An alias for `Package_ MetaCommit`: A package description where
210 | ||| the commit might still contain meta information.
211 | public export
212 | 0 UserPackage : Type
213 | UserPackage = Package_ Maybe MetaCommit
214 |
215 | public export
216 | 0 PackageMap : Type
217 | PackageMap = SortedMap PkgName Package
218 |
219 | public export
220 | 0 CustomMap : Type
221 | CustomMap = SortedMap DBName (SortedMap PkgName UserPackage)
222 |
223 | ||| Proof that a package is a core package
224 | public export
225 | data IsCore : Package -> Type where
226 |   ItIsCore : IsCore (Core c)
227 |
228 | export
229 | Uninhabited (IsCore $ Local {}) where
230 |   uninhabited _ impossible
231 |
232 | export
233 | Uninhabited (IsCore $ Git {}) where
234 |   uninhabited _ impossible
235 |
236 | ||| Decides, if the given package represents
237 | ||| one of the core packages (`base`, `prelude`, etc.)
238 | export
239 | isCore : (p : Package) -> Dec (IsCore p)
240 | isCore (Core {})  = Yes ItIsCore
241 | isCore (Git {})   = No absurd
242 | isCore (Local {}) = No absurd
243 |
244 | ||| Proof that a package is a local package
245 | public export
246 | data IsLocal : Package -> Type where
247 |   ItIsLocal : IsLocal (Local {})
248 |
249 | export
250 | Uninhabited (IsLocal $ Core {}) where
251 |   uninhabited _ impossible
252 |
253 | export
254 | Uninhabited (IsLocal $ Git {}) where
255 |   uninhabited _ impossible
256 |
257 | ||| Decides, if the given package represents
258 | ||| a local package.
259 | export
260 | isLocal : (p : Package) -> Dec (IsLocal p)
261 | isLocal (Core {})  = No absurd
262 | isLocal (Git {})   = No absurd
263 | isLocal (Local {}) = Yes ItIsLocal
264 |
265 | ||| Proof that a package is a Git package
266 | public export
267 | data IsGit : Package -> Type where
268 |   ItIsGit : IsGit (Git {})
269 |
270 | export
271 | Uninhabited (IsGit $ Core {}) where
272 |   uninhabited _ impossible
273 |
274 | export
275 | Uninhabited (IsGit $ Local {}) where
276 |   uninhabited _ impossible
277 |
278 | ||| Decides, if the given package represents
279 | ||| a Git package.
280 | export
281 | isGit : (p : Package) -> Dec (IsGit p)
282 | isGit (Core {})  = No absurd
283 | isGit (Git {})   = Yes ItIsGit
284 | isGit (Local {}) = No absurd
285 |
286 | ||| True, if the given application needs access to the
287 | ||| folders where Idris package are installed.
288 | export
289 | usePackagePath : Package_ I c -> Bool
290 | usePackagePath (Git _ _ _ pp _ _ _) = pp
291 | usePackagePath (Local _ _ pp _)     = pp
292 | usePackagePath (Core _)             = False
293 |
294 | ||| Absolute path to the `.ipkg` file of a package.
295 | export
296 | ipkg : (dir : Path Abs) -> Package -> File Abs
297 | ipkg dir (Git _ _ i _ _ _ _) = toAbsFile dir i
298 | ipkg dir (Local _ i _ _)     = toAbsFile dir i
299 | ipkg dir (Core c)            = toAbsFile dir (coreIpkgPath c)
300 |
301 | --------------------------------------------------------------------------------
302 | --          Resolved Packages
303 | --------------------------------------------------------------------------------
304 |
305 | ||| Installation status of an Idris package.
306 | public export
307 | data PkgStatus : Package -> Type where
308 |   Missing     : Hash -> PkgStatus p
309 |   Installed   : Hash -> (withDocs : Bool) -> PkgStatus p
310 |
311 | export
312 | hash : PkgStatus p -> Hash
313 | hash (Missing h)     = h
314 | hash (Installed h _) = h
315 |
316 | ||| A resolved library, which was cloned from a Git repo
317 | ||| or looked up in the local file system. This comes with
318 | ||| a fully parsed `PkgDesc` (representing the `.ipkg` file).
319 | public export
320 | record ResolvedLib t where
321 |   constructor RL
322 |   pkg     : Package
323 |   hash    : Hash
324 |   name    : PkgName
325 |   desc    : Desc t
326 |   status  : PkgStatus pkg
327 |   deps    : List (DPair Package PkgStatus)
328 |
329 | namespace ResolvedLib
330 |   ||| Extracts the package name from a resolved library.
331 |   export %inline
332 |   nameStr : ResolvedLib t -> String
333 |   nameStr = value . name
334 |
335 |   ||| Change the type-level tag of a resolved library.
336 |   export %inline
337 |   reTag : ResolvedLib s -> Desc t -> ResolvedLib t
338 |   reTag rl d = {desc := d} rl
339 |
340 |   ||| Extracts the dependencies of a resolved library.
341 |   export
342 |   dependencies : ResolvedLib t -> List PkgName
343 |   dependencies rp = dependencies rp.desc
344 |
345 |   ||| Check if the given library is installed
346 |   export
347 |   isInstalled : ResolvedLib t -> Bool
348 |   isInstalled rl = case rl.status of
349 |     Missing _ => False
350 |     _         => True
351 |
352 | namespace AppStatus
353 |   ||| Installation status of an Idris app.
354 |   public export
355 |   data AppStatus : Package -> Type where
356 |     ||| The app has not been compiled and is therfore missing
357 |     Missing      :  Hash -> AppStatus p
358 |
359 |     ||| The app has been built but is not on the `PATH`.
360 |     Installed    :  Hash -> AppStatus p
361 |
362 |     ||| The app has been built and a wrapper script has been added
363 |     ||| to `$PACK_DIR/bin`, so it should be on the `PATH`.
364 |     BinInstalled :  Hash -> AppStatus p
365 |
366 | ||| A resolved application, which was cloned from a Git repo
367 | ||| or looked up in the local file system. This comes with
368 | ||| a fully parsed `PkgDesc` (representing the `.ipkg` file).
369 | public export
370 | record ResolvedApp t where
371 |   constructor RA
372 |   pkg     : Package
373 |   hash    : Hash
374 |   name    : PkgName
375 |   desc    : Desc t
376 |   status  : AppStatus pkg
377 |   exec    : Body
378 |   deps    : List (DPair Package PkgStatus)
379 |
380 | namespace ResolveApp
381 |   ||| Extracts the package name from a resolved application.
382 |   export %inline
383 |   nameStr : ResolvedApp t -> String
384 |   nameStr = value . name
385 |
386 |   ||| Extracts the dependencies of a resolved application.
387 |   export
388 |   dependencies : ResolvedApp t -> List PkgName
389 |   dependencies rp = dependencies rp.desc
390 |
391 |   ||| Change the type-level tag of a resolved application.
392 |   export %inline
393 |   reTag : ResolvedApp s -> Desc t -> ResolvedApp t
394 |   reTag rl d = {desc := d} rl
395 |
396 |   ||| True, if the given application needs access to the
397 |   ||| folders where Idris package are installed.
398 |   export %inline
399 |   usePackagePath : ResolvedApp t -> Bool
400 |   usePackagePath = usePackagePath . pkg
401 |
402 | ||| Either a resolved library or application tagged with the given tag.
403 | ||| This is to be used in build plans, so applications come with the
404 | ||| additional info whether we want to install a wrapper script or not.
405 | public export
406 | data LibOrApp : (t,s : PkgDesc -> Type) -> Type where
407 |   Lib : ResolvedLib t -> LibOrApp t s
408 |   App : (withWrapperScript : Bool) -> ResolvedApp s -> LibOrApp t s
409 |
410 | namespace LibOrApp
411 |   ||| Extract the dependencies of a resolved library or application.
412 |   export
413 |   dependencies : LibOrApp t s -> List PkgName
414 |   dependencies (Lib x)   = dependencies x
415 |   dependencies (App _ x) = dependencies x
416 |
417 |   ||| Extract the package of a resolved library or application.
418 |   export
419 |   pkg : LibOrApp t s -> Package
420 |   pkg (Lib x)   = x.pkg
421 |   pkg (App _ x) = x.pkg
422 |
423 |   ||| Extract the description of a resolved library or application.
424 |   export
425 |   desc : LibOrApp t t -> Desc t
426 |   desc (Lib x)   = x.desc
427 |   desc (App _ x) = x.desc
428 |
429 |   ||| Extract the package name of a resolved library or application.
430 |   export
431 |   name : LibOrApp t s -> PkgName
432 |   name (Lib x)   = x.name
433 |   name (App _ x) = x.name
434 |
435 |   ||| Try to extract the package name of a resolved library.
436 |   export
437 |   libName : LibOrApp t s -> Maybe PkgName
438 |   libName (Lib x)   = Just x.name
439 |   libName (App _ _) = Nothing
440 |
441 | --------------------------------------------------------------------------------
442 | --          Package Database
443 | --------------------------------------------------------------------------------
444 |
445 | ||| DB used for building packages. This includes
446 | ||| the Idris commit to use, together with a curated list of
447 | ||| known packages.
448 | public export
449 | record DB_ c where
450 |   constructor MkDB
451 |   idrisURL     : URL
452 |   idrisCommit  : c
453 |   idrisVersion : PkgVersion
454 |   packages     : SortedMap PkgName (Package_ I c)
455 |
456 | export
457 | Functor DB_ where
458 |   map f db = {
459 |       idrisCommit $= f
460 |     , packages    $= map (map f)
461 |     } db
462 |
463 | public export
464 | 0 DB : Type
465 | DB = DB_ Commit
466 |
467 | public export
468 | 0 MetaDB : Type
469 | MetaDB = DB_ MetaCommit
470 |
471 | ||| Sets the package version.
472 | export %inline
473 | setVersion : PkgVersion -> DB -> DB
474 | setVersion v = {idrisVersion := v}
475 |
476 | ||| Effectfully convert package descriptions in a DB
477 | export
478 | traverseDB :
479 |      {auto _ : Applicative f}
480 |   -> (URL -> a -> f b)
481 |   -> DB_ a
482 |   -> f (DB_ b)
483 | traverseDB g db =
484 |   let ic   := g db.idrisURL db.idrisCommit
485 |       pkgs := traverse (traverse g) db.packages
486 |    in [| adj ic pkgs |]
487 |     where
488 |       adj : b -> SortedMap PkgName (Package_ I b) -> DB_ b
489 |       adj ic cb = {idrisCommit := ic, packages := cb} db
490 |
491 | tomlBool : Bool -> String
492 | tomlBool True  = "true"
493 | tomlBool False = "false"
494 |
495 | testPath : Maybe (File Rel) -> Maybe String
496 | testPath = map (\x => "test        = \{quote x}")
497 |
498 | notice : Maybe String -> Maybe String
499 | notice = map (\x =>   "notice      = \{quote x}")
500 |
501 | -- we need to print `Git` packages as `"github"` at
502 | -- least for the time being for reasons of compatibility
503 | printPair : (PkgName,Package) -> List String
504 | printPair (x, Git url commit ipkg pp t n ro) =
505 |   [ "[db.\{x}]"
506 |   , "type        = \"github\""
507 |   , "url         = \{quote url}"
508 |   , "commit      = \{quote commit}"
509 |   , "ipkg        = \{quote ipkg}"
510 |   , "packagePath = \{tomlBool pp}"
511 |   ] ++ (catMaybes [testPath t, notice n, runsOn ro])
512 |
513 | printPair (x, Local dir ipkg pp t) =
514 |   [ "[db.\{x}]"
515 |   , "type        = \"local\""
516 |   , "path        = \{quote dir}"
517 |   , "ipkg        = \{quote ipkg}"
518 |   , "packagePath = \{tomlBool pp}"
519 |   ] ++ (catMaybes [testPath t])
520 |
521 | printPair (x, Core c) =
522 |   [ "[db.\{x}]"
523 |   , "type        = \"core\""
524 |   ]
525 |
526 | ||| Convert a package collection to a valid TOML string.
527 | export
528 | printDB : DB -> String
529 | printDB (MkDB u c v db) =
530 |   let header := """
531 |         [idris2]
532 |         url     = "\{u}"
533 |         version = "\{v}"
534 |         commit  = "\{c}"
535 |         """
536 |    in unlines $ header :: (SortedMap.toList db >>= \p => "" :: printPair p)
537 |
538 | --------------------------------------------------------------------------------
539 | --          Resolving Packages
540 | --------------------------------------------------------------------------------
541 |
542 | export
543 | mergeUP : UserPackage -> UserPackage -> UserPackage
544 | mergeUP (Git u1 c1 i1 p1 t1 n1 ro1) (Git u2 c2 i2 p2 t2 n2 ro2) =
545 |   Git (u1<|>u2)(c1<|>c2)(i1<|>i2)(p1<|>p2)(t1<|>t2)(n1<|>n2)(ro1<|>ro2)
546 | mergeUP _ y = y
547 |
548 | --------------------------------------------------------------------------------
549 | --          Tests
550 | --------------------------------------------------------------------------------
551 |
552 | -- make sure no core package was forgotten
553 | 0 corePkgsTest : (c : CorePkg) -> Elem c Types.corePkgs
554 | corePkgsTest Prelude  = %search
555 | corePkgsTest Base     = %search
556 | corePkgsTest Contrib  = %search
557 | corePkgsTest Linear   = %search
558 | corePkgsTest Network  = %search
559 | corePkgsTest Test     = %search
560 | corePkgsTest Papers   = %search
561 | corePkgsTest IdrisApi = %search
562 |
563 | -- all core packages should be parsable from their
564 | -- interpolation string
565 | 0 corePkgRoundTrip : (c : CorePkg) -> readCorePkg (interpolate c) === Just c
566 | corePkgRoundTrip Prelude  = Refl
567 | corePkgRoundTrip Base     = Refl
568 | corePkgRoundTrip Contrib  = Refl
569 | corePkgRoundTrip Linear   = Refl
570 | corePkgRoundTrip Network  = Refl
571 | corePkgRoundTrip Test     = Refl
572 | corePkgRoundTrip Papers   = Refl
573 | corePkgRoundTrip IdrisApi = Refl
574 |