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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE NoImplicitPrelude #-}
{- | A general purpose build tool.
Not all of the below design is implemented. Currently:
- with a nix build, results are linked in _/bild/nix/<target>
- for a dev build, results are stored in _/bild/dev/<target>
-----------------------------------------------------------------------------
== Design constraints
* only input is a namespace, no subcommands, no packages
* no need to write specific build rules
* one rule for hs, one for rs, one for scm, and so on
* no need to distinguish between exe and lib, just have a single output
* never concerned with deployment/packaging - leave that to another tool
(scp? tar?)
== Features
* namespace maps to filesystem
* no need for `bild -l` for listing available targets. Use `ls` or `tree`
* you build namespaces, not files/modules/packages/etc
* namespace maps to language modules
* build settings can be set in the file comments
* pwd is always considered the the source directory, no `src` vs `doc` etc.
* build methods automaticatly detected with file extensions
* flags modify the way to interact with the build, some ideas:
* -s = jump into a shell and/or repl
* -p = turn on profiling
* -t = limit build by type (file extension)
* -e = exclude some regex in the ns tree
* -o = optimize level
== Example Commands
> bild [-spt] <target..>
The general scheme is to build the things described by the targets. A target
is a namespace. You can list as many as you want, but you must list at least
one. It could just be `.` for the current directory. Build outputs will go
into the _/bild directory in the root of the project.
> bild a.b
Or `bild a/b`. This shows building a file at ./a/b.hs, this will translate to
something like `ghc --make A.B`.
> bild -s <target>
Starts a repl/shell for target.
- if target.hs, load ghci
- if target.scm, load scheme repl
- if target.clj, load a clojure repl
- if target.nix, load nix-shell
- and so on.
> bild -p <target>
build target with profiling (if available)
> bild -t nix target
only build target.nix, not target.hs and so on (in the case of multiple
targets with the same name but different extension).
== Build Metadata
Metadata is set in the comments with a special syntax. For third-party deps,
we list the deps in comments in the target file, like:
> -- : dep aeson
The output executable is named with:
> -- : exe my-program
or
> -- : exe my-ap.js
When multiple compilers are possible (e.g. ghc vs ghcjs) we chose ghcjs when
the target exe ends in .js.
This method of setting metadata in the module comments works pretty well,
and really only needs to be done in the entrypoint module anyway.
Local module deps are included by just giving the repo root to the compiler
that bild calls out to.
== Questions
* how to handle multiple output formats?
* e.g. that ghcjs and ghc take the same input files...
* say you have a .md file, you want to bild it to pdf, html, and more. What
do?
-}
module Biz.Bild where
import Alpha hiding ((<.>), sym)
import qualified Data.Char as Char
import qualified Data.List as List
import qualified Data.Text as Text
import qualified System.Directory as Dir
import qualified System.Environment as Env
import qualified System.Exit as Exit
import System.FilePath ((</>))
import qualified System.Process as Process
import qualified Text.Regex.Applicative as Regex
import qualified Prelude
main :: IO ()
main = Env.getArgs /> head >>= \case
Nothing -> Exit.die "usage: bild <target>"
Just target -> analyze target >>= build
type Namespace = String
type Dep = String
type Exe = String
data Compiler = Ghc | Ghcjs | Nix
deriving (Show)
data Target
= Target
{ -- | Output executable name
exe :: Exe,
-- | Fully qualified namespace partitioned by '.'
namespace :: Namespace,
-- | Absolute path to file
path :: FilePath,
-- | Parsed/detected dependencies
deps :: [Dep],
-- | Which compiler should we use?
compiler :: Compiler
}
deriving (Show)
analyze :: String -> IO Target
analyze s = do
root <- Env.getEnv "BIZ_ROOT"
cwd <- Dir.getCurrentDirectory
-- this is a hack to support multiple file types. Ideally we would just detect
-- which file extensions exist, then return [Target], which can then be built
-- in parallel
let path = cwd </> reps "." "/" s |> reps "/hs" ".hs" |> reps "/nix" ".nix"
content <- lines </ Prelude.readFile path
let exe = content /> Regex.match metaExe |> catMaybes |> head |> require "exe"
return
Target
{ namespace =
require "namespace"
<| path
|> reps root ""
|> reps ".hs" ""
|> reps ".nix" ""
|> reps "/" "."
|> List.stripPrefix "."
>>= Regex.match metaNamespace,
deps = content /> Regex.match metaDep |> catMaybes,
compiler =
if ".hs" `List.isSuffixOf` path
then if ".js" `List.isSuffixOf` exe then Ghcjs else Ghc
else Nix,
..
}
build :: Target -> IO ()
build Target {..} = do
root <- Env.getEnv "BIZ_ROOT"
case compiler of
Ghc -> do
putText <| "bild: ghc: " <> Text.pack namespace
let out = root </> "_/bild/dev/bin"
Dir.createDirectoryIfMissing True out
Process.callProcess
"ghc"
[ "-Werror",
"-i" <> root,
"-odir",
root </> "_/bild/int",
"-hidir",
root </> "_/bild/int",
"--make",
path,
"-main-is",
namespace,
"-o",
out </> exe
]
Ghcjs -> do
putText <| "bild: ghcjs: " <> Text.pack namespace
let out = root </> "_/bild/dev/static"
Dir.createDirectoryIfMissing True out
Process.callProcess
"ghcjs"
[ "-Werror",
"-i" <> root,
"-odir",
root </> "_/bild/int",
"-hidir",
root </> "_/bild/int",
"--make",
path,
"-main-is",
namespace,
"-o",
out </> exe
]
Nix -> do
putText <| "bild: nix: " <> Text.pack namespace
cwd <- Dir.getCurrentDirectory
let qualifiedTarget = reps root "" cwd <> namespace
Process.callProcess
"nix-build"
[ "-o",
root </> "_/bild/nix" </> qualifiedTarget,
root </> "default.nix",
"--attr",
qualifiedTarget
]
metaNamespace :: Regex.RE Char Namespace
metaNamespace = name <> Regex.many (Regex.sym '.') <> name
where
name = Regex.many (Regex.psym Char.isUpper) <> Regex.many (Regex.psym Char.isLower)
metaDep :: Regex.RE Char Dep
metaDep = Regex.string "-- : dep " *> Regex.many (Regex.psym Char.isAlpha)
metaExe :: Regex.RE Char Exe
metaExe = Regex.string "-- : exe " *> Regex.many (Regex.psym (/= ' '))
require :: Text -> Maybe a -> a
require _ (Just x) = x
require s Nothing = panic <| s <> " not found"
-- | Replace 'a' in 's' with 'b'.
reps :: String -> String -> String -> String
reps a b s@(x : xs) =
if isPrefixOf a s
then-- then, write 'b' and replace jumping 'a' substring
b ++ reps a b (drop (length a) s)
else-- then, write 'x' char and try to replace tail string
x : reps a b xs
reps _ _ [] = []
|