Created
March 20, 2019 19:25
-
-
Save bitc/5be37201445e647b19b079ee09f8d650 to your computer and use it in GitHub Desktop.
messy shake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Monad (forM) | |
import Crypto.Hash | |
import Crypto.Hash.Conduit | |
import Data.List (intercalate) | |
import Data.Monoid ((<>)) | |
import Development.Shake | |
import Development.Shake.FilePath | |
import System.Process (callProcess) | |
main :: IO () | |
main = shakeArgs shakeOptions{shakeFiles="build/"} $ do | |
-- LONG COMMENT | |
-- | |
"build/scss//*.css" %> \out -> do -- out = build/scss/main.css | |
let srcFile = "src" </> dropDirectory1 (dropDirectory1 (out -<.> "scss")) -- srcFile = src/scss/main.scss | |
dFile = out <.> "d" | |
need [srcFile] | |
cmd_ "sassc" [srcFile] [out] "--write-includes" [dFile] | |
importedFiles <- liftIO $ (fmap lines . readFile) dFile | |
need importedFiles | |
"build/petstore/.stamp" %> \out -> do | |
need ["tools/swagger-codegen-cli.jar", "src/petstore-swagger.json"] | |
cmd_ "java -jar tools/swagger-codegen-cli.jar generate -i src/petstore-swagger.json -l typescript-fetch -o build/petstore" | |
writeFile' out "" | |
"build/ts/.stamp" %> \out -> do | |
need ["build/petstore/.stamp", "build/assets/assets.ts"] | |
srcFiles <- getDirectoryFiles "" ["src/ts//*.ts", "src/ts//*.tsx"] | |
need (srcFiles ++ ["tsconfig.json"]) | |
cmd_ "node node_modules/.bin/tsc -b" | |
writeFile' out "" | |
"build/assets/assets.ts" %> \out -> do | |
imgFiles <- getDirectoryFiles "src" ["//*.png"] | |
let getSha1File :: FilePath -> FilePath | |
getSha1File f = "build/assets" </> f <.> "sha1" | |
imgSha1Files = map getSha1File imgFiles | |
need imgSha1Files | |
assets <- forM imgFiles $ \imgFile -> do | |
sha1hash <- liftIO $ readFile (getSha1File imgFile) | |
pure (imgFile, assetFileName imgFile sha1hash) | |
traced "writeAssetsTsFile" $ writeAssetsTsFile out assets | |
"build/assets//*.sha1" %> \out -> do | |
let imgFile = "src" </> dropDirectory1 (dropDirectory1 (dropExtension out)) | |
need [imgFile] | |
sha1hash <- traced "sha1File" (sha1File imgFile) | |
copyFile' imgFile ("build/assets" </> dropDirectory1 (assetFileName imgFile sha1hash)) | |
writeFile' out sha1hash | |
"build/main.px.js" %> \out -> do | |
need ["build/ts/.stamp"] | |
cmd_ "px --for-browser" ["build/ts/src/ts/main.js"] [out] | |
-- | |
-- Clean the build | |
-- | |
phony "clean" $ do | |
putNormal $ "Cleaning files" | |
removeFilesAfter "build" ["//*"] | |
phony "http-server" $ do | |
runAfter $ | |
callProcess "node" ["node_modules/.bin/http-server", "-c-1"] | |
--- | |
--- Final desired artifact | |
--- | |
want ["build/main.px.js"] | |
want ["build/scss/test.css"] | |
--- Rules: | |
assetFileName :: FilePath -> String -> FilePath | |
assetFileName file sig = | |
let (base, ext) = splitExtension file | |
in base ++ "-" ++ sig ++ ext | |
-- TypeScript asset file code generator | |
writeAssetsTsFile :: FilePath -> [(FilePath, String)] -> IO () | |
writeAssetsTsFile outFile assets = | |
writeFile outFile (genAssetsTsCode assets) | |
genAssetsTsCode :: [(FilePath, String)] -> String | |
genAssetsTsCode assets = concat | |
[ "export const Assets = {\n" | |
, intercalate ",\n" (map genAssetLine assets) | |
, "\n};\n" | |
] | |
genAssetLine :: (FilePath, String) -> String | |
genAssetLine (fileName, url) = " \"" <> fileName <> "\": \"" <> url <> "\"" | |
-- Helper functions | |
sha1FileDigest :: FilePath -> IO (Digest SHA1) | |
sha1FileDigest = hashFile | |
sha1File :: FilePath -> IO String | |
sha1File file = do | |
h <- sha1FileDigest file | |
pure (show h) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment