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
|
module CPM.PackageCache.Global
( GlobalCache
, findAllVersions
, findNewestVersion
, findVersion
, packageInstalled
, installedPackageDir
, readGlobalCache, readInstalledPackagesFromDir
, allPackages
, copyPackage
, installMissingDependencies
, acquireAndInstallPackage
, acquireAndInstallPackageFromSource
, tryFindPackage
, missingPackages
, installFromZip
, checkoutPackage
, uninstallPackage
, emptyCache
) where
import Directory
import Either
import IOExts ( readCompleteFile )
import List
import Maybe (isJust)
import FilePath
import CPM.Config ( Config, packageInstallDir, packageTarFilesURL )
import CPM.ErrorLogger
import CPM.FileUtil ( cleanTempDir, copyDirectory, inTempDir
, recreateDirectory, inDirectory
, removeDirectoryComplete, tempDir, whenFileExists
, checkAndGetVisibleDirectoryContents, quote )
import CPM.Package
import CPM.Package.Helpers ( installPackageSourceTo )
import CPM.Repository
data GlobalCache = GlobalCache [Package]
emptyCache :: GlobalCache
emptyCache = GlobalCache []
allPackages :: GlobalCache -> [Package]
allPackages (GlobalCache ps) = ps
findAllVersions :: GlobalCache -> String -> Bool -> [Package]
findAllVersions (GlobalCache ps) p pre = sortBy pkgGt
$ filter filterPre
$ filter ((== p) . name) ps
where
filterPre p' = pre || (not . isPreRelease . version) p'
pkgGt :: Package -> Package -> Bool
pkgGt a b = version a `vgt` version b
findNewestVersion :: GlobalCache -> String -> Maybe Package
findNewestVersion db p = if length pkgs > 0
then Just $ head pkgs
else Nothing
where
pkgs = sortBy pkgGt $ findAllVersions db p False
findVersion :: GlobalCache -> String -> Version -> Maybe Package
findVersion (GlobalCache ps) p v =
if null hits
then Nothing
else Just $ head hits
where
hits = filter ((== v) . version) $ filter ((== p) . name) ps
isPackageInstalled :: GlobalCache -> Package -> Bool
isPackageInstalled db p = isJust $ findVersion db (name p) (version p)
installedPackageDir :: Config -> Package -> String
installedPackageDir cfg pkg = packageInstallDir cfg </> packageId pkg
packageInstalled :: Config -> Package -> IO Bool
packageInstalled cfg pkg =
doesDirectoryExist (installedPackageDir cfg pkg)
copyPackage :: Config -> Package -> String -> IO (ErrorLogger ())
copyPackage cfg pkg dir = do
exists <- doesDirectoryExist srcDir
if not exists
then failIO $ "Package '" ++ packageId pkg ++ "' not installed"
else copyDirectory srcDir dir >> succeedIO ()
where
srcDir = installedPackageDir cfg pkg
acquireAndInstallPackage :: Config -> Package -> ErrorLoggerIO ()
acquireAndInstallPackage cfg pkg = do
pkgDirExists <- execIO $ doesDirectoryExist (installedPackageDir cfg pkg)
if pkgDirExists
then logMsg Info $ "Package '" ++ packageId pkg ++
"' already installed, skipping"
else do
let stdurl = packageTarFilesURL cfg ++ packageId pkg ++ ".tar.gz"
logMsg Info ("Installing package from " ++ stdurl)
(msgs,err) <- execIO $ installPackageSourceTo pkg (Http stdurl)
(packageInstallDir cfg)
case err of
Right _ -> toELM $ return (msgs,err)
Left _ -> toELM $ acquireAndInstallPackageFromSource cfg pkg
acquireAndInstallPackageFromSource :: Config -> Package -> IO (ErrorLogger ())
acquireAndInstallPackageFromSource cfg reppkg =
readPackageFromRepository cfg reppkg |>= \pkg ->
case source pkg of
Nothing -> failIO $ "No source specified for " ++ packageId pkg
Just s -> log Info ("Installing package '" ++ packageId pkg ++ "'...") |>
installFromSource cfg pkg s
installFromSource :: Config -> Package -> PackageSource -> IO (ErrorLogger ())
installFromSource cfg pkg pkgsource = do
pkgDirExists <- doesDirectoryExist pkgDir
if pkgDirExists
then
log Info $ "Package '" ++ packageId pkg ++ "' already installed, skipping"
else
log Info ("Installing package from " ++ showSourceOfPackage pkg) |>
installPackageSourceTo pkg pkgsource (packageInstallDir cfg)
where
pkgDir = installedPackageDir cfg pkg
installFromZip :: Config -> String -> IO (ErrorLogger ())
installFromZip cfg zip = do
t <- tempDir
recreateDirectory (t </> "installtmp")
absZip <- getAbsolutePath zip
c <- inTempDir $ showExecCmd $ "unzip -qq -d installtmp " ++ quote absZip
if c == 0
then
loadPackageSpec (t </> "installtmp") |>= \pkgSpec ->
log Debug ("ZIP contains " ++ packageId pkgSpec) |>
(cleanTempDir >> succeedIO ()) |>
installFromSource cfg pkgSpec (FileSource zip)
else cleanTempDir >> failIO "failed to extract ZIP file"
installMissingDependencies :: Config -> GlobalCache -> [Package]
-> ErrorLoggerIO ()
installMissingDependencies cfg gc deps = whenM (length missing > 0) $ do
logMsg Info logmsg
mapM_ (acquireAndInstallPackage cfg) missing
where
missing = filter (not . isPackageInstalled gc) deps
logmsg = "Installing missing dependencies " ++
intercalate "," (map packageId missing)
missingPackages :: GlobalCache -> [Package] -> [Package]
missingPackages gc = filter (not . isPackageInstalled gc)
checkoutPackage :: Config -> Package -> ErrorLoggerIO ()
checkoutPackage cfg pkg = do
sexists <- execIO $ doesDirectoryExist pkgDir
texists <- execIO $ doesDirectoryExist codir
if texists
then logMsg Error $
"Local package directory '" ++ codir ++ "' already exists."
else if sexists
then do execIO $ copyDirectory pkgDir codir
logMsg Info logmsg
else logMsg Error $ "Package '" ++ pkgId ++ "' is not installed."
where
pkgId = packageId pkg
pkgDir = installedPackageDir cfg pkg
codir = name pkg
logmsg = "Package '" ++ pkgId ++ "' checked out into directory '" ++
codir ++ "'."
uninstallPackage :: Config -> String -> Version -> ErrorLoggerIO ()
uninstallPackage cfg pkgname ver = do
let pkgId = pkgname ++ "-" ++ showVersion ver
pkgDir = packageInstallDir cfg </> pkgId
exists <- execIO $ doesDirectoryExist pkgDir
if exists
then do execIO $ showExecCmd ("rm -Rf " ++ quote pkgDir)
logMsg Info $ "Package '" ++ pkgId ++ "' uninstalled."
else logMsg Info $ "Package '" ++ pkgId ++ "' is not installed."
tryFindPackage :: GlobalCache -> String -> Version -> IO (ErrorLogger Package)
tryFindPackage gc name ver = case findVersion gc name ver of
Just pkg -> succeedIO pkg
Nothing -> failIO $ "Package " ++ name ++ "-" ++ showVersion ver ++
" could not be found."
readGlobalCache :: Config -> Repository -> ErrorLoggerIO GlobalCache
readGlobalCache config repo = do
maybeGC <- execIO $
readInstalledPackagesFromDir repo $ packageInstallDir config
case maybeGC of
Left err -> failELM $ "Error reading global package cache: " ++ err
Right gc -> return gc
readInstalledPackagesFromDir :: Repository -> String
-> IO (Either String GlobalCache)
readInstalledPackagesFromDir repo path = do
debugMessage $ "Reading global package cache from '" ++ path ++ "'..."
pkgPaths <- checkAndGetVisibleDirectoryContents path
specs <- mapIO loadPackageSpecFromDir pkgPaths
if null (lefts specs)
then do debugMessage "Finished reading global package cache"
return (Right $ GlobalCache (rights specs))
else return (Left $ intercalate "; " (lefts specs))
where
readPackageSpecIO = liftIO readPackageSpec
loadPackageSpecFromDir pkgdir = case packageVersionFromFile pkgdir of
Nothing -> readPackageSpecFromFile pkgdir
Just (pn,pv) -> case CPM.Repository.findVersion repo pn pv of
Nothing -> readPackageSpecFromFile pkgdir
Just p -> return (Right p)
readPackageSpecFromFile pkgdir = do
let f = path </> pkgdir </> "package.json"
debugMessage $ "Reading package spec from '" ++ f ++ "'..."
spec <- readPackageSpecIO $ readCompleteFile f
return $ case spec of
Left err -> Left $ err ++ " for file '" ++ f ++ "'"
Right v -> Right v
packageVersionFromFile :: String -> Maybe (String, Version)
packageVersionFromFile fn =
let ps = split (=='-') fn
l = length ps
in if l < 2
then Nothing
else case readVersion (last ps) of
Nothing -> Nothing
Just v -> Just (intercalate "-" (take (l-1) ps), v)
|