Package.hs 12.1 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
        parseField,
ijones's avatar
ijones committed
52
53
54
#ifdef DEBUG        
        hunitTests
#endif
55
  ) where
56

ijones's avatar
ijones committed
57
58
59
import Control.Monad(when)
import Data.Char(isSpace)

ijones's avatar
ijones committed
60
61
import Distribution.Version(Version(..), VersionRange(..), showVersion, parseVersion)
import Distribution.Misc(License(..), Dependency(..), Extension)
ijones's avatar
ijones committed
62
import Distribution.Setup(CompilerFlavor)
63

64
65
import System.IO(openFile, IOMode(..), hGetContents)

ijones's avatar
ijones committed
66
67
import Text.ParserCombinators.Parsec

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

72
73
data PackageIdentifier
    = PackageIdentifier {pkgName::String, pkgVersion::Version}
simonmar's avatar
simonmar committed
74
      deriving (Read, Show, Eq)
75

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

81
82
83
84
85
86
87
88
89
90
-- | 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,
91
        license        :: License,
92
93
94
95
96
97
        copyright      :: String,
        maintainer     :: String,
        stability      :: String,

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

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

ijones's avatar
ijones committed
141
142
143
144
145

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

146
147
148
149
150
-- |Parse the given package file.  FIX: don't use read / show.
parsePackageDesc :: FilePath -> IO PackageDescription
parsePackageDesc p
    = openFile p ReadMode >>= hGetContents >>= return . read

ijones's avatar
ijones committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

parseDesc :: GenParser Char st PackageDescription
parseDesc = error "not yet implemented"

-- |Parse the required fields. FIX: implmenet.  We'll basically run
-- through these a bunch of times updating the state as we go along,
-- as is noted.
-- parseReqFields 
--     = try (do v <- parseField "Version" True parseVersion
--               updateState(setPkgVersion v)
--           )
--       <|> (parseField "Name" False 
--               updateState (setPkgName n)
--       <|> (parseField 

-- |Must know when to stop ... :)
parseFreeText = error "not implemented"

ijones's avatar
ijones committed
169
170
171
172
173
174
175
176
177
178
179
180
181
word :: Parser String
word = many1 letter <?> "word"

--parseWordCommaList :: GenParser Char st [t]
parseCommaList :: GenParser Char st a -- ^The parser for the stuff between commas
               -> GenParser Char st [a]
parseCommaList p
    = do words <- sepBy1 p separator
         newline
         return words
    where separator = skipMany1 (space <|> char ',')

parseWhite = try parseSpaceNotNewline
182
            <|> (try (char '\n' >> parseWhite))
ijones's avatar
ijones committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200

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

parseField :: String              -- ^The field name to parse
           -> Bool                -- ^Require newline?
           -> GenParser Char st t -- ^The parser to use for this field
           -> GenParser Char st t
parseField s newline p
    = do when newline (char '\n'>>return ())
         string s
         skipMany parseWhite
         char ':'
         skipMany parseWhite
         p

ijones's avatar
ijones committed
201
202
203
204
-- ------------------------------------------------------------
-- * Testing
-- ------------------------------------------------------------
#ifdef DEBUG
ijones's avatar
ijones committed
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
testPkgDesc = "-- Required\nName: Cabal\nVersion: 0.1.1.1.1-foo-bar-bang\nLicense: AllRightsReserved\nCopyright: Free Text String\n-- Optional - may be in source?\nStability: Free Text String\nBuild-Depends: haskell-src, HUnit>=1.0.0-foo\nModules: Distribution.Package, Distribution.Version, Distribution.Simple.GHCPackageConfig\nMain-Modules: Distribution.Main\nC-Sources: foo/bar/bang.c, bong/boing.h\nHS-Source-Dir: src\nExposed-Modules: Distribution.Void, Foo.Bar\nExtensions: {some known extensions}\nExtra-Libs: libfoo, bar, bang\nInclude-Dirs: foo/bar, fang/fong\nIncludes: /foo/bar, jedi/night\nOptions: ghc: -fTH, hugs: +TH"

testPkgDescAnswer = 
 PackageDescription {package = PackageIdentifier {pkgName = "Cabal",
                                                 pkgVersion = Version {versionBranch = [0,1],
                                                 versionTags = []}},
                    license = AllRightsReserved,
                    copyright = "",
                    maintainer = "",
                    stability = "",
                    buildDepends = [Dependency "haskell-src" AnyVersion,
                                    Dependency "HUnit-1.0" AnyVersion],

                    allModules = ["Distribution.Package","Distribution.Version",
                                  "Distribution.Misc","Distribution.Setup",
                                  "Distribution.InstalledPackageInfo",
                                  "Distribution.Make","Distribution.Simple",
                                  "Distribution.Simple.Build",
                                  "Distribution.Simple.Install","Distribution.Simple.SrcDist",
                                  "Distribution.Simple.Configure","Distribution.Simple.Utils",
                                  "Distribution.Simple.Register",
                                  "Distribution.Simple.GHCPackageConfig",
                                  "Distribution.GetOpt"],

                    mainModules = [],
                    cSources = [],
                    hsSourceDir = ".",
                    exposedModules = [],
                    extensions = [],
                    extraLibs = [],
                    includeDirs = [],
                    includes = [],
                    options = []
}

ijones's avatar
ijones committed
240
hunitTests :: [Test]
241
242
243
244
245
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
246
247

              TestLabel "skip spaces not newlines" $ TestCase $
248
249
250
251
252
253
254
255
              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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274

              TestLabel "basic fields" $ TestCase $
              do let p1 = parse (do w1 <- parseField "Foo" False parseVersion
                                    skipMany parseWhite
                                    w2 <- parseField "Bar" True word
                                    return (w1, w2)
                                ) ""
                     knownVal1 = (Version {versionBranch = [3,2], versionTags = ["date=one"]},"boo")
                 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"
ijones's avatar
ijones committed
275
276
277
278
279
                   knownVal1 (p1 "Foo:\n 3.2-one \t \n  \nBar:    boo  "),

              TestLabel "Package description" $ TestCase $ 
                 assertRight "entire package description" testPkgDescAnswer
                                                          (parse parseDesc "" testPkgDesc)
ijones's avatar
ijones committed
280
281
282
283
284
285
286
287
288
289
290
291
292
             ]

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

main = runTestTT (TestList hunitTests)
ijones's avatar
ijones committed
293
#endif