Repository.curry 8.5 KB
Newer Older
Michael Hanus 's avatar
Michael Hanus committed
1
------------------------------------------------------------------------------
Michael Hanus 's avatar
Michael Hanus committed
2
--- This module implements functionality surrounding the package *repository*.
Michael Hanus 's avatar
Michael Hanus committed
3
4
--- The repository is the index of all packages known to the package manager.
--- It contains metadata about the packages, such as their names, versions 
Michael Hanus 's avatar
Michael Hanus committed
5
6
7
--- dependencies and where they can be acquired. The repository does not contain
--- the actual packages. For a list of packages that are currently installed 
--- locally, you can consult the *database*.
Michael Hanus 's avatar
Michael Hanus committed
8
------------------------------------------------------------------------------
Michael Hanus 's avatar
Michael Hanus committed
9
10
11
12
13
14
15
16
17
18
19

module CPM.Repository 
  ( Repository
  , emptyRepository
  , allPackages
  , readRepository
  , findVersion
  , findLatestVersion
  , searchPackages
  , listPackages
  , updateRepository
Michael Hanus 's avatar
Michael Hanus committed
20
  , updateRepositoryCache
Michael Hanus 's avatar
Michael Hanus committed
21
22
  ) where

Michael Hanus 's avatar
Michael Hanus committed
23
import Char           ( toLower )
Michael Hanus 's avatar
Michael Hanus committed
24
25
26
import Directory
import Either
import FilePath
Michael Hanus 's avatar
Michael Hanus committed
27
28
29
import List
import ReadShowTerm   ( readQTermFile, writeQTermFile )
import System         ( exitWith )
Michael Hanus 's avatar
Michael Hanus committed
30

Michael Hanus 's avatar
Michael Hanus committed
31
import CPM.Config     ( Config, repositoryDir, packageIndexRepository )
Michael Hanus 's avatar
Michael Hanus committed
32
33
import CPM.ErrorLogger
import CPM.Package
Michael Hanus 's avatar
Michael Hanus committed
34
35
import CPM.FileUtil   ( checkAndGetDirectoryContents, inDirectory
                      , whenFileExists )
36
import CPM.Resolution ( isCompatibleToCompiler )
Michael Hanus 's avatar
Michael Hanus committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50

data Repository = Repository [Package]

--- Creates an empty repository.
emptyRepository :: Repository
emptyRepository = Repository []

--- Finds all versions of a package known to the repository. Returns the 
--- packages sorted from newest to oldest.
---
--- @param r the repository
--- @param p the name of the package to search for
--- @param pre should pre-release versions be included?
findAllVersions :: Repository -> String -> Bool -> [Package]
51
52
53
54
55
56
57
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'
Michael Hanus 's avatar
Michael Hanus committed
58

Michael Hanus 's avatar
Michael Hanus committed
59
60
61
--- Search the names and synopses of all compiler-compatbile packages
--- in the repository for a particular term.
--- Lower/upercase is ignored for the search.
Michael Hanus 's avatar
Michael Hanus committed
62
63
64
--- Returns the newest matching version of each package.
---
--- @param repo - the repository
Michael Hanus 's avatar
Michael Hanus committed
65
--- @searchmod - search for some module?
Michael Hanus 's avatar
Michael Hanus committed
66
--- @param q - the term to search for
Michael Hanus 's avatar
Michael Hanus committed
67
68
searchPackages :: Repository -> Bool -> String -> [Package]
searchPackages (Repository ps) searchmod searchstring =
Michael Hanus 's avatar
Michael Hanus committed
69
70
71
  map (head . sortedByVersion) groupedResults
 where
  groupedResults = groupBy namesEqual allResults
Michael Hanus 's avatar
Michael Hanus committed
72
73
74
  allResults = if searchmod
                 then filter (\p -> searchstring `elem` (exportedModules p)) ps
                 else filter (matches (lowerS searchstring)) ps
Michael Hanus 's avatar
Michael Hanus committed
75
  matches q p = q `isInfixOf` (lowerS $ name p) ||
Michael Hanus 's avatar
Michael Hanus committed
76
77
                q `isInfixOf` (lowerS $ synopsis p) ||
                q `isInfixOf` (lowerS $ unwords (exportedModules p))
Michael Hanus 's avatar
Michael Hanus committed
78
79
80
81
  namesEqual a b = (name a) == (name b)
  sortedByVersion = sortBy (\a b -> (version a) `vgt` (version b))
  lowerS = map toLower

Michael Hanus 's avatar
Michael Hanus committed
82
83
--- Get all packages in the repository and group them by versions
--- (newest first).
Michael Hanus 's avatar
Michael Hanus committed
84
---
Michael Hanus 's avatar
Michael Hanus committed
85
--- @param cfg  - the current CPM configuration
Michael Hanus 's avatar
Michael Hanus committed
86
--- @param repo - the repository
Michael Hanus 's avatar
Michael Hanus committed
87
88
89
listPackages :: Repository -> [[Package]]
listPackages (Repository ps) =
  map sortedByVersion (groupBy (\a b -> name a == name b) ps)
Michael Hanus 's avatar
Michael Hanus committed
90
91
92
 where
  sortedByVersion = sortBy (\a b -> (version a) `vgt` (version b))

Michael Hanus 's avatar
Michael Hanus committed
93
--- Finds the latest compiler-compatbile version of a package.
Michael Hanus 's avatar
Michael Hanus committed
94
---
95
--- @param cfg  - the current CPM configuration
Michael Hanus 's avatar
Michael Hanus committed
96
97
98
--- @param repo - the central package index
--- @param p - the package to search for
--- @param pre - include pre-release versions
99
100
101
102
103
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
Michael Hanus 's avatar
Michael Hanus committed
104
105
106

--- Finds a specific version of a package.
findVersion :: Repository -> String -> Version -> Maybe Package
107
108
109
110
findVersion repo p v =
  maybeHead $ filter ((== v) . version) $ findAllVersions repo p True
 where maybeHead []    = Nothing
       maybeHead (x:_) = Just x
Michael Hanus 's avatar
Michael Hanus committed
111
112
113
114
115

--- Get all packages in the central package index.
allPackages :: Repository -> [Package]
allPackages (Repository ps) = ps

Michael Hanus 's avatar
Michael Hanus committed
116
117
118
--- Reads all package specifications from the default repository.
--- Use the cache if is present or update the cache after reading.
--- If some errors occur, show them and terminate with exit status.
Michael Hanus 's avatar
Michael Hanus committed
119
120
---
--- @param cfg the configuration to use
Michael Hanus 's avatar
Michael Hanus committed
121
122
123
124
125
126
127
128
129
130
131
132
133
readRepository :: Config -> IO Repository
readRepository cfg = do
  mbrepo <- readRepositoryCache cfg
  case mbrepo of
    Nothing -> do
      infoMessage "Writing the 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
Michael Hanus 's avatar
Michael Hanus committed
134
135
136
137
138
139
140
141
142
143

--- Reads all package specifications from a repository.
---
--- @param path the location of the repository
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
Michael Hanus 's avatar
Michael Hanus committed
144
145
  verPaths <- return $ concatMap (\ (d, p) -> map (d </>) (filter dirOrSpec p))
                     $ zip pkgPaths verDirs
Michael Hanus 's avatar
Michael Hanus committed
146
147
148
149
150
151
152
153
154
155
156
157
158
  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 $ readFile f
    return $ case spec of
      Left err -> Left $ "Problem reading '" ++ f ++ "': " ++ err
      Right  s -> Right s

Michael Hanus 's avatar
Michael Hanus committed
159
160
  dirOrSpec d = (not $ isPrefixOf "." d) && takeExtension d /= ".md" &&
                d /= repositoryCacheFileName
Michael Hanus 's avatar
Michael Hanus committed
161
162
163
164

--- Updates the package index from the central Git repository.
updateRepository :: Config -> IO (ErrorLogger ())
updateRepository cfg = do
Michael Hanus 's avatar
Michael Hanus committed
165
  cleanRepositoryCache cfg
Michael Hanus 's avatar
Michael Hanus committed
166
167
168
  gitExists <- doesDirectoryExist $ (repositoryDir cfg) </> ".git"
  if gitExists 
    then do
Michael Hanus 's avatar
Michael Hanus committed
169
170
      c <- inDirectory (repositoryDir cfg) $
             execQuietCmd $ (\q -> "git pull " ++ q ++ " origin master")
Michael Hanus 's avatar
Michael Hanus committed
171
      if c == 0
Michael Hanus 's avatar
Michael Hanus committed
172
173
        then do updateRepositoryCache cfg
                log Info "Successfully updated repository"
Michael Hanus 's avatar
Michael Hanus committed
174
175
        else failIO $ "Failed to update git repository, return code " ++ show c
    else do
Michael Hanus 's avatar
Michael Hanus committed
176
      c <- inDirectory (repositoryDir cfg) $ execQuietCmd cloneCommand
Michael Hanus 's avatar
Michael Hanus committed
177
      if c == 0
Michael Hanus 's avatar
Michael Hanus committed
178
179
        then do updateRepositoryCache cfg
                log Info "Successfully updated repository"
Michael Hanus 's avatar
Michael Hanus committed
180
181
        else failIO $ "Failed to update git repository, return code " ++ show c
 where
Michael Hanus 's avatar
Michael Hanus committed
182
183
  cloneCommand q = unwords ["git clone", q, packageIndexRepository cfg, "."]

Michael Hanus 's avatar
Michael Hanus committed
184

Michael Hanus 's avatar
Michael Hanus committed
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
------------------------------------------------------------------------------
-- Operations implementing the repository cache for faster reading.

--- The local file name containing the repository cache as a Curry term.
repositoryCacheFileName :: String
repositoryCacheFileName = "repository_cache"

--- The file containing the repository cache as a Curry term.
repositoryCache :: Config -> String
repositoryCache cfg = repositoryDir cfg </> repositoryCacheFileName

--- Updates the repository cache with the current repository index.
updateRepositoryCache :: Config -> IO ()
updateRepositoryCache cfg = do
  cleanRepositoryCache cfg
  repo <- readRepository cfg
  writeRepositoryCache cfg repo

--- Stores the given repository in the cache.
writeRepositoryCache :: Config -> Repository -> IO ()
writeRepositoryCache cfg repo =
  writeQTermFile (repositoryCache cfg) repo

--- Reads the given repository from the cache.
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 (readQTermFile cf >>= \t -> return $!! Just t)
               (\_ -> cleanRepositoryCache cfg >> return Nothing)
    else return Nothing

--- Cleans the repository cache.
cleanRepositoryCache :: Config -> IO ()
cleanRepositoryCache cfg = do
  let cachefile = repositoryCache cfg
  whenFileExists cachefile $ removeFile cachefile

------------------------------------------------------------------------------