-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTransformation.hs
57 lines (51 loc) · 1.99 KB
/
Transformation.hs
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
{-# LANGUAGE CPP #-}
module Hemmet.FileTree.Transformation
( haskellify
, pythonify
) where
import Data.Char
#if MIN_VERSION_GLASGOW_HASKELL(9,0,0,0)
import Data.Text as T hiding (concatMap, elem, map)
#else
import Data.Text as T hiding (concatMap, map)
#endif
import Hemmet.Tree
import Hemmet.FileTree.Tree
haskellify :: Transformation FileTreePayload
haskellify File = File
haskellify (Directory nodes) = Directory $ concatMap process nodes
where
process (Node name File) = [Node (haskellifyFile name) File]
process (Node name@(T.uncons -> Just (flag, rest)) d@(Directory ns)) =
case flag of
'*' -> Node (haskellifyFile rest) File : process (Node rest d)
'!' -> [Node rest children]
_ -> [Node (camelCase name) children]
where
children = Directory $ concatMap process ns
process _ = error "Impossible!"
haskellifyFile = modifyFileWith [] ((<> ".hs") . camelCase)
camelCase = T.concat . map T.toTitle . split (== '-')
pythonify :: Transformation FileTreePayload
pythonify File = File
pythonify (Directory nodes) = Directory $ concatMap process nodes
where
process (Node name File) = [Node (pythonifyFile name) File]
process (Node name@(T.uncons -> Just (flag, rest)) (Directory ns)) =
case flag of
'*' -> [Node (snakeCase rest) . Directory
$ Node "__init__.py" File : concatMap process ns]
'!' -> [Node rest children]
_ -> [Node (snakeCase name) children]
where
children = Directory $ concatMap process ns
process _ = error "Impossible!"
pythonifyFile = modifyFileWith ['_'] ((<> ".py") . snakeCase)
snakeCase = T.intercalate "_" . split (== '-')
modifyFileWith :: [Char] -> (Text -> Text) -> Text -> Text
modifyFileWith cs f name@(T.uncons -> Just (first, rest))
| first == '!' = rest
| isAlphaNum first && T.all isGood name = f name
where
isGood c = isAlphaNum c || c == '-' || c `elem` cs
modifyFileWith _ _ name = name