|
| 1 | +//@ts-check |
| 2 | +// usage : node ./script/shake.js bsb |
| 3 | +var cp = require("child_process"); |
| 4 | +var path = require("path"); |
| 5 | +var fs = require("fs"); |
| 6 | + |
| 7 | +var bsc = path.join(__dirname, "..", process.platform, "bsc"); |
| 8 | +var ocamlopt = path.join( |
| 9 | + __dirname, |
| 10 | + "..", |
| 11 | + "native", |
| 12 | + "4.06.1", |
| 13 | + "bin", |
| 14 | + "ocamlopt.opt" |
| 15 | +); |
| 16 | + |
| 17 | +var base = process.argv[2]; |
| 18 | +if (base === undefined) { |
| 19 | + console.error(`please specifiy a base`); |
| 20 | + process.exit(2); |
| 21 | +} |
| 22 | +var file = `${base}.ml`; |
| 23 | +var cwd = path.join(__dirname, "..", "lib", "4.06.1"); |
| 24 | + |
| 25 | +// TODO replace it with yours |
| 26 | +var esy = `/Users/hongbozhang/git/genType/_esy/default/build/install/default/bin`; |
| 27 | + |
| 28 | +/** |
| 29 | + * |
| 30 | + * @param {string} file |
| 31 | + */ |
| 32 | +function dsource(file) { |
| 33 | + var output = cp.spawnSync( |
| 34 | + `${bsc} -bs-no-builtin-ppx -bs-syntax-only -dsource -c ${file} `, |
| 35 | + { |
| 36 | + cwd, |
| 37 | + encoding: "utf8", |
| 38 | + shell: true |
| 39 | + } |
| 40 | + ); |
| 41 | + // check output.status |
| 42 | + if(output.status === 0){ |
| 43 | + fs.writeFileSync(path.join(cwd, file), output.stderr); |
| 44 | + } else { |
| 45 | + console.error(`dsource failure`) |
| 46 | + console.error(output.stderr) |
| 47 | + process.exit(2) |
| 48 | + } |
| 49 | + |
| 50 | + // fs.copyFileSync(path.join(cwd, tmp), path.join(cwd, file)); |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * |
| 55 | + * @param {string} file |
| 56 | + * @param {string} msg |
| 57 | + */ |
| 58 | +function checkDiff(file, msg) { |
| 59 | + var output = cp.spawnSync(`git diff --quiet ${file}`, { shell: true , encoding : 'utf8',cwd}); |
| 60 | + if (output.status !== 0) { |
| 61 | + var output = cp.spawnSync(`git add ${file} && git commit -m "${msg} for ${file}"`, { shell: true,encoding:'utf8',cwd }); |
| 62 | + if(output.status !== 0){ |
| 63 | + console.error(`diff failure for ${file} -- ${msg}`) |
| 64 | + process.exit(2) |
| 65 | + } else { |
| 66 | + console.log(output.stdout) |
| 67 | + } |
| 68 | + |
| 69 | + } else { |
| 70 | + console.log(`nothing changes to ${file}`); |
| 71 | + } |
| 72 | +} |
| 73 | +/** |
| 74 | + * |
| 75 | + * @param {string} file |
| 76 | + */ |
| 77 | +function shake(file) { |
| 78 | + var output = cp.spawnSync( |
| 79 | + `${bsc} -bs-no-builtin-ppx -bs-syntax-only -dsource -ppx ${esy}/deadcodeppx.exe -c ${file}`, |
| 80 | + { |
| 81 | + cwd, |
| 82 | + encoding: "utf8", |
| 83 | + shell: true |
| 84 | + } |
| 85 | + ); |
| 86 | + if(output.status !== 0){ |
| 87 | + console.error(`shake failure`) |
| 88 | + console.error(output.stderr) |
| 89 | + process.exit(2) |
| 90 | + } else { |
| 91 | + fs.writeFileSync(path.join(cwd, file), output.stderr); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * |
| 97 | + * @param {string} file |
| 98 | + */ |
| 99 | +function attachDead(file) { |
| 100 | + var output = cp.spawnSync(`${ocamlopt} -bin-annot -c ${file}`, { |
| 101 | + cwd, |
| 102 | + encoding: "utf8", |
| 103 | + shell : true |
| 104 | + }); |
| 105 | + |
| 106 | + var genType = path.join(esy, "genType.exe"); |
| 107 | + |
| 108 | + output = cp.spawnSync(`Write=1 ${genType} -dce-cmt ${base}.cmt`, { |
| 109 | + cwd, |
| 110 | + encoding: "utf8", |
| 111 | + shell: true |
| 112 | + }); |
| 113 | + if(output.status!==0){ |
| 114 | + console.error(`dce failure`) |
| 115 | + console.error(output.stderr) |
| 116 | + process.exit(2) |
| 117 | + } |
| 118 | + // fs.writeFileSync(path.join(cwd,file),output.stderr) |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | +// normalize files |
| 123 | +dsource(file); |
| 124 | +checkDiff(file, `dsource changes`); |
| 125 | +attachDead(file); |
| 126 | +shake(file); |
| 127 | +checkDiff(file, `shake`); |
0 commit comments