4 | module Pack.Core.Types
6 | import public Data.FilePath.File
10 | import Derive.Prelude
11 | import Idris.Package.Types
15 | %language ElabReflection
26 | quote : Interpolation a => a -> String
27 | quote v = "\"\{v}\""
37 | relativeTo : (origin, target : Path Abs) -> Path Rel
38 | relativeTo (PAbs _ sx) (PAbs _ sy) = PRel $
go (sx <>> []) (sy <>> [])
41 | go : (o,t : List Body) -> SnocList Body
42 | go [>] t = Lin <>< t
43 | go xs [] = Lin <>< xs $> ".."
44 | go (x :: xs) (y :: ys) = case x == y of
46 | False => Lin <>< (((x :: xs) $> "..") ++ (y :: ys))
50 | isIpkgBody : Body -> Bool
51 | isIpkgBody = (Just "ipkg" ==) . extension
55 | isTomlBody : Body -> Bool
56 | isTomlBody = (Just "toml" ==) . extension
60 | isHtmlBody : Body -> Bool
61 | isHtmlBody = (Just "html" ==) . extension
63 | toRelPath : String -> Path Rel
64 | toRelPath s = case the FilePath (fromString s) of
65 | FP (PAbs _ sx) => PRel sx
66 | FP (PRel sx) => PRel sx
72 | toAbsPath : (parent : Path Abs) -> FilePath -> Path Abs
73 | toAbsPath parent (FP $
PAbs d sx) = PAbs d sx
74 | toAbsPath parent (FP $
PRel sx) = parent </> PRel sx
80 | parse : String -> (parentDir : Path Abs) -> Path Abs
81 | parse s parentDir = toAbsPath parentDir (fromString s)
84 | Cast String (Path Rel) where
87 | export infixl 5 <//>
91 | (<//>) : Cast a (Path Rel) => Path t -> a -> Path t
92 | p <//> v = p </> cast v
99 | (//>) : Cast a Body => Path t -> a -> Path t
100 | p //> v = p /> cast v
102 | export infixl 8 <->
106 | (<->) : Cast a Body => Cast b Body => a -> b -> Body
107 | x <-> y = the Body (cast x) <+> "-" <+> cast y
111 | Cast PkgVersion Body where
112 | cast = fromMaybe "0" . parse . show
116 | Cast (Maybe PkgVersion) Body where
118 | cast (Just v) = cast v
123 | toAbsFile : Path Abs -> File Rel -> File Abs
124 | toAbsFile parentDir (MkF p f) = MkF (parentDir </> p) f
127 | Cast (File t) (Path t) where cast = toPath
136 | record PackDirs where
182 | data CurDir : Type where
184 | CD : (dir : Path Abs) -> CurDir
187 | Interpolation CurDir where
188 | interpolate (CD dir) = interpolate dir
193 | curDir : (cd : CurDir) => Path Abs
194 | curDir {cd = CD dir} = dir
199 | data TmpDir : Type where
201 | TD : (dir : Path Abs) -> TmpDir
204 | Interpolation TmpDir where
205 | interpolate (TD dir) = interpolate dir
210 | tmpDir : (td : TmpDir) => Path Abs
211 | tmpDir {td = TD dir} = dir
219 | record TTCVersion where
222 | version : Maybe Body
227 | ttcVersion : (ttc : TTCVersion) => Maybe Body
228 | ttcVersion = ttc.version
237 | Interpolation FileError where interpolate = show
242 | Interpolation PkgVersion where interpolate = show
254 | %runElab derive "URL" [Show,Eq,FromString]
257 | Interpolation URL where interpolate = value
260 | Cast URL (Path Rel) where
261 | cast (MkURL s) = case unpack s of
262 | 'h'::'t'::'t'::'p'::'s'::':'::'/'::'/':: t => cast (pack t)
263 | 'h'::'t'::'t'::'p'::':'::'/'::'/':: t => cast (pack t)
272 | record Commit where
273 | constructor MkCommit
276 | %runElab derive "Commit" [Show,Eq,FromString]
279 | Interpolation Commit where interpolate = value
282 | Cast Commit (Path Rel) where
283 | cast = toRelPath . value
291 | %runElab derive "Hash" [Show,Eq,FromString]
294 | Interpolation Hash where interpolate = value
297 | Cast Hash (Path Rel) where
298 | cast = toRelPath . value
306 | record Branch where
307 | constructor MkBranch
310 | %runElab derive "Branch" [Show,Eq,FromString]
313 | Interpolation Branch where interpolate = value
316 | Cast Branch (Path Rel) where
317 | cast = toRelPath . value
325 | record PkgName where
326 | constructor MkPkgName
329 | %runElab derive "PkgName" [Show,Eq,Ord,FromString]
332 | Interpolation PkgName where interpolate = value
335 | Cast PkgName (Path Rel) where
336 | cast = toRelPath . value
345 | data PkgOrIpkg : Type where
346 | Pkg : PkgName -> PkgOrIpkg
347 | Ipkg : File Abs -> PkgOrIpkg
355 | data PkgType = PLib | PApp
358 | Interpolation PkgType where
359 | interpolate PLib = "lib"
360 | interpolate PApp = "app"
364 | PLib == PLib = True
365 | PApp == PApp = True
370 | compare PLib PApp = LT
371 | compare PApp PLib = GT
382 | data InstallType : Type where
383 | Library : InstallType
384 | App : (withWrapperScript : Bool) -> InstallType
387 | Interpolation InstallType where
388 | interpolate Library = "library"
389 | interpolate (App b) = "app"
392 | Eq InstallType where
393 | Library == Library = True
394 | App b1 == App b2 = b1 == b2
398 | Ord InstallType where
399 | compare Library (App _) = LT
400 | compare (App _) Library = GT
401 | compare Library Library = EQ
402 | compare (App b1) (App b2) = compare b1 b2
411 | record DBName where
412 | constructor MkDBName
416 | Eq DBName where (==) = (==) `on` value
419 | Ord DBName where compare = compare `on` value
422 | Interpolation DBName where interpolate = interpolate . value
425 | Cast DBName Body where
430 | Head = MkDBName "HEAD"
434 | All = MkDBName "all"
442 | data MachineType = Ubuntu | MacOS
444 | %runElab derive "MachineType" [Eq,Ord]
446 | machineTypeToNat : MachineType -> Nat
447 | machineTypeToNat Ubuntu = 0
448 | machineTypeToNat MacOS = 1
451 | Interpolation MachineType where
452 | interpolate Ubuntu = "ubuntu"
453 | interpolate MacOS = "macos"
456 | runsOn : Maybe (List MachineType) -> Maybe String
457 | runsOn = map $
\xs =>
459 | concat (intersperse ", " $
map quote xs) ++
472 | record Desc (t : PkgDesc -> Type) where
488 | 0 U : PkgDesc -> Type
494 | dependencies : PkgDesc -> List PkgName
495 | dependencies d = map (MkPkgName . pkgname) $
d.depends
499 | dependencies : Desc t -> List PkgName
500 | dependencies d = dependencies d.desc
508 | data CmdArg : Type where
514 | Escapable : String -> CmdArg
521 | NoEscape : String -> CmdArg
528 | interface CmdArgable a where
529 | toCmdArg : a -> CmdArg
532 | CmdArgable CmdArg where
538 | Interpolation a => CmdArgable a where
539 | toCmdArg = Escapable . interpolate
551 | data CmdArgList : Type where
553 | (::) : CmdArgable a => a -> CmdArgList -> CmdArgList
555 | cmdArgList : CmdArgList -> List CmdArg
557 | cmdArgList (x::xs) = toCmdArg x :: cmdArgList xs
566 | escapeCmd : CmdArgList -> String
567 | escapeCmd = unwords . map manageArg . cmdArgList where
568 | manageArg : CmdArg -> String
569 | manageArg $
Escapable s = escapeArg s
570 | manageArg $
NoEscape s = s
583 | (++) : CmdArg -> CmdArg -> CmdArg
584 | Escapable x ++ Escapable y = Escapable $
x ++ y
585 | Escapable x ++ NoEscape y = NoEscape $
escapeArg x ++ y
586 | NoEscape x ++ Escapable y = NoEscape $
x ++ escapeArg y
587 | NoEscape x ++ NoEscape y = NoEscape $
x ++ y
589 | namespace CmdArgList
593 | (++) : CmdArgList -> CmdArgList -> CmdArgList
595 | (x::xs) ++ ys = x :: xs ++ ys
598 | Semigroup CmdArgList where
602 | Monoid CmdArgList where
608 | concatMap : Monoid m => (CmdArg -> m) -> CmdArgList -> m
609 | concatMap f [] = neutral
610 | concatMap f (x::xs) = f (toCmdArg x) <+> concatMap f xs
615 | fromStrList : List String -> CmdArgList
616 | fromStrList = foldr (\x, xs => x::xs) Nil
625 | record LineBufferingCmd where
627 | constructor MkLineBufferingCmd
628 | lineBufferingCmd : CmdArgList
636 | data LogLevel : Type where
645 | llToNat : LogLevel -> Nat
650 | llToNat Warning = 4
651 | llToNat Silence = 5
654 | Eq LogLevel where (==) = (==) `on` llToNat
657 | Ord LogLevel where compare = compare `on` llToNat
660 | Interpolation LogLevel where
661 | interpolate Debug = "debug"
662 | interpolate Build = "build"
663 | interpolate Info = "info"
664 | interpolate Cache = "cache"
665 | interpolate Warning = "warning"
666 | interpolate Silence = ""
669 | logLevels : List (String, LogLevel)
671 | [ ("debug" , Debug )
672 | , ("build" , Build )
674 | , ("cache" , Cache )
675 | , ("warning", Warning)
676 | , ("silence", Silence)
681 | record LogRef where
683 | constructor MkLogRef
692 | data TOMLErr : Type where
695 | MissingKey : (path : List String) -> TOMLErr
698 | WrongType : (path : List String) -> (type : String) -> TOMLErr
700 | tomlPath : List String -> String
701 | tomlPath = concat . intersperse "."
703 | printTOMLErr : TOMLErr -> String
704 | printTOMLErr (MissingKey path) = "Missing toml key: \{tomlPath path}."
705 | printTOMLErr (WrongType path type) =
706 | "Wrong type at \{tomlPath path}. Expect \{type}."
711 | prefixKey : (key : String) -> Either TOMLErr a -> Either TOMLErr a
712 | prefixKey k = mapFst $
\case
713 | MissingKey p => MissingKey (k :: p)
714 | WrongType p t => WrongType (k :: p) t
718 | data PackErr : Type where
723 | NoPackDir : PackErr
729 | MkDir : (path : Path Abs) -> (err : FileError) -> PackErr
732 | ReadFile : (path : File Abs) -> (err : FileError) -> PackErr
735 | WriteFile : (path : File Abs) -> (err : FileError) -> PackErr
738 | DirEntries : (path : Path Abs) -> (err : FileError) -> PackErr
741 | Sys : (cmd : CmdArgList) -> (err : Int) -> PackErr
744 | ChangeDir : (path : Path Abs) -> PackErr
747 | UnknownPkg : (name : PkgName) -> PackErr
750 | IncompletePkg : (name : PkgName) -> PackErr
753 | NotLocalPkg : (name : PkgName) -> PackErr
757 | InstallOnIncompatibleMachinePkg : (mt : MachineType) -> (name : PkgName) -> PackErr
761 | NoApp : (rep : PkgName) -> PackErr
765 | NoAppIpkg : (path : File Abs) -> PackErr
768 | InvalidPackageDesc : (s : String) -> PackErr
771 | EmptyPkgDB : PackErr
774 | InvalidDBHeader : (s : String) -> PackErr
777 | InvalidDBName : (s : String) -> PackErr
780 | InvalidPkgType : (s : String) -> PackErr
783 | InvalidPkgVersion : (s : String) -> PackErr
786 | InvalidLogLevel : (s : String) -> PackErr
789 | InvalidIpkgFile : (path : File Abs) -> PackErr
792 | InvalidBody : (s : String) -> PackErr
795 | NoFilePath : (str : String) -> PackErr
799 | MissingCorePackage :
801 | -> (version : PkgVersion)
802 | -> (commit : Commit)
806 | UnknownArg : (arg : String) -> PackErr
809 | InvalidArgs : (args : List String) -> PackErr
812 | ErroneousArg : (err : String) -> PackErr
816 | BuildMany : List Body -> PackErr
819 | UnknownCommand : String -> (usage : String) -> PackErr
824 | -> (args : List String)
825 | -> (usage : String)
830 | DirExists : Path Abs -> PackErr
833 | TOMLFile : (file : File Abs) -> (err : TOMLErr) -> PackErr
836 | TOMLParse : (err : String) -> PackErr
839 | BuildFailures : Nat -> PackErr
842 | ManualInstallPackApp : PackErr
845 | SafetyAbort : PackErr
847 | CyclicDeps : List PkgName -> PackErr
851 | printErr : PackErr -> String
852 | printErr NoCurDir = "Failed to get current directory."
854 | printErr NoPackDir = """
855 | Failed to figure out package directory.
856 | This means, that neither environment variable \"PACK_DIR\"
857 | nor environment varaible \"HOME\" was set.
860 | printErr NoTmpDir = """
861 | Failed to create temporary directory.
862 | Please check directory `PACK_DIR` and make sure to remove
863 | all `.tmpXY` directories you no longer need.
866 | printErr (MkDir path err) =
867 | "Error when creating directory \{quote path}: \{err}."
869 | printErr (ReadFile path err) =
870 | "Error when reading file \{quote path}: \{err}."
872 | printErr (WriteFile path err) =
873 | "Error when writing to file \{quote path}: \{err}."
875 | printErr (DirEntries path err) =
876 | "Error when reading directory \{quote path}: \{err}."
878 | printErr (Sys cmd err) = """
879 | Error when executing system command.
880 | Command: \{escapeCmd cmd}
881 | Error code: \{show err}
884 | printErr (ChangeDir path) =
885 | "Failed to change to directory \{quote path}."
887 | printErr (InvalidPackageDesc s) = """
888 | Invalid package description: \{quote s}.
889 | This should be of the format \"name,url,commit hash,ipkg file\".
892 | printErr (InvalidDBHeader s) = """
893 | Invalid data base header: \{quote s}.
894 | This should be of the format \"idris2 commit hash,idris2 version\".
897 | printErr (InvalidDBName s) = """
898 | Invalid data collection name: \{quote s}.
899 | This should be a non-empty string without path separators.
902 | printErr (InvalidBody s) = "Invalid file path body: \{quote s}."
904 | printErr (InvalidPkgType s) = """
905 | Invalid package type: \{quote s}.
906 | Valid types are `lib` and `bin`.
909 | printErr (InvalidPkgVersion s) = "Invalid package version: \{quote s}."
911 | printErr (InvalidLogLevel s) = """
912 | Invalid log level: \{quote s}. Valid values are
913 | \{joinBy "\n" $ ("- " ++) . fst <$> logLevels}
916 | printErr (UnknownPkg name) = "Unknown package: \{name}"
918 | printErr (IncompletePkg name) = "Incomplete data for custom package: \{name}"
920 | printErr (NotLocalPkg name) = "Not a local package: \{name}"
922 | printErr (InstallOnIncompatibleMachinePkg mt name) = """
923 | Trying to install the following package on
924 | an incompatible machine type (\{mt}): \{name}
927 | printErr (NoApp rep) = "Package \{rep} is not an application"
929 | printErr (NoAppIpkg p) = "Package \{p} is not an application"
931 | printErr EmptyPkgDB = "Empty package data base"
933 | printErr (InvalidIpkgFile path) =
934 | "Failed to parse .ipkg file: \{path}"
936 | printErr (MissingCorePackage nm v c) =
937 | "Core package \{quote nm} missing for Idris2 version \{v} (commit: \{c})"
939 | printErr (UnknownArg arg) = "Unknown command line arg: \{arg}"
941 | printErr (InvalidArgs args) = "Invalid command line args: \{unwords args}"
943 | printErr (ErroneousArg err) = err
945 | printErr (UnknownCommand cmd usage) =
947 | Unknown command: "\{cmd}"
952 | printErr (InvalidCmdArgs cmd args usage) =
954 | Invalid argument(s) for command `\{cmd}`.
959 | printErr (BuildMany []) = "No local `.ipkg` files found."
960 | printErr (BuildMany fs@(_::_)) = """
961 | Ambiguous `.ipkg` files:
962 | \{joinBy "\n" $ ("- " ++) . interpolate <$> fs}
963 | Please choose only one.
966 | printErr (NoFilePath s) = "Not a file path : \{s}"
968 | printErr (DirExists path) = """
969 | Failed to clone Git repository into \{path}.
970 | Directory already exists.
973 | printErr (TOMLFile file err) =
974 | "Error in file \{file}: \{printTOMLErr err}."
976 | printErr (TOMLParse err) = err
978 | printErr (BuildFailures 1) = "1 package failed to build."
980 | printErr (BuildFailures n) = "\{show n} packages failed to build."
982 | printErr ManualInstallPackApp = """
983 | You are not supposed to manually install or remove the pack
984 | application. In order to update pack to the latest version on
985 | GitHub, run `pack update`.
987 | Note: If you didn't run `pack install-app pack` or a similar
988 | operation, "pack" might be listed as an auto-install application
989 | in one of your pack.toml files. Please remove it from there.
992 | printErr (CyclicDeps ps) =
993 | let cycle = fastConcat $
intersperse " -> " (map value ps)
995 | Dependency cylce detected:
999 | printErr SafetyAbort = "Aborted."
1004 | readDBName : String -> Either PackErr DBName
1005 | readDBName s = case Body.parse s of
1006 | Just b => Right $
MkDBName b
1007 | Nothing => Left (InvalidDBName s)
1011 | readBody : String -> Either PackErr Body
1012 | readBody s = case Body.parse s of
1014 | Nothing => Left (InvalidBody s)
1018 | readPkgType : String -> Either PackErr PkgType
1019 | readPkgType "lib" = Right PLib
1020 | readPkgType "bin" = Right PApp
1021 | readPkgType "app" = Right PApp
1022 | readPkgType s = Left (InvalidPkgType s)
1029 | readAbsFile : (curdir : Path Abs) -> String -> Either PackErr (File Abs)
1030 | readAbsFile cd s = case split $
toAbsPath cd (fromString s) of
1031 | Just (p,b) => Right $
MkF p b
1032 | Nothing => Left (NoFilePath s)
1035 | readLogLevel : String -> Either PackErr LogLevel
1036 | readLogLevel str = maybeToEither (InvalidLogLevel str) $
lookup str logLevels