diff options
author | pacien | 2019-12-26 01:13:42 +0100 |
---|---|---|
committer | pacien | 2019-12-26 01:13:42 +0100 |
commit | 2a6467272e18af4864745b9d0267f9fa3ed382dd (patch) | |
tree | a81672b9a0d7d9ad0577453c263cbd4d4d6f315c /compiler/src/Lib.hs | |
parent | 45163fbc93b2bf2f7cb1fc3242ce5d3f51076601 (diff) | |
download | ldgallery-2a6467272e18af4864745b9d0267f9fa3ed382dd.tar.gz |
compiler: implement output dir cleanup
Diffstat (limited to 'compiler/src/Lib.hs')
-rw-r--r-- | compiler/src/Lib.hs | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/compiler/src/Lib.hs b/compiler/src/Lib.hs index 2068b4a..643e5f6 100644 --- a/compiler/src/Lib.hs +++ b/compiler/src/Lib.hs | |||
@@ -26,15 +26,17 @@ module Lib | |||
26 | 26 | ||
27 | import GHC.Generics (Generic) | 27 | import GHC.Generics (Generic) |
28 | import Data.Function ((&)) | 28 | import Data.Function ((&)) |
29 | import System.Directory (createDirectoryIfMissing) | 29 | import Data.Ord (comparing) |
30 | import Data.List (sortBy, length) | ||
31 | import System.Directory (createDirectoryIfMissing, removePathForcibly) | ||
30 | import System.FilePath (dropFileName, (</>)) | 32 | import System.FilePath (dropFileName, (</>)) |
31 | 33 | ||
32 | import Data.Aeson (ToJSON, FromJSON) | 34 | import Data.Aeson (ToJSON, FromJSON) |
33 | import qualified Data.Aeson as JSON | 35 | import qualified Data.Aeson as JSON |
34 | 36 | ||
35 | import Files (FileName, readDirectory) | 37 | import Files (FileName, readDirectory, localPath, flattenDir, root, (/>)) |
36 | import Input (decodeYamlFile, readInputTree) | 38 | import Input (decodeYamlFile, readInputTree) |
37 | import Resource (buildResourceTree) | 39 | import Resource (ResourceTree, buildResourceTree, outputDiff) |
38 | import Gallery (buildGalleryTree) | 40 | import Gallery (buildGalleryTree) |
39 | 41 | ||
40 | 42 | ||
@@ -60,10 +62,6 @@ process inputDirPath outputDirPath = | |||
60 | putStrLn "\nINPUT DIR" | 62 | putStrLn "\nINPUT DIR" |
61 | putStrLn (show inputDir) | 63 | putStrLn (show inputDir) |
62 | 64 | ||
63 | outputDir <- readDirectory outputDirPath | ||
64 | putStrLn "\nOUTPUT DIR" | ||
65 | putStrLn (show outputDir) | ||
66 | |||
67 | inputTree <- readInputTree inputDir | 65 | inputTree <- readInputTree inputDir |
68 | putStrLn "\nINPUT TREE" | 66 | putStrLn "\nINPUT TREE" |
69 | putStrLn (show inputTree) | 67 | putStrLn (show inputTree) |
@@ -79,18 +77,26 @@ process inputDirPath outputDirPath = | |||
79 | -- (or recompile everything if the config file has changed!) | 77 | -- (or recompile everything if the config file has changed!) |
80 | -- execute in parallel | 78 | -- execute in parallel |
81 | 79 | ||
82 | -- TODO: clean up output dir by comparing its content with the resource tree | 80 | cleanup resourceTree outputDirPath |
83 | -- aggregate both trees as list | ||
84 | -- compute the difference | ||
85 | -- sort by deepest and erase files and dirs | ||
86 | 81 | ||
87 | -- TODO: execute (in parallel) the resource compilation strategy list | 82 | -- TODO: execute (in parallel) the resource compilation strategy list |
88 | -- need to find a good library for that | 83 | -- need to find a good library for that |
89 | 84 | ||
90 | buildGalleryTree resourceTree & writeJSON (outputDirPath </> "index.json") | 85 | buildGalleryTree resourceTree |
91 | writeJSON (outputDirPath </> "viewer.json") (viewer config) | 86 | & writeJSON (outputDirPath </> "index.json") |
87 | |||
88 | viewer config | ||
89 | & writeJSON (outputDirPath </> "viewer.json") | ||
92 | 90 | ||
93 | where | 91 | where |
92 | cleanup :: ResourceTree -> FileName -> IO () | ||
93 | cleanup resourceTree outputDir = | ||
94 | readDirectory outputDir | ||
95 | >>= return . outputDiff resourceTree . root | ||
96 | >>= return . sortBy (flip $ comparing length) -- nested files before dirs | ||
97 | >>= return . map (localPath . (/>) outputDir) | ||
98 | >>= mapM_ removePathForcibly | ||
99 | |||
94 | writeJSON :: ToJSON a => FileName -> a -> IO () | 100 | writeJSON :: ToJSON a => FileName -> a -> IO () |
95 | writeJSON path obj = | 101 | writeJSON path obj = |
96 | createDirectoryIfMissing True (dropFileName path) | 102 | createDirectoryIfMissing True (dropFileName path) |