Skip to content

Instantly share code, notes, and snippets.

@tfausak
Last active March 29, 2016 00:11
Show Gist options
  • Save tfausak/0f29f51b79fac16cebd9 to your computer and use it in GitHub Desktop.
Save tfausak/0f29f51b79fac16cebd9 to your computer and use it in GitHub Desktop.
Automatically upgrades a Haskell Stack project to the latest Stackage nightly snapshot.
#!/usr/bin/env stack
{-
stack
--install-ghc
--resolver lts-5.10
runghc
--package aeson
--package bytestring
--package http-client
--package http-client-tls
--package turtle
--package unordered-containers
--package yaml
-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
import qualified Data.Aeson as JSON
import qualified Data.ByteString as Bytes
import qualified Data.HashMap.Strict as Hash
import qualified Data.Yaml as YAML
import qualified Data.Yaml.Pretty as YAML
import qualified Network.HTTP.Client as HTTP
import qualified Network.HTTP.Client.TLS as TLS
import qualified Turtle as Shell
main :: IO ()
main = do
let configPath = "stack.yaml"
let configKey = "resolver"
let snapshotUrl = "https://www.stackage.org/download/lts-snapshots.json"
let snapshotKey = "nightly"
-- Run the test suite.
Shell.procs "stack" ["test"] (return "")
-- Find the current snapshot.
Just (config :: YAML.Object) <- YAML.decodeFile configPath
let Just (YAML.String current) = Hash.lookup configKey config
-- Get the list of snapshots.
manager <- HTTP.newManager TLS.tlsManagerSettings
request <- HTTP.parseUrl snapshotUrl
response <- HTTP.httpLbs request manager
-- Find the newest snapshot.
let body = HTTP.responseBody response
let Just (snapshots :: JSON.Object) = JSON.decode body
let Just (JSON.String nightly) = Hash.lookup snapshotKey snapshots
-- Update the current snapshot.
if current == nightly then return () else do
let newConfig = Hash.insert configKey (YAML.String nightly) config
-- Write the new config file.
let yamlConfig = YAML.setConfCompare compare YAML.defConfig
let yaml = YAML.encodePretty yamlConfig newConfig
Bytes.writeFile configPath yaml
-- Run the test suite again.
Shell.procs "stack" ["test"] (return "")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment