Repository navigation
Expand file tree
/
Copy pathrender.hs
More file actions
executable file
·73 lines (63 loc) · 1.71 KB
/
Copy pathrender.hs
File metadata and controls
executable file
·73 lines (63 loc) · 1.71 KB
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
#!/usr/bin/env stack
{- stack script
--resolver lts-24.34
--install-ghc
--package bytestring
--package directory
--package filepath
--package utf8-string
--profile
--ghc-options -Wall
-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE CPP #-}
-- base
import Control.Monad
import Data.List (sort)
import GHC.Stack
-- bytestring
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.UTF8 as BS
-- directory
import System.Directory
-- filepath
import System.FilePath
-- | Like 'listDirectory', but recursive
listFilesRec ::
HasCallStack =>
FilePath ->
IO [FilePath]
listFilesRec dir = do
dirContents <- listDirectory dir
fmap concat $ forM dirContents $ \p -> do
isDir <- doesDirectoryExist (dir </> p)
if isDir then do
subfiles <- listFilesRec (dir </> p)
pure (map (p </>) subfiles)
else
pure [p]
-- | Read project directory and gather files into a '.hsfiles' file
mkStackTemplate ::
HasCallStack =>
-- | Directory in projects/
FilePath ->
IO ByteString
mkStackTemplate projectDir = do
contents <- sort <$> listFilesRec projectDir
fmap mconcat $ forM contents $ \p -> do
pContents <- BS.readFile (projectDir </> p)
pure $
"{-# START_FILE " <> BS.fromString p <> " #-}"
<> "\n" <> pContents <> "\n"
-- | Read files in project/ and collect them into .hsfiles
main :: HasCallStack => IO ()
main = do
let here = dropFileName __FILE__
setCurrentDirectory here
let projectsDir = here </> "projects"
projects <- listDirectory projectsDir
void $ forM projects $ \p -> do
hsfiles <- mkStackTemplate (projectsDir </> p)
putStrLn $ p <> ".hsfiles"
BS.writeFile (here </> p <> ".hsfiles") hsfiles