Skip to content

Commit

Permalink
implement command extractSource, resolve #84
Browse files Browse the repository at this point in the history
  • Loading branch information
qjebbs committed Nov 29, 2017
1 parent ee14fa4 commit 4f52074
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 6 deletions.
4 changes: 3 additions & 1 deletion langs/lang.nls.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@
"{0} Dokumente, {1} Diagramme, {2} Dateien exportiert in {3} Sekunden:",
"Keine Diagramme exportiert.",
"Diagram unnamed. Try \"@startuml name\"",
"Duplicate diagram name \"{0}\"."
"Duplicate diagram name \"{0}\".",
"Select to Extract...",
"Extracting ({0}/{1}): {2}"
]
4 changes: 3 additions & 1 deletion langs/lang.nls.ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@
"{3} 秒で {0} 個のドキュメント, {1} 個のダイアグラム, {2} 個のファイルをエクスポートしました",
"ダイアグラムはエクスポートされませんでした",
"ダイアグラム名が未設定です。 \"@startuml name\" のように設定してください",
"ダイアグラム名が重複しています: \"{0}\""
"ダイアグラム名が重複しています: \"{0}\"",
"Select to Extract...",
"Extracting ({0}/{1}): {2}"
]
4 changes: 3 additions & 1 deletion langs/lang.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@
"{0} documents, {1} digrams, {2} files exported in {3} seconds:",
"No diagram exported.",
"Diagram unnamed. Try \"@startuml name\"",
"Duplicate diagram name \"{0}\"."
"Duplicate diagram name \"{0}\".",
"Select to Extract...",
"Extracting ({0}/{1}): {2}"
]
4 changes: 3 additions & 1 deletion langs/lang.nls.zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@
"共导出{0}个文档,{1}个图表,{2}个文件,用时 {3} 秒:",
"没有导出任何图表",
"图表未命名。请使用 \"@startuml 图表名称\"",
"重复的图表名称 \"{0}\"."
"重复的图表名称 \"{0}\".",
"选择并提取...",
"正在提取 ({0}/{1}): {2}"
]
4 changes: 3 additions & 1 deletion langs/lang.nls.zh-tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@
"{0} documents, {1} digrams, {2} files exported in {3} seconds:",
"No diagram exported.",
"Diagram unnamed. Try \"@startuml name\"",
"Duplicate diagram name \"{0}\"."
"Duplicate diagram name \"{0}\".",
"Select to Extract...",
"Extracting ({0}/{1}): {2}"
]
21 changes: 20 additions & 1 deletion src/plantuml/common.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
import * as vscode from 'vscode';
import * as nls from "vscode-nls";
import * as child_process from 'child_process';
import { join } from "path";

export const languageid = "diagram";
export const java: string = "java";

export var outputPanel = vscode.window.createOutputChannel("PlantUML");
export var context: vscode.ExtensionContext;
export var localize: nls.LocalizeFunc;
export var bar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);

let _javaInstalled: boolean = undefined;

export function setContext(ctx: vscode.ExtensionContext) {
context = ctx;
nls.config(<nls.Options>{ locale: vscode.env.language });
localize = nls.loadMessageBundle(join(context.extensionPath, "langs", "lang.json"));
}
}

export async function javaInstalled(): Promise<boolean> {
if (_javaInstalled === undefined)
_javaInstalled = await new Promise<boolean>((resolve, reject) => {
child_process.exec(java + " -version", (e, stdout, stderr) => {
if (e instanceof Error) {
resolve(false);
}
resolve(true);
});
});
return _javaInstalled;
}
84 changes: 84 additions & 0 deletions src/plantuml/sourceExtracter/extractSource.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,89 @@
import * as vscode from 'vscode';
import * as child_process from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import { localize, languageid, javaInstalled, java, bar } from '../common';
import { config } from '../config';
import { processWrapper } from '../tools';

export async function extractSource() {

if (!await javaInstalled()) {
vscode.window.showErrorMessage(localize(5, null));
return;
}

let imgs = await vscode.window.showOpenDialog(<vscode.OpenDialogOptions>{
openLabel: localize(32, null),
defaultUri: vscode.Uri.file(vscode.workspace.rootPath),
canSelectMany: true,
filters: { 'Images': ['png'] },
});
if (!imgs || !imgs.length) return;

let sources = await extract(imgs);
vscode.workspace.openTextDocument({
language: languageid,
content: sources.reduce((srcs, src) => srcs + '\n' + src)
}).then(doc => vscode.window.showTextDocument(doc));
bar.hide();
}

function extract(imgs: vscode.Uri[]) {
let sources: string[] = [];
let pms = imgs.reduce(
(pChain, img, index) => {

if (!fs.existsSync(img.fsPath)) {
sources.push("File not found: " + img.fsPath);
return Promise.resolve(null);
}

let params = [
'-Djava.awt.headless=true',
'-jar',
config.jar,
"-metadata",
img.fsPath,
];

// processes.push(process);
return pChain.then(
() => {
if (bar) {
bar.show();
bar.text = localize(33, null, index + 1, imgs.length, path.basename(img.fsPath));
}
let process = child_process.spawn(java, params);

let pms = processWrapper(process).then(
result => new Promise<Buffer>((resolve, reject) => {
let stdout = result[0].toString();
let stderr = result[1].toString();
if (stderr.length) {
sources.push(stderr);
} else {
sources.push(stdout);
};
resolve(null)
})
);
return pms;
},
err => {
console.log(err);
}
)
},
Promise.resolve(null)
);
return new Promise<string[]>(
(resolve, reject) => {
pms.then(
() => {
resolve(sources);
}
)
}
)
}
32 changes: 32 additions & 0 deletions src/plantuml/tools.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
import * as child_process from 'child_process';

import { RenderError } from './renders/interfaces';
import { Diagram } from './diagram/diagram';
Expand Down Expand Up @@ -111,4 +112,35 @@ export function addFileIndex(fileName: string, index: number, count: number): st
path.dirname(fileName),
bsName.substr(0, bsName.length - ext.length) + " page " + (index + 1) + ext,
);
}
export function processWrapper(process: child_process.ChildProcess, pipeFilePath?: string): Promise<[Buffer, Buffer]> {
return new Promise<[Buffer, Buffer]>((resolve, reject) => {
let buffOut: Buffer[] = [];
let buffOutLen = 0;
let buffErr: Buffer[] = [];
let buffErrLen = 0;

// let pipeFile = pipeFilePath ? fs.createWriteStream(pipeFilePath) : null;
// if (pipeFile) process.stdout.pipe(pipeFile);

process.stdout.on('data', function (x: Buffer) {
buffOut.push(x);
buffOutLen += x.length;
});

process.stderr.on('data', function (x: Buffer) {
buffErr.push(x);
buffErrLen += x.length;
});

process.stdout.on('close', () => {
let stdout = Buffer.concat(buffOut, buffOutLen);
if (pipeFilePath && stdout.length) {
fs.writeFileSync(pipeFilePath, stdout);
stdout = new Buffer(pipeFilePath);
}
let stderr = Buffer.concat(buffErr, buffErrLen);
resolve([stdout, stderr]);
});
});
}

0 comments on commit 4f52074

Please sign in to comment.