Skip to content

Commit c9f9a58

Browse files
committed
Brief: add more pre/post steps for janet-pm
1. Adding these extra steps allows more user control of the build process for complicated builds, including pre/post steps for build, install, test and clean. This also fixes #225, so that prebuild is only run before build. 2. Also removes redundant calls to build in the install hook since Janet's core install hook calls build first as well. Having this in spork too resulted in two calls to build when `janet-pm install` was run. 3. Adds local assertf in `janet-pm` for older Janet 4. Fixes existing problem in `janet-pm` where hardcoded syspath at the top-level is not honored when run as `(defn main ...`. This change adds a copy of the hardcoded paths inside defn main in a binscript, if main exists in the input.
1 parent 6589799 commit c9f9a58

File tree

11 files changed

+373
-10
lines changed

11 files changed

+373
-10
lines changed

bin/janet-pm

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55
(import spork/pm)
66
(import spork/pm-config)
77
(import spork/sh)
8+
9+
# Fix for janet without assertf
10+
(compwhen (not (dyn 'assertf))
11+
(defmacro- assertf
12+
"Convenience macro that combines `assert` and `string/format`."
13+
[x fmt & args]
14+
(def v (gensym))
15+
~(do
16+
(def ,v ,x)
17+
(if ,v
18+
,v
19+
(,errorf ,fmt ,;args)))))
20+
821
# Do not import declare-cc here - keep a clean separation for faster load times
922

1023
(def help-text

spork/declare-cc.janet

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,18 @@
266266
(def bd (build-dir))
267267
(def rules (get-rules))
268268
# Initialize build rules
269-
(build-rules/build-rule rules :install ["build" "pre-install"])
269+
(build-rules/build-rule rules :install [])
270270
(build-rules/build-rule rules :pre-install [])
271+
(build-rules/build-rule rules :post-install [])
271272
(build-rules/build-rule rules :build [])
272273
(build-rules/build-rule rules :pre-build [])
274+
(build-rules/build-rule rules :post-build [])
275+
(build-rules/build-rule rules :check [])
273276
(build-rules/build-rule rules :pre-check [])
277+
(build-rules/build-rule rules :post-check [])
278+
(build-rules/build-rule rules :clean [])
279+
(build-rules/build-rule rules :pre-clean [])
280+
(build-rules/build-rule rules :post-clean [])
274281
# Add hooks
275282
(def e (curenv))
276283
(defn- prebuild
@@ -279,34 +286,59 @@
279286
(os/mkdir bd)
280287
(os/mkdir (path/join bd "static"))
281288
(build-rules/build-run e "pre-build" (dyn :workers)))
289+
(defn- postbuild
290+
[]
291+
(build-rules/build-run e "post-build" (dyn :workers)))
282292
(defn- precheck
283293
[]
284294
(build-rules/build-run e "pre-check" (dyn :workers)))
295+
(defn- postcheck
296+
[]
297+
(build-rules/build-run e "post-check" (dyn :workers)))
298+
(defn- preinstall
299+
[]
300+
(build-rules/build-run e "pre-install" (dyn :workers)))
301+
(defn- postinstall
302+
[]
303+
(build-rules/build-run e "post-install" (dyn :workers)))
304+
(defn- preclean
305+
[]
306+
(build-rules/build-run e "pre-clean" (dyn :workers)))
307+
(defn- postclean
308+
[]
309+
(build-rules/build-run e "post-clean" (dyn :workers)))
285310
(defn build [&opt man target]
286311
(prebuild)
287312
(default target "build")
288-
(build-rules/build-run e target (dyn :workers)))
313+
(build-rules/build-run e target (dyn :workers))
314+
(postbuild))
289315
(defn install [manifest &]
290-
(build)
316+
# (build) - removed since install in janet/src/boot/boot.janet calls build in the install hook
317+
(preinstall)
291318
(with-dyns [*install-manifest* manifest]
292-
(build-rules/build-run e "install" (dyn :workers))))
319+
(build-rules/build-run e "install" (dyn :workers)))
320+
(postinstall))
293321
(defn check [&]
294322
(build)
295323
(precheck)
296-
(run-tests))
324+
(run-tests)
325+
(postcheck))
297326
(defn list-rules [&]
298327
(each k (sorted (filter string? (keys rules)))
299328
(print k)))
300329
(defn rule-tree [&]
301330
(show-rule-tree rules))
302331
(defn clean [&]
332+
(preclean)
303333
(print "removing directory " bd)
304-
(sh/rm bd))
334+
(sh/rm bd)
335+
(postclean))
305336
(defn clean-all [&]
337+
(preclean)
306338
(print "removing directory " br)
307-
(sh/rm br))
339+
(sh/rm br)
340+
(postclean))
308341
(defn run-task [task]
309-
(prebuild)
310342
(build-rules/build-run e task (dyn :workers)))
311343
(defglobal 'install install)
312344
(defglobal 'build build)
@@ -351,6 +383,19 @@
351383
[&named main]
352384
(install-rule main "bin" 8r755 (mkbin)))
353385

386+
(defn- main-replacer [dynamic-syspath hardcode-syspath l2 l3 l4]
387+
(def parts @[])
388+
(if (or dynamic-syspath hardcode-syspath) (array/push parts (string "\n " l2)))
389+
(if hardcode-syspath (array/push parts (string " " l3)))
390+
(if hardcode-syspath (array/push parts (string " " l4)))
391+
(fn [m & args] (string m (string/join parts "") "\n")))
392+
393+
(defn- hardcode-syspath-in-main [dynamic-syspath hardcode-syspath contents l2 l3 l4]
394+
(def patt (peg/compile '{:ws (some (choice :a :d :s "[" "&"))
395+
:defn (sequence "(defn" (any "-") :s "main" :ws "]" (any (choice " " "\n")))
396+
:main :defn}))
397+
(peg/replace patt (main-replacer dynamic-syspath hardcode-syspath l2 l3 l4) contents))
398+
354399
(defn declare-binscript
355400
``Declare a janet file to be installed as an executable script. Creates
356401
a shim on windows. If hardcode is true, will insert code into the script
@@ -366,7 +411,7 @@
366411
(def auto-shebang (and is-janet (not (string/has-prefix? "#!" first-line))))
367412
(def dynamic-syspath (= hardcode-syspath :dynamic))
368413
(def second-line (string/format "(put root-env :original-syspath (os/realpath (dyn *syspath*))) # auto generated\n"))
369-
(def third-line (string/format "(put root-env :syspath %v) # auto generated\n" (dyn *syspath*)))
414+
(def third-line (string/format "(put root-env :syspath %v) # auto generated\n" (dyn *syspath*)))
370415
(def fourth-line (string/format "(put root-env :install-time-syspath %v) # auto generated\n" (dyn *syspath*)))
371416
(def last-line "\n(put root-env :syspath (get root-env :original-syspath)) # auto generated\n")
372417
(def rest (:read f :all))
@@ -375,7 +420,9 @@
375420
(if (or dynamic-syspath hardcode-syspath) second-line)
376421
(if hardcode-syspath third-line)
377422
(if hardcode-syspath fourth-line)
378-
rest
423+
(if hardcode-syspath
424+
(hardcode-syspath-in-main dynamic-syspath hardcode-syspath rest second-line third-line fourth-line)
425+
rest)
379426
(if dynamic-syspath last-line))))
380427
(install-buffer contents dest 8r755 (mkbin))
381428
(when (is-win-or-mingw)

test-project/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/build
2+
/bundle
3+
/_build

test-project/bin/bin-no-hardcode

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env janet
2+
3+
(import spork)
4+
5+
(defn- main
6+
[&]
7+
(printf "hello"))

test-project/bin/bin-no-main

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env janet
2+
3+
(print "hello")

test-project/bin/bin-with-main

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env janet
2+
3+
(import spork)
4+
5+
(defn- main
6+
[&]
7+
(printf "hello"))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env janet
2+
3+
(import spork)
4+
5+
(defn main [&] (printf "hello"))

test-project/hello/init.janet

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
(defn hello [arg]
2+
(string/format "hello %s" arg))

test-project/project.janet

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
(declare-project
2+
:name "test-project"
3+
:description "Janet project to test pre/post steps"
4+
:version "0.0.1"
5+
:dependencies [])
6+
7+
(declare-source
8+
:source @["hello"])
9+
10+
(declare-binscript
11+
:main "bin/bin-no-main"
12+
:hardcode-syspath true
13+
:is-janet true)
14+
15+
(declare-binscript
16+
:main "bin/bin-with-main"
17+
:hardcode-syspath true
18+
:is-janet true)
19+
20+
(declare-binscript
21+
:main "bin/bin-with-oneline-main"
22+
:hardcode-syspath true
23+
:is-janet true)
24+
25+
(declare-binscript
26+
:main "bin/bin-no-hardcode"
27+
:hardcode-syspath false
28+
:is-janet true)
29+
30+
(task "pre-build" ["pre-build-test"])
31+
(task "post-build" ["post-build-test"])
32+
33+
(task "pre-check" ["pre-check-test"])
34+
(task "post-check" ["post-check-test"])
35+
36+
(task "pre-install" ["pre-install-test"])
37+
(task "post-install" ["post-install-test"])
38+
39+
(task "pre-clean" ["pre-clean-test"])
40+
(task "post-clean" ["post-clean-test"])
41+
42+
(task "pre-build-test" []
43+
(printf "****** pre-build"))
44+
45+
(task "post-build-test" []
46+
(printf "****** post-build"))
47+
48+
(task "pre-check-test" []
49+
(printf "****** pre-check"))
50+
51+
(task "post-check-test" []
52+
(printf "****** post-check"))
53+
54+
(task "pre-install-test" []
55+
(printf "****** pre-install"))
56+
57+
(task "post-install-test" []
58+
(printf "****** post-install"))
59+
60+
(task "pre-clean-test" []
61+
(printf "****** pre-clean"))
62+
63+
(task "post-clean-test" []
64+
(printf "****** post-clean"))

test-project/test/test-hello.janet

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(assert true)

0 commit comments

Comments
 (0)