This repository has been archived by the owner on Feb 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Build.hs
159 lines (131 loc) · 5.15 KB
/
Build.hs
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
{-
-- Given a .cabal file with the platform specification, generate a tree in
-- hackage format, with a 00-index.tgz file, so that cabal-install will be
-- able to treat the local tree as a db.
-}
import Data.List hiding (intercalate)
import System.Directory
import Control.Monad
import System.IO
import System.FilePath
import qualified Data.ByteString.Lazy as BS
import qualified Data.ByteString.Lazy.Char8 as BS.Char8
import qualified Control.Exception as C
import System.IO.Error (isDoesNotExistError)
import Data.Monoid
import Data.Maybe
import System.Environment
-- cabal-install
-- import qualified Tar as Tar
-- import Types
import Distribution.Package ( PackageIdentifier(..), Package(..) , Dependency(Dependency) )
import Distribution.PackageDescription.Parse
import Distribution.Version
import Distribution.Simple.Utils
import Distribution.Simple.PackageIndex
import Distribution.PackageDescription.Configuration
import Distribution.PackageDescription hiding (packageDescription)
import Distribution.Verbosity
import Distribution.Text ( display, simpleParse )
import qualified Distribution.Simple.PackageIndex as PackageIndex
import System.Cmd
import System.FilePath
-- import Network.Curl.Download.Lazy
main = do
[cabalpath,outdir] <- getArgs
pwd <- getCurrentDirectory
setCurrentDirectory outdir
let cabal = pwd </> cabalpath
cabalsrc <- readPackageDescription normal cabal
let final = flattenPackageDescription cabalsrc
dependencies = buildDepends final ++
concatMap buildTools (allBuildInfo final)
-- relies on the fact that meta cabal package is a simple list of
-- dependencies and versions. nothing suss.
--
let urls =
[ (d, display name ++ "-" ++ display vers)
| d@(Dependency name (ThisVersion vers)) <- dependencies ]
forM_ urls $ \(dep@(Dependency name vers), package) -> do
system $ "cabal unpack " ++ package
--
-- Too sleepy. Future: solve this via the cabal library
--
let cmd = "cabal install --dry-run --reinstall " ++ (
intercalate " " [ package
| (_, package) <- urls
] ) ++ " > platform.packages.raw"
--writeFile "platform.packages.cmd" cmd
system cmd
src <- readFile "platform.packages.raw"
let programs = drop 2 $ lines src
writeFile "platform.packages" (unlines programs)
removeFile "platform.packages.raw"
setCurrentDirectory pwd
------------------------------------------------------------------------
{-
e <- openLazyURI url
case e of
Left err -> error $ "Unable to download " ++ show url ++ ": " ++ show err
Right s -> do createDirectory name
createDirectory (name </> display vers)
BS.writeFile (name </> display vers </>
(name ++ "-" ++ display vers ++ ".tar.gz")) s
-}
{-
home <- getHomeDirectory
let index = home </> ".cabal/packages/hackage.haskell.org"
x <- readRepoIndex index
print x
-}
-- lookupDependency :: Package pkg => PackageIndex pkg -> Dependency
-- -> [pkg]
------------------------------------------------------------------------
-- Stolen from cabal-install
{-
-- | Read a repository index from disk, from the local file specified by
-- the 'Repo'.
--
readRepoIndex :: FilePath -> IO (PackageIndex AvailablePackage)
readRepoIndex path =
handleNotFound $ do
let indexFile = repoLocalDir path </> "00-index.tar"
pkgs <- either fail return . parseRepoIndex =<< BS.readFile indexFile
C.evaluate (PackageIndex.fromList pkgs)
where
-- | Parse a repository index file from a 'ByteString'.
--
-- All the 'AvailablePackage's are marked as having come from the given 'Repo'.
--
parseRepoIndex :: BS.ByteString -> Either String [AvailablePackage]
parseRepoIndex = either Left (Right . catMaybes . map extractPkg)
. check [] . Tar.read
check _ (Tar.Fail err) = Left err
check ok Tar.Done = Right ok
check ok (Tar.Next e es) = check (e:ok) es
extractPkg :: Tar.Entry -> Maybe AvailablePackage
extractPkg entry
| takeExtension fileName == ".cabal"
= case splitDirectories (normalise fileName) of
[pkgname,vers,_] -> case simpleParse vers of
Just ver -> Just AvailablePackage {
packageInfoId = PackageIdentifier ({-PackageName-} pkgname) ver,
packageDescription = descr,
packageSource = RepoTarballPackage path
}
_ -> Nothing
where
parsed = parsePackageDescription . fromUTF8 . BS.Char8.unpack
. Tar.fileContent $ entry
descr = case parsed of
ParseOk _ d -> d
_ -> error $ "Couldn't read cabal file "
++ show fileName
_ -> Nothing
| otherwise = Nothing
where
fileName = Tar.fileName entry
handleNotFound action = catch action $ \e -> if isDoesNotExistError e
then return mempty
else ioError e
-}