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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
173
174
175
176
177
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
|
module Job.Types where
import Control.Monad.IO.Class
import Data.Containers.ListUtils
import Data.Kind
import Data.Text (Text)
import Data.Text qualified as T
import System.FilePath
import System.FilePath.Glob
import System.Process
import {-# SOURCE #-} Config
import Destination
import Expr
import Repo
data Declared
data Evaluated
type family ExprD d c a :: Type where
ExprD Declared c a = Expr c a
ExprD Evaluated c a = a
data Job' d = Job
{ jobId :: JobId' d
, jobName :: JobName
, jobCheckout :: [ JobCheckout d ]
, jobRecipe :: Maybe [ Either CreateProcess Text ]
, jobArtifacts :: [ ( ArtifactName, Pattern ) ]
, jobUses :: [ ArtifactSpec d ]
, jobPublish :: [ JobPublish d ]
, jobPush :: [ JobPush d ]
}
type Job = Job' Evaluated
type DeclaredJob = Job' Declared
type family JobId' d :: Type where
JobId' Declared = JobName
JobId' Evaluated = JobId
data JobName = JobName Text
deriving (Eq, Ord, Show)
stringJobName :: JobName -> String
stringJobName (JobName name) = T.unpack name
textJobName :: JobName -> Text
textJobName (JobName name) = name
jobRequiredArtifacts :: Ord (JobId' d) => Job' d -> [ ArtifactSpec d ]
jobRequiredArtifacts job = nubOrd $ jobUses job ++ (map jpArtifact $ jobPublish job)
type family JobRepo d :: Type where
JobRepo Declared = Maybe ( RepoName, Maybe Text )
JobRepo Evaluated = Tree
data JobCheckout d = JobCheckout
{ jcRepo :: JobRepo d
, jcSubtree :: Maybe FilePath
, jcDestination :: Maybe FilePath
}
type family JobDestination d :: Type where
JobDestination Declared = DestinationName
JobDestination Evaluated = Destination
data JobPublish d = JobPublish
{ jpArtifact :: ArtifactSpec d
, jpDestination :: JobDestination d
, jpPath :: Maybe FilePath
}
data JobPush d = JobPush
{ jpushSource :: ExprD d JobSetContext Commit
, jpushDestination :: ExprD d JobSetContext Branch
}
data ArtifactName = ArtifactName Text
deriving (Eq, Ord, Show)
type ArtifactSpec d = ( JobId' d, ArtifactName )
data JobSet' d = JobSet
{ jobsetId :: JobSetId' d
, jobsetConfig :: Maybe Config
, jobsetCommit :: Maybe Commit
, jobsetExplicitlyRequested :: [ JobId' d ]
, jobsetJobsEither :: Either String [ Job' d ]
}
type JobSet = JobSet' Evaluated
type DeclaredJobSet = JobSet' Declared
type family JobSetId' d :: Type where
JobSetId' Declared = ()
JobSetId' Evaluated = JobSetId
jobsetJobs :: JobSet -> [ Job ]
jobsetJobs = either (const []) id . jobsetJobsEither
newtype JobId = JobId [ JobIdPart ]
deriving (Eq, Ord)
newtype JobSetId = JobSetId [ JobIdPart ]
deriving (Eq, Ord)
data JobIdPart
= JobIdName JobName
| JobIdRepo (Maybe RepoName) JobIdRepoPart
deriving (Eq, Ord)
data JobIdRepoPart
= JobIdTree FilePath TreeId
| JobIdCommit CommitId
| JobIdTag CommitId TagId
deriving (Eq, Ord)
newtype JobRef = JobRef [ Text ]
deriving (Eq, Ord)
textJobIdPart :: JobIdPart -> Text
textJobIdPart = \case
JobIdName name -> textJobName name
JobIdRepo _ (JobIdTree _ tid) -> textTreeId tid
JobIdRepo _ (JobIdCommit cid) -> textCommitId cid
JobIdRepo _ (JobIdTag cid tid) -> textCommitId cid <> "^" <> textTagId tid
textJobId :: JobId -> Text
textJobId (JobId ids) = T.intercalate "." $ map textJobIdPart ids
parseJobRef :: Text -> JobRef
parseJobRef = JobRef . go 0 ""
where
go :: Int -> Text -> Text -> [ Text ]
go plevel cur s = do
let bchars | plevel > 0 = [ '(', ')' ]
| otherwise = [ '.', '(', ')' ]
let ( part, rest ) = T.break (`elem` bchars) s
case T.uncons rest of
Just ( '.', rest' ) -> (cur <> part) : go plevel "" rest'
Just ( '(', rest' ) -> go (plevel + 1) (cur <> part) rest'
Just ( ')', rest' ) -> go (plevel - 1) (cur <> part) rest'
_ -> [ cur <> part ]
lastJobNameId :: JobId -> Maybe JobName
lastJobNameId (JobId ids) = go Nothing ids
where
go _ (JobIdName name : rest) = go (Just name) rest
go cur (_ : rest) = go cur rest
go cur [] = cur
data JobSetDep
= SiblingJobDependency JobName
| RepoDependency RepoName RepoDepLevel
data RepoDepLevel
= RepoDepSubtree FilePath
| RepoDepCommit
| RepoDepTag
instance Semigroup RepoDepLevel where
RepoDepTag <> _ = RepoDepTag
_ <> RepoDepTag = RepoDepTag
RepoDepCommit <> _ = RepoDepCommit
_ <> RepoDepCommit = RepoDepCommit
RepoDepSubtree path <> RepoDepSubtree path' = RepoDepSubtree $
joinPath $ commonPrefix (splitDirectories path) (splitDirectories path')
where
commonPrefix (x : xs) (y : ys) | x == y = x : commonPrefix xs ys
commonPrefix _ _ = []
repoDepPath :: RepoDepLevel -> FilePath
repoDepPath = \case
RepoDepSubtree path -> path
RepoDepCommit -> ""
RepoDepTag -> ""
data RepoRef
= RepoRefTree Tree
| RepoRefCommit Commit
| RepoRefTag Commit (Tag Commit)
repoRefRepo :: RepoRef -> Repo
repoRefRepo = \case
RepoRefTree tree -> treeRepo tree
RepoRefCommit commit -> commitRepo commit
RepoRefTag commit _ -> commitRepo commit
repoRefTree :: (MonadIO m, MonadFail m) => RepoRef -> m Tree
repoRefTree = \case
RepoRefTree tree -> return tree
RepoRefCommit commit -> getCommitTree commit
RepoRefTag commit _ -> getCommitTree commit
repoRefToIdPart :: MonadIO m => RepoRef -> m JobIdRepoPart
repoRefToIdPart = \case
RepoRefTree tree -> return $ JobIdTree (treeSubdir tree) (treeId tree)
RepoRefCommit commit -> return $ JobIdCommit (commitId commit)
RepoRefTag commit tag -> return $ JobIdTag (commitId commit) (tagId tag)
data JobSetContext = JobSetContext
{ jscRepos :: [ ( Maybe RepoName, Repo ) ]
, jscRepoRefs :: [ ( Maybe RepoName, RepoRef ) ]
}
instance ExprContext JobSetContext where
type ExprDependency JobSetContext = [ JobSetDep ]
|