Implement entry command
[xyz.git] / app / Main.hs
1 {-# LANGUAGE OverloadedStrings #-}
2
3 module Main where
4
5 import Lib
6 import System.Environment
7 import System.Exit
8 import System.Directory
9 import Prelude hiding (init)
10 import Data.List (intercalate)
11 import Text.JSON
12 import Text.JSON.Generic
13 import Data.Text hiding (intercalate, unlines, init, length)
14 import Data.Time.Clock.POSIX
15 import System.File.Tree hiding (mapM, mapM_)
16 import System.Directory (doesFileExist)
17
18 main :: IO ()
19 main = getArgs >>= parse
20
21 continueIfValidProject :: IO() -> IO ()
22 continueIfValidProject nextfn = do
23 jsonExists <- doesFileExist "xyz.json"
24 if jsonExists
25 then nextfn
26 else putStrLn "This does not appear to be a valid project directory"
27
28 init :: IO ()
29 init = do
30 dirExists <- doesFileExist "xyz.json"
31 if dirExists
32 then putStrLn "Project already initialised"
33 else do createDirectory "entries"
34 home <- getHomeDirectory
35 templates <- allFilesIn $ home ++ "/.xyz/themes"
36 putStrLn "Enter a name for your project:"
37 projectName <- getLine
38 putStrLn "Enter short description for your project:"
39 projectDescription <- getLine
40 putStrLn $ "Which theme would you like to use?\n\n" ++ (ununlines templates)
41 theme <- getLine
42 writeFile "xyz.json" . encode . toJSObject $ [("name", projectName), ("description", projectDescription), ("theme", theme)]
43 putStrLn "Initialised empty project"
44
45 build :: IO ()
46 build = do
47 fileExists <- doesFileExist "xyz.json"
48 if fileExists
49 then do
50 files <- allFilesIn "entries"
51 home <- getHomeDirectory
52 config <- readFile "xyz.json"
53 someFunc (decodeJSON config :: ProjectConfig) files >> putStrLn "Build successful"
54 else putStrLn "This does not appear to be a valid project directory"
55
56 ununlines :: [String] -> String
57 ununlines = intercalate "\n\n"
58
59 entry :: IO ()
60 entry = do
61 putStrLn "Enter a name for this entry:"
62 entryName <- getLine
63 entryNum <- fmap ((+1) . length) $ allFilesIn "entries"
64 stamp <- fmap round getPOSIXTime
65 let filename = "entries/" ++ (show entryNum) ++ "-" ++ (unpack . (toLower. replace " " "-") $ pack entryName) ++ ".txt"
66 writeFile filename (show stamp ++ "::" ++ entryName ++ "\n")
67 putStrLn $ "Created " ++ filename
68
69 usage :: IO ()
70 usage = putStr . ununlines $ [
71 "usage: xyz <command>",
72 "Commands:", unlines [
73 "\tinit\t\tInitialise a new site",
74 "\tbuild\t\tBuild the site",
75 "\tentry\t\tInitialise an entry"
76 ]
77 ]
78
79 parse ["init"] = init >> exit
80 parse ["build"] = continueIfValidProject build >> exit
81 parse ["entry"] = continueIfValidProject entry >> exit
82 parse [_] = usage >> exit
83 parse [] = usage >> exit
84
85 exit = exitWith ExitSuccess