3Description : Code to handle import declarations.
6Maintainer : The Idris Community.
9 IFileType(..), findIBC, findImport, findInPath, findPkgIndex
10 , ibcPathNoFallback, installedPackages, pkgIndex
11 , PkgName, pkgName, unPkgName, unInitializedPkgName
17import IRTS.System (getIdrisLibDir)
19import Control.Applicative ((<$>))
20import Control.Monad.State.Strict
21import Data.Char (isAlpha, isDigit, toLower)
22import Data.List (isSuffixOf)
23import System.Directory
26data IFileType = IDR FilePath | LIDR FilePath | IBC FilePath IFileType
29instance Eq IFileType where
30 IDR x == IDR y = x == y
31 LIDR x == LIDR y = x == y
32 IBC x _ == IBC y _ = x == y
35newtype PkgName = PkgName String
37unPkgName :: PkgName -> String
38unPkgName (PkgName s) = map toLower s
40instance Show PkgName where
41 show (PkgName pkg) = pkg
42instance Eq PkgName where
43 a == b = unPkgName a == unPkgName b
45unInitializedPkgName :: PkgName
46unInitializedPkgName = PkgName ""
48pkgName :: String -> Either String PkgName
49pkgName "" = Left "empty package name"
52 Left "package name need to start by a letter"
53 | not $ all (\c -> isAlpha c || isDigit c || c `elem` "-_") l =
54 Left "package name need to contain only letter, digits, and -_"
55 | otherwise = Right $ PkgName s
57-- | Get the index file name for a package name
58pkgIndex :: PkgName -> FilePath
59pkgIndex s = "00" ++ unPkgName s ++ "-idx.ibc"
61srcPath :: FilePath -> FilePath
62srcPath fp = let (_, ext) = splitExtension fp in
67lsrcPath :: FilePath -> FilePath
68lsrcPath fp = let (_, ext) = splitExtension fp in
73-- Get name of byte compiled version of an import
74ibcPath :: FilePath -> Bool -> FilePath -> FilePath
75ibcPath ibcsd use_ibcsd fp = let (d_fp, n_fp) = splitFileName fp
76 d = if (not use_ibcsd) || ibcsd == ""
79 n = dropExtension n_fp
82ibcPathWithFallback :: FilePath -> FilePath -> IO FilePath
83ibcPathWithFallback ibcsd fp = do let ibcp = ibcPath ibcsd True fp
84 ibc <- doesFileExist' ibcp
87 else ibcPath ibcsd False fp)
89ibcPathNoFallback :: FilePath -> FilePath -> FilePath
90ibcPathNoFallback ibcsd fp = ibcPath ibcsd True fp
92findImport :: [FilePath] -> FilePath -> FilePath -> Idris IFileType
93findImport [] ibcsd fp = ierror . Msg $ "Can't find import " ++ fp
94findImport (d:ds) ibcsd fp = do let fp_full = d </> fp
95 ibcp <- runIO $ ibcPathWithFallback ibcsd fp_full
96 let idrp = srcPath fp_full
97 let lidrp = lsrcPath fp_full
98 ibc <- runIO $ doesFileExist' ibcp
99 idr <- runIO $ doesFileExist' idrp
100 lidr <- runIO $ doesFileExist' lidrp
105 then return (IBC ibcp isrc)
106 else if (idr || lidr)
108 else findImport ds ibcsd fp
110-- Only look for IBCs and not source
111findIBC :: [FilePath] -> FilePath -> FilePath -> Idris (Maybe FilePath)
112findIBC [] _ fp = return Nothing
113findIBC (d:ds) ibcsd fp = do let fp_full = d </> fp
114 ibcp <- runIO $ ibcPathWithFallback ibcsd fp_full
115 ibc <- runIO $ doesFileExist' ibcp
117 then return $ Just ibcp
118 else findIBC ds ibcsd fp
121-- find a specific filename somewhere in a path
123findInPath :: [FilePath] -> FilePath -> IO FilePath
124findInPath [] fp = fail $ "Can't find file " ++ fp
125findInPath (d:ds) fp = do let p = d </> fp
126 e <- doesFileExist' p
127 if e then return p else findInPath ds fp
129findPkgIndex :: PkgName -> Idris FilePath
130findPkgIndex p = do let idx = pkgIndex p
132 runIO $ findInPath ids idx
135installedPackages :: IO [String]
136installedPackages = do
137 idir <- getIdrisLibDir
138 filterM (goodDir idir) =<< dirContents idir
140 allFilesInDir base fp = do
141 let fullpath = base </> fp
142 isDir <- doesDirectoryExist' fullpath
144 then fmap concat (mapM (allFilesInDir fullpath) =<< dirContents fullpath)
146 dirContents = fmap (filter (not . (`elem` [".", ".."]))) . getDirectoryContents
147 goodDir idir d = any (".ibc" `isSuffixOf`) <$> allFilesInDir idir d
150-- | Case sensitive file existence check for Mac OS X.
151doesFileExist' :: FilePath -> IO Bool
152doesFileExist' = caseSensitive doesFileExist
154-- | Case sensitive directory existence check for Mac OS X.
155doesDirectoryExist' :: FilePath -> IO Bool
156doesDirectoryExist' = caseSensitive doesDirectoryExist
158caseSensitive :: (FilePath -> IO Bool) -> FilePath -> IO Bool
159caseSensitive existsCheck name =
160 do exists <- existsCheck name
162 then do contents <- getDirectoryContents (takeDirectory name)
163 return $ (takeFileName name) `elem` contents
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list