From 6bc29b5db2c8de62e2d9f21c25fa8dcd1ec5a75b Mon Sep 17 00:00:00 2001 From: pacien Date: Fri, 27 Dec 2019 10:32:35 +0100 Subject: compiler: extracting funcs --- compiler/src/Compiler.hs | 48 +++++++++++++--------------------------------- compiler/src/Files.hs | 14 ++++++++++++-- compiler/src/Processors.hs | 6 +++--- compiler/src/Resource.hs | 14 +++++++++++--- 4 files changed, 39 insertions(+), 43 deletions(-) diff --git a/compiler/src/Compiler.hs b/compiler/src/Compiler.hs index 991de9c..5c47521 100644 --- a/compiler/src/Compiler.hs +++ b/compiler/src/Compiler.hs @@ -29,10 +29,7 @@ module Compiler import Control.Monad import Data.Function ((&)) -import Data.Ord (comparing) -import Data.List (sortBy, length) -import System.Directory (createDirectoryIfMissing, removePathForcibly) -import System.FilePath (dropFileName, ()) +import System.FilePath (()) import Data.Aeson (ToJSON) import qualified Data.Aeson as JSON @@ -40,25 +37,25 @@ import qualified Data.Aeson as JSON import Config import Files (FileName, readDirectory, localPath, isHidden, nodeName, filterDir, flattenDir, root, (/>), ensureParentDir) import Input (decodeYamlFile, readInputTree) -import Resource (ResourceTree, buildResourceTree, outputDiff) +import Resource (ResourceTree, buildResourceTree, cleanupResourceDir) import Gallery (buildGalleryTree) import Processors -itemsDir :: String -itemsDir = "items" - -thumbnailsDir :: String -thumbnailsDir = "thumbnails" +writeJSON :: ToJSON a => FileName -> a -> IO () +writeJSON outputPath object = + do + putStrLn $ "Generating:\t" ++ outputPath + ensureParentDir JSON.encodeFile outputPath object compileGallery :: FilePath -> FilePath -> IO () compileGallery inputDirPath outputDirPath = do - config <- readConfig (inputDirPath "gallery.yaml") + config <- readConfig (inputDirPath galleryConf) inputDir <- readDirectory inputDirPath - let isGalleryFile = \n -> nodeName n == "gallery.yaml" + let isGalleryFile = \n -> nodeName n == galleryConf let galleryTree = filterDir (liftM2 (&&) (not . isGalleryFile) (not . isHidden)) inputDir inputTree <- readInputTree galleryTree @@ -68,10 +65,7 @@ compileGallery inputDirPath outputDirPath = let thumbnailProc = thumbnailFileProcessor (Resolution 150 50) skipCached inputDirPath outputDirPath thumbnailsDir resourceTree <- buildResourceTree dirProc itemProc thumbnailProc inputTree - putStrLn "\nRESOURCE TREE" - putStrLn (show resourceTree) - - cleanup resourceTree outputDirPath + cleanupResourceDir resourceTree outputDirPath buildGalleryTree resourceTree & writeJSON (outputDirPath "index.json") @@ -80,22 +74,6 @@ compileGallery inputDirPath outputDirPath = & writeJSON (outputDirPath "viewer.json") where - cleanup :: ResourceTree -> FileName -> IO () - cleanup resourceTree outputDir = - readDirectory outputDir - >>= return . outputDiff resourceTree . root - >>= return . sortBy (flip $ comparing length) -- nested files before dirs - >>= return . map (localPath . (/>) outputDir) - >>= mapM_ remove - - remove :: FileName -> IO () - remove path = - do - putStrLn $ "Removing: " ++ path - removePathForcibly path - - writeJSON :: ToJSON a => FileName -> a -> IO () - writeJSON outputPath object = - do - putStrLn $ "Generating: " ++ outputPath - ensureParentDir JSON.encodeFile outputPath object + galleryConf = "gallery.yaml" + itemsDir = "items" + thumbnailsDir = "thumbnails" diff --git a/compiler/src/Files.hs b/compiler/src/Files.hs index 0392efe..23daf3a 100644 --- a/compiler/src/Files.hs +++ b/compiler/src/Files.hs @@ -26,7 +26,7 @@ module Files , (), (), localPath, webPath , FSNode(..), AnchoredFSNode(..) , nodePath, nodeName, isHidden, flattenDir, filterDir, readDirectory - , ensureParentDir + , ensureParentDir, remove ) where @@ -34,7 +34,11 @@ import Control.Monad (filterM, mapM) import Data.Bool (bool) import Data.List (isPrefixOf, length, deleteBy) import Data.Function ((&)) -import System.Directory (doesDirectoryExist, listDirectory, createDirectoryIfMissing) +import System.Directory + ( doesDirectoryExist + , listDirectory + , createDirectoryIfMissing + , removePathForcibly ) import qualified System.FilePath import qualified System.FilePath.Posix @@ -118,3 +122,9 @@ ensureParentDir writer filePath a = >> writer filePath a where parentDir = System.FilePath.dropFileName filePath + +remove :: FileName -> IO () +remove path = + do + putStrLn $ "Removing:\t" ++ path + removePathForcibly path diff --git a/compiler/src/Processors.hs b/compiler/src/Processors.hs index a296215..aaa178f 100644 --- a/compiler/src/Processors.hs +++ b/compiler/src/Processors.hs @@ -73,7 +73,7 @@ type FileProcessor = copyFileProcessor :: FileProcessor copyFileProcessor inputPath outputPath = - (putStrLn $ "Copying: " ++ outputPath) + (putStrLn $ "Copying:\t" ++ outputPath) >> ensureParentDir (flip System.Directory.copyFile) outputPath inputPath eitherIOToIO :: Either String (IO a) -> IO a @@ -99,7 +99,7 @@ type StaticImageWriter = FilePath -> DynamicImage -> IO () resizeStaticGeneric :: StaticImageReader -> StaticImageWriter -> Resolution -> FileProcessor resizeStaticGeneric reader writer maxRes inputPath outputPath = - (putStrLn $ "Generating: " ++ outputPath) + (putStrLn $ "Generating:\t" ++ outputPath) >> reader inputPath >>= eitherResToIO >>= return . (fitDynamicImage maxRes) @@ -142,7 +142,7 @@ withCached processor inputPath outputPath = where noop = return () update = processor inputPath outputPath - skip = putStrLn $ "Skipping: " ++ outputPath + skip = putStrLn $ "Skipping:\t" ++ outputPath isOutdated :: FilePath -> FilePath -> IO Bool isOutdated ref target = diff --git a/compiler/src/Resource.hs b/compiler/src/Resource.hs index 83f7438..a8be913 100644 --- a/compiler/src/Resource.hs +++ b/compiler/src/Resource.hs @@ -28,13 +28,13 @@ module Resource , ItemProcessor , ThumbnailProcessor , buildResourceTree - , flattenResourceTree - , outputDiff + , cleanupResourceDir ) where import Data.Function ((&)) -import Data.List ((\\), subsequences) +import Data.List ((\\), subsequences, sortBy) +import Data.Ord (comparing) import Data.Maybe (mapMaybe) import Files import Input (InputTree(..), Sidecar) @@ -104,3 +104,11 @@ outputDiff resources ref = fsPaths :: FSNode -> [Path] fsPaths = map nodePath . tail . flattenDir + +cleanupResourceDir :: ResourceTree -> FileName -> IO () +cleanupResourceDir resourceTree outputDir = + readDirectory outputDir + >>= return . outputDiff resourceTree . root + >>= return . sortBy (flip $ comparing length) -- nested files before dirs + >>= return . map (localPath . (/>) outputDir) + >>= mapM_ remove -- cgit v1.2.3