Skip to content

Commit 6ec9e78

Browse files
committed
add a template showing how to use it
1 parent 3d9e4eb commit 6ec9e78

File tree

9 files changed

+356
-0
lines changed

9 files changed

+356
-0
lines changed

docs/Manual.html

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ <h1><a href="https://github.com/bucklescript/bucklescript">BuckleScript</a> User
679679
</li>
680680
<li><a href="#_a_real_world_example_of_using_code_bsb_code">A real world example of using <code>bsb</code></a></li>
681681
<li><a href="#_build_using_make">Build using Make</a></li>
682+
<li><a href="#_customize_rules_generators_support_since_1_7_4">Customize rules (generators support, @since 1.7.4)</a></li>
682683
</ul>
683684
</li>
684685
<li><a href="#_faq">FAQ</a></li>
@@ -5086,6 +5087,61 @@ <h3 id="_build_using_make"><a class="anchor" href="#_build_using_make"></a>Build
50865087
<p>Now in your working directory, type <code>watchman -j &lt; build.json</code> and enjoy the lightning build speed.</p>
50875088
</div>
50885089
</div>
5090+
<div class="sect2">
5091+
<h3 id="_customize_rules_generators_support_since_1_7_4"><a class="anchor" href="#_customize_rules_generators_support_since_1_7_4"></a>Customize rules (generators support, @since 1.7.4)</h3>
5092+
<div class="paragraph">
5093+
<p>It is quite common that programmers use some pre-processors to generate some bolierpolate code during developement.</p>
5094+
</div>
5095+
<div class="paragraph">
5096+
<p>Note pre-processors can be classified as two categories, one is system-dependent which should be delayed until running on user machines, the other is system-indepdent , lex, yacc, m4, re2c, etc, which could be executed anytime.</p>
5097+
</div>
5098+
<div class="paragraph">
5099+
<p>BuckleScript has built in support for conditional compilation, this section is about the second part, since it is system-indepdent, we ask users to always generate such code and check in before shipping, this would help cut the dependencies for end users.</p>
5100+
</div>
5101+
<div class="paragraph">
5102+
<p>A typical example would be like this</p>
5103+
</div>
5104+
<div class="listingblock">
5105+
<div class="title">Bsb using ocamlyacc</div>
5106+
<div class="content">
5107+
<pre class="pygments highlight"><code data-lang="js"><span class="tok-p">{</span>
5108+
<span class="tok-s2">&quot;generators&quot;</span> <span class="tok-o">:</span> <span class="tok-p">[</span>
5109+
<span class="tok-p">{</span> <span class="tok-s2">&quot;name&quot;</span> <span class="tok-o">:</span> <span class="tok-s2">&quot;ocamlyacc&quot;</span> <span class="tok-p">,</span>
5110+
<span class="tok-s2">&quot;command&quot;</span> <span class="tok-o">:</span> <span class="tok-s2">&quot;ocamlyacc $in&quot;</span> <span class="tok-p">}</span>
5111+
<span class="tok-p">],</span>
5112+
<span class="tok-p">...,</span>
5113+
<span class="tok-s2">&quot;sources&quot;</span> <span class="tok-o">:</span> <span class="tok-p">{</span>
5114+
<span class="tok-s2">&quot;dir&quot;</span> <span class="tok-o">:</span> <span class="tok-s2">&quot;src&quot;</span><span class="tok-p">,</span>
5115+
<span class="tok-s2">&quot;generators&quot;</span> <span class="tok-o">:</span> <span class="tok-p">[</span>
5116+
<span class="tok-p">{</span>
5117+
<span class="tok-s2">&quot;name&quot;</span> <span class="tok-o">:</span> <span class="tok-s2">&quot;ocamlyacc&quot;</span><span class="tok-p">,</span>
5118+
<span class="tok-s2">&quot;edge&quot;</span> <span class="tok-o">:</span> <span class="tok-p">[</span><span class="tok-s2">&quot;test.ml&quot;</span><span class="tok-p">,</span> <span class="tok-s2">&quot;test.mli&quot;</span><span class="tok-p">,</span> <span class="tok-s2">&quot;:&quot;</span><span class="tok-p">,</span> <span class="tok-s2">&quot;test.mly&quot;</span><span class="tok-p">]</span>
5119+
<span class="tok-p">}</span>
5120+
<span class="tok-p">]</span>
5121+
<span class="tok-p">}</span>
5122+
<span class="tok-p">}</span></code></pre>
5123+
</div>
5124+
</div>
5125+
<div class="paragraph">
5126+
<p>Note <code>ocamlyacc</code> will generate in <code>test.ml</code> and <code>test.mli</code> in the same directory with <code>test.mly</code>, user should check in generated file since then users would not need run ocamlyacc again, this would apply to <code>menhir</code> as well.</p>
5127+
</div>
5128+
<div class="paragraph">
5129+
<p>When users are developing current project, <code>bsb</code> will track the dependencies between <code>test.ml</code> and <code>test.mly</code> properly, when released
5130+
as a package, <code>bsb</code> will cut such dependency, so that users will
5131+
only need the generated <code>test.ml</code>, to help test such behavior in development mode, users could set it manually</p>
5132+
</div>
5133+
<div class="listingblock">
5134+
<div class="content">
5135+
<pre class="pygments highlight"><code data-lang="js"><span class="tok-p">{</span>
5136+
<span class="tok-p">...,</span>
5137+
<span class="tok-s2">&quot;cut-generators&quot;</span> <span class="tok-o">:</span> <span class="tok-kc">true</span>
5138+
<span class="tok-p">}</span></code></pre>
5139+
</div>
5140+
</div>
5141+
<div class="paragraph">
5142+
<p>Then <code>bsb</code> will not re-generate <code>test.ml</code> whenever <code>test.mly</code> changes.</p>
5143+
</div>
5144+
</div>
50895145
</div>
50905146
</div>
50915147
<div class="sect1">

jscomp/bin/bsb.ml

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10460,6 +10460,109 @@ let root = OCamlRes.Res.([
1046010460
*.mliast\n\
1046110461
.vscode\n\
1046210462
.merlin")]) ;
10463+
Dir ("generator", [
10464+
Dir ("src", [
10465+
File ("test.cpp.ml",
10466+
"\n\
10467+
\n\
10468+
#define FS_VAL(name,ty) external name : ty = \"\" [@@bs.module \"fs\"]\n\
10469+
\n\
10470+
\n\
10471+
FS_VAL(readdirSync, string -> string array)\n\
10472+
\n\
10473+
") ;
10474+
File ("demo.ml",
10475+
"\n\
10476+
\n\
10477+
let () = Js.log \"Hello, BuckleScript\"")]) ;
10478+
File ("README.md",
10479+
"\n\
10480+
\n\
10481+
# Build\n\
10482+
```\n\
10483+
npm run build\n\
10484+
```\n\
10485+
\n\
10486+
# Watch\n\
10487+
\n\
10488+
```\n\
10489+
npm run watch\n\
10490+
```\n\
10491+
\n\
10492+
\n\
10493+
# Editor\n\
10494+
If you use `vscode`, Press `Windows + Shift + B` it will build automatically") ;
10495+
File ("package.json",
10496+
"{\n\
10497+
\ \"name\": \"${bsb:name}\",\n\
10498+
\ \"version\": \"${bsb:proj-version}\",\n\
10499+
\ \"scripts\": {\n\
10500+
\ \"clean\": \"bsb -clean-world\",\n\
10501+
\ \"build\": \"bsb -make-world\",\n\
10502+
\ \"watch\": \"bsb -make-world -w\"\n\
10503+
\ },\n\
10504+
\ \"keywords\": [\n\
10505+
\ \"BuckleScript\"\n\
10506+
\ ],\n\
10507+
\ \"license\": \"MIT\",\n\
10508+
\ \"devDependencies\": {\n\
10509+
\ \"bs-platform\": \"${bsb:bs-version}\"\n\
10510+
\ }\n\
10511+
}") ;
10512+
File ("bsconfig.json",
10513+
"{\n\
10514+
\ \"name\": \"${bsb:name}\",\n\
10515+
\ \"version\": \"${bsb:proj-version}\",\n\
10516+
\ \"sources\": [\n\
10517+
\ {\n\
10518+
\ \"dir\": \"src\",\n\
10519+
\ \"generators\": [\n\
10520+
\ {\n\
10521+
\ \"name\": \"cpp\",\n\
10522+
\ \"edge\": [\n\
10523+
\ \"test.ml\", \":\", \"test.cpp.ml\"\n\
10524+
\ ]\n\
10525+
\ }\n\
10526+
\ ]\n\
10527+
\ }\n\
10528+
\ \n\
10529+
\ ],\n\
10530+
\ \"generators\": [\n\
10531+
\ {\n\
10532+
\ \"name\" : \"cpp\",\n\
10533+
\ \"command\": \"gcc -x c -P -traditional-cpp -C -E $in -o $out\"\n\
10534+
\ }\n\
10535+
\ ],\n\
10536+
\ \"bs-dependencies\" : [\n\
10537+
\ // add your bs-dependencies here \n\
10538+
\ ]\n\
10539+
}") ;
10540+
File (".gitignore",
10541+
"*.exe\n\
10542+
*.obj\n\
10543+
*.out\n\
10544+
*.compile\n\
10545+
*.native\n\
10546+
*.byte\n\
10547+
*.cmo\n\
10548+
*.annot\n\
10549+
*.cmi\n\
10550+
*.cmx\n\
10551+
*.cmt\n\
10552+
*.cmti\n\
10553+
*.cma\n\
10554+
*.a\n\
10555+
*.cmxa\n\
10556+
*.obj\n\
10557+
*~\n\
10558+
*.annot\n\
10559+
*.cmj\n\
10560+
*.bak\n\
10561+
lib/bs\n\
10562+
*.mlast\n\
10563+
*.mliast\n\
10564+
.vscode\n\
10565+
.merlin")]) ;
1046310566
Dir ("minimal", [
1046410567
Dir ("src", [ File ("main.ml", "")]) ;
1046510568
File ("README.md",

jscomp/bsb/bsb_templates.ml

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,109 @@ let root = OCamlRes.Res.([
238238
*.mliast\n\
239239
.vscode\n\
240240
.merlin")]) ;
241+
Dir ("generator", [
242+
Dir ("src", [
243+
File ("test.cpp.ml",
244+
"\n\
245+
\n\
246+
#define FS_VAL(name,ty) external name : ty = \"\" [@@bs.module \"fs\"]\n\
247+
\n\
248+
\n\
249+
FS_VAL(readdirSync, string -> string array)\n\
250+
\n\
251+
") ;
252+
File ("demo.ml",
253+
"\n\
254+
\n\
255+
let () = Js.log \"Hello, BuckleScript\"")]) ;
256+
File ("README.md",
257+
"\n\
258+
\n\
259+
# Build\n\
260+
```\n\
261+
npm run build\n\
262+
```\n\
263+
\n\
264+
# Watch\n\
265+
\n\
266+
```\n\
267+
npm run watch\n\
268+
```\n\
269+
\n\
270+
\n\
271+
# Editor\n\
272+
If you use `vscode`, Press `Windows + Shift + B` it will build automatically") ;
273+
File ("package.json",
274+
"{\n\
275+
\ \"name\": \"${bsb:name}\",\n\
276+
\ \"version\": \"${bsb:proj-version}\",\n\
277+
\ \"scripts\": {\n\
278+
\ \"clean\": \"bsb -clean-world\",\n\
279+
\ \"build\": \"bsb -make-world\",\n\
280+
\ \"watch\": \"bsb -make-world -w\"\n\
281+
\ },\n\
282+
\ \"keywords\": [\n\
283+
\ \"BuckleScript\"\n\
284+
\ ],\n\
285+
\ \"license\": \"MIT\",\n\
286+
\ \"devDependencies\": {\n\
287+
\ \"bs-platform\": \"${bsb:bs-version}\"\n\
288+
\ }\n\
289+
}") ;
290+
File ("bsconfig.json",
291+
"{\n\
292+
\ \"name\": \"${bsb:name}\",\n\
293+
\ \"version\": \"${bsb:proj-version}\",\n\
294+
\ \"sources\": [\n\
295+
\ {\n\
296+
\ \"dir\": \"src\",\n\
297+
\ \"generators\": [\n\
298+
\ {\n\
299+
\ \"name\": \"cpp\",\n\
300+
\ \"edge\": [\n\
301+
\ \"test.ml\", \":\", \"test.cpp.ml\"\n\
302+
\ ]\n\
303+
\ }\n\
304+
\ ]\n\
305+
\ }\n\
306+
\ \n\
307+
\ ],\n\
308+
\ \"generators\": [\n\
309+
\ {\n\
310+
\ \"name\" : \"cpp\",\n\
311+
\ \"command\": \"gcc -x c -P -traditional-cpp -C -E $in -o $out\"\n\
312+
\ }\n\
313+
\ ],\n\
314+
\ \"bs-dependencies\" : [\n\
315+
\ // add your bs-dependencies here \n\
316+
\ ]\n\
317+
}") ;
318+
File (".gitignore",
319+
"*.exe\n\
320+
*.obj\n\
321+
*.out\n\
322+
*.compile\n\
323+
*.native\n\
324+
*.byte\n\
325+
*.cmo\n\
326+
*.annot\n\
327+
*.cmi\n\
328+
*.cmx\n\
329+
*.cmt\n\
330+
*.cmti\n\
331+
*.cma\n\
332+
*.a\n\
333+
*.cmxa\n\
334+
*.obj\n\
335+
*~\n\
336+
*.annot\n\
337+
*.cmj\n\
338+
*.bak\n\
339+
lib/bs\n\
340+
*.mlast\n\
341+
*.mliast\n\
342+
.vscode\n\
343+
.merlin")]) ;
241344
Dir ("minimal", [
242345
Dir ("src", [ File ("main.ml", "")]) ;
243346
File ("README.md",
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
*.exe
2+
*.obj
3+
*.out
4+
*.compile
5+
*.native
6+
*.byte
7+
*.cmo
8+
*.annot
9+
*.cmi
10+
*.cmx
11+
*.cmt
12+
*.cmti
13+
*.cma
14+
*.a
15+
*.cmxa
16+
*.obj
17+
*~
18+
*.annot
19+
*.cmj
20+
*.bak
21+
lib/bs
22+
*.mlast
23+
*.mliast
24+
.vscode
25+
.merlin
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
3+
# Build
4+
```
5+
npm run build
6+
```
7+
8+
# Watch
9+
10+
```
11+
npm run watch
12+
```
13+
14+
15+
# Editor
16+
If you use `vscode`, Press `Windows + Shift + B` it will build automatically
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "${bsb:name}",
3+
"version": "${bsb:proj-version}",
4+
"sources": [
5+
{
6+
"dir": "src",
7+
"generators": [
8+
{
9+
"name": "cpp",
10+
"edge": [
11+
"test.ml", ":", "test.cpp.ml"
12+
]
13+
}
14+
]
15+
}
16+
17+
],
18+
"generators": [
19+
{
20+
"name" : "cpp",
21+
"command": "gcc -x c -P -traditional-cpp -C -E $in -o $out"
22+
}
23+
],
24+
"bs-dependencies" : [
25+
// add your bs-dependencies here
26+
]
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "${bsb:name}",
3+
"version": "${bsb:proj-version}",
4+
"scripts": {
5+
"clean": "bsb -clean-world",
6+
"build": "bsb -make-world",
7+
"watch": "bsb -make-world -w"
8+
},
9+
"keywords": [
10+
"BuckleScript"
11+
],
12+
"license": "MIT",
13+
"devDependencies": {
14+
"bs-platform": "${bsb:bs-version}"
15+
}
16+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
3+
let () = Js.log "Hello, BuckleScript"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
3+
#define FS_VAL(name,ty) external name : ty = "" [@@bs.module "fs"]
4+
5+
6+
FS_VAL(readdirSync, string -> string array)
7+

0 commit comments

Comments
 (0)