-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
136 lines (129 loc) · 4.35 KB
/
Main.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ViewPatterns #-}
module Main where
import Data.Text (Text)
import qualified Data.Text as T
import Development.Shake hiding ((~>))
import Development.Shake.Config
import Development.Shake.FilePath
import Paths_plugin_scaffold (getDataDir)
import Text.Mustache
main :: IO ()
main = shakeArgs shakeOptions $ do
usingConfigFile "build.cfg"
fileRule
want $
("out" </>)
<$> [
-- gradle wrapper
"gradle/wrapper/gradle-wrapper.jar",
"gradle/wrapper/gradle-wrapper.properties",
"gradlew",
"gradlew.bat",
-- project gradle
"gradle.properties",
"build.gradle.kts",
"settings.gradle.kts",
-- project gitignore
".gitignore",
"README.md",
-- nix
"flake.nix",
"shell.nix",
-- app gradle
"app/build.gradle.kts",
-- app gitignore
"app/.gitignore",
-- app manifest
"app/src/main/AndroidManifest.xml",
-- app res
"app/src/main/res/values/strings.xml",
"app/src/main/res/xml/plugin.xml",
-- app assets
"app/src/main/assets/README.md"
]
phony "clean" $ removeFilesAfter "out" ["//*"]
fileRule :: Rules ()
fileRule =
"out//*" %> \out -> do
dataDir <- liftIO getDataDir
let fp = dropDirectory1 out
cfg <- getProjectConfig
let templateFile = dataDir </> "templates" </> fp <.> "mustache"
hasTemplate <- doesFileExist templateFile
if hasTemplate
then
readFile' templateFile >>= \content -> case compileTemplate templateFile (T.pack content) of
Right template -> case checkedSubstitute template cfg of
([], result) -> writeFile' out (T.unpack result)
(err, _) -> fail $ "Failed to substitute template: " <> show err
Left err -> fail $ "Failed to compile template: " <> show err
else copyFile' (dataDir </> "fixtures" </> fp) out
getProjectConfig :: Action ProjectConfig
getProjectConfig =
ProjectConfig
<$> getConfig' "project_name"
<*> getConfig' "aboutlibraries_version"
<*> getConfig' "main_version"
<*> getConfig' "desugarJDKLibs_version"
<*> getConfig' "kotlin_version"
<*> getConfig' "android_version"
<*> getConfig' "build_version_name"
<*> (read . T.unpack <$> getConfig' "version_code")
<*> getConfig' "build_commit_hash"
<*> getConfig' "package_name"
<*> getConfig' "app_name_debug"
<*> getConfig' "app_name_release"
<*> getConfig' "plugin_description"
<*> getConfig' "plugin_api_version"
<*> ( getConfig "plugin_domain" >>= \case
Just (T.pack -> x) | not $ T.null x -> pure $ pure x
_ -> pure mempty
)
where
getConfig' key =
getConfig key >>= \case
Just x -> pure $ T.pack x
_ -> fail $ "Missing " <> key <> " in config"
data ProjectConfig = ProjectConfig
{ projectName :: Text,
aboutlibrariesVersion :: Text,
mainVersion :: Text,
desugarJDKLibsVersion :: Text,
kotlinVersion :: Text,
androidVersion :: Text,
buildVersionName :: Text,
versionCode :: Int,
buildCommitHash :: Text,
packageName :: Text,
appNameDebug :: Text,
appNameRelease :: Text,
pluginDescription :: Text,
pluginAPIVersion :: Text,
pluginDomain :: Maybe Text
}
deriving (Show, Eq)
instance ToMustache ProjectConfig where
toMustache ProjectConfig {..} =
object $
[ "project_name" ~> projectName,
"aboutlibraries_version" ~> aboutlibrariesVersion,
"main_version" ~> mainVersion,
"desugarJDKLibs_version" ~> desugarJDKLibsVersion,
"kotlin_version" ~> kotlinVersion,
"android_version" ~> androidVersion,
"build_version_name" ~> buildVersionName,
"version_code" ~> versionCode,
"build_commit_hash" ~> buildCommitHash,
"package_name" ~> packageName,
"app_name_debug" ~> appNameDebug,
"app_name_release" ~> appNameRelease,
"plugin_description" ~> pluginDescription,
"plugin_api_version" ~> pluginAPIVersion
]
<> maybe
["has_domain" ~> False]
(\domain -> ["has_domain" ~> True, "plugin_domain" ~> domain])
pluginDomain