Package.hs 8.51 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
60
import Control.Monad(when)
import Data.Char(isSpace)

import Distribution.Version(Version(..), showVersion, parseVersion)
61
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]) ]
    }
109
    deriving (Show, Read)
110
111
112
113

emptyPackageDescription :: PackageDescription
emptyPackageDescription
    =  PackageDescription {package      = undefined,
114
                      license      = AllRightsReserved,
115
116
117
118
119
                      copyright    = "",
                      maintainer   = "",
                      stability    = "",
                      buildDepends = [],
                      allModules   = [],
ijones's avatar
ijones committed
120
                      mainModules   = [],
simonmar's avatar
simonmar committed
121
		      cSources     = [],
122
		      hsSourceDir  = ".", -- FIX: FileUtils.currentDir
123
124
125
126
127
128
		      exposedModules = [],
                      extensions   = [],
                      extraLibs    = [],
                      includeDirs  = [],
                      includes     = [],
                      options      = []
129
                     }
130

ijones's avatar
ijones committed
131
132
133
134
135

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

136
137
138
139
140
-- |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
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
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
            <|> (char '\n' >> parseWhite)

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
173
174
175
176
177
-- ------------------------------------------------------------
-- * Testing
-- ------------------------------------------------------------
#ifdef DEBUG
hunitTests :: [Test]
ijones's avatar
ijones committed
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
hunitTests = [TestLabel "newline before word" $ TestCase $
                    assertRight "newline before word"
                     "foo" (parse (skipMany parseWhite>>char '\n'>>word) "" "   \n  \nfoo"),

              TestLabel "skip spaces not newlines" $ TestCase $
                 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")),

              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"
                   knownVal1 (p1 "Foo:\n 3.2-one \t \n  \nBar:    boo  ")
             ]

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
224
#endif