Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 104 lines (91 sloc) 4.718 kB
e0425c5 @leiffrenzel [ 1922446 ] [Editor] multipart Cabal editor
leiffrenzel authored
1 -- Copyright (c) 2008 by Leif Frenzel - see http://leiffrenzel.de
2 -- This code is made available under the terms of the Eclipse Public License,
3 -- version 1.0 (EPL). See http://www.eclipse.org/legal/epl-v10.html
4 module ManipulateCabalFile where
5
6 -- We must import Cohatoe.API and implement resource so that this code
7 -- can be dynamically loaded as plugin.
8 import Cohatoe.API
9
10 import Data.Maybe( fromJust )
11 import Data.Version( parseVersion, showVersion )
12 import Distribution.Package( PackageIdentifier(..) )
13 import Distribution.PackageDescription(
14 parsePackageDescription,
15 showPackageDescription,
16 PackageDescription(..),
e2f2294 @leiffrenzel additional API for querying src folders from Cabal file
leiffrenzel authored
17 GenericPackageDescription(..),
18 Library(..),
19 Executable(..),
20 BuildInfo(..) )
e0425c5 @leiffrenzel [ 1922446 ] [Editor] multipart Cabal editor
leiffrenzel authored
21 import Distribution.InstalledPackageInfo( ParseResult(..) )
22 import Distribution.ParseUtils( locatedErrorMsg )
23 import Text.ParserCombinators.ReadP( readP_to_S )
24
25
26 resource :: Interface
27 resource = plugin {
28 pluginMain = performManipulateCabaFile
29 }
30
31 -- convention: first arg is the buffer, second the command, third a param
32 -- convention: if we return an error, the first result elem is empty, the second
33 -- contains the error message
34 performManipulateCabaFile :: [String] -> IO [String]
35 performManipulateCabaFile args = do
36 let (buffer, cmd, param) = readArgs args
37 return $ case parsePackageDescription buffer of
38 ParseFailed err -> [ "", snd $ locatedErrorMsg err ]
39 ParseOk _ (GenericPackageDescription pd _ _ _) -> case cmd of
40 -- accessors
e2f2294 @leiffrenzel additional API for querying src folders from Cabal file
leiffrenzel authored
41 "GET_NAME" -> [pkgName $ package pd]
42 "GET_VERSION" -> [showVersion $ pkgVersion $ package pd]
43 "GET_COPYRIGHT" -> [copyright pd]
44 "GET_LICENSE" -> [show $ license pd]
45 "GET_LICENSE_FILE" -> [licenseFile pd]
46 "GET_DESCRIPTION" -> [description pd]
47 "GET_SYNOPSIS" -> [synopsis pd]
48 "GET_HOMEPAGE" -> [homepage pd]
49 "GET_CATEGORY" -> [category pd]
50 "GET_AUTHOR" -> [author pd]
51 "GET_MAINTAINER" -> [maintainer pd]
52 "GET_ALL_SOURCE_DIRS" -> collectSourceDirs pd
e0425c5 @leiffrenzel [ 1922446 ] [Editor] multipart Cabal editor
leiffrenzel authored
53 -- mutators
54 -- TODO lf shouldn't use fromJust
55 "SET_NAME" -> setName pd param
56 "SET_VERSION" -> setVersion pd param
57 "SET_COPYRIGHT" -> update (pd { copyright = fromJust param }) param
58 "SET_LICENSE" -> setLicense pd param
59 "SET_LICENSE_FILE" -> update (pd { licenseFile = fromJust param }) param
60 "SET_DESCRIPTION" -> update (pd { description = fromJust param }) param
61 "SET_SYNOPSIS" -> update (pd { synopsis = fromJust param }) param
62 "SET_HOMEPAGE" -> update (pd { homepage = fromJust param }) param
63 "SET_CATEGORY" -> update (pd { category = fromJust param }) param
64 "SET_AUTHOR" -> update (pd { author = fromJust param }) param
65 "SET_MAINTAINER" -> update (pd { maintainer = fromJust param }) param
66 _ -> ["", "Unknown call"]
67
68 setName :: PackageDescription -> Maybe String -> [String]
69 setName _ Nothing = ["", "No name given!"]
70 setName pd (Just newName) = [showPackageDescription mkNewPkgIdentifier] where
71 mkNewPkgIdentifier = (pd { package = (package pd) { pkgName = newName } } )
72
73 setVersion :: PackageDescription -> Maybe String -> [String]
74 setVersion _ Nothing = ["", "No version given!"]
75 setVersion pd (Just newVersion)
76 = [showPackageDescription $ pd { package = mkNewPkgIdentifier }] where
77 mkNewPkgIdentifier = case reverse $ readP_to_S parseVersion newVersion of
78 [] -> package pd -- revert to the old value if parse failed
79 (pres:_) -> (package pd) { pkgVersion = fst pres }
80
81 setLicense :: PackageDescription -> Maybe String -> [String]
82 setLicense _ Nothing = ["", "No license given!"]
83 setLicense pd (Just param)
84 = [showPackageDescription $ pd { license = parseLicense }] where
85 parseLicense = case reverse $ reads param of
86 [] -> license pd -- revert to the old value if parse failed
87 (pres:_) -> fst pres
88
89 update :: PackageDescription -> Maybe String -> [String]
90 update _ Nothing = ["", "No param!"]
91 update newPkgDesc _ = [showPackageDescription $ newPkgDesc]
e2f2294 @leiffrenzel additional API for querying src folders from Cabal file
leiffrenzel authored
92
93 collectSourceDirs :: PackageDescription -> [String]
94 collectSourceDirs pd = (collectFromLib $ library pd) ++ (concatMap collectFromExe (executables pd)) where
95 collectFromLib Nothing = []
96 collectFromLib (Just (Library _ bi)) = hsSourceDirs bi
97 collectFromExe (Executable _ _ bi) = hsSourceDirs bi
98
e0425c5 @leiffrenzel [ 1922446 ] [Editor] multipart Cabal editor
leiffrenzel authored
99 readArgs :: [String] -> (String, String, Maybe String)
100 readArgs (s1:s2:s3:_) = (s1, s2, Just s3)
101 readArgs (s1:s2:_) = (s1, s2, Nothing)
102 readArgs _ = ("", "", Nothing)
103
Something went wrong with that request. Please try again.