1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
module CPM.Repository
( Repository
, emptyRepository
, allPackages
, readRepository
, findAllVersions, findVersion, findLatestVersion
, searchPackages
, listPackages
, updateRepository
, updateRepositoryCache
) where
import Char ( toLower, toUpper )
import Directory
import Either
import FilePath
import IO
import IOExts ( readCompleteFile )
import List
import ReadShowTerm ( showQTerm, readQTerm )
import System ( exitWith )
import CPM.Config ( Config, repositoryDir, packageIndexRepository
, packageInstallDir )
import CPM.ConfigPackage ( packageVersion )
import CPM.ErrorLogger
import CPM.Package
import CPM.FileUtil ( checkAndGetDirectoryContents, inDirectory
, whenFileExists, removeDirectoryComplete )
import CPM.Resolution ( isCompatibleToCompiler )
data Repository = Repository [Package]
emptyRepository :: Repository
emptyRepository = Repository []
findAllVersions :: Repository -> String -> Bool -> [Package]
findAllVersions (Repository ps) p pre =
sortedByVersion $ preFiltered $ sameName ps
where
sortedByVersion = sortBy (\a b -> (version a) `vgt` (version b))
preFiltered = filter filterPre
sameName = filter ((== p) . name)
filterPre p' = pre || (not . isPreRelease . version) p'
searchPackages :: Repository -> Bool -> Bool -> String -> [[Package]]
searchPackages (Repository ps) searchmod searchexec searchterm =
map sortedByVersion (groupBy (\a b -> name a == name b) allResults)
where
allResults = let s = lowerS searchterm
in if searchmod
then filter (\p -> searchterm `elem` exportedModules p) ps
else if searchexec
then filter (\p -> s `isInfixOf`
(lowerS $ execOfPackage p)) ps
else filter (matches s) ps
matches q p = q `isInfixOf` (lowerS $ name p) ||
q `isInfixOf` (lowerS $ synopsis p) ||
q `isInfixOf` (lowerS $ unwords (exportedModules p))
sortedByVersion = sortBy (\a b -> version a `vgt` version b)
lowerS = map toLower
listPackages :: Repository -> [[Package]]
listPackages (Repository ps) =
map sortedByVersion (groupBy (\a b -> name a == name b) ps)
where
sortedByVersion = sortBy (\a b -> (version a) `vgt` (version b))
findLatestVersion :: Config -> Repository -> String -> Bool -> Maybe Package
findLatestVersion cfg repo pn pre =
case filter (isCompatibleToCompiler cfg) (findAllVersions repo pn pre) of
[] -> Nothing
(p:_) -> Just p
findVersion :: Repository -> String -> Version -> Maybe Package
findVersion repo p v =
maybeHead $ filter ((== v) . version) $ findAllVersions repo p True
where maybeHead [] = Nothing
maybeHead (x:_) = Just x
allPackages :: Repository -> [Package]
allPackages (Repository ps) = ps
readRepository :: Config -> IO Repository
readRepository cfg = do
mbrepo <- readRepositoryCache cfg
case mbrepo of
Nothing -> do
infoMessage "Writing repository cache..."
(repo, repoErrors) <- readRepositoryFrom (repositoryDir cfg)
if null repoErrors
then writeRepositoryCache cfg repo >> return repo
else do putStrLn "Problems while reading the package index:"
mapIO putStrLn repoErrors
exitWith 1
Just repo -> return repo
readRepositoryFrom :: String -> IO (Repository, [String])
readRepositoryFrom path = do
debugMessage $ "Reading repository index from '" ++ path ++ "'..."
pkgDirs <- checkAndGetDirectoryContents path
pkgPaths <- return $ map (path </>) $ filter dirOrSpec pkgDirs
verDirs <- mapIO checkAndGetDirectoryContents pkgPaths
verPaths <- return $ concatMap (\ (d, p) -> map (d </>) (filter dirOrSpec p))
$ zip pkgPaths verDirs
specPaths <- return $ map (</> "package.json") verPaths
specs <- mapIO readPackageFile specPaths
when (null (lefts specs)) $ debugMessage "Finished reading repository"
return $ (Repository $ rights specs, lefts specs)
where
readPackageSpecIO = liftIO readPackageSpec
readPackageFile f = do
spec <- readPackageSpecIO $ readCompleteFile f
return $ case spec of
Left err -> Left $ "Problem reading '" ++ f ++ "': " ++ err
Right s -> Right s
dirOrSpec d = (not $ isPrefixOf "." d) && takeExtension d /= ".md" &&
(not $ isPrefixOf repositoryCacheFileName (map toUpper d))
updateRepository :: Config -> IO (ErrorLogger ())
updateRepository cfg = do
cleanRepositoryCache cfg
debugMessage $ "Deleting global package cache: '" ++
packageInstallDir cfg ++ "'"
removeDirectoryComplete (packageInstallDir cfg)
gitExists <- doesDirectoryExist $ (repositoryDir cfg) </> ".git"
if gitExists
then do
c <- inDirectory (repositoryDir cfg) $
execQuietCmd $ (\q -> "git pull " ++ q ++ " origin master")
if c == 0
then do updateRepositoryCache cfg
log Info "Successfully updated repository"
else failIO $ "Failed to update git repository, return code " ++ show c
else do
c <- inDirectory (repositoryDir cfg) $ execQuietCmd cloneCommand
if c == 0
then do updateRepositoryCache cfg
log Info "Successfully updated repository"
else failIO $ "Failed to update git repository, return code " ++ show c
where
cloneCommand q = unwords ["git clone", q, packageIndexRepository cfg, "."]
repositoryCacheFileName :: String
repositoryCacheFileName = "REPOSITORY_CACHE"
repositoryCache :: Config -> String
repositoryCache cfg = repositoryDir cfg </> repositoryCacheFileName
updateRepositoryCache :: Config -> IO ()
updateRepositoryCache cfg = do
cleanRepositoryCache cfg
repo <- readRepository cfg
writeRepositoryCache cfg repo
writeRepositoryCache :: Config -> Repository -> IO ()
writeRepositoryCache cfg repo =
writeFile (repositoryCache cfg)
(packageVersion ++ "\n" ++ showQTerm repo)
readRepositoryCache :: Config -> IO (Maybe Repository)
readRepositoryCache cfg = do
let cf = repositoryCache cfg
excache <- doesFileExist cf
if excache
then debugMessage ("Reading repository cache from '" ++ cf ++ "'...") >>
catch (readTermInCacheFile cf)
(\_ -> do infoMessage "Cleaning broken repository cache..."
cleanRepositoryCache cfg
return Nothing )
else return Nothing
where
readTermInCacheFile cf = do
h <- openFile cf ReadMode
pv <- hGetLine h
if pv == packageVersion
then hGetContents h >>= \t -> return $!! Just (readQTerm t)
else do infoMessage "Cleaning repository cache (wrong version)..."
cleanRepositoryCache cfg
return Nothing
cleanRepositoryCache :: Config -> IO ()
cleanRepositoryCache cfg = do
let cachefile = repositoryCache cfg
whenFileExists cachefile $ removeFile cachefile
|