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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
module CPM.Repository
( Repository
, emptyRepository
, allPackages
, readRepository
, findAllVersions, findVersion, findLatestVersion
, searchPackages
, listPackages
, useUpdateHelp, updateRepository, cleanRepositoryCache
, readPackageFromRepository, getAllPackageVersions, getPackageVersion
) where
import Char ( toLower )
import Directory
import Either
import FilePath
import IO
import IOExts ( readCompleteFile )
import List
import ReadShowTerm ( showQTerm, readQTerm, showTerm, readUnqualifiedTerm )
import System ( exitWith, system )
import Time
import CPM.Config ( Config, repositoryDir, packageIndexRepository
, packageInstallDir )
import CPM.ConfigPackage ( packageVersion )
import CPM.ErrorLogger
import CPM.Package
import CPM.FileUtil ( checkAndGetVisibleDirectoryContents, 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 -> Bool -> IO Repository
readRepository cfg large = do
warnOldRepo cfg
mbrepo <- readRepositoryCache cfg large
case mbrepo of
Nothing -> do
infoMessage $ "Writing " ++ (if large then "large " else "") ++
"repository cache..."
(repo, repoErrors) <- readRepositoryFrom (repositoryDir cfg)
if null repoErrors
then writeRepositoryCache cfg large repo >> return repo
else do errorMessage "Problems while reading the package index:"
mapM_ errorMessage repoErrors
exitWith 1
Just repo -> return repo
setLastUpdate :: Config -> IO ()
setLastUpdate cfg =
system (unwords ["touch", repositoryDir cfg </> "README.md"]) >> done
warnOldRepo :: Config -> IO ()
warnOldRepo cfg = do
let updatefile = repositoryDir cfg </> "README.md"
updexists <- doesFileExist updatefile
if updexists
then do
utime <- getModificationTime updatefile
ctime <- getClockTime
let warntime = addDays 10 utime
when (compareClockTime ctime warntime == GT) $ do
let timediff = clockTimeToInt ctime - clockTimeToInt utime
days = timediff `div` (60*60*24)
infoMessage $ "Warning: your repository index is older than " ++
show days ++ " days.\n" ++ useUpdateHelp
else infoMessage $ "Warning: your repository index is not up-to-date.\n" ++
useUpdateHelp
useUpdateHelp :: String
useUpdateHelp = "Use 'cypm update' to download the newest package index."
readRepositoryFrom :: String -> IO (Repository, [String])
readRepositoryFrom path = do
debugMessage $ "Reading repository index from '" ++ path ++ "'..."
repos <- checkAndGetVisibleDirectoryContents path
pkgPaths <- mapIO getDir (map (path </>) repos) >>= return . concat
verDirs <- mapIO checkAndGetVisibleDirectoryContents pkgPaths
verPaths <- return $ concatMap (\ (d, p) -> map (d </>) 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
getDir d = doesDirectoryExist d >>= \b -> return $ if b then [d] else []
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 $ cleanPullCmd
if c == 0
then do setLastUpdate cfg
cleanRepositoryCache 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 setLastUpdate cfg
cleanRepositoryCache cfg
log Info "Successfully updated repository"
else failIO $ "Failed to update git repository, return code " ++ show c
where
cleanPullCmd q = "git clean -d -f && git pull " ++ q ++ " origin master"
cloneCommand q = unwords ["git clone", q, packageIndexRepository cfg, "."]
repositoryCacheFileName :: String
repositoryCacheFileName = "REPOSITORY_CACHE"
repositoryCache :: Config -> Bool -> String
repositoryCache cfg large =
repositoryDir cfg </> repositoryCacheFileName ++
(if large then "_LARGE" else "")
repoCacheVersion :: String
repoCacheVersion = packageVersion ++ "-1"
writeRepositoryCache :: Config -> Bool -> Repository -> IO ()
writeRepositoryCache cfg large repo =
writeFile (repositoryCache cfg large) $ unlines $
repoCacheVersion :
map (if large then showTerm . package2largetuple
else showTerm . package2smalltuple)
(allPackages repo)
where
package2smalltuple p =
( name p, version p, dependencies p, compilerCompatibility p )
package2largetuple p =
(package2smalltuple p,
(synopsis p, category p, sourceDirs p, exportedModules p,
executableSpec p))
readRepositoryCache :: Config -> Bool -> IO (Maybe Repository)
readRepositoryCache cfg large = do
let cf = repositoryCache cfg large
excache <- doesFileExist cf
if excache
then debugMessage ("Reading repository cache from '" ++ cf ++ "'...") >>
catch ((if large
then readTermInCacheFile cfg (largetuple2package . uread) cf
else readTermInCacheFile cfg (smalltuple2package . uread) cf)
>>= \repo ->
debugMessage "Finished reading repository cache" >> return repo)
(\_ -> do infoMessage "Cleaning broken repository cache..."
cleanRepositoryCache cfg
return Nothing )
else return Nothing
where
uread s = readUnqualifiedTerm ["CPM.Package","Prelude"] s
smalltuple2package (nm,vs,dep,cmp) =
emptyPackage { name = nm
, version = vs
, dependencies = dep
, compilerCompatibility = cmp
}
largetuple2package (basics,(sy,cat,srcs,exps,exec)) =
(smalltuple2package basics)
{ synopsis = sy
, category = cat
, sourceDirs = srcs
, exportedModules = exps
, executableSpec = exec
}
readTermInCacheFile :: Config -> (String -> Package) -> String
-> IO (Maybe Repository)
readTermInCacheFile cfg trans cf = do
h <- openFile cf ReadMode
pv <- hGetLine h
if pv == repoCacheVersion
then hGetContents h >>= \t ->
return $!! Just (Repository (map trans (lines t)))
else do infoMessage "Cleaning repository cache (wrong version)..."
cleanRepositoryCache cfg
return Nothing
cleanRepositoryCache :: Config -> IO ()
cleanRepositoryCache cfg = do
let smallcachefile = repositoryCache cfg False
largecachefile = repositoryCache cfg True
whenFileExists smallcachefile $ removeFile smallcachefile
whenFileExists largecachefile $ removeFile largecachefile
readPackageFromRepository :: Config -> Package -> IO (ErrorLogger Package)
readPackageFromRepository cfg pkg =
let pkgdir = repositoryDir cfg </> name pkg </> showVersion (version pkg)
in loadPackageSpec pkgdir
getAllPackageVersions :: Config -> String -> Bool -> IO [Package]
getAllPackageVersions cfg pkgname pre = do
repo <- readRepository cfg False
return (findAllVersions repo pkgname pre)
getPackageVersion :: Config -> String -> Version -> IO (Maybe Package)
getPackageVersion cfg pkgname ver = do
repo <- readRepository cfg False
return (findVersion repo pkgname ver)
|