-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement command extractSource, resolve #84
- Loading branch information
Showing
8 changed files
with
151 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
) | ||
} | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters