0 | module Pack.Database.Types
2 | import Core.Name.Namespace
4 | import Data.List.Elem
5 | import Data.SortedMap
7 | import Derive.Prelude
8 | import Idris.Package.Types
9 | import Pack.Core.Types
12 | %language ElabReflection
20 | data MetaCommit : Type where
21 | MC : Commit -> MetaCommit
22 | Latest : Branch -> MetaCommit
23 | Fetch : Branch -> MetaCommit
26 | FromString MetaCommit where
27 | fromString s = case forget $
split (':' ==) s of
28 | ["latest",branch] => Latest $
MkBranch branch
29 | ["fetch-latest",branch] => Fetch $
MkBranch branch
30 | _ => MC $
MkCommit s
33 | Interpolation MetaCommit where
34 | interpolate (Latest b) = "latest:\{b}"
35 | interpolate (Fetch b) = "fetch-latest:\{b}"
36 | interpolate (MC c) = "\{c}"
39 | toLatest : MetaCommit -> MetaCommit
40 | toLatest (MC x) = Latest (MkBranch x.value)
45 | data FetchMethod : Type where
48 | MissingOnly : FetchMethod
57 | ClearCommits : FetchMethod
59 | %runElab derive "FetchMethod" [Show,Eq,Ord]
79 | corePkgs : List CorePkg
80 | corePkgs = [Prelude, Base, Contrib, Linear, Network, Test, Papers, IdrisApi]
83 | Interpolation CorePkg where
84 | interpolate Prelude = "prelude"
85 | interpolate Base = "base"
86 | interpolate Contrib = "contrib"
87 | interpolate Linear = "linear"
88 | interpolate Network = "network"
89 | interpolate Test = "test"
90 | interpolate Papers = "papers"
91 | interpolate IdrisApi = "idris2"
94 | Cast CorePkg Body where
95 | cast Prelude = "prelude"
97 | cast Contrib = "contrib"
98 | cast Linear = "linear"
99 | cast Network = "network"
101 | cast Papers = "papers"
102 | cast IdrisApi = "idris2"
105 | Cast CorePkg (Path Rel) where
106 | cast c = PRel [< cast c]
110 | corePkgName : CorePkg -> PkgName
111 | corePkgName = MkPkgName . interpolate
115 | coreIpkgFile : CorePkg -> Body
116 | coreIpkgFile IdrisApi = "idris2api.ipkg"
117 | coreIpkgFile c = cast c <+> ".ipkg"
122 | coreIpkgPath : CorePkg -> File Rel
123 | coreIpkgPath IdrisApi = MkF neutral "idris2api.ipkg"
124 | coreIpkgPath c = MkF (neutral /> "libs" //> c) (coreIpkgFile c)
128 | readCorePkg : String -> Maybe CorePkg
129 | readCorePkg "prelude" = Just Prelude
130 | readCorePkg "base" = Just Base
131 | readCorePkg "contrib" = Just Contrib
132 | readCorePkg "linear" = Just Linear
133 | readCorePkg "network" = Just Network
134 | readCorePkg "test" = Just Test
135 | readCorePkg "papers" = Just Papers
136 | readCorePkg "idris2" = Just IdrisApi
137 | readCorePkg _ = Nothing
141 | isCorePkg : String -> Bool
142 | isCorePkg = isJust . readCorePkg
154 | data Package_ : (c : Type) -> Type where
162 | -> (ipkg : File Rel)
163 | -> (pkgPath : Bool)
164 | -> (testIpkg : Maybe (File Rel))
165 | -> (notice : Maybe String)
173 | Local : (dir : Path Abs)
174 | -> (ipkg : File Rel)
175 | -> (pkgPath : Bool)
176 | -> (testIpkg : Maybe (File Rel))
180 | Core : (core : CorePkg) -> Package_ c
183 | Functor Package_ where
184 | map f (Git u c i p t n) = Git u (f c) i p t n
185 | map f (Local d i p t) = Local d i p t
186 | map f (Core c) = Core c
189 | traverse : Applicative f => (URL -> a -> f b) -> Package_ a -> f (Package_ b)
190 | traverse g (Git u c i p t n) = (\c' => Git u c' i p t n) <$> g u c
191 | traverse _ (Local d i p t) = pure $
Local d i p t
192 | traverse _ (Core c) = pure $
Core c
198 | Package = Package_ Commit
203 | 0 UserPackage : Type
204 | UserPackage = Package_ MetaCommit
208 | data IsCore : Package -> Type where
209 | ItIsCore : IsCore (Core c)
212 | Uninhabited (IsCore $
Local {}) where
213 | uninhabited _ impossible
216 | Uninhabited (IsCore $
Git {}) where
217 | uninhabited _ impossible
222 | isCore : (p : Package) -> Dec (IsCore p)
223 | isCore (Core {}) = Yes ItIsCore
224 | isCore (Git {}) = No absurd
225 | isCore (Local {}) = No absurd
229 | data IsLocal : Package -> Type where
230 | ItIsLocal : IsLocal (Local {})
233 | Uninhabited (IsLocal $
Core {}) where
234 | uninhabited _ impossible
237 | Uninhabited (IsLocal $
Git {}) where
238 | uninhabited _ impossible
243 | isLocal : (p : Package) -> Dec (IsLocal p)
244 | isLocal (Core {}) = No absurd
245 | isLocal (Git {}) = No absurd
246 | isLocal (Local {}) = Yes ItIsLocal
250 | data IsGit : Package -> Type where
251 | ItIsGit : IsGit (Git {})
254 | Uninhabited (IsGit $
Core {}) where
255 | uninhabited _ impossible
258 | Uninhabited (IsGit $
Local {}) where
259 | uninhabited _ impossible
264 | isGit : (p : Package) -> Dec (IsGit p)
265 | isGit (Core {}) = No absurd
266 | isGit (Git {}) = Yes ItIsGit
267 | isGit (Local {}) = No absurd
272 | usePackagePath : Package_ c -> Bool
273 | usePackagePath (Git _ _ _ pp _ _) = pp
274 | usePackagePath (Local _ _ pp _) = pp
275 | usePackagePath (Core _) = False
279 | ipkg : (dir : Path Abs) -> Package -> File Abs
280 | ipkg dir (Git _ _ i _ _ _) = toAbsFile dir i
281 | ipkg dir (Local _ i _ _) = toAbsFile dir i
282 | ipkg dir (Core c) = toAbsFile dir (coreIpkgPath c)
290 | data PkgStatus : Package -> Type where
291 | Missing : Hash -> PkgStatus p
292 | Installed : Hash -> (withDocs : Bool) -> PkgStatus p
295 | hash : PkgStatus p -> Hash
296 | hash (Missing h) = h
297 | hash (Installed h _) = h
303 | record ResolvedLib t where
309 | status : PkgStatus pkg
310 | deps : List (DPair Package PkgStatus)
312 | namespace ResolvedLib
315 | nameStr : ResolvedLib t -> String
316 | nameStr = value . name
320 | reTag : ResolvedLib s -> Desc t -> ResolvedLib t
321 | reTag rl d = {desc := d} rl
325 | dependencies : ResolvedLib t -> List PkgName
326 | dependencies rp = dependencies rp.desc
330 | isInstalled : ResolvedLib t -> Bool
331 | isInstalled rl = case rl.status of
335 | namespace AppStatus
338 | data AppStatus : Package -> Type where
340 | Missing : Hash -> AppStatus p
343 | Installed : Hash -> AppStatus p
347 | BinInstalled : Hash -> AppStatus p
353 | record ResolvedApp t where
359 | status : AppStatus pkg
361 | deps : List (DPair Package PkgStatus)
363 | namespace ResolveApp
366 | nameStr : ResolvedApp t -> String
367 | nameStr = value . name
371 | dependencies : ResolvedApp t -> List PkgName
372 | dependencies rp = dependencies rp.desc
376 | reTag : ResolvedApp s -> Desc t -> ResolvedApp t
377 | reTag rl d = {desc := d} rl
382 | usePackagePath : ResolvedApp t -> Bool
383 | usePackagePath = usePackagePath . pkg
389 | data LibOrApp : (t,s : PkgDesc -> Type) -> Type where
390 | Lib : ResolvedLib t -> LibOrApp t s
391 | App : (withWrapperScript : Bool) -> ResolvedApp s -> LibOrApp t s
396 | dependencies : LibOrApp t s -> List PkgName
397 | dependencies (Lib x) = dependencies x
398 | dependencies (App _ x) = dependencies x
402 | pkg : LibOrApp t s -> Package
403 | pkg (Lib x) = x.pkg
404 | pkg (App _ x) = x.pkg
408 | desc : LibOrApp t t -> Desc t
409 | desc (Lib x) = x.desc
410 | desc (App _ x) = x.desc
414 | name : LibOrApp t s -> PkgName
415 | name (Lib x) = x.name
416 | name (App _ x) = x.name
420 | libName : LibOrApp t s -> Maybe PkgName
421 | libName (Lib x) = Just x.name
422 | libName (App _ _) = Nothing
436 | idrisVersion : PkgVersion
437 | packages : SortedMap PkgName (Package_ c)
443 | , packages $= map (map f)
452 | MetaDB = DB_ MetaCommit
456 | setVersion : PkgVersion -> DB -> DB
457 | setVersion v = {idrisVersion := v}
462 | {auto _ : Applicative f}
463 | -> (URL -> a -> f b)
467 | let ic := g db.idrisURL db.idrisCommit
468 | pkgs := traverse (traverse g) db.packages
469 | in [| adj ic pkgs |]
471 | adj : b -> SortedMap PkgName (Package_ b) -> DB_ b
472 | adj ic cb = {idrisCommit := ic, packages := cb} db
474 | tomlBool : Bool -> String
475 | tomlBool True = "true"
476 | tomlBool False = "false"
478 | testPath : Maybe (File Rel) -> Maybe String
479 | testPath = map (\x => "test = \{quote x}")
481 | notice : Maybe String -> Maybe String
482 | notice = map (\x => "notice = \{quote x}")
486 | printPair : (PkgName,Package) -> List String
487 | printPair (x, Git url commit ipkg pp t n) =
489 | , "type = \"github\""
490 | , "url = \{quote url}"
491 | , "commit = \{quote commit}"
492 | , "ipkg = \{quote ipkg}"
493 | , "packagePath = \{tomlBool pp}"
494 | ] ++ (catMaybes [testPath t, notice n])
496 | printPair (x, Local dir ipkg pp t) =
498 | , "type = \"local\""
499 | , "path = \{quote dir}"
500 | , "ipkg = \{quote ipkg}"
501 | , "packagePath = \{tomlBool pp}"
502 | ] ++ (catMaybes [testPath t])
504 | printPair (x, Core c) =
506 | , "type = \"core\""
511 | printDB : DB -> String
512 | printDB (MkDB u c v db) =
519 | in unlines $
header :: (SortedMap.toList db >>= \p => "" :: printPair p)
526 | 0 corePkgsTest : (c : CorePkg) -> Elem c Types.corePkgs
527 | corePkgsTest Prelude = %search
528 | corePkgsTest Base = %search
529 | corePkgsTest Contrib = %search
530 | corePkgsTest Linear = %search
531 | corePkgsTest Network = %search
532 | corePkgsTest Test = %search
533 | corePkgsTest Papers = %search
534 | corePkgsTest IdrisApi = %search
538 | 0 corePkgRoundTrip : (c : CorePkg) -> readCorePkg (interpolate c) === Just c
539 | corePkgRoundTrip Prelude = Refl
540 | corePkgRoundTrip Base = Refl
541 | corePkgRoundTrip Contrib = Refl
542 | corePkgRoundTrip Linear = Refl
543 | corePkgRoundTrip Network = Refl
544 | corePkgRoundTrip Test = Refl
545 | corePkgRoundTrip Papers = Refl
546 | corePkgRoundTrip IdrisApi = Refl