stack-templatizer 0.1.1.0 → 0.1.2.0
raw patch · 4 files changed
+52/−20 lines, 4 filesdep +base64-bytestringdep ~bytestring
Dependencies added: base64-bytestring
Dependency ranges changed: bytestring
Files
- CHANGELOG.md +6/−0
- README.md +7/−3
- src/Main.hs +34/−11
- stack-templatizer.cabal +5/−6
CHANGELOG.md view
@@ -2,6 +2,12 @@ ## master ++## v0.1.2.0++* Embed files that are not valid UTF-8 as base64-encoded+ `{-# START_FILE BASE64 <name> #-}` sections.+ ## v0.1.1.0 * Output files in sorted ordering.
README.md view
@@ -1,6 +1,6 @@ # stack-templatizer -[](https://travis-ci.org/prikhi/stack-templatizer)+[](https://github.com/prikhi/stack-templatizer/actions/workflows/main.yml) Stack Templatizer is a small application that lets you generate stack template@@ -8,7 +8,7 @@ Install or clone & build the project using `stack`: -```+```sh # Install from Stack Nightly stack install stack-templatizer --resolver nightly @@ -21,11 +21,15 @@ Once installed, you can run `stack-templatizer my-template-folder` to generate a `my-template-folder.hsfiles` stack template. +Files that are not valid UTF-8 (images, archives, etc.) are embedded as+base64-encoded `{-# START_FILE BASE64 <name> #-}` sections, which `stack new`+decodes back into the original binary files. + For an example repository that generates a stack template, see [hpack-template](https://github.com/prikhi/hpack-template). -# LICENSE+## LICENSE BSD-3-Clause
src/Main.hs view
@@ -3,7 +3,14 @@ {-# LANGUAGE TupleSections #-} module Main where -import Data.List ( sort )+import Data.ByteString.Builder ( Builder+ , byteString+ , stringUtf8+ , toLazyByteString+ )+import Data.List ( intersperse+ , sort+ ) import System.Directory ( listDirectory , doesDirectoryExist )@@ -11,13 +18,14 @@ import System.Exit ( exitFailure ) import System.FilePath ( (</>) ) +import qualified Data.ByteString as BS+import qualified Data.ByteString.Base64 as Base64 import qualified Data.ByteString.Lazy as LBS-import qualified Data.ByteString.Lazy.Char8 as LC main :: IO () main = getArgs >>= \case- [folderName] ->- templatize folderName >>= LBS.writeFile (folderName ++ ".hsfiles")+ [folderName] -> templatize folderName+ >>= LBS.writeFile (folderName ++ ".hsfiles") . toLazyByteString _ -> printHelp >> exitFailure @@ -29,14 +37,15 @@ , "Usage: stack-templatizer FOLDER_NAME" , "" , "The generated file will be named `<folder-name>.hsfiles`"+ , "Files that are not valid UTF-8 are embedded base64-encoded." ] -templatize :: FilePath -> IO LBS.ByteString+templatize :: FilePath -> IO Builder templatize folder = do fileNames <- getFilesInDirectory folder namesAndContents <- mapM- (\file -> (file, ) <$> LBS.readFile (folder </> file))+ (\file -> (file, ) <$> BS.readFile (folder </> file)) fileNames return $ generateHFiles namesAndContents @@ -61,9 +70,23 @@ else return [templatePath] -generateHFiles :: [(FilePath, LBS.ByteString)] -> LBS.ByteString-generateHFiles = LBS.intercalate "\n" . map prefixName+generateHFiles :: [(FilePath, BS.ByteString)] -> Builder+generateHFiles = mconcat . intersperse "\n" . map renderSection where- prefixName :: (FilePath, LBS.ByteString) -> LBS.ByteString- prefixName (file, contents) =- "{-# START_FILE " <> LC.pack file <> " #-}\n" <> contents+ renderSection :: (FilePath, BS.ByteString) -> Builder+ renderSection (file, contents)+ | BS.isValidUtf8 contents =+ "{-# START_FILE " <> stringUtf8 file <> " #-}\n"+ <> byteString contents+ | otherwise =+ "{-# START_FILE BASE64 " <> stringUtf8 file <> " #-}\n"+ <> foldMap ((<> "\n") . byteString)+ (chunksOf 76 $ Base64.encode contents)+++chunksOf :: Int -> BS.ByteString -> [BS.ByteString]+chunksOf size bytes+ | BS.null bytes = []+ | otherwise =+ let (chunk, rest) = BS.splitAt size bytes+ in chunk : chunksOf size rest
stack-templatizer.cabal view
@@ -1,13 +1,11 @@ cabal-version: 1.12 --- This file has been generated from package.yaml by hpack version 0.35.1.+-- This file has been generated from package.yaml by hpack version 0.39.1. -- -- see: https://github.com/sol/hpack------ hash: c53e807db04e8b8ab72ceacd797c38f9885d39cd532a917e3f101bac72614b20 name: stack-templatizer-version: 0.1.1.0+version: 0.1.2.0 synopsis: Generate a stack template from a folder. description: @stack-templatizer@ is an application that generates a @.hsfiles@ stack template from a folder of template files.@@ -26,7 +24,7 @@ bug-reports: https://github.com/prikhi/stack-templatizer/issues author: Pavan Rikhi maintainer: pavan.rikhi@gmail.com-copyright: 2020-2023 Pavan Rikhi+copyright: 2020-2026 Pavan Rikhi license: BSD3 license-file: LICENSE build-type: Simple@@ -46,7 +44,8 @@ src build-depends: base >=4.7 && <5- , bytestring <1+ , base64-bytestring ==1.*+ , bytestring >=0.11.2 && <1 , directory ==1.* , filepath ==1.* default-language: Haskell2010