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
|
module CPM.Config
( Config ( Config, packageInstallDir, binInstallDir, repositoryDir
, appPackageDir, packageIndexURLs, packageTarFilesURLs
, homePackageDir, curryExec
, compilerVersion, compilerBaseVersion )
, readConfigurationWith, defaultConfig
, showConfiguration, showCompilerVersion ) where
import Control.Monad ( unless )
import Data.Char ( toUpper )
import System.Directory ( doesDirectoryExist, createDirectoryIfMissing
, getHomeDirectory, doesFileExist )
import qualified Curry.Compiler.Distribution as Dist
import System.FilePath ( (</>), isAbsolute )
import Data.Maybe ( mapMaybe )
import Data.List ( split, splitOn, intercalate, intersperse )
import Control.Monad ( when )
import System.IOExts ( evalCmd )
import Data.PropertyFile ( readPropertyFile )
import Language.Curry.Resources ( curryPackagesURL )
import System.Path ( getFileInPath )
import CPM.ErrorLogger
import CPM.FileUtil ( ifFileExists )
import CPM.Helpers ( stripSpaces )
packageTarFilesDefaultURLs :: [String]
packageTarFilesDefaultURLs = [curryPackagesURL ++ "PACKAGES"]
packageIndexDefaultURLs :: [String]
packageIndexDefaultURLs =
map (++"/INDEX.tar.gz") packageTarFilesDefaultURLs
data Config = Config {
packageInstallDir :: String
, binInstallDir :: String
, repositoryDir :: String
, appPackageDir :: String
, packageIndexURLs :: [String]
, packageTarFilesURLs :: [String]
, homePackageDir :: String
, curryExec :: String
, compilerVersion :: (String,Int,Int,Int)
, compilerBaseVersion :: String
}
defaultConfig :: Config
defaultConfig = Config
{ packageInstallDir = "$HOME/.cpm/packages"
, binInstallDir = "$HOME/.cpm/bin"
, repositoryDir = "$HOME/.cpm/index"
, appPackageDir = ""
, packageIndexURLs = packageIndexDefaultURLs
, packageTarFilesURLs = packageTarFilesDefaultURLs
, homePackageDir = ""
, curryExec = Dist.installDir </> "bin" </> Dist.curryCompiler
, compilerVersion = ( Dist.curryCompiler
, Dist.curryCompilerMajorVersion
, Dist.curryCompilerMinorVersion
, Dist.curryCompilerRevisionVersion )
, compilerBaseVersion = Dist.baseVersion
}
showConfiguration :: Config -> String
showConfiguration cfg = unlines
[ "Compiler version : " ++ showCompilerVersion cfg
, "Compiler base version : " ++ compilerBaseVersion cfg
, "CURRY_BIN : " ++ curryExec cfg
, "REPOSITORY_PATH : " ++ repositoryDir cfg
, "PACKAGE_INSTALL_PATH : " ++ packageInstallDir cfg
, "BIN_INSTALL_PATH : " ++ binInstallDir cfg
, "APP_PACKAGE_PATH : " ++ appPackageDir cfg
, "HOME_PACKAGE_PATH : " ++ homePackageDir cfg
, "PACKAGE_INDEX_URL : " ++ intercalate "|" (packageIndexURLs cfg)
, "PACKAGE_TARFILES_URL : " ++ intercalate "|" (packageTarFilesURLs cfg)
]
showCompilerVersion :: Config -> String
showCompilerVersion cfg =
let (cname,cmaj,cmin,crev) = compilerVersion cfg
in cname ++ ' ' : showVersionNumber '.' (cmaj,cmin,crev)
showVersionNumber :: Char -> (Int,Int,Int) -> String
showVersionNumber c (maj,min,rev) =
show maj ++ [c] ++ show min ++ [c] ++ show rev
setCompilerExecutable :: Config -> ErrorLogger Config
setCompilerExecutable cfg = do
let exec = curryExec cfg
if isAbsolute exec
then do isexec <- liftIOEL $ doesFileExist exec
if isexec
then return cfg
else do
logInfo $ "Warning: executable '" ++ exec ++ "' not found!"
logInfo $ "Looking for 'curry' in path..."
findExecutable "curry"
else findExecutable exec
where
findExecutable exec = liftIOEL $
getFileInPath exec >>=
maybe (error $ "Executable '" ++ exec ++ "' not found in path!")
(\absexec -> return cfg { curryExec = absexec })
setAppPackageDir :: Config -> IO Config
setAppPackageDir cfg
| null (appPackageDir cfg)
= do homedir <- getHomeDirectory
let cpmdir = homedir </> ".cpm"
(cname,cmaj,cmin,crev) = compilerVersion cfg
cmpname = cname ++ "_" ++ showVersionNumber '.' (cmaj,cmin,crev)
return cfg { appPackageDir = cpmdir </> "apps_" ++ cmpname }
| otherwise = return cfg
setHomePackageDir :: Config -> IO Config
setHomePackageDir cfg
| null (homePackageDir cfg)
= do homedir <- getHomeDirectory
let cpmdir = homedir </> ".cpm"
(cname,cmaj,cmin,crev) = compilerVersion cfg
cvname = cname ++ "-" ++ showVersionNumber '_' (cmaj,cmin,crev)
homepkgdir = cpmdir </> cvname ++ "-homepackage"
return cfg { homePackageDir = homepkgdir }
| otherwise = return cfg
setCompilerVersion :: Config -> ErrorLogger Config
setCompilerVersion cfg0 = do
cfg <- setCompilerExecutable cfg0
if curryExec cfg == Dist.installDir </> "bin" </> Dist.curryCompiler
then return cfg { compilerVersion = currVersion
, compilerBaseVersion = Dist.baseVersion }
else do (sname,svers,sbver) <- getCompilerVersion (curryExec cfg)
let cname = stripSpaces sname
cvers = stripSpaces svers
bvers = stripSpaces sbver
(majs:mins:revs:_) = split (=='.') cvers
logDebug $ unwords ["Compiler version:",cname,cvers]
logDebug $ "Base lib version: " ++ bvers
return cfg { compilerVersion = (cname, read majs,
read mins, read revs)
, compilerBaseVersion = bvers }
where
getCompilerVersion currybin = do
logDebug $ "Getting version information from " ++ currybin
(r,s,e) <- liftIOEL $ evalCmd currybin
["--compiler-name","--numeric-version","--base-version"] ""
if r>0
then error $ "Cannot determine compiler version:\n" ++ e
else case lines s of
[sname,svers,sbver] -> return (sname,svers,sbver)
_ -> do logDebug $ "Query version information again..."
(c1,sname,e1) <- getCompilerInfo "--compiler-name"
(c2,svers,e2) <- getCompilerInfo "--numeric-version"
(c3,sbver,e3) <- getCompilerInfo "--base-version"
when (c1 > 0 || c2 > 0 || c3 > 0) $
error $ "Cannot determine compiler version:\n" ++
unlines (filter (not . null) [e1,e2,e3])
return (sname,svers,sbver)
where
getCompilerInfo infopt = liftIOEL $ evalCmd currybin [infopt] ""
currVersion = (Dist.curryCompiler, Dist.curryCompilerMajorVersion,
Dist.curryCompilerMinorVersion
, Dist.curryCompilerRevisionVersion)
readConfigurationWith :: [(String,String)] -> ErrorLogger (Either String Config)
readConfigurationWith defsettings = do
home <- liftIOEL $ getHomeDirectory
let configFile = home </> ".cpmrc"
exfile <- liftIOEL $ doesFileExist configFile
rcsettings <- liftIOEL $
if exfile
then do rcdefs <- readPropertyFile configFile >>= return . stripProps
return rcdefs
else return []
unless (null rcsettings) $ logDebug $
"Properties defined in " ++ configFile ++ ":\n" ++
unlines (map (\ (x,y) -> " " ++ x ++ "=" ++ y) rcsettings)
let mergedSettings = mergeConfigSettings defaultConfig
(rcsettings ++ stripProps defsettings)
case mergedSettings of
Left e -> return $ Left e
Right s0 -> do s1 <- liftIOEL $ replaceHome s0
s2 <- setCompilerVersion s1
s3 <- liftIOEL $ setAppPackageDir s2
s4 <- liftIOEL $ setHomePackageDir s3
liftIOEL $ createDirectories s4
return $ Right s4
replaceHome :: Config -> IO Config
replaceHome cfg = do
homeDir <- getHomeDirectory
return $ cfg {
packageInstallDir = replaceHome' homeDir (packageInstallDir cfg)
, binInstallDir = replaceHome' homeDir (binInstallDir cfg)
, repositoryDir = replaceHome' homeDir (repositoryDir cfg)
, appPackageDir = replaceHome' homeDir (appPackageDir cfg)
}
where
replaceHome' h s = concat $ intersperse h $ splitOn "$HOME" s
createDirectories :: Config -> IO ()
createDirectories cfg =
mapM_ (\df -> createDirectoryIfMissing True (df cfg))
[packageInstallDir, binInstallDir, repositoryDir, appPackageDir]
mergeConfigSettings :: Config -> [(String, String)] -> Either String Config
mergeConfigSettings cfg props = applyEither setters cfg
where
setters = map maybeApply props
maybeApply (k, v) = case lookup k keySetters of
Nothing -> \_ -> Left $ "Unknown .cpmrc property: " ++ k ++ "\n\n" ++
"The following .cpmrc properties are allowed:\n" ++
unlines (map fst keySetters)
Just s -> \c -> Right $ s v c
stripProps :: [(String, String)] -> [(String, String)]
stripProps =
map (\(a,b) -> (map toUpper $ filter (/='_') $ stripSpaces a, stripSpaces b))
keySetters :: [(String, String -> Config -> Config)]
keySetters =
[ ("APPPACKAGEPATH" , \v c -> c { appPackageDir = v })
, ("BININSTALLPATH" , \v c -> c { binInstallDir = v })
, ("CURRYBIN" , \v c -> c { curryExec = v })
, ("HOMEPACKAGEPATH" , \v c -> c { homePackageDir = v })
, ("PACKAGEINDEXURL" , \v c -> c { packageIndexURLs = breakURLs v })
, ("PACKAGETARFILESURL" , \v c -> c { packageTarFilesURLs = breakURLs v })
, ("PACKAGEINSTALLPATH" , \v c -> c { packageInstallDir = v })
, ("REPOSITORYPATH" , \v c -> c { repositoryDir = v })
]
where
breakURLs = splitOn "|"
applyEither :: [a -> Either c a] -> a -> Either c a
applyEither [] z = Right z
applyEither (f:fs) z = case f z of
Left err -> Left err
Right z' -> applyEither fs z'
|