Skip to content

Commit b96cf2f

Browse files
committed
prepare watcher for bs-platform itself
1 parent 251699b commit b96cf2f

32 files changed

+2145
-486
lines changed

Diff for: .vscode/tasks.json

+31-34
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,36 @@
11
{
2-
// See https://go.microsoft.com/fwlink/?LinkId=733558
3-
// for the documentation about the tasks.json format
42
"version": "0.1.0",
5-
"command": "make",
3+
"command": "node",
4+
"options": {
5+
"cwd": "${workspaceRoot}/jscomp"
6+
},
67
"isShellCommand": true,
7-
"showOutput": "always",
8-
"tasks": [
9-
{
10-
"taskName": "bsb",
11-
"args": [
12-
"-C",
13-
"jscomp",
14-
"bin/bsb.exe"
15-
],
16-
"showOutput": "silent",
17-
"isBuildCommand": true,
18-
"problemMatcher": {
19-
// "fileLocation": "absolute",
20-
"owner": "ocaml",
21-
"pattern": [
22-
{
23-
"regexp": "^File \"(.*)\", line (\\d+)(?:, characters (\\d+)-(\\d+))?:$",
24-
"file": 1,
25-
"line": 2,
26-
"column": 3,
27-
"endColumn": 4
28-
},
29-
{
30-
"regexp": "^(?:(?:Parse\\s+)?(Warning|[Ee]rror)(?:\\s+\\d+)?:)?\\s+(.*)$",
31-
"severity": 1,
32-
"message": 2,
33-
"loop": true
34-
}
35-
]
8+
"args": [
9+
"../scripts/watcher.js"
10+
],
11+
"isBackground": true,
12+
"problemMatcher": {
13+
"fileLocation": "absolute",
14+
"owner": "ocaml",
15+
"watching": {
16+
"activeOnStart": true,
17+
"beginsPattern": ">>>> Start compiling",
18+
"endsPattern": ">>>> Finish compiling"
19+
},
20+
"pattern": [
21+
{
22+
"regexp": "^File \"(.*)\", line (\\d+)(?:, characters (\\d+)-(\\d+))?:$",
23+
"file": 1,
24+
"line": 2,
25+
"column": 3,
26+
"endColumn": 4
27+
},
28+
{
29+
"regexp": "^(?:(?:Parse\\s+)?(Warning|[Ee]rror)(?:\\s+\\d+)?:)?\\s+(.*)$",
30+
"severity": 1,
31+
"message": 2,
32+
"loop": true
3633
}
37-
}
38-
]
34+
]
35+
}
3936
}

Diff for: jscomp/bin/bsb_watcher.future.js

+212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
'use strict';
2+
3+
var Path = require('path');
4+
var Process = require('process');
5+
var Fs = require('fs');
6+
var Child_process = require('child_process');
7+
8+
function filterInPlace(p, a) {
9+
var i = 0;
10+
var j = 0;
11+
while(i < a.length) {
12+
var v = a[i];
13+
if (p(v)) {
14+
a[j] = v;
15+
j = j + 1 | 0;
16+
}
17+
i = i + 1 | 0;
18+
}
19+
a.splice(j);
20+
return /* () */0;
21+
}
22+
23+
function memByRef(x, xs) {
24+
return +(xs.indexOf(x) >= 0);
25+
}
26+
27+
28+
/* No side effect */
29+
30+
function getExn(x) {
31+
if (x) {
32+
return x[0];
33+
} else {
34+
var str = " " + (String("File \"bs_option.ml\", line 39, characters 34-42") + ": Bs.Option.unsafeGet");
35+
throw new Error(str);
36+
}
37+
}
38+
39+
40+
/* No side effect */
41+
42+
function undefined_to_opt(x) {
43+
if (x === undefined) {
44+
return /* None */0;
45+
} else {
46+
return /* Some */[x];
47+
}
48+
}
49+
50+
51+
/* No side effect */
52+
53+
/* unsafeDeleteKey Not a pure module */
54+
55+
function putEnvVar(key, $$var) {
56+
Process.env[key] = $$var;
57+
return /* () */0;
58+
}
59+
60+
61+
/* Js_dict Not a pure module */
62+
63+
function getWatchFiles(file) {
64+
if (Fs.existsSync(file)) {
65+
return Fs.readFileSync(file, "utf8").split("\n").filter(function (x) {
66+
return +(x.trim().length !== 0);
67+
});
68+
} else {
69+
return /* array */[];
70+
}
71+
}
72+
73+
function makeEventObj() {
74+
return {
75+
events: /* array */[],
76+
empty: function () {
77+
var self = this;
78+
var a = self.events;
79+
a.splice(0);
80+
return /* () */0;
81+
},
82+
push: function (a) {
83+
var self = this;
84+
var xs = self.events;
85+
xs.push(a);
86+
return /* () */0;
87+
},
88+
needRebuild: function () {
89+
var self = this;
90+
return +(self.events.length !== 0);
91+
},
92+
currentEvents: function () {
93+
var self = this;
94+
return self.events;
95+
}
96+
};
97+
}
98+
99+
function makeLock() {
100+
return {
101+
isBuilding: /* false */0,
102+
acquire: function () {
103+
var self = this;
104+
if (self.isBuilding) {
105+
return /* false */0;
106+
} else {
107+
self.isBuilding = /* true */1;
108+
return /* true */1;
109+
}
110+
},
111+
release: function () {
112+
var self = this;
113+
return self.isBuilding = /* false */0;
114+
}
115+
};
116+
}
117+
118+
function build(cmd, $$event, lock, idle) {
119+
if (lock.acquire()) {
120+
console.log(">>>> Start compiling");
121+
var events = $$event.currentEvents();
122+
console.log("Rebuilding since " + (String(events) + " "));
123+
$$event.empty();
124+
Child_process.spawn(cmd, ( [ ]), ( { "stdio" : "inherit" })).on("exit", function () {
125+
console.log(">>>> Finish compiling");
126+
lock.release();
127+
if ($$event.needRebuild()) {
128+
return build(cmd, $$event, lock, idle);
129+
} else {
130+
return idle();
131+
}
132+
});
133+
return /* () */0;
134+
} else {
135+
return 0;
136+
}
137+
}
138+
139+
function makeWatcher(file, onChange) {
140+
return /* record */[
141+
/* dir */file,
142+
/* watcher */Fs.watch(file, onChange)
143+
];
144+
}
145+
146+
147+
/* Not a pure module */
148+
149+
var sourceDirs = Path.join("lib", "bs", ".sourcedirs");
150+
151+
var lock = makeLock(/* () */0);
152+
153+
var events = makeEventObj(/* () */0);
154+
155+
var watchers = /* array */[];
156+
157+
var source_dirname = getExn(undefined_to_opt(typeof (__dirname) === "undefined" ? undefined : (__dirname)));
158+
159+
var bsb = Path.join(source_dirname, "bsb.exe");
160+
161+
var bsconfig = "bsconfig.json";
162+
163+
function onChange(eventType, fileName) {
164+
console.log("Event " + (String(eventType) + (" " + (String(fileName) + ""))));
165+
events.push(/* tuple */[
166+
eventType,
167+
fileName
168+
]);
169+
return build(bsb, events, lock, function () {
170+
return idle(/* () */0);
171+
});
172+
}
173+
174+
function idle() {
175+
var watchFiles = getWatchFiles(sourceDirs);
176+
filterInPlace(function (param) {
177+
var dir = param[/* dir */0];
178+
if (dir === bsconfig || memByRef(dir, watchFiles)) {
179+
return /* true */1;
180+
} else {
181+
console.log(" " + (String(dir) + " is no longer watched"));
182+
param[/* watcher */1].close();
183+
return /* false */0;
184+
}
185+
}, watchers);
186+
watchFiles.forEach(function (dir) {
187+
if (watchers.some(function (param) {
188+
return +(param[/* dir */0] === dir);
189+
})) {
190+
return 0;
191+
} else {
192+
console.log("watching dir " + (String(dir) + " now "));
193+
var x = makeWatcher(dir, onChange);
194+
watchers.push(x);
195+
return /* () */0;
196+
}
197+
});
198+
return /* () */0;
199+
}
200+
201+
putEnvVar("BS_VSCODE", "1");
202+
203+
var x = makeWatcher(bsconfig, onChange);
204+
205+
watchers.push(x);
206+
207+
build(bsb, events, lock, function () {
208+
return idle(/* () */0);
209+
});
210+
211+
212+
/* sourceDirs Not a pure module */

Diff for: jscomp/install-bsb-watcher.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
3+
# dev small utils
4+
# hot replace global bsb.exe for quick testing
5+
cp bin/bsb_watcher.js /usr/local/lib/node_modules/bs-platform/bin/

Diff for: jscomp/others/.depend

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ js_json.cmj : js_types.cmj js_string.cmj js_dict.cmj js_array.cmj \
1212
js_obj.cmj :
1313
bs_dyn.cmj : bs_dyn.cmi
1414
bs_dyn_lib.cmj : bs_dyn.cmj bs_dyn_lib.cmi
15+
bs_array.cmj : js_array.cmj bs_array.cmi
16+
bs_list.cmj : bs_list.cmi
17+
bs_option.cmj : bs_option.cmi
18+
bs_result.cmj :
1519
node_child_process.cmj : node.cmj
1620
js_boolean.cmj : js_boolean.cmi
1721
js_math.cmj :
@@ -27,6 +31,9 @@ js_types.cmi :
2731
js_json.cmi : js_types.cmi js_string.cmj js_dict.cmi
2832
bs_dyn.cmi :
2933
bs_dyn_lib.cmi : bs_dyn.cmi
34+
bs_array.cmi :
35+
bs_list.cmi :
36+
bs_option.cmi :
3037
js_boolean.cmi :
3138
js_dict.cmi :
3239
js_cast.cmi :

Diff for: jscomp/others/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ COMPILER=../bin/bsc.exe
55
MAP_FILES= node bs
66

77
SOURCE_LIST= node_path node_fs node_process dict node_module js_array js_string \
8-
js_re js_null_undefined node_buffer js_types js_json js_obj bs_dyn bs_dyn_lib \
8+
js_re js_null_undefined node_buffer js_types js_json js_obj \
9+
bs_dyn bs_dyn_lib bs_array bs_list bs_option bs_result \
910
node_child_process js_boolean js_math js_dict js_date js_global js_cast js_promise dom
1011

1112
$(addsuffix .cmj, $(SOURCE_LIST)): $(addsuffix .cmj, $(MAP_FILES))

Diff for: jscomp/others/bs.ml

+5
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ module Dyn = Bs_dyn
2929

3030
module Dyn_lib = Bs_dyn_lib
3131
(**/*)
32+
33+
module Array = Bs_array
34+
module List = Bs_list
35+
module Option = Bs_option
36+
module Result = Bs_result

Diff for: jscomp/others/bs_array.ml

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
type 'a t = 'a array
26+
27+
(** @param a array
28+
@param p predicate
29+
*)
30+
let filterInPlace p a =
31+
let i = ref 0 in
32+
let j = ref 0 in
33+
while !i < Array.length a do
34+
let v = Array.unsafe_get a !i in
35+
if p v [@bs] then
36+
begin
37+
Array.unsafe_set a !j v ;
38+
incr j
39+
end;
40+
incr i
41+
done;
42+
ignore @@ Js_array.removeFromInPlace ~pos:!j a
43+
44+
let empty a =
45+
ignore @@ Js.Array.removeFromInPlace ~pos:0 a
46+
47+
let pushBack x xs =
48+
ignore @@ Js.Array.push x xs
49+
50+
(** Find by JS (===) equality *)
51+
let memByRef x xs =
52+
Js.Array.indexOf x xs >= 0
53+
54+
let iter f xs =
55+
for i = 0 to Array.length xs - 1 do
56+
f (Array.unsafe_get xs i) [@bs]
57+
done
58+
59+
(* here [f] is of type ['a -> 'b]
60+
let iterX f xs =
61+
for i = 0 to Array.length xs - 1 do
62+
f (Array.unsafe_get xs i)
63+
done
64+
*)

0 commit comments

Comments
 (0)