Skip to content

Instantly share code, notes, and snippets.

@bfitzpat
Created February 13, 2019 21:07
Show Gist options
  • Select an option

  • Save bfitzpat/304440ee5d419a618b9cc610b312042a to your computer and use it in GitHub Desktop.

Select an option

Save bfitzpat/304440ee5d419a618b9cc610b312042a to your computer and use it in GitHub Desktop.
Updated to make it work with the output channel - but it's not finding the jar
'use strict';
import * as child_process from 'child_process';
import * as fs from 'fs-extra';
import * as path from 'path';
import * as requirements from './requirements';
import * as vscode from 'vscode';
import * as fileUrl from 'file-url';
const options: vscode.OpenDialogOptions = {
canSelectMany: false,
openLabel: 'Open WSDL File',
filters: {
'WSDL files': ['wsdl'],
'All files': ['*']
}
};
var outputChannel;
export function activate(context: vscode.ExtensionContext) {
let wsdl2restExecutablePath = context.asAbsolutePath(path.join('jars','wsdl2rest.jar'));
let logPath = context.asAbsolutePath(path.join('jars', 'logging.properties'));
outputChannel = vscode.window.createOutputChannel("WSDL2Rest");
context.subscriptions.push(vscode.commands.registerCommand('extension.wsdl2rest', () => {
doWsdl2Rest(wsdl2restExecutablePath, logPath);
}));
}
async function doWsdl2Rest(wsdl2restExecutablePath: string, logPath : string) {
try {
let wsdlFileUri: any = await vscode.window.showOpenDialog(options);
if (wsdlFileUri) {
let dslChoice = await vscode.window.showQuickPick(
['Spring', 'Blueprint'],
{
placeHolder:
'Specify which DSL to generate the Camel configuration for'
}
);
if (dslChoice) {
let outputDir = await vscode.window.showInputBox({
prompt: 'Output Directory',
placeHolder: 'Enter the output directory for generated artifacts',
value: 'src/main/java'
});
if (outputDir) {
let jaxWs = await vscode.window.showInputBox({
prompt: 'JAXWS Endpoint',
placeHolder: 'Enter the address for the running jaxws endpoint',
value: 'http://localhost:8080/somepath'
});
if (jaxWs) {
let jaxRs = await vscode.window.showInputBox({
prompt: 'JAXRS Endpoint',
placeHolder: 'Enter the address for the jaxrs endpoint',
value: 'http://localhost:8081/jaxrs'
});
if (jaxRs) {
await callWsdl2Rest(
wsdl2restExecutablePath,
logPath,
outputDir,
wsdlFileUri.toString(),
dslChoice,
jaxRs,
jaxWs,
true
);
}
}
}
}
}
} catch (error) {
vscode.window.showErrorMessage('Error while processing wsdl2rest: ' + error);
}
}
async function callWsdl2Rest(
wsdl2restExecutablePath: string,
logPath: string,
outputDirectory: any,
wsdlUrl: any,
dsl: any,
jaxrs: any,
jaxws: any,
isDebug: boolean
) {
let storagePath = vscode.workspace.rootPath;
let logUrl = fileUrl(logPath);
let actualJavaOutDirectory: string = outputDirectory;
if (actualJavaOutDirectory.endsWith('/java')) {
actualJavaOutDirectory = actualJavaOutDirectory.substring(0, actualJavaOutDirectory.indexOf('/java'));
}
let outPath: string = path.join(storagePath, actualJavaOutDirectory.toString());
let wsdlFileUrl: string;
if (wsdlUrl.startsWith('http')) {
wsdlFileUrl = wsdlUrl;
} else if (!wsdlUrl.startsWith('file:')) {
wsdlFileUrl = fileUrl(wsdlUrl);
} else {
wsdlFileUrl = wsdlUrl;
}
if (!fs.existsSync(outPath.toString())) {
if (outputChannel) {
outputChannel.append(`Creating wsdl2rest java output directory\n`);
}
await fs.ensureDir(outPath.toString());
}
var restContextPath;
var rawContextPath: any;
const isBlueprint: boolean = dsl.match(/blueprint/i);
const isSpringBoot: boolean = dsl.match(/spring-boot/i);
const isSpring: boolean = dsl.match(/spring/i);
if (isBlueprint) {
rawContextPath = 'src/main/resources/OSGI-INF/blueprint/blueprint.xml';
} else if (isSpringBoot) {
rawContextPath = 'src/main/resources/camel-context.xml';
} else if (isSpring) {
rawContextPath = 'src/main/resources/META-INF/spring/camel-context.xml';
}
restContextPath = path.join(storagePath, rawContextPath);
// build the java command with classpath, class name, and the passed parameters
var cmdString = 'java '
+ ' -Dlog4j.configuration=' + logUrl
+ ' -jar ' + wsdl2restExecutablePath
+ ' --wsdl ' + wsdlFileUrl
+ ' --out ' + outPath;
if (isBlueprint) {
cmdString = cmdString + ' --blueprint-context ' + restContextPath;
} else {
cmdString = cmdString + ' --camel-context ' + restContextPath;
}
if (jaxrs) {
cmdString = cmdString + ' --jaxrs ' + jaxrs;
}
if (jaxws) {
cmdString = cmdString + ' --jaxws ' + jaxws;
}
if (outputChannel) {
outputChannel.append('Calling wsdl2rest \n');
if (isDebug) {
outputChannel.append(' command used: ' + cmdString + '\n');
}
}
return new Promise((resolve, reject) => {
requirements.resolveRequirements()
.then(requirements => {
let process = child_process.exec(cmdString);
process.stdout.on('data', function (data:any) {
outputChannel.append(`stdout: ${data} \n`);
});
process.stderr.on('data', function (data:any) {
outputChannel.append(`stderr: ${data} \n`);
});
process.on('close', function (code:any) {
if (code === 0) {
if (outputChannel) {
outputChannel.append('Created CXF artifacts for specified WSDL at ' + outputDirectory + '\n');
outputChannel.append('Created ' + rawContextPath + '\n');
}
resolve();
} else {
vscode.window.showErrorMessage(`Wsdl2Rest did not generate artifacts successfully - please check the output channel for details\n`);
reject(code);
}
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment