Skip to content

Commit fa55e3b

Browse files
committed
add a task for vscode
if you code using vscode, run the task, it has a watch mode, enjoy the better editing experience - -absname for precise error reporting
1 parent 821decd commit fa55e3b

File tree

5 files changed

+241
-4
lines changed

5 files changed

+241
-4
lines changed

syntax/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ Napkinscript2.ml
1414
./lib/napkinscript2.exe
1515
./lib/Napkinscript.ml
1616
lib/napkinscript.ml
17+
.vscode/settings.json
18+
# each person has its own preferences

syntax/.vscode/ninja.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env node
2+
var path = require("path");
3+
var cp = require("child_process");
4+
var jscompDir = path.join(__dirname, "..");
5+
var vendorNinjaPath = "make";
6+
function main() {
7+
var subcommand = process.argv[2];
8+
switch (subcommand) {
9+
case "build":
10+
cp.execSync(vendorNinjaPath, {
11+
encoding: "utf8",
12+
cwd: jscompDir,
13+
stdio: [0, 1, 2],
14+
});
15+
// cp.execSync(`make depend`, {
16+
// cwd: jscompDir,
17+
// stdio: [0, 1, 2],
18+
// });
19+
break;
20+
case "clean":
21+
cp.execSync(`make clean`, {
22+
cwd: jscompDir,
23+
stdio: [0, 1, 2],
24+
});
25+
break;
26+
case "config":
27+
cp.execSync(`make depend`, {
28+
cwd: jscompDir,
29+
stdio: [0, 1, 2],
30+
});
31+
break;
32+
case "cleanbuild":
33+
cp.execSync(`make clean && make depend && make -j9`, {
34+
cwd: jscompDir,
35+
stdio: [0, 1, 2],
36+
});
37+
break;
38+
case "help":
39+
console.log(`supported subcommands:
40+
[exe] config
41+
[exe] build
42+
[exe] cleanbuild
43+
[exe] help
44+
[exe] clean
45+
`);
46+
break;
47+
default:
48+
break;
49+
}
50+
}
51+
52+
main();

syntax/.vscode/task.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#!/usr/bin/env node
2+
//@ts-check
3+
var fs = require("fs");
4+
var path = require("path");
5+
var cp = require("child_process");
6+
var sourceDirs = ["src"];
7+
8+
var buildAppending = false;
9+
var isBuilding = false;
10+
11+
var jscompDir = path.join(__dirname, "..");
12+
/**
13+
*
14+
* @param {Date} d
15+
*/
16+
function showDate(d) {
17+
return `${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}`;
18+
}
19+
function rebuild() {
20+
if (isBuilding) {
21+
buildAppending = true;
22+
} else {
23+
console.log(">>>> Start compiling");
24+
isBuilding = true;
25+
26+
cp.fork(path.join(__dirname, "ninja.js"), ["build"]).on(
27+
"exit",
28+
buildFinished
29+
);
30+
}
31+
}
32+
/**
33+
*
34+
* @param {number} code
35+
* @param {string} signal
36+
*/
37+
function buildFinished(code, signal) {
38+
isBuilding = false;
39+
if (buildAppending) {
40+
buildAppending = false;
41+
rebuild();
42+
} else {
43+
if (code !== 0) {
44+
// console.log(`File "package.json", line 1, characters 1-1:`);
45+
// console.log(`Error: Failed to build ${showDate(new Date())}`);
46+
}
47+
console.log(">>>> Finish compiling (options: R|clean|config)");
48+
// TODO: check ninja exit error code
49+
if (code === 0) {
50+
// This is not always correct
51+
// ninjaFile.updateDev();
52+
// This file is cached
53+
cp.fork(path.join(__dirname, "ninja.js"), ["-dev"]);
54+
}
55+
}
56+
}
57+
/**
58+
*
59+
* @param {string} eventType
60+
* @param {string} filename
61+
*/
62+
function onSourceChange(eventType, filename) {
63+
// console.log('event ', eventType,filename)
64+
if (
65+
filename.endsWith(".ml") ||
66+
filename.endsWith(".mli") ||
67+
filename.endsWith(".json") ||
68+
filename.endsWith(".re") ||
69+
filename.endsWith(".rei")
70+
) {
71+
rebuild();
72+
}
73+
}
74+
75+
sourceDirs.forEach((x) => {
76+
fs.watch(path.join(jscompDir, x), "utf8", onSourceChange);
77+
});
78+
79+
// fs.watch(path.join("..", "package.json"), "utf8", onSourceChange);
80+
81+
rebuild();
82+
83+
var readline = require("readline");
84+
readline
85+
.createInterface({
86+
input: process.stdin,
87+
output: process.stdout,
88+
})
89+
.on("line", (input) => {
90+
switch (input.toLowerCase()) {
91+
case "r":
92+
rebuild();
93+
break;
94+
case "config":
95+
if (isBuilding) {
96+
console.log(`it's building`);
97+
} else {
98+
isBuilding = true;
99+
cp.fork(path.join(__dirname, "ninja.js"), ["config"]).on(
100+
"close",
101+
() => {
102+
isBuilding = false;
103+
rebuild();
104+
}
105+
);
106+
}
107+
break;
108+
case "clean":
109+
if (isBuilding) {
110+
console.log(`it's building`);
111+
} else {
112+
isBuilding = true;
113+
cp.fork(path.join(__dirname, "ninja.js"), ["cleanbuild"]).on(
114+
"close",
115+
() => {
116+
isBuilding = false;
117+
}
118+
);
119+
}
120+
121+
break;
122+
}
123+
});

syntax/.vscode/tasks.json

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "Watch build",
6+
"command": "node",
7+
"options": {
8+
"cwd": "${workspaceRoot}"
9+
},
10+
// "isShellCommand": true,
11+
"type": "shell",
12+
"group": {
13+
"kind" : "build",
14+
"isDefault": true
15+
},
16+
"args": [
17+
".vscode/task.js"
18+
],
19+
"presentation": {
20+
"echo": true,
21+
"reveal": "always",
22+
"focus": false,
23+
"panel": "shared",
24+
"showReuseMessage": true,
25+
"clear": false
26+
},
27+
"isBackground": true,
28+
"problemMatcher": {
29+
"fileLocation": "absolute",
30+
"owner": "ocaml",
31+
"background": {
32+
"activeOnStart": false,
33+
"beginsPattern": ">>>> Start compiling",
34+
"endsPattern": ">>>> Finish compiling"
35+
},
36+
"pattern": [
37+
{
38+
"regexp": "^File \"(.*)\", line (\\d+)(?:, characters (\\d+)-(\\d+))?:$",
39+
"file": 1,
40+
"line": 2,
41+
"column": 3,
42+
"endColumn": 4
43+
},
44+
{
45+
// "regexp": "^(?:(?:Parse\\s+)?(Warning|[Ee]rror)(?:\\s+\\d+)?:)?\\s+(.*)$",
46+
"regexp": "^(?:(?:Parse\\s+)?(Warning|[Ee]rror)(?:\\s+\\(warning \\d+\\))?(?:\\s+\\d+)?:)?\\s+(.*)$",
47+
"severity": 1,
48+
"message": 2,
49+
"loop": true
50+
}
51+
]
52+
}
53+
}
54+
]
55+
}

syntax/Makefile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
OCAMLOPT=ocamlopt.opt
2-
OCAMLFLAGS= -w +a-4-42-40-9-48 -warn-error +a -bin-annot -I +compiler-libs -I src -I tests
2+
OCAMLFLAGS= -w +a-4-42-40-9-48 -warn-error +a -bin-annot -I +compiler-libs -I src -I tests -absname
33
OCAMLDEP=ocamldep.opt
44
%.cmi : %.mli
55
$(OCAMLOPT) $(OCAMLFLAGS) -c $<
@@ -35,14 +35,19 @@ FILES = \
3535
src/napkin_driver_binary.cmx \
3636
src/napkin_ast_debugger.cmx \
3737
src/napkin_outcome_printer.cmx \
38-
src/napkin_multi_printer.cmx
38+
src/napkin_multi_printer.cmx \
39+
src/napkin_cli.cmx
3940

4041
TEST_FILES = \
4142
tests/napkin_diff.cmx
4243

4344
.DEFAULT_GOAL := build-native
44-
build-native: lib/refmt.exe $(FILES) src/napkin_cli.cmx depend
45-
$(OCAMLOPT) $(OCAMLFLAGS) -O2 -o ./lib/napkinscript.exe -I +compiler-libs ocamlcommon.cmxa -I src $(FILES) src/napkin_cli.cmx
45+
46+
lib/napkinscript.exe: $(FILES)
47+
$(OCAMLOPT) $(OCAMLFLAGS) -O2 -o ./lib/napkinscript.exe -I +compiler-libs ocamlcommon.cmxa -I src $(FILES)
48+
49+
build-native: lib/refmt.exe lib/napkinscript.exe depend
50+
4651

4752
bootstrap: build-native
4853
ocaml unix.cma ./scripts/bspack.ml -bs-main Napkin_cli -I src -o ./lib/napkinscript.ml

0 commit comments

Comments
 (0)