1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
-- ldgallery - A static generator which turns a collection of tagged
-- pictures into a searchable web gallery.
--
-- Copyright (C) 2019-2021 Pacien TRAN-GIRARD
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as
-- published by the Free Software Foundation, either version 3 of the
-- License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
module Main where
import GHC.Generics (Generic)
import Paths_ldgallery_compiler (version)
import Control.Monad (when)
import Data.Functor ((<&>))
import Data.Maybe (isJust)
import Data.Version (showVersion)
import Data.Aeson (ToJSON)
import System.FilePath ((</>))
import System.Directory (canonicalizePath, listDirectory)
import System.Console.CmdArgs
import Compiler
import Files (readDirectory, copyTo, remove)
import ViewerDist (viewerDistPath)
newtype ViewerConfig = ViewerConfig
{ galleryRoot :: String
} deriving (Generic, Show, ToJSON)
data Options = Options
{ inputDir :: FilePath
, outputDir :: FilePath
, outputIndex :: FilePath
, galleryConfig :: FilePath
, rebuildAll :: Bool
, cleanOutput :: Bool
, withViewer :: Maybe FilePath
} deriving (Show, Data, Typeable)
options :: Options
options = Options
{ inputDir = "./"
&= typDir
&= name "i"
&= name "input-dir"
&= explicit
&= help "Gallery source directory (default=./)"
, outputDir = "./out"
&= typDir
&= name "o"
&= name "output-dir"
&= explicit
&= help "Generated gallery output path (default=./out)"
, outputIndex = ""
&= typFile
&= name "x"
&= name "output-index"
&= explicit
&= help "Generated gallery index output path (default=<output-dir>/index.json)"
, galleryConfig = ""
&= typFile
&= name "g"
&= name "gallery-config"
&= explicit
&= help "Gallery configuration file (default=<input-dir>/gallery.yaml)"
, rebuildAll = False
&= name "r"
&= name "rebuild-all"
&= explicit
&= help "Invalidate cache and recompile everything"
, cleanOutput = False
&= name "c"
&= name "clean-output"
&= explicit
&= help "Remove unnecessary files from the output directory"
, withViewer = Nothing
&= typDir
&= opt ("" :: FilePath)
&= name "w"
&= name "with-viewer"
&= explicit
&= help "Deploy either the bundled or the given static web viewer to the output directory"
}
&= summary ("ldgallery v" ++ showVersion version ++ " - a static web gallery generator with tags")
&= details ["This software is distributed under the terms of the GNU Affero General Public License v3.0."]
&= program "ldgallery"
&= help "Compile a gallery"
&= helpArg [explicit, name "h", name "help"]
&= versionArg [explicit, name "version"]
main :: IO ()
main =
do
opts <- cmdArgs options
buildGallery opts
deployViewer opts
where
gallerySubdir :: String
gallerySubdir = "gallery"
viewerConfig :: ViewerConfig
viewerConfig = ViewerConfig (gallerySubdir ++ "/")
buildGallery :: Options -> IO ()
buildGallery opts =
checkDistinctPaths (inputDir opts) (outputDir opts)
>> compileGallery
(galleryConfig opts)
(inputDir opts)
(galleryOutputDir opts)
(outputIndex opts)
[outputDir opts]
(rebuildAll opts)
(cleanOutput opts)
where
checkDistinctPaths :: FilePath -> FilePath -> IO ()
checkDistinctPaths a b = do
canonicalA <- canonicalizePath a
canonicalB <- canonicalizePath b
when (canonicalA == canonicalB) $ error "Input and output paths refer to the same location."
galleryOutputDir :: Options -> FilePath
galleryOutputDir Options{withViewer, outputDir}
| isJust withViewer = outputDir </> gallerySubdir
| otherwise = outputDir
deployViewer :: Options -> IO ()
deployViewer Options{withViewer = Nothing} = pure ()
deployViewer Options{withViewer = Just viewerPath, outputDir, cleanOutput} =
when cleanOutput (cleanViewerDir outputDir)
>> viewerDistOr viewerPath >>= deployTo outputDir
>> writeJSON (outputDir </> "config.json") viewerConfig
where
cleanViewerDir :: FilePath -> IO ()
cleanViewerDir target =
listDirectory target
<&> filter (/= gallerySubdir)
>>= mapM_ (remove . (target </>))
viewerDistOr :: FilePath -> IO FilePath
viewerDistOr "" = viewerDistPath
viewerDistOr custom = pure custom
deployTo :: FilePath -> FilePath -> IO ()
deployTo target dist =
putStrLn "Copying viewer webapp"
>> readDirectory dist
>>= copyTo target
|