Package.hs 17.4 KB
Newer Older
ijones's avatar
ijones committed
1
{-# OPTIONS -cpp -DDEBUG #-}
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
-----------------------------------------------------------------------------
-- |
-- Module      :  Distribution.Package
-- Copyright   :  Isaac Jones 2003-2004
-- 
-- Maintainer  :  Isaac Jones <ijones@syntaxpolice.org>
-- Stability   :  alpha
-- Portability :  
--
-- Explanation: <FIX>
-- WHERE DOES THIS MODULE FIT IN AT A HIGH-LEVEL <FIX>

{- Copyright (c) 2003-2004, Isaac Jones
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

    * Neither the name of Isaac Jones nor the names of other
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -}

45
module Distribution.Package (
simonmar's avatar
simonmar committed
46
47
	PackageIdentifier(..), 
	showPackageId,
48
	PackageDescription(..),
ijones's avatar
ijones committed
49
	emptyPackageDescription,
50
        parsePackageDesc,
ijones's avatar
ijones committed
51
#ifdef DEBUG        
52
        hunitTests,
ijones's avatar
ijones committed
53
        test
ijones's avatar
ijones committed
54
#endif
55
  ) where
56

md9ms's avatar
md9ms committed
57
58
59
60
import Control.Monad.State
import Control.Monad(when, foldM)
import Control.Monad.Error
import Data.Char(isSpace, toLower)
ijones's avatar
ijones committed
61

62
63
import Distribution.Version(Version(..), VersionRange(..),
                            showVersion, parseVersion, parseVersionRange)
ijones's avatar
ijones committed
64
import Distribution.Misc(License(..), Dependency(..), Extension)
ijones's avatar
ijones committed
65
import Distribution.Setup(CompilerFlavor)
66

67
68
import System.IO(openFile, IOMode(..), hGetContents)

ijones's avatar
ijones committed
69
70
import Text.ParserCombinators.Parsec

ijones's avatar
ijones committed
71
#ifdef DEBUG
ijones's avatar
ijones committed
72
import HUnit (Test(..), (~:), (~=?), assertEqual, assertBool, Assertion, runTestTT)
ijones's avatar
ijones committed
73
74
#endif

75
76
data PackageIdentifier
    = PackageIdentifier {pkgName::String, pkgVersion::Version}
simonmar's avatar
simonmar committed
77
      deriving (Read, Show, Eq)
78

simonmar's avatar
simonmar committed
79
showPackageId :: PackageIdentifier -> String
ijones's avatar
ijones committed
80
showPackageId (PackageIdentifier n (Version [] _)) = n -- if no version, don't show version.
simonmar's avatar
simonmar committed
81
82
83
showPackageId pkgid = 
  pkgName pkgid ++ '-': showVersion (pkgVersion pkgid)

84
85
86
87
88
89
90
91
92
93
-- | This data type is the internal representation of the file @pkg.descr@.
-- It contains two kinds of information about the package: information
-- which is needed for all packages, such as the package name and version, and 
-- information which is needed for the simple build system only, such as 
-- the compiler options and library name.
-- 
data PackageDescription
    =  PackageDescription {
	-- the following are required by all packages:
	package        :: PackageIdentifier,
94
        license        :: License,
95
96
97
98
99
100
        copyright      :: String,
        maintainer     :: String,
        stability      :: String,

	-- the following are required by the simple build infrastructure only:
        buildDepends   :: [ Dependency ],
ijones's avatar
ijones committed
101
102
        allModules     :: [ String ],
        mainModules    :: [ String ],
simonmar's avatar
simonmar committed
103
        cSources       :: [ FilePath ],
ijones's avatar
ijones committed
104
	hsSourceDir    :: FilePath,
105
106
107
108
109
110
111
	exposedModules :: [ String ],
        extensions     :: [ Extension ],
        extraLibs      :: [ String ],
        includeDirs    :: [ FilePath ],
        includes       :: [ FilePath ],
        options        :: [ (CompilerFlavor, [String]) ]
    }
ijones's avatar
ijones committed
112
113
114
115
116
117
118
119
120
121
122
    deriving (Show, Read, Eq)

-- |Set the name for this package. Convenience function.
setPkgName :: String -> PackageDescription -> PackageDescription
setPkgName n desc@PackageDescription{package=pkgIdent}
    = desc{package=pkgIdent{pkgName=n}}

-- |Set the version for this package. Convenience function.
setPkgVersion :: Version -> PackageDescription -> PackageDescription
setPkgVersion v desc@PackageDescription{package=pkgIdent}
    = desc{package=pkgIdent{pkgVersion=v}}
123
124
125

emptyPackageDescription :: PackageDescription
emptyPackageDescription
ijones's avatar
ijones committed
126
    =  PackageDescription {package      = PackageIdentifier "" (Version [] []),
127
                      license      = AllRightsReserved,
128
129
130
131
132
                      copyright    = "",
                      maintainer   = "",
                      stability    = "",
                      buildDepends = [],
                      allModules   = [],
ijones's avatar
ijones committed
133
                      mainModules   = [],
simonmar's avatar
simonmar committed
134
		      cSources     = [],
135
		      hsSourceDir  = ".", -- FIX: FileUtils.currentDir
136
137
138
139
140
141
		      exposedModules = [],
                      extensions   = [],
                      extraLibs    = [],
                      includeDirs  = [],
                      includes     = [],
                      options      = []
142
                     }
143

ijones's avatar
ijones committed
144
145
146
147
148

-- ------------------------------------------------------------
-- * Parsing
-- ------------------------------------------------------------

ijones's avatar
ijones committed
149
150
151
notImp :: String -> a
notImp s = error $ s ++ " not yet implemented"

152
153
154
155
156
-- |Parse the given package file.  FIX: don't use read / show.
parsePackageDesc :: FilePath -> IO PackageDescription
parsePackageDesc p
    = openFile p ReadMode >>= hGetContents >>= return . read

md9ms's avatar
md9ms committed
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
data PError = Parsec ParseError | FromString String
        deriving Show

instance Error PError where
        strMsg = FromString

parseDescription :: String -> Either PError PackageDescription
parseDescription inp = foldM parseDescHelp emptyPackageDescription (splitLines inp)
  where -- Required fields
        parseDescHelp pkg (f@"name",      val) = return (setPkgName val pkg)
        parseDescHelp pkg (f@"version",   val) =
          do v <- runP f parseVersion val
             return (setPkgVersion v pkg)
        parseDescHelp pkg (f@"copyright", val) = return pkg{copyright=val}
        parseDescHelp pkg (f@"license",   val) =
          do l <- runP f parseLicense val
             return pkg{license=l}
        -- Misc.
        parseDescHelp pkg (f@"maintainer", val) = return pkg{maintainer=val}
        parseDescHelp pkg (f@"stability",  val) = return pkg{stability=val}
        parseDescHelp pkg (f@"extra-libs", val) =
          do xs <- runP f (parseCommaList word) val
             return pkg{extraLibs=xs}
        parseDescHelp pkg (f@"build-depends", val) =
          do xs <- runP f (parseCommaList parseDependency) val
             return pkg{buildDepends=xs}
        -- Paths and stuff
        parseDescHelp pkg (f@"c-sources", val) =
185
186
          do paths <- runP f (parseCommaList parseFilePath) val
             return pkg{cSources=paths}
md9ms's avatar
md9ms committed
187
        parseDescHelp pkg (f@"include-dirs", val) =
188
189
          do paths <- runP f (parseCommaList parseFilePath) val
             return pkg{includeDirs=paths}
md9ms's avatar
md9ms committed
190
        parseDescHelp pkg (f@"includes", val) =
191
192
          do paths <- runP f (parseCommaList parseFilePath) val
             return pkg{includes=paths}
md9ms's avatar
md9ms committed
193
194
        parseDescHelp pkg (f@"hs-source-dir", val) =
          do path <- runP f parseFilePath val
195
             return pkg{hsSourceDir=path}
md9ms's avatar
md9ms committed
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
        -- Module related
        parseDescHelp pkg (f@"main-modules", val) =
          do xs <- runP f (parseCommaList moduleName) val
             return pkg{mainModules=xs}
        parseDescHelp pkg (f@"exposed-modules", val) =
          do xs <- runP f (parseCommaList moduleName) val
             return pkg{exposedModules=xs}
        parseDescHelp pkg (f@"modules", val) =
          do xs <- runP f (parseCommaList moduleName) val
             return pkg{allModules=xs}
        parseDescHelp pkg (f@"extensions", val) =
          do -- ...
             return pkg
        parseDescHelp pkg (f@"options", val) =
          do -- ...
             return pkg
        parseDescHelp pkg (field, val) = error $ "Unknown field :: " ++ field
        -- ...
        runP f p s = case parse p f s of
                       Left pe -> Left (Parsec pe)
                       Right a -> Right a

splitLines :: String -> [(String,String)]
splitLines = merge . filter validLine . lines
  where validLine s = case dropWhile isSpace s of
                        ""        -> False      -- Empty line
                        '-':'-':_ -> False      -- Comment
                        _         -> True
        merge (x:(' ':s):ys) = case dropWhile isSpace s of
                                 "." -> merge ((x++"\n"):ys)
                                 s'  -> merge ((x++"\n"++s'):ys)
        merge (x:ys) = brk x : merge ys
        merge []     = []
        brk xs = case break (==':') xs of
                   (fld, ':':val) -> (map toLower fld, dropWhile isSpace val)
                   (fld, "")      -> error "FIXME"
ijones's avatar
ijones committed
232
233
234
235
236
237
238
239

-- |A parser for any of the given parsers.  This actually seems to
-- behave differently from "choice".

anyOf :: [GenParser tok st a] -> GenParser tok st a
anyOf [a] = a
anyOf (h:t) = foldl ((<|>) . try) (try h) t

240
-- |parse a module name
md9ms's avatar
md9ms committed
241
moduleName = many (alphaNum <|> oneOf "_'.") <?> "moduleName"
ijones's avatar
ijones committed
242

243
-- |FIX: must learn to escape whitespace
244
245
246
247
248
249
parseFilePath :: GenParser Char st FilePath
parseFilePath = liftM concat (many1 (
                        do try word
                           <|> toStr digit
                           <|> toStr (oneOf "!@#$%^&*()?></\\|]}[{.")
                       ))
md9ms's avatar
md9ms committed
250
        <?> "parseFilePath"
251

ijones's avatar
ijones committed
252
253
parseLicense :: GenParser Char st License
parseLicense = anyOf [string s>>return l | (s,l) <- licenses]
md9ms's avatar
md9ms committed
254
        <?> "parseLicense"
ijones's avatar
ijones committed
255

256
parseDependency :: GenParser Char st Dependency
md9ms's avatar
md9ms committed
257
parseDependency = do name <- many1 (letter <|> digit <|> oneOf "-_")
258
                     skipMany parseWhite
259
                     ver <- parseVersionRange <|> return AnyVersion
260
261
                     skipMany parseWhite
                     return $ Dependency name ver
md9ms's avatar
md9ms committed
262
        <?> "parseDependency"
263

ijones's avatar
ijones committed
264
265
266
267
268
269
270
271
272
-- |Mapping between the licenses and their names
licenses :: [(String, License)]
licenses= [("GPL", GPL),
           ("LGPL", LGPL),
           ("BSD3", BSD3),
           ("BSD4", BSD4),
           ("PublicDomain", PublicDomain),
           ("AllRightsReserved", AllRightsReserved)]

273
toStr c = c >>= \x -> return [x]
ijones's avatar
ijones committed
274
275

word :: GenParser Char st String
ijones's avatar
ijones committed
276
277
278
279
280
281
282
word = many1 letter <?> "word"

parseCommaList :: GenParser Char st a -- ^The parser for the stuff between commas
               -> GenParser Char st [a]
parseCommaList p
    = do words <- sepBy1 p separator
         return words
md9ms's avatar
md9ms committed
283
    where separator = spaces >> char ',' >> spaces
ijones's avatar
ijones committed
284
285

parseWhite = try parseSpaceNotNewline
286
            <|> (try (char '\n' >> parseWhite))
ijones's avatar
ijones committed
287
288
289
290
291
292

parseSpaceNotNewline = (satisfy isSpaceNotNewline <?> "space, not newline")
    where isSpaceNotNewline :: Char -> Bool
          isSpaceNotNewline '\n' = False
          isSpaceNotNewline n    = isSpace n

ijones's avatar
ijones committed
293
294
295
296
-- ------------------------------------------------------------
-- * Testing
-- ------------------------------------------------------------
#ifdef DEBUG
297
testPkgDesc = "-- Required\nName: Cabal\nVersion: 0.1.1.1.1-rain\nLicense: LGPL\nCopyright: Free Text String\n-- Optional - may be in source?\nStability: Free Text String\nBuild-Depends: haskell-src, HUnit>=1.0.0-rain\nModules: Distribution.Package, Distribution.Version, Distribution.Simple.GHCPackageConfig\nMain-Modules: Distribution.Main\nC-Sources: not/even/rain.c, such/small/hands\nHS-Source-Dir: src\nExposed-Modules: Distribution.Void, Foo.Bar\nExtensions: {some known extensions}\nExtra-Libs: libfoo, bar, bang\nInclude-Dirs: your/slightest, look/will\nIncludes: /easily/unclose, /me\nOptions: ghc: -fTH, hugs: +TH"
ijones's avatar
ijones committed
298
299
300

testPkgDescAnswer = 
 PackageDescription {package = PackageIdentifier {pkgName = "Cabal",
ijones's avatar
ijones committed
301
                                                 pkgVersion = Version {versionBranch = [0,1,1,1,1],
md9ms's avatar
md9ms committed
302
                                                 versionTags = ["rain"]}},
ijones's avatar
ijones committed
303
                    license = LGPL,
ijones's avatar
ijones committed
304
                    copyright = "Free Text String",
ijones's avatar
ijones committed
305
                    maintainer = "",
ijones's avatar
ijones committed
306
                    stability = "Free Text String",
ijones's avatar
ijones committed
307
                    buildDepends = [Dependency "haskell-src" AnyVersion,
ijones's avatar
ijones committed
308
                                    Dependency "HUnit"
md9ms's avatar
md9ms committed
309
310
                                     (UnionVersionRanges (ThisVersion (Version [1,0,0] ["rain"]))
                                      (LaterVersion (Version [1,0,0] ["rain"])))],
ijones's avatar
ijones committed
311
312

                    allModules = ["Distribution.Package","Distribution.Version",
ijones's avatar
ijones committed
313
314
315
                                  "Distribution.Simple.GHCPackageConfig"],

                    mainModules = ["Distribution.Main"],
316
                    cSources = ["not/even/rain.c", "such/small/hands"],
ijones's avatar
ijones committed
317
318
                    hsSourceDir = "src",
                    exposedModules = ["Distribution.Void", "Foo.Bar"],
ijones's avatar
ijones committed
319
                    extensions = [],
ijones's avatar
ijones committed
320
                    extraLibs = ["libfoo", "bar", "bang"],
321
322
                    includeDirs = ["your/slightest", "look/will"],
                    includes = ["/easily/unclose", "/me"],
ijones's avatar
ijones committed
323
324
325
                    options = []
}

ijones's avatar
ijones committed
326
hunitTests :: [Test]
327
328
329
330
331
hunitTests = [TestLabel "newline before word (parsewhite)" $ TestCase $
              do assertRight "newline before word 1"
                  "foo" (parse (skipMany parseWhite>>char '\n'>>word) "" "   \n  \nfoo")
                 assertRight "newline before word 2"
                  "foo" (parse (skipMany parseWhite>>char '\n'>>word) "" "   \n \t    \n  \nfoo"),
ijones's avatar
ijones committed
332
333

              TestLabel "skip spaces not newlines" $ TestCase $
334
335
336
337
338
339
340
341
              do assertRight "spaces with newlines"
                  "foo" (parse (skipMany parseWhite>>word) "" "   \n  foo")
                 assertRight "spaces with newlines"
                  "foo" (parse (skipMany parseWhite>>word) "" "   \n \t\n   foo")
                 assertRight "no preceding spaces"
                  "foo" (parse (skipMany parseWhite>>word) "" "foo")
                 assertBool "newline before data without in-between spaces"
                  (isError (parse (skipMany parseWhite>>word) "" "   \n  \nfoo")),
ijones's avatar
ijones committed
342

md9ms's avatar
md9ms committed
343
344
345
346
347
348
--              TestLabel "basic fields" $ TestCase $
--              do let p1 = parse (do w1 <- parseField "Foo" False parseVersion
--                                    skipMany parseWhite
--                                    w2 <- parseField "Bar" True word
--                                    return (w1, w2)
--                                ) ""
md9ms's avatar
md9ms committed
349
--                     knownVal1 = (Version {versionBranch = [3,2], versionTags = ["one"]},"boo")
md9ms's avatar
md9ms committed
350
351
352
353
354
355
356
357
358
359
360
361
--                 assertRight "basic spaces 1"
--                   knownVal1 (p1 "Foo: 3.2-one\nBar: boo")
--                 assertRight "basic spaces 2"
--                   knownVal1 (p1 "Foo: 3.2-one \t   \nBar: boo")
--                 assertRight "basic spaces 3"
--                   knownVal1 (p1 "Foo : 3.2-one \t   \nBar:    boo  ")
--                 assertRight "basic spaces 3"
--                   knownVal1 (p1 "Foo:3.2-one \t   \nBar:    boo  ")
--                 assertRight "basic spaces with newline"
--                   knownVal1 (p1 "Foo:\n 3.2-one \t   \nBar:    boo  ")
--                 assertRight "basic spaces with newline"
--                   knownVal1 (p1 "Foo:\n 3.2-one \t \n  \nBar:    boo  "),
ijones's avatar
ijones committed
362

ijones's avatar
ijones committed
363
364
365
366
367
              TestCase (assertRight "BSD4" BSD4 (parse parseLicense "" "BSD4")),

              TestLabel "license parsers" $ 
                        TestCase (sequence_ [assertRight ("license " ++ lName) lVal
                                                    (parse parseLicense "" lName)
ijones's avatar
ijones committed
368
369
370
371
372
                                             | (lName, lVal) <- licenses]),

              TestLabel "Required fields" $ TestCase $
                 do assertRight "some fields"
                       emptyPackageDescription{package=(PackageIdentifier "foo"
md9ms's avatar
md9ms committed
373
                                                        (Version [0,0] ["asdf"]))}
ijones's avatar
ijones committed
374
375
376
377
                       (parseDescription "Name: foo\nVersion: 0.0-asdf")

                    assertRight "more fields foo"
                       emptyPackageDescription{package=(PackageIdentifier "foo"
md9ms's avatar
md9ms committed
378
                                                        (Version [0,0]["asdf"])),
ijones's avatar
ijones committed
379
380
381
382
383
                                               license=GPL}
                       (parseDescription "Name: foo\nVersion:0.0-asdf\nLicense: GPL")

                    assertRight "required fields for foo"
                       emptyPackageDescription{package=(PackageIdentifier "foo"
md9ms's avatar
md9ms committed
384
                                                        (Version [0,0]["asdf"])),
ijones's avatar
ijones committed
385
386
387
388
389
390
391
                                        license=GPL, copyright="2004 isaac jones"}
                       (parseDescription "Name: foo\nVersion:0.0-asdf\nCopyright: 2004 isaac jones\nLicense: GPL"),
                                          

             TestLabel "Package description" $ TestCase $ 
                assertRight "entire package description" testPkgDescAnswer
                                                         (parseDescription testPkgDesc)
md9ms's avatar
md9ms committed
392

ijones's avatar
ijones committed
393
394
             ]

ijones's avatar
ijones committed
395

ijones's avatar
ijones committed
396
397
398
399
400
401
402
403
404
405
assertRight :: (Eq val) => String -> val -> (Either a val) -> Assertion
assertRight mes expected actual
    =  assertBool mes
           (case actual of
             (Right v) -> v == expected
             _         -> False)

isError (Left _) = True
isError _        = False

ijones's avatar
ijones committed
406
test = runTestTT (TestList hunitTests)
ijones's avatar
ijones committed
407
#endif