diff options
Diffstat (limited to 'compiler/app/Main.hs')
-rw-r--r-- | compiler/app/Main.hs | 117 |
1 files changed, 95 insertions, 22 deletions
diff --git a/compiler/app/Main.hs b/compiler/app/Main.hs index 1a42abf..48e5644 100644 --- a/compiler/app/Main.hs +++ b/compiler/app/Main.hs | |||
@@ -18,20 +18,33 @@ | |||
18 | 18 | ||
19 | module Main where | 19 | module Main where |
20 | 20 | ||
21 | import GHC.Generics (Generic) | ||
21 | import Paths_ldgallery_compiler (version, getDataFileName) | 22 | import Paths_ldgallery_compiler (version, getDataFileName) |
23 | import Control.Monad (when) | ||
24 | import Data.Maybe (isJust) | ||
22 | import Data.Version (showVersion) | 25 | import Data.Version (showVersion) |
26 | import Data.Aeson (ToJSON) | ||
23 | import System.FilePath ((</>)) | 27 | import System.FilePath ((</>)) |
28 | import System.Directory (canonicalizePath, listDirectory) | ||
24 | import System.Console.CmdArgs | 29 | import System.Console.CmdArgs |
25 | 30 | ||
26 | import Compiler | 31 | import Compiler |
27 | import Files (readDirectory, copyTo) | 32 | import Files (readDirectory, copyTo, remove) |
33 | |||
34 | |||
35 | data ViewerConfig = ViewerConfig | ||
36 | { galleryRoot :: String | ||
37 | } deriving (Generic, Show, ToJSON) | ||
28 | 38 | ||
29 | 39 | ||
30 | data Options = Options | 40 | data Options = Options |
31 | { inputDir :: String | 41 | { inputDir :: FilePath |
32 | , outputDir :: String | 42 | , outputDir :: FilePath |
43 | , outputIndex :: FilePath | ||
44 | , galleryConfig :: FilePath | ||
33 | , rebuilAll :: Bool | 45 | , rebuilAll :: Bool |
34 | , withViewer :: Bool | 46 | , cleanOutput :: Bool |
47 | , withViewer :: Maybe FilePath | ||
35 | } deriving (Show, Data, Typeable) | 48 | } deriving (Show, Data, Typeable) |
36 | 49 | ||
37 | options :: Options | 50 | options :: Options |
@@ -48,16 +61,35 @@ options = Options | |||
48 | &= name "output-dir" | 61 | &= name "output-dir" |
49 | &= explicit | 62 | &= explicit |
50 | &= help "Generated gallery output path (default=./out)" | 63 | &= help "Generated gallery output path (default=./out)" |
64 | , outputIndex = "" | ||
65 | &= typFile | ||
66 | &= name "x" | ||
67 | &= name "output-index" | ||
68 | &= explicit | ||
69 | &= help "Generated gallery index output path (default=<output-dir>/index.json)" | ||
70 | , galleryConfig = "" | ||
71 | &= typFile | ||
72 | &= name "g" | ||
73 | &= name "gallery-config" | ||
74 | &= explicit | ||
75 | &= help "Gallery configuration file (default=<input-dir>/gallery.yaml)" | ||
51 | , rebuilAll = False | 76 | , rebuilAll = False |
52 | &= name "r" | 77 | &= name "r" |
53 | &= name "rebuild-all" | 78 | &= name "rebuild-all" |
54 | &= explicit | 79 | &= explicit |
55 | &= help "Invalidate cache and recompile everything" | 80 | &= help "Invalidate cache and recompile everything" |
56 | , withViewer = False | 81 | , cleanOutput = False |
82 | &= name "c" | ||
83 | &= name "clean-output" | ||
84 | &= explicit | ||
85 | &= help "Remove unnecessary files from the output directory" | ||
86 | , withViewer = Nothing | ||
87 | &= typDir | ||
88 | &= opt ("" :: FilePath) | ||
57 | &= name "w" | 89 | &= name "w" |
58 | &= name "with-viewer" | 90 | &= name "with-viewer" |
59 | &= explicit | 91 | &= explicit |
60 | &= help "Include the static web viewer in the output" | 92 | &= help "Deploy either the bundled or the given static web viewer to the output directory" |
61 | } | 93 | } |
62 | 94 | ||
63 | &= summary ("ldgallery v" ++ (showVersion version) ++ " - a static web gallery generator with tags") | 95 | &= summary ("ldgallery v" ++ (showVersion version) ++ " - a static web gallery generator with tags") |
@@ -71,21 +103,62 @@ main :: IO () | |||
71 | main = | 103 | main = |
72 | do | 104 | do |
73 | opts <- cmdArgs options | 105 | opts <- cmdArgs options |
74 | compileGallery (inputDir opts) (galleryOutputDir "gallery" opts) (rebuilAll opts) | 106 | buildGallery opts |
75 | if (withViewer opts) then copyViewer (outputDir opts) else noop | 107 | |
108 | when (isJust $ withViewer opts) $ do | ||
109 | viewerDist <- viewerDistPath $ withViewer opts | ||
110 | deployViewer viewerDist opts | ||
76 | 111 | ||
77 | where | 112 | where |
78 | galleryOutputDir :: FilePath -> Options -> FilePath | 113 | gallerySubdir :: String |
79 | galleryOutputDir gallerySubdir opts = | 114 | gallerySubdir = "gallery" |
80 | if withViewer opts then outputBase </> gallerySubdir else outputBase | 115 | |
81 | where outputBase = outputDir opts | 116 | viewerConfig :: ViewerConfig |
82 | 117 | viewerConfig = ViewerConfig (gallerySubdir ++ "/") | |
83 | copyViewer :: FilePath -> IO () | 118 | |
84 | copyViewer target = | 119 | viewerDistPath :: Maybe FilePath -> IO FilePath |
85 | putStrLn "Copying viewer webapp" | 120 | viewerDistPath (Just "") = getDataFileName "viewer" |
86 | >> getDataFileName "viewer" | 121 | viewerDistPath (Just dist) = return dist |
87 | >>= readDirectory | 122 | viewerDistPath Nothing = fail "No viewer distribution" |
88 | >>= copyTo target | 123 | |
89 | 124 | buildGallery :: Options -> IO () | |
90 | noop :: IO () | 125 | buildGallery opts = |
91 | noop = return () | 126 | checkDistinctPaths (inputDir opts) (outputDir opts) |
127 | >> compileGallery | ||
128 | (galleryConfig opts) | ||
129 | (inputDir opts) | ||
130 | (galleryOutputDir opts) | ||
131 | (outputIndex opts) | ||
132 | [outputDir opts] | ||
133 | (rebuilAll opts) | ||
134 | (cleanOutput opts) | ||
135 | where | ||
136 | checkDistinctPaths :: FilePath -> FilePath -> IO () | ||
137 | checkDistinctPaths a b = do | ||
138 | canonicalA <- canonicalizePath a | ||
139 | canonicalB <- canonicalizePath b | ||
140 | when (canonicalA == canonicalB) $ error "Input and output paths refer to the same location." | ||
141 | |||
142 | galleryOutputDir :: Options -> FilePath | ||
143 | galleryOutputDir Options{withViewer, outputDir} | ||
144 | | isJust withViewer = outputDir </> gallerySubdir | ||
145 | | otherwise = outputDir | ||
146 | |||
147 | deployViewer :: FilePath -> Options -> IO () | ||
148 | deployViewer distPath Options{outputDir, cleanOutput} = | ||
149 | (when cleanOutput $ cleanViewerDir outputDir) | ||
150 | >> copyViewer distPath outputDir | ||
151 | >> writeJSON (outputDir </> "config.json") viewerConfig | ||
152 | |||
153 | where | ||
154 | cleanViewerDir :: FilePath -> IO () | ||
155 | cleanViewerDir target = | ||
156 | listDirectory target | ||
157 | >>= return . filter (/= gallerySubdir) | ||
158 | >>= mapM_ remove . map (target </>) | ||
159 | |||
160 | copyViewer :: FilePath -> FilePath -> IO () | ||
161 | copyViewer dist target = | ||
162 | putStrLn "Copying viewer webapp" | ||
163 | >> readDirectory dist | ||
164 | >>= copyTo target | ||