Skip to content

Instantly share code, notes, and snippets.

@3noch
Last active August 28, 2016 12:48
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 3noch/4696feff5c602e126681 to your computer and use it in GitHub Desktop.
Save 3noch/4696feff5c602e126681 to your computer and use it in GitHub Desktop.
Install GHCJS for Stack
#!/usr/bin/env stack
-- stack --install-ghc runghc --package turtle --package wreq
{-# LANGUAGE OverloadedStrings #-}
import qualified Control.Foldl as Fold
import Control.Lens ((^.))
import Control.Monad (when)
import Data.ByteString.Lazy (hPut)
import Data.Maybe (fromMaybe)
import qualified Data.Text as T
import Data.Time.Format (formatTime, defaultTimeLocale)
import Network.Wreq (get, responseBody)
import Turtle
main = do
dateStr <- T.pack . formatTime defaultTimeLocale "%Y%m%d" <$> date
let url = "http://ghcjs.luite.com/master-"<>dateStr<>".tar.gz"
echo $ format ("Downloading "%s%"...") url
r <- get (T.unpack url)
let tempdir = "temp-ghcjs-archive"
echo $ format ("Using temp directory: "%fp) tempdir
alreadyThere <- testdir tempdir
when alreadyThere $ rmtree tempdir
mkdir tempdir
let tempfile = tempdir</>"archive.tar.gz"
echo $ format ("Writing to "%fp%"...") tempfile
with (writeonly tempfile) $ \temphandle->
hPut temphandle (r ^. responseBody)
echo "Unpacking..."
proc "tar" ["-xzf", format fp tempfile, format ("-C"%fp) tempdir] empty
.||. die "failed to unpack archive"
Just ghcjsDir <- fold
(ls tempdir)
(Fold.find (T.isPrefixOf "ghcjs-" . format fp . filename))
let newFolderName = ghcjsDir <.> dateStr
let ghcjsStackVersion = format (fp%"_ghc-7.10.2") (filename newFolderName)
let outputArchive = tempdir </> fromText ghcjsStackVersion <.> "tar.gz"
mv ghcjsDir newFolderName
echo $ format ("Archiving to "%fp) outputArchive
targzPack (parent newFolderName) (filename newFolderName) outputArchive
-- Install archive to the approprate stack folder
Just outDir <- fold (inproc "stack" ["path", "--ghc-paths"] empty) Fold.head
echo $ format ("Copying to "%s) outDir
cp outputArchive (fromText outDir </> filename outputArchive)
echo $ format ("DONE: Add 'compiler: "%s%"' or 'resolver: "%s%"' to your stack.yaml and run 'stack setup' to boot it.")
ghcjsStackVersion ghcjsStackVersion
targzPack inDir inFile outFilePath =
proc "tar" ["-czf", format fp outFilePath, format ("-C"%fp) inDir, format fp inFile] empty
.||. die (format ("failed to create archive: "%fp) outFilePath)
@3noch
Copy link
Author

3noch commented Nov 29, 2015

Make this executable and run it. You need stack and tar on your PATH and I really hope you have installed wreq or the build time will be pretty horrid.

Once complete, it will tell you what to do.

@jbgi
Copy link

jbgi commented Nov 29, 2015

great script thanks. to be a bit more explicit about the next step, maybe the script should echo
"Add 'compiler: "%s%"' to your stack.yaml... instead of
"Point to "%s%" in your stack.yaml...

@3noch
Copy link
Author

3noch commented Nov 29, 2015

Great idea, thanks. I edited it. I was originally ambiguous because you can also specify it in the resolver field (to not use a snapshot), but being explicit is much better.

@tolysz
Copy link

tolysz commented Dec 10, 2015

It would be great it you could patch the .cabal to include the same version :)

@mgsloan
Copy link

mgsloan commented Dec 10, 2015

Nice, thanks for writing this! So, the reason this script is necessary is that the folder within the sdist tarball needs to be renamed? Seems to me like we should just make stack agnostic of the particular "ghcjs-*" folder copied.

@yamafaktory
Copy link

After successfully executing the script, either using the compiler or the resolver field leads to:

No information found for ghcjs-0.2.0.20151227_ghc-7.10.2.
Supported versions for OS key 'source': GhcjsVersion 0.1.0.20150924 7.10.2

@3noch
Copy link
Author

3noch commented Jan 8, 2016

Wow...apparently GitHub doesn't notify me when people comment on this. :/

@yamafaktory: Yes, apparently newer versions of stack don't play as nicely with this. I've found it still works but you must "fake" out stack by filling in the whole setup-info thing:

So for today's build, I would run the script and then add this to my .cabal file:

compiler: ghcjs-0.2.0.20160107_ghc-7.10.2
compiler-check: match-exact
setup-info:
  ghcjs:
      source:
            ghcjs-0.2.0.20160107_ghc-7.10.2:
                    url: "http://ghcjs.luite.com/master-20160107.tar.gz"

@3noch
Copy link
Author

3noch commented Jan 8, 2016

@mgsloan: I'd be much happier if stack didn't care about the folder name!

@3noch
Copy link
Author

3noch commented Jan 11, 2016

@mgsloan: @yamafaktory: Yay! This script will soon be obsolete! commercialhaskell/stack#1622

@mgsloan
Copy link

mgsloan commented Apr 14, 2016

Note that this script is still handy if you want your ghcjs version to include the date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment