Skip to content

Commit

Permalink
feat: cli 版本支持 handle_options, 目前支持-h -v -o -j
Browse files Browse the repository at this point in the history
  • Loading branch information
lxx.imac committed Nov 4, 2019
1 parent f5494ba commit 2a6d8e8
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 41 deletions.
2 changes: 1 addition & 1 deletion dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<link rel="stylesheet" href="https://unpkg.com/vue-material/dist/vue-material.min.css">
<link rel="stylesheet" href="https://unpkg.com/vue-material/dist/theme/default.css">
<link rel="stylesheet" type="text/css" href="./main.css">
<title>COStreamJS-v0.4.0</title>
<title>COStreamJS-v0.4.1</title>
</head>

<body>
Expand Down
42 changes: 8 additions & 34 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { GreedyPartition } from "./src/BackEnd/GreedyPartition"
import { GetSpeedUpInfo, PrintSpeedUpInfo } from "./src/BackEnd/ComputeSpeedUp"
import { StageAssignment } from "./src/BackEnd/StageAssignment"
import { codeGeneration } from "./src/LifeCycle/codeGeneration"
import { version } from './package.json';
import handle_options from './src/LifeCycle/handle_options'

Object.assign(COStreamJS.__proto__, {
parser,
Expand All @@ -28,7 +30,8 @@ Object.assign(COStreamJS.__proto__, {
PrintSpeedUpInfo,
StageAssignment,
codeGeneration,
SymbolTable
SymbolTable,
version
})
COStreamJS.main = function(str, cpuCoreNum = 4){
debugger
Expand Down Expand Up @@ -58,39 +61,10 @@ Object.assign(COStreamJS.global, NodeTypes, {
COStreamJS
})

export default COStreamJS

/** 下面的代码用于支持命令行功能 */
if (typeof require !== 'undefined' && typeof exports !== 'undefined') {

const fs = require('fs')
const argv = require('yargs')
.option('j', {
alias: 'nCpucore',
demand: false,
default: '4',
describe: '设置可用核数',
type: 'string'
})
.argv;

exports.main = function commonjsMain(args) {
if (!args[1]) {
console.log('Usage: ' + args[0] + ' FILE');
process.exit(1);
}

const source_content = fs.readFileSync(require('path').normalize(args[1]), "utf8");
const source_filename = args[1].split('/').pop().split('.')[0]
COStreamJS.main(source_content, argv.j); //执行编译
fs.rmdirSync('./dist/' + source_filename, { recursive: true })
fs.mkdirSync('./dist/' + source_filename)
Object.entries(COStreamJS.files).forEach(([out_filename, content]) =>{
fs.writeFileSync(`./dist/${source_filename}/${out_filename}`, content)
})
};
if (typeof module !== 'undefined' && require.main === module) {
exports.main(process.argv.slice(1));
}
if (typeof module !== 'undefined' && require.main === module) {
handle_options.main(process.argv);
}

export default COStreamJS

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "costreamjs",
"description": "A high-performance streaming programming language for parallel architecture. This repo (js-version) is created for better using & reading & debugging.",
"version": "0.4.0",
"version": "0.4.1",
"main": "main.js",
"repository": "git@github.com:DML308/COStreamJS.git",
"author": "lxx.imac <lxx2013@mail.ustc.edu.cn>",
Expand All @@ -24,7 +24,9 @@
"t": "mocha --require babel-register",
"test:cover": "istanbul cover test/lexer.test.js"
},
"dependencies": {},
"dependencies": {
"yargs": "^14.2.0"
},
"devDependencies": {
"babel-preset-env": "^1.7.0",
"babel-register": "^6.26.0",
Expand All @@ -34,6 +36,7 @@
"mocha": "^6.1.4",
"rollup": "^1.11.3",
"rollup-plugin-commonjs": "^9.3.4",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-resolve": "^4.2.3",
"stylus": "^0.54.5"
},
Expand Down
8 changes: 5 additions & 3 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import json from 'rollup-plugin-json';

export default {
input: 'main.js',
output: [
Expand All @@ -11,10 +13,10 @@ export default {
{
file: 'dist/costreamjs-cli.js',
format: 'iife',
name: 'COStreamJS',
banner:'#!/usr/bin/env node'
name: 'COStreamJS',
banner: '#!/usr/bin/env node'
}
],
plugins: [ resolve(), commonjs() ],
plugins: [ resolve(), commonjs(), json() ],
external: [ 'fs' ]
};
3 changes: 2 additions & 1 deletion src/LifeCycle/ast2ssg.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export function AST2FlatStaticStreamGraph(mainComposite,unfold){
/* 将每个composite重命名 */
ssg.ResetFlatNodeNames();
ssg.SetFlatNodesWeights();
debug("--------- 执行AST2FlatStaticStreamGraph后, 查看静态数据流图 ssg 的结构中的全部 FlatNode ---------\n",ssg)
debug("--------- 执行AST2FlatStaticStreamGraph后, 查看静态数据流图 ssg 的结构中的全部 FlatNode ---------\n");
typeof window !== 'undefined' && debug(ssg);
return ssg
}

Expand Down
52 changes: 52 additions & 0 deletions src/LifeCycle/handle_options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import COStreamJS from '../../main';
import { version } from '../../package.json';

const handle_options = { main: () => {} };
const Usage = `
Version ${version}
Usage: COStream [options] [file]
Parses <file> as a COStream program, reporting syntax and type errors, and writes paralleled program out to <file>. If <file> is null, uses stdin and stdout.
`;

(function handleOptions_Main() {
if (typeof require !== 'undefined' && typeof exports !== 'undefined') {
const fs = require('fs');
const argv = require('yargs').usage(Usage).option({
j: { alias: 'nCpucore', type: 'number', default: 4},
w: { alias: 'nowarning' },
o: { alias: 'output', describe: '设置输出路径, 默认为 dist/filename' },
v: { alias: 'version' }
}).argv;

handle_options.main = function commonjsMain(args) {
console.log(`控制台参数:\n`,argv);
if (!argv._[0]) {
require('yargs').showHelp();
process.exit(1);
}
if (argv.v) {
console.log(`COStreamJS Version: ${COStreamJS.version}`);
process.exit(1);
}

const source_path = require('path').normalize(argv._[0]);
const source_content = fs.readFileSync(source_path, 'utf8');
const source_filename = source_path.split('/').pop().split('.')[0];
console.log(`输入文件信息:\n`, source_path, source_filename);

const removeLastChar = (s) => (s[s.length - 1] === '/' ? s.slice(0, -1) : s); //移除路径最后的一个'/'
/** 设置输出文件夹的路径 */
const outDir = (argv.o && removeLastChar(argv.o)) || `./dist/${source_filename}`;

COStreamJS.main(source_content, argv.j || 4); //执行编译
fs.rmdirSync(outDir, { recursive: true });
fs.mkdirSync(outDir);
Object.entries(COStreamJS.files).forEach(([ out_filename, content ]) => {
fs.writeFileSync(`${outDir}/${out_filename}`, content);
});
};
}
})();

export default handle_options;

0 comments on commit 2a6d8e8

Please sign in to comment.