Skip to content

Commit

Permalink
can add Java function to the function project
Browse files Browse the repository at this point in the history
  • Loading branch information
jdneo committed Nov 8, 2017
1 parent c4bde38 commit cc8d3b7
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 39 deletions.
87 changes: 49 additions & 38 deletions src/commands/createFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import { localize } from '../localize';
import { ConfigSetting, ValueType } from '../templates/ConfigSetting';
import { EnumValue } from '../templates/EnumValue';
import { Template } from '../templates/Template';
import { TemplateLanguage } from '../templates/Template';
import { TemplateData } from '../templates/TemplateData';
import * as fsUtil from '../utils/fs';
import { runCommandInTerminal } from '../utils/terminal';
import * as workspaceUtil from '../utils/workspace';
import { VSCodeUI } from '../VSCodeUI';
import * as CreateNewProject from './createNewProject';
Expand Down Expand Up @@ -109,47 +111,56 @@ export async function createFunction(
const functionAppPath: string = await workspaceUtil.selectWorkspaceFolder(ui, folderPlaceholder);
await validateIsFunctionApp(outputChannel, functionAppPath);

const localAppSettings: LocalAppSettings = new LocalAppSettings(ui, azureAccount, functionAppPath);

const templatePicks: PickWithData<Template>[] = (await templateData.getTemplates()).map((t: Template) => new PickWithData<Template>(t, t.name));
const templatePlaceHolder: string = localize('azFunc.selectFuncTemplate', 'Select a function template');
const template: Template = (await ui.showQuickPick<Template>(templatePicks, templatePlaceHolder)).data;

if (template.bindingType !== 'httpTrigger') {
await localAppSettings.validateAzureWebJobsStorage();
}

const name: string = await promptForFunctionName(ui, functionAppPath, template);

let showPrompts: boolean = true;
for (const settingName of template.userPromptedSettings) {
const setting: ConfigSetting | undefined = await templateData.getSetting(template.bindingType, settingName);
if (setting) {
try {
let settingValue: string | undefined;
const defaultValue: string | undefined = template.getSetting(settingName);
if (showPrompts) {
settingValue = await promptForSetting(ui, localAppSettings, setting, defaultValue);
} else {
settingValue = defaultValue;
}
const languageType: string = workspaceUtil.getProjectType(functionAppPath);
switch (languageType) {
case TemplateLanguage.Java:
// For Java function, using Maven for now.
runCommandInTerminal(`mvn azure-functions:add -f ${path.join(functionAppPath, 'pom.xml')} -B`);
break;
default:
const localAppSettings: LocalAppSettings = new LocalAppSettings(ui, azureAccount, functionAppPath);

const templatePicks: PickWithData<Template>[] = (await templateData.getTemplates()).map((t: Template) => new PickWithData<Template>(t, t.name));
const templatePlaceHolder: string = localize('azFunc.selectFuncTemplate', 'Select a function template');
const template: Template = (await ui.showQuickPick<Template>(templatePicks, templatePlaceHolder)).data;

if (template.bindingType !== 'httpTrigger') {
await localAppSettings.validateAzureWebJobsStorage();
}

template.setSetting(settingName, settingValue);
} catch (error) {
if (error instanceof UserCancelledError) {
const message: string = localize('azFunc.IncompleteFunction', 'Function \'{0}\' was created, but you must finish specifying settings in \'function.json\'.', name);
vscode.window.showWarningMessage(message);
showPrompts = false;
} else {
throw error;
const name: string = await promptForFunctionName(ui, functionAppPath, template);
let showPrompts: boolean = true;
for (const settingName of template.userPromptedSettings) {
const setting: ConfigSetting | undefined = await templateData.getSetting(template.bindingType, settingName);
if (setting) {
try {
let settingValue: string | undefined;
const defaultValue: string | undefined = template.getSetting(settingName);
if (showPrompts) {
settingValue = await promptForSetting(ui, localAppSettings, setting, defaultValue);
} else {
settingValue = defaultValue;
}

template.setSetting(settingName, settingValue);
} catch (error) {
if (error instanceof UserCancelledError) {
const message: string = localize('azFunc.IncompleteFunction', 'Function \'{0}\' was created, but you must finish specifying settings in \'function.json\'.', name);
vscode.window.showWarningMessage(message);
showPrompts = false;
} else {
throw error;
}
}
}
}
}
}

const functionPath: string = path.join(functionAppPath, name);
await template.writeTemplateFiles(functionPath);
const functionPath: string = path.join(functionAppPath, name);
await template.writeTemplateFiles(functionPath);

const newFileUri: vscode.Uri = vscode.Uri.file(path.join(functionPath, 'index.js'));
vscode.window.showTextDocument(await vscode.workspace.openTextDocument(newFileUri));
break;
}

const newFileUri: vscode.Uri = vscode.Uri.file(path.join(functionPath, 'index.js'));
vscode.window.showTextDocument(await vscode.workspace.openTextDocument(newFileUri));
}
2 changes: 1 addition & 1 deletion src/commands/createNewProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function createNewProject(outputChannel: vscode.OutputChannel, func
case TemplateLanguage.Java:
// Get parameters for Maven command
const { groupId, artifactId, version, packageName, appName } = await promotForMavenParameters(ui, functionAppPath);
// Use maven command currently, will change to function CLI when the function CLI init project command is ready.
// Use maven command to init Java function project.
await FunctionsCli.createNewProject(
outputChannel,
functionAppPath,
Expand Down
14 changes: 14 additions & 0 deletions src/utils/terminal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';

const terminal: vscode.Terminal = vscode.window.createTerminal('Azure Functions');

// tslint:disable-next-line:export-name
export function runCommandInTerminal(command: string, addNewLine: boolean = true): void {
terminal.show();
terminal.sendText(command, addNewLine);
}
14 changes: 14 additions & 0 deletions src/utils/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as fse from 'fs-extra';
import * as path from 'path';
import * as vscode from 'vscode';
import { IUserInterface, PickWithData } from '../IUserInterface';
import { localize } from '../localize';
import { TemplateLanguage } from '../templates/Template';

export async function selectWorkspaceFolder(ui: IUserInterface, placeholder: string): Promise<string> {
const browse: string = ':browse';
Expand All @@ -33,3 +35,15 @@ export function isFolderOpenInWorkspace(fsPath: string): boolean {
return false;
}
}

export function getProjectType(projectPath: string): string {
let language: string = TemplateLanguage.JavaScript;
fse.readdirSync(projectPath).forEach((file: string) => {
const stat: fse.Stats = fse.statSync(path.join(projectPath, file));
// Currently checking the existing pom.xml to determine whether the function project is Java language based.
if (stat.isFile() && file === 'pom.xml') {
language = TemplateLanguage.Java;
}
});
return language;
}

0 comments on commit cc8d3b7

Please sign in to comment.