summaryrefslogtreecommitdiff
path: root/src/main/frege/buildclient/config/BuildConfig.fr
blob: 89be3a7bb9afbf2371fac1f63c1ee11dc51c742d (plain)
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
module buildclient.config.BuildConfig where
import buildclient.config.BuildParser



(<?>) :: Maybe b -> a -> Either a b
(<?>) = flip maybeToEither

(<#>) :: (c -> Maybe b) -> a -> (c -> Either a b)
(<#>) f a b = f b <?> a

maybeToEither :: a -> Maybe b -> Either a b
maybeToEither _ (Just v) = Right v
maybeToEither e _ = Left e

extractSingleArg :: ASTDirective -> Maybe ASTValue
extractSingleArg (ASTDirective _ [value] []) = Just value
extractSingleArg _ = Nothing


getArgDirective ::  String -> [ASTDirective] -> Maybe ASTValue
getArgDirective name directives = findDirective name directives >>= extractSingleArg

getTypedArg :: String -> String -> [ASTDirective] -> (ASTValue -> Maybe a) -> Either String a
getTypedArg name label directives mapper =
    (findDirective name directives <?> ("Please add the missing " ++ label))
        >>= (extractSingleArg <#> ("Please add an argument to the " ++ label))
        >>= (mapper <#> ("Invalid argument type for " ++ label))

getArg :: ASTDirective -> Int -> Maybe ASTValue
getArg (ASTDirective _ args _) = getArg' args
    where getArg' (a : _) 0 = Just a
          getArg' (a : as) i = getArg' as (i - 1)
          getArg' [] _ = Nothing

toString :: ASTValue -> Maybe String
toString (ASTValue.ASTString str) = Just str
toString (ASTValue.ASTWord str) = Just str
toString _ = Nothing

expectString :: ASTValue -> Maybe String
expectString (ASTValue.ASTString str) = Just str
expectString _ = Nothing




data BuildSource = GitSource {upstream :: String, branch :: Maybe String}
derive Show BuildSource

findSource :: ASTDirective -> Either String BuildSource
findSource (ASTDirective _ [ASTWord "git"] block) = do
    upstream <- getTypedArg "upstream" "git upstream directive" block expectString
    return $ GitSource upstream (getArgDirective "branch" block >>= expectString)
findSource (ASTDirective _ [ASTWord name] _) = Left ("Unknown source type " ++ name)
findSource _ = Left "Please provide a source type"


data BuildSystem = GradleBuild { task::String, project :: Maybe String }
derive Show BuildSystem


findBuildSystem :: ASTDirective -> Either String BuildSystem
findBuildSystem (ASTDirective _ [ASTWord "gradle"] block) = do
    task <- getTypedArg "task" "gradle task" block expectString
    return $ GradleBuild task $ (getArgDirective "project" block >>= expectString)

parseBuildConfig :: [ASTDirective] -> Either String BuildConfig
parseBuildConfig directives = do
    modid <- getTypedArg "modid" "modid" directives expectString
    label <- getTypedArg "label" "label" directives expectString
    description <- getTypedArg "description" "description" directives expectString
    author <- getTypedArg "author" "author" directives expectString
    source <- findDirective "source" directives <?> "Please provide a source block" >>= findSource
    build <- findDirective "build" directives <?> "Please provide a build block" >>= findBuildSystem
    return $ BuildConfig modid label description author (getArgDirective "webpresence" directives >>= expectString) source build

data BuildConfig = BuildConfig
    { modid :: String
    , label :: String
    , description :: String
    , author :: String
    , webpresence :: Maybe String
    , source :: BuildSource
    , build :: BuildSystem
    }

derive Show BuildConfig