|
| 1 | +#!/usr/bin/env stack |
| 2 | +{- stack |
| 3 | + --resolver nightly-2019-02-04 |
| 4 | + --install-ghc |
| 5 | + runghc |
| 6 | + --package shake |
| 7 | + --package text |
| 8 | +-} |
| 9 | + |
| 10 | +{-# LANGUAGE OverloadedStrings #-} |
| 11 | + |
| 12 | +import Data.Char (isSpace) |
| 13 | +import Data.List (dropWhileEnd) |
| 14 | +import Data.Maybe (catMaybes) |
| 15 | +import Data.Text (Text) |
| 16 | +import qualified Data.Text as T |
| 17 | +import qualified Data.Text.IO as TIO |
| 18 | +import Development.Shake |
| 19 | +import Development.Shake.FilePath |
| 20 | + |
| 21 | +workflowFile :: FilePath |
| 22 | +workflowFile = "Hoogle.alfredworkflow" |
| 23 | + |
| 24 | +executableFile :: FilePath |
| 25 | +executableFile = "alfred-hoogle" |
| 26 | + |
| 27 | +main :: IO () |
| 28 | +main = |
| 29 | + shakeArgs shakeOptions |
| 30 | + $ action |
| 31 | + $ withTempDir |
| 32 | + $ \tempDir -> do |
| 33 | + |
| 34 | + -- Build the executable and copy to temp directory |
| 35 | + stackBuild |
| 36 | + binaryFp <- binaryPath |
| 37 | + copyFile' binaryFp $ tempDir </> executableFile |
| 38 | + |
| 39 | + -- Copy the plist to temp dir and set the version number |
| 40 | + copyFile' ("workflow_skeleton" </> "info.plist") |
| 41 | + (tempDir </> "info.plist") |
| 42 | + ver <- cabalVer |
| 43 | + liftIO $ setPlistVersion ver $ tempDir </> "info.plist" |
| 44 | + |
| 45 | + -- Compress the skeleton directory |
| 46 | + command_ [] |
| 47 | + "zip" |
| 48 | + ["-r", "-X", "-j", workflowFile, "workflow_skeleton"] |
| 49 | + |
| 50 | + -- Add the executable and updated plist file to the archive |
| 51 | + command_ [] "zip" ["-r", "-X", "-j", workflowFile, tempDir] |
| 52 | + |
| 53 | + |
| 54 | +stackBuild :: Action () |
| 55 | +stackBuild = command_ [] "stack" ["build"] |
| 56 | + |
| 57 | +binaryPath :: Action FilePath |
| 58 | +binaryPath = do |
| 59 | + Stdout localbin <- command [] "stack" ["path", "--local-install-root"] |
| 60 | + return $ dropWhileEnd isSpace localbin </> "bin" </> executableFile |
| 61 | + |
| 62 | +cabalVer :: Action Text |
| 63 | +cabalVer = do |
| 64 | + Stdout ver <- command [] "stack" ["ls", "dependencies"] |
| 65 | + return $ defVer $ catMaybes $ T.stripPrefix "alfred-hoogle " <$> T.lines (T.pack ver) |
| 66 | + where defVer [] = "0.0.0" |
| 67 | + defVer (x:_) = x |
| 68 | + |
| 69 | +setPlistVersion :: Text -> FilePath -> IO () |
| 70 | +setPlistVersion ver fp = do |
| 71 | + f <- TIO.readFile fp |
| 72 | + TIO.writeFile fp $ T.replace "VERSIONPLACEHOLDER" ver f |
0 commit comments