Skip to content

Commit

Permalink
add draft info languages (#80)
Browse files Browse the repository at this point in the history
* add draft info languages

* Oliver Kings feedback

* let to const

* fix tests and error handling

* download to ensure function name

* prettier

* why dont we use our own errorable interface pls

* error was truncated

* get binary before tests

* import

* longer timeout for setup

* remove extra done

* log url in download error

* treat ia32 arch as amd64

* add errorable on ensuredraft

* fix merged typos and format with prettier

* use 32bit windows vscode binary

* clean up merge conflict refactor issues

* update ensure draft binary to errorable

* install prettier from package.json
  • Loading branch information
davidgamero committed Jan 9, 2023
1 parent 4b1c22f commit 29f830f
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 36 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/formatting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3

- name: Install Prettier
run: npm install prettier
- name: NPM install
run: npm i

- name: Ensure Prettier
run: npx prettier --check .
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
"watch-tests": "tsc -p . -w --outDir out",
"pretest": "npm run compile-tests && npm run compile && npm run lint",
"lint": "eslint src --ext ts",
"format": "prettier --write .",
"test": "node ./out/test/runTest.js"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions src/commands/runDraftTool/helper/commonPrompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
IActionContext
} from '@microsoft/vscode-azext-utils';
import {AzApi, ResourceGroupItem, SubscriptionItem} from '../../../utils/az';
import {getAysncResult} from '../../../utils/errorable';
import {getAsyncResult} from '../../../utils/errorable';
import {getAsyncOptions, removeRecentlyUsed} from '../../../utils/quickPick';
import {sort} from '../../../utils/sort';

Expand Down Expand Up @@ -31,7 +31,7 @@ export class PromptSubscription<
}

public async prompt(wizardContext: T): Promise<void> {
const subs = getAysncResult(this.az.listSubscriptions());
const subs = getAsyncResult(this.az.listSubscriptions());
const subToItem = (sub: SubscriptionItem) => ({
label: sub.subscription.displayName || '',
description: sub.subscription.subscriptionId || ''
Expand Down Expand Up @@ -87,7 +87,7 @@ export class PromptResourceGroup<
throw Error('Subscription is undefined');
}

const rgs = getAysncResult(this.az.listResourceGroups(sub));
const rgs = getAsyncResult(this.az.listResourceGroups(sub));
const rgToItem = (rg: ResourceGroupItem) => ({
label: rg.resourceGroup.name || ''
});
Expand Down
4 changes: 4 additions & 0 deletions src/commands/runDraftTool/helper/draftCommandBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,7 @@ export function buildUpdateIngressCommand(
osm ? 'true' : 'false'
} --variable ingress-host="${host}"`;
}

export function buildInfoCommand(): string {
return `info`;
}
62 changes: 62 additions & 0 deletions src/commands/runDraftTool/helper/languages.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,70 @@
import {Errorable, failed, Succeeded} from '../../../utils/errorable';
import {buildInfoCommand} from './draftCommandBuilder';
import {ensureDraftBinary, runDraftCommand} from './runDraftHelper';

/**
* The respresentation of a Draft language for use in this extension.
*/
export interface DraftLanguage {
name: string;
id: string;
versions: string[];
}

/**
* The full return of the draft info command.
*/
interface DraftInfo {
supportedLanguages: DraftInfoLanguage[];
supportedDeployTypes: string[];
}
/**
* The respresentation of a Draft language as returned by the draft info command.
*/
interface DraftInfoLanguage {
name: string;
displayName: string;
variableExampleValues: DraftInfoExampleValues;
}
interface DraftInfoExampleValues {
// eslint-disable-next-line @typescript-eslint/naming-convention
VERSION: string[]; // All caps since it comes from draft builder variable conventions
}
export async function getDraftLanguages(): Promise<Errorable<DraftLanguage[]>> {
const ensureDraftResult = await ensureDraftBinary();
if (failed<null>(ensureDraftResult)) {
return {
succeeded: false,
error: ensureDraftResult.error
};
}

const [result, err] = await runDraftCommand(buildInfoCommand());
if (err) {
return {
succeeded: false,
error: err
};
}
const resultJSON = JSON.parse(result);
const draftInfo = resultJSON as DraftInfo;

const infoLanguages: DraftLanguage[] = draftInfo.supportedLanguages.map(
(infoLanguage: DraftInfoLanguage): DraftLanguage => {
const language: DraftLanguage = {
name: infoLanguage.displayName,
id: infoLanguage.name,
versions: infoLanguage.variableExampleValues.VERSION
};
return language;
}
);

return {
succeeded: true,
result: infoLanguages
};
}
export const draftLanguages: DraftLanguage[] = [
{id: 'clojure', name: 'Clojure', versions: ['8-jdk-alpine']},
{id: 'csharp', name: 'C#', versions: ['6.0', '5.0', '4.0', '3.1']},
Expand Down
16 changes: 11 additions & 5 deletions src/commands/runDraftTool/helper/runDraftHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ function checkIfDraftBinaryExist(destinationFile: string): boolean {
return fs.existsSync(destinationFile);
}

export async function downloadDraftBinary() {
export async function ensureDraftBinary(): Promise<Errorable<null>> {
// 0. Get latest release tag.
// 1: check if file already exist.
// 2: if not Download latest.
const latestReleaseTag = await getLatestDraftReleaseTag();

if (!latestReleaseTag) {
return;
return {
succeeded: false,
error: `failed to get latest release tag`
};
}

const draftBinaryFile = getBinaryFileName();
Expand All @@ -47,7 +50,7 @@ export async function downloadDraftBinary() {
}

if (checkIfDraftBinaryExist(destinationFile)) {
return {succeeded: true};
return {succeeded: true, result: null};
}

const draftDownloadUrl = `https://github.com/Azure/draft/releases/download/${latestReleaseTag}/${draftBinaryFile}`;
Expand All @@ -59,12 +62,15 @@ export async function downloadDraftBinary() {
if (failed(downloadResult)) {
return {
succeeded: false,
error: [`Failed to download draft binary: ${downloadResult.error[0]}`]
error: `Failed to download draft binary: ${downloadResult.error} from ${draftDownloadUrl}`
};
}
//If linux check -- make chmod 0755
fs.chmodSync(destinationFile, '0755');
return succeeded(downloadResult);
return {
succeeded: true,
result: null
};
}

function getBinaryFileName() {
Expand Down
18 changes: 9 additions & 9 deletions src/commands/runDraftTool/runDraftDeployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Context} from './model/context';
import * as vscode from 'vscode';
import {State, StateApi} from '../../utils/state';
import {longRunning} from '../../utils/host';
import {downloadDraftBinary, runDraftCommand} from './helper/runDraftHelper';
import {ensureDraftBinary, runDraftCommand} from './helper/runDraftHelper';
import {
AzureWizard,
AzureWizardExecuteStep,
Expand All @@ -23,7 +23,7 @@ import {
getHelm,
getKubectl
} from '../../utils/kubernetes';
import {failed, getAysncResult} from '../../utils/errorable';
import {failed, getAsyncResult} from '../../utils/errorable';
import {
RegistryItem,
RepositoryItem,
Expand Down Expand Up @@ -94,7 +94,7 @@ export async function runDraftDeployment(

// Ensure Draft Binary
const downloadResult = await longRunning(`Downloading Draft.`, () =>
downloadDraftBinary()
getAsyncResult(ensureDraftBinary())
);
if (!downloadResult) {
vscode.window.showErrorMessage('Failed to download Draft');
Expand Down Expand Up @@ -225,7 +225,7 @@ class PromptNamespace extends AzureWizardPromptStep<WizardContext> {
}

public async prompt(wizardContext: WizardContext): Promise<void> {
const namespaces = getAysncResult(this.k8s.listNamespaces());
const namespaces = getAsyncResult(this.k8s.listNamespaces());
const newOption = 'New Namespace';
const getOptions = async (): Promise<vscode.QuickPickItem[]> => {
const namespaceOptions: vscode.QuickPickItem[] = (
Expand Down Expand Up @@ -326,7 +326,7 @@ class PromptAcrSubscription extends AzureWizardPromptStep<WizardContext> {
}

public async prompt(wizardContext: WizardContext): Promise<void> {
const subs = getAysncResult(this.az.listSubscriptions());
const subs = getAsyncResult(this.az.listSubscriptions());
const subToItem = (sub: SubscriptionItem) => ({
label: sub.subscription.displayName || '',
description: sub.subscription.subscriptionId || ''
Expand Down Expand Up @@ -363,7 +363,7 @@ class PromptAcrResourceGroup extends AzureWizardPromptStep<WizardContext> {
throw Error('ACR Subscription is undefined');
}

const rgs = getAysncResult(
const rgs = getAsyncResult(
this.az.listResourceGroups(wizardContext.acrSubscription)
);
const rgToItem = (rg: ResourceGroupItem) => ({
Expand Down Expand Up @@ -400,7 +400,7 @@ class PromptAcrRegistry extends AzureWizardPromptStep<WizardContext> {
throw Error('ACR Resource Group is undefined');
}

const registries = getAysncResult(
const registries = getAsyncResult(
this.az.listContainerRegistries(
wizardContext.acrSubscription,
wizardContext.acrResourceGroup
Expand Down Expand Up @@ -439,7 +439,7 @@ class PromptAcrRepository extends AzureWizardPromptStep<WizardContext> {
throw Error('ACR Registry is undefined');
}

const repositories = getAysncResult(
const repositories = getAsyncResult(
this.az.listRegistryRepositories(wizardContext.acrRegistry)
);
const repositoryToItem = (r: RepositoryItem) => ({
Expand Down Expand Up @@ -478,7 +478,7 @@ class PromptAcrTag extends AzureWizardPromptStep<WizardContext> {
throw Error('ACR Repository is undefined');
}

const tags = getAysncResult(
const tags = getAsyncResult(
this.az.listRepositoryTags(
wizardContext.acrRegistry,
wizardContext.acrRepository
Expand Down
19 changes: 12 additions & 7 deletions src/commands/runDraftTool/runDraftDockerfile.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import {longRunning} from '../../utils/host';
import {Context} from './model/context';
import * as vscode from 'vscode';
import {downloadDraftBinary, runDraftCommand} from './helper/runDraftHelper';
import {DraftLanguage, draftLanguages} from './helper/languages';
import {ensureDraftBinary, runDraftCommand} from './helper/runDraftHelper';
import {
DraftLanguage,
draftLanguages,
getDraftLanguages
} from './helper/languages';
import {
AzureWizard,
AzureWizardExecuteStep,
Expand All @@ -19,6 +23,8 @@ import {State, StateApi} from '../../utils/state';
import {ignoreFocusOut} from './helper/commonPrompts';
import {CompletedSteps} from './model/guidedExperience';
import {ValidatePort} from '../../utils/validation';
import {getAsyncResult} from '../../utils/errorable';
import {getAsyncOptions} from '../../utils/quickPick';

const title = 'Draft a Dockerfile from source code';

Expand All @@ -41,7 +47,7 @@ export async function runDraftDockerfile(

// Ensure Draft Binary
const downloadResult = await longRunning(`Downloading Draft.`, () =>
downloadDraftBinary()
getAsyncResult(ensureDraftBinary())
);
if (!downloadResult) {
vscode.window.showErrorMessage('Failed to download Draft');
Expand Down Expand Up @@ -109,17 +115,16 @@ class PromptSourceCodeFolder extends AzureWizardPromptStep<WizardContext> {
class PromptLanguage extends AzureWizardPromptStep<WizardContext> {
public async prompt(wizardContext: WizardContext): Promise<void> {
const languageToItem = (lang: DraftLanguage) => ({label: lang.name});
const languageOptions: vscode.QuickPickItem[] =
draftLanguages.map(languageToItem);
const draftLanguages = getAsyncResult(getDraftLanguages());
const languagePick = await wizardContext.ui.showQuickPick(
languageOptions,
getAsyncOptions(draftLanguages, languageToItem),
{
ignoreFocusOut,
stepName: 'Programming Language',
placeHolder: 'Select the programming language'
}
);
const language = draftLanguages.find(
const language = (await draftLanguages).find(
(lang) => languageToItem(lang).label === languagePick.label
);
if (language === undefined) {
Expand Down
14 changes: 7 additions & 7 deletions src/commands/runDraftTool/runDraftIngress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as vscode from 'vscode';
import {Context} from './model/context';
import {StateApi, State} from '../../utils/state';
import {longRunning} from '../../utils/host';
import {downloadDraftBinary, runDraftCommand} from './helper/runDraftHelper';
import {ensureDraftBinary, runDraftCommand} from './helper/runDraftHelper';
import {
AzureWizard,
AzureWizardExecuteStep,
Expand All @@ -21,7 +21,7 @@ import {
ResourceGroupItem,
SubscriptionItem
} from '../../utils/az';
import {Errorable, failed, getAysncResult} from '../../utils/errorable';
import {Errorable, failed, getAsyncResult} from '../../utils/errorable';
import {sort} from '../../utils/sort';
import {
ignoreFocusOut,
Expand Down Expand Up @@ -70,7 +70,7 @@ export async function runDraftIngress(
) {
// Ensure Draft Binary
const downloadResult = await longRunning(`Downloading Draft`, () =>
downloadDraftBinary()
getAsyncResult(ensureDraftBinary())
);
if (!downloadResult) {
vscode.window.showErrorMessage('Failed to download Draft');
Expand Down Expand Up @@ -189,7 +189,7 @@ class PromptKv extends AzureWizardPromptStep<WizardContext> {
throw Error('Key Vault Resource Group undefined');
}

const kvs = getAysncResult(
const kvs = getAsyncResult(
this.az.listKeyVaults(
wizardContext.kvSubscription,
wizardContext.kvResourceGroup
Expand Down Expand Up @@ -278,7 +278,7 @@ class PromptDnsZone extends AzureWizardPromptStep<WizardContext> {
throw Error('DNS Zone resource group is undefined');
}

const dnsZones = getAysncResult(
const dnsZones = getAsyncResult(
this.az.listDnsZones(
wizardContext.dnsSubscription,
wizardContext.dnsResourceGroup
Expand Down Expand Up @@ -315,7 +315,7 @@ class PromptCertificate extends AzureWizardPromptStep<WizardContext> {
throw Error('Key Vault undefined');
}

const certs = getAysncResult(this.az.listCertificates(wizardContext.kv));
const certs = getAsyncResult(this.az.listCertificates(wizardContext.kv));
const certToItem = (cert: CertificateItem) => ({
label: cert.certificate.name || ''
});
Expand Down Expand Up @@ -352,7 +352,7 @@ class PromptAksCluster extends AzureWizardPromptStep<WizardContext> {
throw Error('AKS Resource Group is undefined');
}

const clusters = getAysncResult(
const clusters = getAsyncResult(
this.az.listAksClusters(
wizardContext.aksSubscription,
wizardContext.aksResourceGroup
Expand Down
6 changes: 4 additions & 2 deletions src/test/runTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as path from 'path';

import * as os from 'os';
import {runTests} from '@vscode/test-electron';

async function main() {
Expand All @@ -11,9 +11,11 @@ async function main() {
// The path to test runner
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, './suite/index');
const platform =
os.platform() === 'win32' ? 'win32-x64-archive' : undefined;

// Download VS Code, unzip it and run the integration test
await runTests({extensionDevelopmentPath, extensionTestsPath});
await runTests({extensionDevelopmentPath, extensionTestsPath, platform});
} catch (err) {
console.error('Failed to run tests');
process.exit(1);
Expand Down

0 comments on commit 29f830f

Please sign in to comment.