Skip to content

Commit

Permalink
Generate dockerfile for java app (#235)
Browse files Browse the repository at this point in the history
* Generate dockerfile for java
  • Loading branch information
testforstephen authored and chrisdias committed Mar 14, 2018
1 parent ec5e219 commit 916d9c7
Show file tree
Hide file tree
Showing 4 changed files with 355 additions and 35 deletions.
1 change: 1 addition & 0 deletions configureWorkspace/config-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export async function quickPickPlatform(): Promise<string>{

const items: string[] = [];
items.push('Go');
items.push('Java');
items.push('.NET Core');
items.push('Node.js');
items.push('Python');
Expand Down
113 changes: 104 additions & 9 deletions configureWorkspace/configure.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import vscode = require('vscode');
import * as path from 'path';
import * as fs from 'fs';
import * as pomParser from 'pom-parser';
import * as gradleParser from 'gradle-to-js/lib/parser';
import { promptForPort, quickPickPlatform } from './config-utils';
import { reporter } from '../telemetry/telemetry';

function genDockerFile(serviceName: string, platform: string, port: string, { cmd, author, version }: PackageJson): string {
function genDockerFile(serviceName: string, platform: string, port: string, { cmd, author, version, artifactName }: PackageJson): string {
switch (platform.toLowerCase()) {
case 'node.js':

Expand Down Expand Up @@ -80,6 +82,20 @@ CMD ["python3", "-m", "${serviceName}"]
# Using miniconda (make sure to replace 'myenv' w/ your environment name):
#RUN conda env create -f environment.yml
#CMD /bin/bash -c "source activate myenv && python3 -m ${serviceName}"
`;

case 'java':
const artifact = artifactName ? artifactName : `${serviceName}.jar`;
return `
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAVA_OPTS
ENV JAVA_OPTS=$JAVA_OPTS
ADD ${artifact} ${serviceName}.jar
EXPOSE ${port}
ENTRYPOINT exec java $JAVA_OPTS -jar ${serviceName}.jar
# For Spring-Boot project, use the entrypoint below to reduce Tomcat startup time.
#ENTRYPOINT exec java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar ${serviceName}.jar
`;

default:
Expand Down Expand Up @@ -137,6 +153,15 @@ build: .
ports:
- ${port}:${port}`;

case 'java':
return `version: '2.1'
services:
${serviceName}:
image: ${serviceName}
build: .
ports:
- ${port}:${port}`;

default:
return `version: '2.1'
Expand Down Expand Up @@ -221,6 +246,22 @@ services:
- ${port}:${port}
`;

case 'java':
return `version: '2.1'
services:
${serviceName}:
image: ${serviceName}
build:
context: .
dockerfile: Dockerfile
environment:
JAVA_OPTS: -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005,quiet=y
ports:
- ${port}:${port}
- 5005:5005
`;

default:
return `version: '2.1'
Expand Down Expand Up @@ -255,23 +296,29 @@ interface PackageJson {
cmd: string,
fullCommand: string, //full command
author: string,
version: string
version: string,
artifactName: string
}

async function getPackageJson(folder: vscode.WorkspaceFolder): Promise<vscode.Uri[]> {
return vscode.workspace.findFiles(new vscode.RelativePattern(folder, 'package.json'), null, 1, null);
}

async function readPackageJson(folder: vscode.WorkspaceFolder): Promise<PackageJson> {
// open package.json and look for main, scripts start
const uris: vscode.Uri[] = await getPackageJson(folder);
var pkg: PackageJson = {
function getDefaultPackageJson(): PackageJson {
return {
npmStart: true,
fullCommand: 'npm start',
cmd: 'npm start',
author: 'author',
version: '0.0.1'
}; //default
version: '0.0.1',
artifactName: ''
};
}

async function readPackageJson(folder: vscode.WorkspaceFolder): Promise<PackageJson> {
// open package.json and look for main, scripts start
const uris: vscode.Uri[] = await getPackageJson(folder);
var pkg: PackageJson = getDefaultPackageJson(); //default

if (uris && uris.length > 0) {
const json = JSON.parse(fs.readFileSync(uris[0].fsPath, 'utf8'));
Expand Down Expand Up @@ -300,6 +347,49 @@ async function readPackageJson(folder: vscode.WorkspaceFolder): Promise<PackageJ
return pkg;
}

async function readPomOrGradle(folder: vscode.WorkspaceFolder): Promise<PackageJson> {
var pkg: PackageJson = getDefaultPackageJson(); //default

if (fs.existsSync(path.join(folder.uri.fsPath, 'pom.xml'))) {
const json = await new Promise<any>((resolve, reject) => {
pomParser.parse({
filePath: path.join(folder.uri.fsPath, 'pom.xml')
}, (error, response) => {
if (error) {
reject(`Failed to parse pom.xml: ${error}`);
return;
}
resolve(response.pomObject);
});
});

if (json.project.version) {
pkg.version = json.project.version;
}

if (json.project.artifactid) {
pkg.artifactName = `target/${json.project.artifactid}-${pkg.version}.jar`;
}
} else if (fs.existsSync(path.join(folder.uri.fsPath, 'build.gradle'))) {
const json = await gradleParser.parseFile(path.join(folder.uri.fsPath, 'build.gradle'));

if (json.jar && json.jar.version) {
pkg.version = json.jar.version;
} else if (json.version) {
pkg.version = json.version;
}

if (json.jar && json.jar.archiveName) {
pkg.artifactName = `build/libs/${json.jar.archiveName}`;
} else {
const baseName = json.jar && json.jar.baseName ? json.jar.baseName : json.archivesBaseName || folder.name;
pkg.artifactName = `build/libs/${baseName}-${pkg.version}.jar`;
}
}

return pkg;
}

const DOCKER_FILE_TYPES = {
'docker-compose.yml': genDockerCompose,
'docker-compose.debug.yml': genDockerComposeDebug,
Expand Down Expand Up @@ -342,7 +432,12 @@ export async function configure(): Promise<void> {
if (!port) return;

const serviceName = path.basename(folder.uri.fsPath).toLowerCase();
const pkg = await readPackageJson(folder);
let pkg: PackageJson = getDefaultPackageJson();
if (platformType.toLowerCase() === 'java') {
pkg = await readPomOrGradle(folder);
} else {
pkg = await readPackageJson(folder);
}

await Promise.all(Object.keys(DOCKER_FILE_TYPES).map((fileName) => {
return createWorkspaceFileIfNotExists(fileName, DOCKER_FILE_TYPES[fileName]);
Expand Down
Loading

0 comments on commit 916d9c7

Please sign in to comment.