-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stratify.hs
79 lines (68 loc) · 2.84 KB
/
Stratify.hs
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
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, DeriveDataTypeable #-}
module Main where
import Stratify.Internal
import Stratify.Html
import Stratify.Json
import Stratify.Types
import Text.Blaze.Renderer.String
import Data.Aeson
import System.Console.CmdLib
import System.IO
import System.FilePath.Posix
import System.Directory
import System.Directory.Tree
import System.Posix.Files
import Data.Map ((!))
renderJsonAsHtml :: String -> Either String String
renderJsonAsHtml input = case parseJsonFromString input of
Left msg -> Left $ "Unable to parse json: "++msg
Right value -> case parseDependenciesFromJson value of
Left msg -> Left $ "Unable to create dependency tree from json: "++msg
Right deps -> let
ndeps = normalize deps
stratified = stratify ndeps
metrics = computeMetrics ndeps
in Right $ renderHtml $ page ((map . map) (metrics !) stratified)
data Main = Main {
output :: String,
args :: [String],
input :: String,
template :: String,
develop :: Bool
}
deriving (Typeable, Data, Eq)
instance Attributes Main where
attributes _ = group "Options:" [
output %> [ Help "Directory to put the output. Will be created if it doesn't exist", ArgHelp "PATH", Default "output" ],
template %> [ Help "Template directory containing static files", ArgHelp "PATH", Default "static" ],
args %> [ Extra True ],
input %> [ Positional 0, Default "-" ],
develop %> [ Help "Link static directory, rather than copying" ]]
instance RecordCommand Main where
mode_summary _ = "Generate a webpage of stratified dependencies from an input file"
copyStaticDir :: FilePath -> FilePath -> FilePath -> IO ()
copyStaticDir templateDir outputDir name = do
sourceDir <- readDirectoryWithL readFile (joinPath [templateDir, name])
failedFiles <- writeDirectory $ outputDir :/ free sourceDir
return ()
copyStaticContent :: FilePath -> FilePath -> IO ()
copyStaticContent template output = do
createDirectoryIfMissing True output
copyStaticDir template output "js"
copyStaticDir template output "css"
linkStaticContent :: FilePath -> FilePath -> IO ()
linkStaticContent template output = do
template' <- canonicalizePath template
createSymbolicLink template' output
main = getArgs >>= executeR Main {} >>= \opts -> do
inputData <- if input opts == "-"
then getContents
else readFile $ input opts
page <- case renderJsonAsHtml inputData of
Left msg -> error msg
Right page -> return page
let outputPage = joinPath [output opts, "index.html"]
createDirectoryIfMissing True $ output opts
writeFile outputPage page
let distributeContent = if develop opts then linkStaticContent else copyStaticContent
distributeContent (template opts) (joinPath [output opts, "static"])