Learn to initialise projects with configurable themes
[xyz.git] / app / Main.hs
1 module Main where
2
3 import Lib
4 import System.Environment
5 import System.Exit
6 import System.Directory
7 import Prelude hiding (init)
8 import Data.List (intercalate)
9 import Text.JSON
10 import Text.JSON.Generic
11 import Data.Text hiding (intercalate, unlines, init)
12 import System.File.Tree hiding (mapM, mapM_)
13 import System.Directory (doesFileExist)
14
15 main :: IO ()
16 main = getArgs >>= parse
17
18 init :: IO ()
19 init = do
20 dirExists <- doesFileExist "xyz.json"
21 if dirExists
22 then putStrLn "Project already initialised"
23 else do createDirectory "entries"
24 home <- getHomeDirectory
25 templates <- allFilesIn $ home ++ "/.xyz/themes"
26 putStrLn "Enter a name for your project:"
27 projectName <- getLine
28 putStrLn "Enter short description for your project:"
29 projectDescription <- getLine
30 putStrLn $ "Which theme would you like to use?\n\n" ++ (ununlines templates)
31 theme <- getLine
32 writeFile "xyz.json" . encode . toJSObject $ [("name", projectName), ("description", projectDescription), ("theme", theme)]
33 putStrLn "Initialised empty project"
34
35 build :: IO ()
36 build = do
37 fileExists <- doesFileExist "xyz.json"
38 if fileExists
39 then do
40 files <- allFilesIn "entries"
41 home <- getHomeDirectory
42 config <- readFile "xyz.json"
43 someFunc (decodeJSON config :: ProjectConfig) files >> putStrLn "Build successful"
44 else putStrLn "This does not appear to be a valid project directory"
45
46 ununlines :: [String] -> String
47 ununlines = intercalate "\n\n"
48
49 usage :: IO ()
50 usage = putStr . ununlines $ [
51 "usage: xyz <command>",
52 "Commands:", unlines [
53 "\tinit\t\tInitialise a new site",
54 "\tbuild\t\tBuild the site"
55 ]
56 ]
57
58 parse ["init"] = init >> exit
59 parse ["build"] = build >> exit
60 parse [_] = usage >> exit
61 parse [] = usage >> exit
62
63 exit = exitWith ExitSuccess