This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
/
Internal.hs
116 lines (86 loc) · 3.59 KB
/
Internal.hs
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
module Compiler.Internal where
import Compiler.Types
import Compiler.Utils
import Language.Types
import System.FilePath ((</>))
import Types
import Utils
preCompileChecks :: Card -> [PreCompileError]
preCompileChecks c = runIdentity $ execWriterT $ cleanCard c
dirty :: String -> Precompiler ()
dirty s = tell ["Precompilation check failed: " ++ s]
cleanCard :: Card -> Precompiler ()
cleanCard (Card name d) = do
cleanCardName name
cleanDeclaration d
cleanDeclaration :: Declaration -> Precompiler ()
cleanDeclaration (Deploy src dst _) = do
cleanFilePath src
cleanFilePath dst
cleanDeclaration (SparkOff cr) = cleanCardReference cr
cleanDeclaration (IntoDir dir) = cleanFilePath dir
cleanDeclaration (OutofDir dir) = cleanFilePath dir
cleanDeclaration (DeployKindOverride _) = return () -- Nothing can go wrong.
cleanDeclaration (Alternatives fs) = mapM_ cleanFilePath fs
cleanDeclaration (Block ds) = mapM_ cleanDeclaration ds
cleanCardReference :: CardReference -> Precompiler ()
cleanCardReference (CardFile cfr) = cleanCardFileReference cfr
cleanCardReference (CardName cnr) = cleanCardNameReference cnr
cleanCardFileReference :: CardFileReference -> Precompiler ()
cleanCardFileReference (CardFileReference fp mcnr) = do
cleanFilePath fp
case mcnr of
Nothing -> return ()
Just cnr -> cleanCardNameReference cnr
cleanCardNameReference :: CardNameReference -> Precompiler ()
cleanCardNameReference (CardNameReference cn) = cleanCardName cn
cleanCardName :: CardName -> Precompiler ()
cleanCardName n
| containsNewline n = dirty $ "Card name contains newline character(s): " ++ n
| otherwise = return ()
cleanFilePath :: FilePath -> Precompiler ()
cleanFilePath [] = dirty "Empty filepath"
cleanFilePath fp
| containsNewline fp =
dirty $ "Filepath contains newline character(s): " ++ fp
| containsMultipleConsequtiveSlashes fp =
dirty $ "Filepath contains multiple consequtive slashes: " ++ fp
| otherwise = return ()
compileUnit :: Card -> PureCompiler ([Deployment], [CardReference])
compileUnit card = do
initSt <- initialState
execWriterT $ evalStateT (compileDecs [cardContent card]) initSt
compileDecs :: [Declaration] -> InternalCompiler ()
compileDecs = mapM_ compileDec
compileDec :: Declaration -> InternalCompiler ()
compileDec (Deploy src dst kind) = do
override <- gets state_deployment_kind_override
superOverride <- asks conf_compile_override
let resultKind = case (superOverride, override, kind) of
(Nothing, Nothing, Nothing) -> LinkDeployment
(Nothing, Nothing, Just k ) -> k
(Nothing, Just o , _ ) -> o
(Just o , _ , _ ) -> o
outof <- gets state_outof_prefix
into <- gets state_into
let alternates = resolvePrefix $ outof ++ [sources src]
let destination = into </> dst
addDeployment $ Put alternates destination resultKind
compileDec (SparkOff cr) = addCardRef cr
compileDec (IntoDir dir) = do
ip <- gets state_into
if null ip
then modify (\s -> s {state_into = dir} )
else modify (\s -> s {state_into = ip </> dir} )
compileDec (OutofDir dir) = do
op <- gets state_outof_prefix
modify (\s -> s {state_outof_prefix = op ++ [Literal dir]})
compileDec (DeployKindOverride kind) = do
modify (\s -> s { state_deployment_kind_override = Just kind })
compileDec (Block ds) = do
before <- get
compileDecs ds
put before
compileDec (Alternatives ds) = do
op <- gets state_outof_prefix
modify (\s -> s { state_outof_prefix = op ++ [Alts ds] })