From 641ea85d4da795cb2c67d9777cb3db3dfede1d8b Mon Sep 17 00:00:00 2001 From: pacien Date: Tue, 31 Dec 2019 06:58:53 +0100 Subject: compiler: add option to include static web app in the output GitHub: closes #6 --- compiler/app/Main.hs | 54 ++++++++++++++++++++++++++++++++++++++++-------- compiler/data/.gitignore | 1 + compiler/data/readme.md | 1 + compiler/package.yaml | 3 +++ compiler/src/Files.hs | 17 +++++++++++++-- 5 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 compiler/data/.gitignore create mode 100644 compiler/data/readme.md diff --git a/compiler/app/Main.hs b/compiler/app/Main.hs index 1773073..24d8aad 100644 --- a/compiler/app/Main.hs +++ b/compiler/app/Main.hs @@ -22,37 +22,73 @@ module Main where -import Paths_ldgallery_compiler (version) +import Paths_ldgallery_compiler (version, getDataFileName) import Data.Version (showVersion) +import System.FilePath (()) import System.Console.CmdArgs + import Compiler +import Files (readDirectory, copyTo) data Options = Options { inputDir :: String , outputDir :: String - , rebuild :: Bool + , rebuilAll :: Bool + , withViewer :: Bool } deriving (Show, Data, Typeable) options = Options { inputDir = "./" &= typDir + &= explicit + &= name "i" + &= name "input-dir" &= help "Gallery source directory (default=./)" , outputDir = "./out" &= typDir + &= explicit + &= name "o" + &= name "output-dir" &= help "Generated gallery output path (default=./out)" - , rebuild = False + , rebuilAll = False + &= explicit + &= name "r" + &= name "rebuild-all" &= help "Invalidate cache and recompile everything" + , withViewer = False + &= explicit + &= name "w" + &= name "with-viewer" + &= help "Include the static web viewer in the output" } - &= summary ("ldgallery v" ++ (showVersion version) ++ " - a static gallery generator with tags") - &= program "ldgallery" - &= help "Compile a gallery" - &= helpArg [explicit, name "h", name "help"] - &= versionArg [explicit, name "v", name "version"] + + &= summary ("ldgallery v" ++ (showVersion version) ++ " - a static gallery generator with tags") + &= program "ldgallery" + &= help "Compile a gallery" + &= helpArg [explicit, name "h", name "help"] + &= versionArg [explicit, name "version"] main :: IO () main = do opts <- cmdArgs options - compileGallery (inputDir opts) (outputDir opts) (rebuild opts) + compileGallery (inputDir opts) (galleryOutputDir "gallery" opts) (rebuilAll opts) + if (withViewer opts) then copyViewer (outputDir opts) else noop + + where + galleryOutputDir :: FilePath -> Options -> FilePath + galleryOutputDir gallerySubdir opts = + if withViewer opts then outputBase gallerySubdir else outputBase + where outputBase = outputDir opts + + copyViewer :: FilePath -> IO () + copyViewer target = + putStrLn "Copying viewer webapp" + >> getDataFileName "viewer" + >>= readDirectory + >>= copyTo target + + noop :: IO () + noop = return () diff --git a/compiler/data/.gitignore b/compiler/data/.gitignore new file mode 100644 index 0000000..dd4395c --- /dev/null +++ b/compiler/data/.gitignore @@ -0,0 +1 @@ +viewer diff --git a/compiler/data/readme.md b/compiler/data/readme.md new file mode 100644 index 0000000..9aded74 --- /dev/null +++ b/compiler/data/readme.md @@ -0,0 +1 @@ +Link `../../viewer/dist` to `./viewer` for full distribution. diff --git a/compiler/package.yaml b/compiler/package.yaml index 90c4ea5..0b03887 100644 --- a/compiler/package.yaml +++ b/compiler/package.yaml @@ -27,6 +27,9 @@ dependencies: - JuicyPixels-extra - parallel-io +data-dir: data +data-files: ["**/*"] + library: source-dirs: src diff --git a/compiler/src/Files.hs b/compiler/src/Files.hs index a6649c8..a658ded 100644 --- a/compiler/src/Files.hs +++ b/compiler/src/Files.hs @@ -19,6 +19,7 @@ {-# LANGUAGE DuplicateRecordFields , DeriveGeneric + , NamedFieldPuns #-} module Files @@ -27,7 +28,8 @@ module Files , fileName, maybeFileName, subPaths, pathLength , localPath, webPath , FSNode(..), AnchoredFSNode(..) - , nodePath, nodeName, isHidden, flattenDir, filterDir, readDirectory + , nodePath, nodeName, isHidden, flattenDir, filterDir + , readDirectory, copyTo , ensureParentDir, remove, isOutdated ) where @@ -46,7 +48,8 @@ import System.Directory , getModificationTime , listDirectory , createDirectoryIfMissing - , removePathForcibly ) + , removePathForcibly + , copyFile ) import qualified System.FilePath import qualified System.FilePath.Posix @@ -146,6 +149,16 @@ readDirectory root = mkNode (Path []) >>= return . AnchoredFSNode root >>= mapM (mkNode . ((>= return . Dir path +copyTo :: FilePath -> AnchoredFSNode -> IO () +copyTo target AnchoredFSNode{anchor, root} = copyNode root + where + copyNode :: FSNode -> IO () + copyNode (File path) = + copyFile (localPath $ anchor /> path) (localPath $ target /> path) + + copyNode (Dir path items) = + createDirectoryIfMissing True (localPath $ target /> path) + >> mapM_ copyNode items ensureParentDir :: (FileName -> a -> IO b) -> FileName -> a -> IO b ensureParentDir writer filePath a = -- cgit v1.2.3