Skip to content

Commit 6f189ab

Browse files
rwtolbertbakpakin
authored andcommitted
Brief: add more pre/post steps for janet-pm building
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. 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.
1 parent bcad982 commit 6f189ab

File tree

5 files changed

+247
-8
lines changed

5 files changed

+247
-8
lines changed

spork/declare-cc.janet

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,15 @@
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 [])
274278
# Add hooks
275279
(def e (curenv))
276280
(defn- prebuild
@@ -279,34 +283,59 @@
279283
(os/mkdir bd)
280284
(os/mkdir (path/join bd "static"))
281285
(build-rules/build-run e "pre-build" (dyn :workers)))
286+
(defn- postbuild
287+
[]
288+
(build-rules/build-run e "post-build" (dyn :workers)))
282289
(defn- precheck
283290
[]
284291
(build-rules/build-run e "pre-check" (dyn :workers)))
292+
(defn- postcheck
293+
[]
294+
(build-rules/build-run e "post-check" (dyn :workers)))
295+
(defn- preinstall
296+
[]
297+
(build-rules/build-run e "pre-install" (dyn :workers)))
298+
(defn- postinstall
299+
[]
300+
(build-rules/build-run e "post-install" (dyn :workers)))
301+
(defn- preclean
302+
[]
303+
(build-rules/build-run e "pre-clean" (dyn :workers)))
304+
(defn- postclean
305+
[]
306+
(build-rules/build-run e "post-clean" (dyn :workers)))
285307
(defn build [&opt man target]
286308
(prebuild)
287309
(default target "build")
288-
(build-rules/build-run e target (dyn :workers)))
310+
(build-rules/build-run e target (dyn :workers))
311+
(postbuild))
289312
(defn install [manifest &]
290-
(build)
313+
# (build) - removed since install in janet/src/boot/boot.janet calls build in the install hook
314+
(preinstall)
291315
(with-dyns [*install-manifest* manifest]
292-
(build-rules/build-run e "install" (dyn :workers))))
316+
(build-rules/build-run e "install" (dyn :workers)))
317+
(postinstall))
293318
(defn check [&]
294319
(build)
295320
(precheck)
296-
(run-tests))
321+
(run-tests)
322+
(postcheck))
297323
(defn list-rules [&]
298324
(each k (sorted (filter string? (keys rules)))
299325
(print k)))
300326
(defn rule-tree [&]
301327
(show-rule-tree rules))
302328
(defn clean [&]
329+
(preclean)
303330
(print "removing directory " bd)
304-
(sh/rm bd))
331+
(sh/rm bd)
332+
(postclean))
305333
(defn clean-all [&]
334+
(preclean)
306335
(print "removing directory " br)
307-
(sh/rm br))
336+
(sh/rm br)
337+
(postclean))
308338
(defn run-task [task]
309-
(prebuild)
310339
(build-rules/build-run e task (dyn :workers)))
311340
(defglobal 'install install)
312341
(defglobal 'build build)

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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
(task "pre-build" ["pre-build-test"])
11+
(task "post-build" ["post-build-test"])
12+
13+
(task "pre-check" ["pre-check-test"])
14+
(task "post-check" ["post-check-test"])
15+
16+
(task "pre-install" ["pre-install-test"])
17+
(task "post-install" ["post-install-test"])
18+
19+
(task "pre-clean" ["pre-clean-test"])
20+
(task "post-clean" ["post-clean-test"])
21+
22+
(task "pre-build-test" []
23+
(printf "****** pre-build"))
24+
25+
(task "post-build-test" []
26+
(printf "****** post-build"))
27+
28+
(task "pre-check-test" []
29+
(printf "****** pre-check"))
30+
31+
(task "post-check-test" []
32+
(printf "****** post-check"))
33+
34+
(task "pre-install-test" []
35+
(printf "****** pre-install"))
36+
37+
(task "post-install-test" []
38+
(printf "****** post-install"))
39+
40+
(task "pre-clean-test" []
41+
(printf "****** pre-clean"))
42+
43+
(task "post-clean-test" []
44+
(printf "****** post-clean"))

test-project/test/test-hello.janet

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(import hello)
2+
3+
(assert (= (hello/hello "tim") "hello tim"))

test/suite-pm-pre-post.janet

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
(use ../spork/test)
2+
(import ../spork/pm)
3+
(import ../spork/pm-config)
4+
(import ../spork/sh)
5+
(import ../spork/path)
6+
7+
(start-suite)
8+
9+
(assert true) # smoke test
10+
11+
# Copy since not exposed in boot.janet
12+
(defn- bundle-rpath
13+
[path]
14+
(string/replace-all "\\" "/" (os/realpath path)))
15+
16+
(defn randdir
17+
"Get a random directory name"
18+
[]
19+
(string (os/cwd) "/tmp/tmp_dir_" (slice (string (math/random) ".tmp") 2)))
20+
21+
(defn count-match
22+
"Count number of occurrence of pat in str"
23+
[pat str]
24+
(length (string/find-all pat str)))
25+
26+
(defn after?
27+
"Verify pat2 occurs after pat1 in str"
28+
[pat2 pat1 str]
29+
(let [f1 (string/find pat1 str)
30+
f2 (string/find pat2 str)]
31+
(> f2 f1)))
32+
33+
(defn divider [title]
34+
(printf "------------------------------------------\n%s" title))
35+
36+
(defn dump-out [output]
37+
(let [out (string/split "\n" (output :out))
38+
err (string/split "\n" (output :err))]
39+
(when (> (length out) 0)
40+
(each line out
41+
(printf line)))
42+
(when (> (length err) 0)
43+
(each line err
44+
(printf line)))))
45+
46+
# Create a temporary directory for our janet tree
47+
(math/seedrandom (os/cryptorand 16))
48+
(def syspath (randdir))
49+
(sh/rm syspath)
50+
(os/mkdir "tmp")
51+
(assert (os/mkdir syspath))
52+
(pm-config/read-env-variables root-env)
53+
(put root-env :build-dir nil) # jpm test sets this and it messes things up
54+
(defer (sh/rm "tmp")
55+
(put root-env *syspath* (bundle-rpath syspath))
56+
(put root-env :binpath (string syspath "/bin"))
57+
(put root-env :manpath (string syspath "/man"))
58+
(unless (os/getenv "VERBOSE")
59+
(setdyn *out* @""))
60+
(assert (empty? (bundle/list)) "initial bundle/list")
61+
(assert (empty? (bundle/topolist)) "initial bundle/topolist")
62+
63+
# install spork
64+
(pm/pm-install "file::.")
65+
(assert (= 1 (length (bundle/list))) "bundle/list after install")
66+
67+
# make sure the right janet-pm is found
68+
(def binpath (path/join (dyn *syspath*) "bin"))
69+
(def janet-pm (path/join binpath (string "janet-pm" (if (= (os/which) :windows) ".exe" ""))))
70+
(assert (sh/exists? janet-pm))
71+
72+
(os/cd "test-project")
73+
74+
# run janet-pm commands and collect output
75+
76+
# check "build"
77+
(divider "Running janet-pm build")
78+
(def build-out (sh/exec-slurp-all janet-pm "build"))
79+
(dump-out build-out)
80+
(assert (= 1 (count-match "** pre-build" (build-out :out))))
81+
(assert (= 1 (count-match "** post-build" (build-out :out))))
82+
(assert (= 0 (count-match "** pre-install" (build-out :out))))
83+
84+
# check "install"
85+
(divider "Running janet-pm install")
86+
(def install-out (sh/exec-slurp-all janet-pm "install"))
87+
(dump-out install-out)
88+
# verify proper entries only occur once
89+
(assert (= 1 (count-match "** pre-build" (install-out :out))))
90+
(assert (= 1 (count-match "** post-build" (install-out :out))))
91+
(assert (= 1 (count-match "** pre-install" (install-out :out))))
92+
(assert (= 1 (count-match "** post-install" (install-out :out))))
93+
(assert (= 0 (count-match "** pre-check" (install-out :out))))
94+
(assert (= 0 (count-match "** post-check" (install-out :out))))
95+
96+
# verify order of entries
97+
(assert (after? "post-build" "pre-build" (install-out :out)))
98+
(assert (after? "pre-install" "post-build" (install-out :out)))
99+
(assert (after? "post-install" "pre-install" (install-out :out)))
100+
101+
# check "test"
102+
(divider "Running janet-pm test")
103+
(def test-out (sh/exec-slurp-all janet-pm "test"))
104+
(dump-out test-out)
105+
# verify proper entries only occur once
106+
(assert (= 1 (count-match "** pre-build" (test-out :out))))
107+
(assert (= 1 (count-match "** post-build" (test-out :out))))
108+
(assert (= 0 (count-match "** pre-install" (test-out :out))))
109+
(assert (= 0 (count-match "** post-install" (test-out :out))))
110+
(assert (= 1 (count-match "** pre-check" (test-out :out))))
111+
(assert (= 1 (count-match "** post-check" (test-out :out))))
112+
(assert (= 1 (count-match "running test" (test-out :out))))
113+
114+
# verify order of entries
115+
(assert (after? "post-build" "pre-build" (test-out :out)))
116+
(assert (after? "pre-check" "post-build" (test-out :out)))
117+
(assert (after? "running test" "pre-check" (test-out :out)))
118+
(assert (after? "post-check" "running test" (test-out :out)))
119+
120+
# check "clean"
121+
(divider "Running janet-pm clean")
122+
(def clean-out (sh/exec-slurp-all janet-pm "clean"))
123+
(dump-out clean-out)
124+
# verify proper entries only occur once
125+
(assert (= 1 (count-match "** pre-clean" (clean-out :out))))
126+
(assert (= 1 (count-match "removing directory _build/" (clean-out :out))))
127+
(assert (= 1 (count-match "** post-clean" (clean-out :out))))
128+
(assert (= 0 (count-match "** pre-build" (clean-out :out))))
129+
(assert (= 0 (count-match "** post-build" (clean-out :out))))
130+
(assert (= 0 (count-match "** pre-install" (clean-out :out))))
131+
(assert (= 0 (count-match "** post-install" (clean-out :out))))
132+
(assert (= 0 (count-match "** pre-check" (clean-out :out))))
133+
(assert (= 0 (count-match "** post-check" (clean-out :out))))
134+
135+
# verify order of entries
136+
(assert (after? "removing directory" "pre-clean" (clean-out :out)))
137+
(assert (after? "post-clean" "removing directory" (clean-out :out)))
138+
139+
# check "clean-all"
140+
(divider "Running janet-pm clean-all")
141+
(def clean-all-out (sh/exec-slurp-all janet-pm "clean-all"))
142+
(dump-out clean-all-out)
143+
# verify proper entries only occur once
144+
(assert (= 1 (count-match "** pre-clean" (clean-all-out :out))))
145+
(assert (= 1 (count-match "removing directory _build" (clean-all-out :out))))
146+
(assert (= 1 (count-match "** post-clean" (clean-all-out :out))))
147+
(assert (= 0 (count-match "** pre-build" (clean-all-out :out))))
148+
(assert (= 0 (count-match "** post-build" (clean-all-out :out))))
149+
(assert (= 0 (count-match "** pre-install" (clean-all-out :out))))
150+
(assert (= 0 (count-match "** post-install" (clean-all-out :out))))
151+
(assert (= 0 (count-match "** pre-check" (clean-all-out :out))))
152+
(assert (= 0 (count-match "** post-check" (clean-all-out :out))))
153+
154+
# verify order of entries
155+
(assert (after? "removing directory" "pre-clean" (clean-all-out :out)))
156+
(assert (after? "post-clean" "removing directory" (clean-all-out :out))))
157+
158+
# reset *out* so we can see # tests passed even if VERBOSE is not defined
159+
(setdyn *out* nil)
160+
161+
(end-suite)

0 commit comments

Comments
 (0)