]> glassweightruler.freedombox.rocks Git - xdg-ninja.git/blob - lib/Checks.hs
Create ./programs/ directory if missing
[xdg-ninja.git] / lib / Checks.hs
1 {-# LANGUAGE OverloadedStrings #-}
2
3 module Checks where
4
5 import Data.List (isSuffixOf)
6 import qualified Data.Text as T
7 import Data.Text.ANSI
8 import qualified Data.Text.Lazy as TL
9 import Output
10 import Program
11 import System.Directory.Extra
12 import System.Environment (getEnv)
13 import System.FilePath
14 import Text.Replace
15
16 data Result = Exist | MustExist | NoExist
17 deriving Show
18
19 expandPath :: String -> IO String
20 expandPath path = do
21 home <- getEnv "HOME"
22 let replacements = [ Replace "$HOME" (T.pack home) ]
23 let result = replaceWithList replacements (TL.pack path)
24 return (TL.unpack result)
25
26
27 checkFile :: T.Text -> Bool -> File -> IO Result
28 checkFile programName verbose file = do
29 path <- expandPath (path file)
30 existsFile <- doesFileExist path
31 existsDir <- doesDirectoryExist path
32 case (existsFile || existsDir) of
33 False -> case verbose of
34 False -> return NoExist
35 True -> do
36 logFile programName file False
37 return NoExist
38 True -> do
39 logFile programName file True
40 case (supportLevel file) of
41 Unsupported -> return MustExist
42 _ -> return Exist
43
44 checkProgram :: Bool -> Program -> IO [Result]
45 checkProgram verbose program = sequence (map (checkFile (name program) verbose) (files program))
46
47 checkProgramFile :: String -> IO [Result]
48 checkProgramFile filename = do
49 x <- readProgram filename
50 case x of
51 Just program -> do
52 results <- checkProgram False program
53 return results
54 Nothing -> do
55 putStrLn (T.unpack (red (T.pack "Error.")))
56 return []
57
58 checkDir :: FilePath -> IO ()
59 checkDir dirname = do
60 files <- getDirectoryContents dirname
61 jsonFiles <- return ( map (\x -> dirname </> x) (filter (isSuffixOf ".json") files ))
62 results <- sequence (map checkProgramFile jsonFiles)
63 return ()