Skip to content

Commit d9ec5a2

Browse files
authored
Merge pull request #25 from bergmark/lts-build-constraints
Base LTS builds on an lts-build-constraints.yaml
2 parents c61f8c3 + 275840a commit d9ec5a2

File tree

1 file changed

+55
-36
lines changed

1 file changed

+55
-36
lines changed

app-curator/Main.hs

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Network.HTTP.Client.TLS (tlsManagerSettings)
1414
import Network.HTTP.Download (download)
1515
import Options.Applicative.Simple hiding (action)
1616
import qualified Pantry
17-
import Path (toFilePath)
17+
import Path ((</>), addExtension, toFilePath, Path, Abs, File)
1818
import Path.IO (doesFileExist, removeFile, resolveFile', resolveDir')
1919
import Paths_curator (version)
2020
import qualified RIO.ByteString.Lazy as BL
@@ -119,41 +119,60 @@ update = do
119119
void $ updateHackageIndex $ Just "Updating hackage index"
120120

121121
constraints :: Target -> RIO PantryApp ()
122-
constraints target =
123-
case target of
124-
TargetLts x y | y > 0 -> do
125-
let prev = y - 1
126-
url = concat [ "https://raw.githubusercontent.com/" ++ constraintsRepo ++ "/master/lts/"
127-
, show x
128-
, "/"
129-
, show prev
130-
, ".yaml"
131-
]
132-
logInfo $ "Will reuse constraints.yaml from lts-" <> display x <> "." <> display prev
133-
req <- parseUrlThrow url
134-
constraintsPath <- resolveFile' constraintsFilename
135-
exists <- doesFileExist constraintsPath
136-
when exists $ do
137-
logWarn "Local constraints file will be deleted before downloading reused constraints"
138-
removeFile constraintsPath
139-
downloaded <- download req constraintsPath
140-
unless downloaded $
141-
error $ "Could not download constraints.yaml from " <> url
142-
_ -> do
143-
buildConstraintsPath <- resolveFile' "build-constraints.yaml"
144-
exists <- doesFileExist buildConstraintsPath
145-
stackageConstraints <- if exists
146-
then do
147-
logInfo "Reusing already existing file build-constraints.yaml"
148-
loadStackageConstraints $ toFilePath buildConstraintsPath
149-
else do
150-
logInfo $ "Downloading build-constraints from commercialhaskell/stackage"
151-
req <- parseUrlThrow "https://raw.githubusercontent.com/commercialhaskell/stackage/master/build-constraints.yaml"
152-
man <- liftIO $ newManager tlsManagerSettings
153-
liftIO (httpLbs req man) >>=
154-
loadStackageConstraintsBs . BL.toStrict . responseBody
155-
logInfo "Writing constraints.yaml"
156-
liftIO $ encodeFile constraintsFilename stackageConstraints
122+
constraints target = do
123+
stackageConstraints <- case target of
124+
TargetNightly _ -> nightlyConstraints
125+
TargetLts major minor -> ltsConstraints major minor
126+
logInfo "Writing constraints.yaml"
127+
liftIO $ encodeFile constraintsFilename stackageConstraints
128+
129+
nightlyConstraints = do
130+
buildConstraintsPath <- resolveFile' "build-constraints.yaml"
131+
exists <- doesFileExist buildConstraintsPath
132+
if exists
133+
then do
134+
logInfo $ "Reusing already existing file " <> fromString (toFilePath buildConstraintsPath)
135+
loadStackageConstraints $ toFilePath buildConstraintsPath
136+
else do
137+
logInfo $ "Downloading " <> fromString (toFilePath buildConstraintsPath)
138+
<> " from commercialhaskell/stackage"
139+
req <- parseUrlThrow $ "https://raw.githubusercontent.com/commercialhaskell/stackage/master/build-constraints.yaml"
140+
man <- liftIO $ newManager tlsManagerSettings
141+
liftIO (httpLbs req man) >>= loadStackageConstraintsBs . BL.toStrict . responseBody
142+
143+
ltsConstraints major minor = do
144+
when (minor > 0) $ do
145+
verifyPreviousLtsMinorExists major minor
146+
let buildConstraintsName = "lts-" <> show major <> "-build-constraints.yaml"
147+
buildConstraintsPath <- resolveFile' buildConstraintsName
148+
exists <- doesFileExist buildConstraintsPath
149+
logInfo $ "Downloading " <> fromString (buildConstraintsName) <> " from commercialhaskell/lts-haskell"
150+
req <- parseUrlThrow $ "https://raw.githubusercontent.com/commercialhaskell/lts-haskell/lts-build-constraints/build-constraints/" <> buildConstraintsName
151+
man <- liftIO $ newManager tlsManagerSettings
152+
liftIO (httpLbs req man) >>= loadStackageConstraintsBs . BL.toStrict . responseBody
153+
154+
-- Performs a download of the previous LTS minor just to verify that it has been published.
155+
verifyPreviousLtsMinorExists :: Int -> Int -> RIO PantryApp ()
156+
verifyPreviousLtsMinorExists major minor = do
157+
let prevMinor = minor - 1
158+
url = concat
159+
[ "https://raw.githubusercontent.com/" ++ constraintsRepo ++ "/master/lts/"
160+
, show major
161+
, "/"
162+
, show prevMinor
163+
, ".yaml"
164+
]
165+
-- Hacky, use a presumably unique file name for this download (the file is not used).
166+
constraintsPath <- addExtension ".previous" =<< resolveFile' constraintsFilename
167+
exists <- doesFileExist constraintsPath
168+
when exists $
169+
removeFile constraintsPath
170+
logInfo $ "Verifying existance of constraints.yaml from "
171+
<> "lts-" <> display major <> "." <> display prevMinor
172+
req <- parseUrlThrow url
173+
downloaded <- download req constraintsPath
174+
unless downloaded $
175+
error $ "Could not download constraints.yaml from " <> url
157176

158177
snapshotIncomplete :: Target -> RIO PantryApp ()
159178
snapshotIncomplete target = do

0 commit comments

Comments
 (0)