1 {-# LANGUAGE OverloadedStrings #-}
6 import System.Environment
8 import System.Directory
9 import Prelude hiding (init)
10 import Data.List (intercalate)
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)
19 main = getArgs >>= parse
21 continueIfValidProject :: IO() -> IO ()
22 continueIfValidProject nextfn = do
23 jsonExists <- doesFileExist "xyz.json"
26 else putStrLn "This does not appear to be a valid project directory"
30 dirExists <- doesFileExist "xyz.json"
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)
42 writeFile "xyz.json" . encode . toJSObject $ [("name", projectName), ("description", projectDescription), ("theme", theme)]
43 putStrLn "Initialised empty project"
47 fileExists <- doesFileExist "xyz.json"
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"
56 ununlines :: [String] -> String
57 ununlines = intercalate "\n\n"
61 putStrLn "Enter a name for this entry:"
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
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"
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
85 exit = exitWith ExitSuccess