aboutsummaryrefslogtreecommitdiff
path: root/compiler/app/Main.hs
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/app/Main.hs')
-rw-r--r--compiler/app/Main.hs49
1 files changed, 24 insertions, 25 deletions
diff --git a/compiler/app/Main.hs b/compiler/app/Main.hs
index 48e5644..3e6f254 100644
--- a/compiler/app/Main.hs
+++ b/compiler/app/Main.hs
@@ -1,7 +1,7 @@
1-- ldgallery - A static generator which turns a collection of tagged 1-- ldgallery - A static generator which turns a collection of tagged
2-- pictures into a searchable web gallery. 2-- pictures into a searchable web gallery.
3-- 3--
4-- Copyright (C) 2019-2020 Pacien TRAN-GIRARD 4-- Copyright (C) 2019-2021 Pacien TRAN-GIRARD
5-- 5--
6-- This program is free software: you can redistribute it and/or modify 6-- This program is free software: you can redistribute it and/or modify
7-- it under the terms of the GNU Affero General Public License as 7-- it under the terms of the GNU Affero General Public License as
@@ -19,8 +19,9 @@
19module Main where 19module Main where
20 20
21import GHC.Generics (Generic) 21import GHC.Generics (Generic)
22import Paths_ldgallery_compiler (version, getDataFileName) 22import Paths_ldgallery_compiler (version)
23import Control.Monad (when) 23import Control.Monad (when)
24import Data.Functor ((<&>))
24import Data.Maybe (isJust) 25import Data.Maybe (isJust)
25import Data.Version (showVersion) 26import Data.Version (showVersion)
26import Data.Aeson (ToJSON) 27import Data.Aeson (ToJSON)
@@ -30,9 +31,9 @@ import System.Console.CmdArgs
30 31
31import Compiler 32import Compiler
32import Files (readDirectory, copyTo, remove) 33import Files (readDirectory, copyTo, remove)
34import ViewerDist (viewerDistPath)
33 35
34 36newtype ViewerConfig = ViewerConfig
35data ViewerConfig = ViewerConfig
36 { galleryRoot :: String 37 { galleryRoot :: String
37 } deriving (Generic, Show, ToJSON) 38 } deriving (Generic, Show, ToJSON)
38 39
@@ -42,7 +43,7 @@ data Options = Options
42 , outputDir :: FilePath 43 , outputDir :: FilePath
43 , outputIndex :: FilePath 44 , outputIndex :: FilePath
44 , galleryConfig :: FilePath 45 , galleryConfig :: FilePath
45 , rebuilAll :: Bool 46 , rebuildAll :: Bool
46 , cleanOutput :: Bool 47 , cleanOutput :: Bool
47 , withViewer :: Maybe FilePath 48 , withViewer :: Maybe FilePath
48 } deriving (Show, Data, Typeable) 49 } deriving (Show, Data, Typeable)
@@ -73,7 +74,7 @@ options = Options
73 &= name "gallery-config" 74 &= name "gallery-config"
74 &= explicit 75 &= explicit
75 &= help "Gallery configuration file (default=<input-dir>/gallery.yaml)" 76 &= help "Gallery configuration file (default=<input-dir>/gallery.yaml)"
76 , rebuilAll = False 77 , rebuildAll = False
77 &= name "r" 78 &= name "r"
78 &= name "rebuild-all" 79 &= name "rebuild-all"
79 &= explicit 80 &= explicit
@@ -92,7 +93,8 @@ options = Options
92 &= help "Deploy either the bundled or the given static web viewer to the output directory" 93 &= help "Deploy either the bundled or the given static web viewer to the output directory"
93 } 94 }
94 95
95 &= summary ("ldgallery v" ++ (showVersion version) ++ " - a static web gallery generator with tags") 96 &= summary ("ldgallery v" ++ showVersion version ++ " - a static web gallery generator with tags")
97 &= details ["This software is distributed under the terms of the GNU Affero General Public License v3.0."]
96 &= program "ldgallery" 98 &= program "ldgallery"
97 &= help "Compile a gallery" 99 &= help "Compile a gallery"
98 &= helpArg [explicit, name "h", name "help"] 100 &= helpArg [explicit, name "h", name "help"]
@@ -104,10 +106,7 @@ main =
104 do 106 do
105 opts <- cmdArgs options 107 opts <- cmdArgs options
106 buildGallery opts 108 buildGallery opts
107 109 deployViewer opts
108 when (isJust $ withViewer opts) $ do
109 viewerDist <- viewerDistPath $ withViewer opts
110 deployViewer viewerDist opts
111 110
112 where 111 where
113 gallerySubdir :: String 112 gallerySubdir :: String
@@ -116,11 +115,6 @@ main =
116 viewerConfig :: ViewerConfig 115 viewerConfig :: ViewerConfig
117 viewerConfig = ViewerConfig (gallerySubdir ++ "/") 116 viewerConfig = ViewerConfig (gallerySubdir ++ "/")
118 117
119 viewerDistPath :: Maybe FilePath -> IO FilePath
120 viewerDistPath (Just "") = getDataFileName "viewer"
121 viewerDistPath (Just dist) = return dist
122 viewerDistPath Nothing = fail "No viewer distribution"
123
124 buildGallery :: Options -> IO () 118 buildGallery :: Options -> IO ()
125 buildGallery opts = 119 buildGallery opts =
126 checkDistinctPaths (inputDir opts) (outputDir opts) 120 checkDistinctPaths (inputDir opts) (outputDir opts)
@@ -130,7 +124,7 @@ main =
130 (galleryOutputDir opts) 124 (galleryOutputDir opts)
131 (outputIndex opts) 125 (outputIndex opts)
132 [outputDir opts] 126 [outputDir opts]
133 (rebuilAll opts) 127 (rebuildAll opts)
134 (cleanOutput opts) 128 (cleanOutput opts)
135 where 129 where
136 checkDistinctPaths :: FilePath -> FilePath -> IO () 130 checkDistinctPaths :: FilePath -> FilePath -> IO ()
@@ -144,21 +138,26 @@ main =
144 | isJust withViewer = outputDir </> gallerySubdir 138 | isJust withViewer = outputDir </> gallerySubdir
145 | otherwise = outputDir 139 | otherwise = outputDir
146 140
147 deployViewer :: FilePath -> Options -> IO () 141 deployViewer :: Options -> IO ()
148 deployViewer distPath Options{outputDir, cleanOutput} = 142 deployViewer Options{withViewer = Nothing} = pure ()
149 (when cleanOutput $ cleanViewerDir outputDir) 143 deployViewer Options{withViewer = Just viewerPath, outputDir, cleanOutput} =
150 >> copyViewer distPath outputDir 144 when cleanOutput (cleanViewerDir outputDir)
145 >> viewerDistOr viewerPath >>= deployTo outputDir
151 >> writeJSON (outputDir </> "config.json") viewerConfig 146 >> writeJSON (outputDir </> "config.json") viewerConfig
152 147
153 where 148 where
154 cleanViewerDir :: FilePath -> IO () 149 cleanViewerDir :: FilePath -> IO ()
155 cleanViewerDir target = 150 cleanViewerDir target =
156 listDirectory target 151 listDirectory target
157 >>= return . filter (/= gallerySubdir) 152 <&> filter (/= gallerySubdir)
158 >>= mapM_ remove . map (target </>) 153 >>= mapM_ (remove . (target </>))
154
155 viewerDistOr :: FilePath -> IO FilePath
156 viewerDistOr "" = viewerDistPath
157 viewerDistOr custom = pure custom
159 158
160 copyViewer :: FilePath -> FilePath -> IO () 159 deployTo :: FilePath -> FilePath -> IO ()
161 copyViewer dist target = 160 deployTo target dist =
162 putStrLn "Copying viewer webapp" 161 putStrLn "Copying viewer webapp"
163 >> readDirectory dist 162 >> readDirectory dist
164 >>= copyTo target 163 >>= copyTo target