Skip to content

Commit 1ba4d63

Browse files
committed
Use nc parameter if available when generating project
Checks the Code Quarkus openapi for the `nc` parameter. If it is available for the given Code Quarkus API, then it will pass `nc=true` to the project generation API when the user wants to exclude example code, instead of the deprecated `ne=true` parameter. Addresses comments on #322 Signed-off-by: David Thompson <[email protected]>
1 parent 81e515c commit 1ba4d63

File tree

6 files changed

+38
-22
lines changed

6 files changed

+38
-22
lines changed

.github/workflows/tests.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ jobs:
55
runs-on: ${{ matrix.os }}
66
strategy:
77
matrix:
8-
os: [macos-latest, ubuntu-latest]
8+
os: [ubuntu-latest]
99
include:
10-
- os: macos-latest
11-
label: 'darwin'
1210
- os: ubuntu-latest
1311
label: 'linux'
1412
steps:

src/definitions/inputState.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export interface ProjectGenState extends State {
3434
packageName: string;
3535
resourceName: string;
3636
targetDir: Uri;
37-
isGenerateSampleCode: boolean;
37+
shouldGenerateCode: boolean;
3838
}
3939

4040
export interface AddExtensionsState extends State {

src/test/vscodeUiTest/suite/projectGenerationTest.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('Project generation tests', function() {
6363
* in the command palette
6464
*/
6565
it('should open project generation wizard', async function() {
66-
this.timeout(30000);
66+
this.timeout(60000);
6767
const wizard: ProjectGenerationWizard = await ProjectGenerationWizard.openWizard(driver);
6868
expect(await wizardExists(), 'wizard did not open').to.be.true;
6969
await wizard.cancel();

src/utils/codeQuarkusApiUtils.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,20 @@ import { QuarkusConfig } from "../QuarkusConfig";
2222

2323
const HTTP_MATCHER = new RegExp('^http://');
2424

25+
/**
26+
* Represents the capabilities of a Code Quarkus API, such as code.quarkus.io/api or code.quarkus.redhat.com/api
27+
*/
2528
export interface CodeQuarkusFunctionality {
26-
canExcludeSampleCode: boolean;
29+
/**
30+
* This Code Quarkus API supports the `ne=...` parameter to specify that example code should not be generated
31+
*
32+
* @deprecated the `ne=...` parameter will be removed in favour of the `nc=...` parameter
33+
*/
34+
supportsNoExamplesParameter: boolean;
35+
/**
36+
* This Code Quarkus API supports the `nc=...` to specify that starter code should not be generated
37+
*/
38+
supportsNoCodeParameter: boolean;
2739
}
2840

2941
/**
@@ -46,7 +58,8 @@ export async function getCodeQuarkusApiFunctionality(): Promise<CodeQuarkusFunct
4658
const openApiData: any = yaml.load(openApiYaml);
4759

4860
return {
49-
canExcludeSampleCode: openApiData?.paths?.['/api/download']?.get?.parameters?.filter(p => p?.name === 'ne').length > 0
61+
supportsNoExamplesParameter: openApiData?.paths?.['/api/download']?.get?.parameters?.filter(p => p?.name === 'ne').length > 0,
62+
supportsNoCodeParameter: openApiData?.paths?.['/api/download']?.get?.parameters?.filter(p => p?.name === 'nc').length > 0,
5063
} as CodeQuarkusFunctionality;
5164
}
5265

@@ -57,7 +70,8 @@ export async function getCodeQuarkusApiFunctionality(): Promise<CodeQuarkusFunct
5770
*/
5871
export function getDefaultFunctionality() {
5972
return {
60-
canExcludeSampleCode: false
73+
supportsNoExamplesParameter: false,
74+
supportsNoCodeParameter: false,
6175
} as CodeQuarkusFunctionality;
6276
}
6377

src/utils/requestUtils.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { QExtension, APIExtension } from '../definitions/QExtension';
2424
import { Readable } from 'stream';
2525
import { ZipFile, fromBuffer } from 'yauzl';
2626
import { convertToQExtension } from '../definitions/QExtension';
27+
import { CodeQuarkusFunctionality } from './codeQuarkusApiUtils';
2728

2829
const HEADERS = {
2930
'Client-Name': 'vscode-quarkus',
@@ -65,17 +66,20 @@ function removeDuplicateArtifactIds(extensions: QExtension[]): QExtension[] {
6566
}, [] as QExtension[]);
6667
}
6768

68-
export async function downloadProject(state: ProjectGenState): Promise<ZipFile> {
69+
export async function downloadProject(state: ProjectGenState, codeQuarkusFunctionality: CodeQuarkusFunctionality): Promise<ZipFile> {
6970
const apiUrl: string = QuarkusConfig.getApiUrl();
7071
const chosenIds: string[] = state.extensions!.map((it) => `${it.groupId}:${it.artifactId}`);
7172

73+
const canSpecifyGenerateCode = codeQuarkusFunctionality.supportsNoCodeParameter || codeQuarkusFunctionality.supportsNoExamplesParameter;
74+
const parameterToSpecifyGenerateCode = codeQuarkusFunctionality.supportsNoCodeParameter ? 'nc' : 'ne';
75+
7276
const qProjectUrl: string = `${apiUrl}/download?` +
7377
`b=${state.buildTool.toUpperCase()}&` +
7478
`g=${state.groupId}&` +
7579
`a=${state.artifactId}&` +
7680
`v=${state.projectVersion}&` +
7781
`c=${state.packageName}.${state.resourceName}&` +
78-
`${(state.isGenerateSampleCode === undefined ? '' : `ne=${!state.isGenerateSampleCode}&`)}` +
82+
`${(canSpecifyGenerateCode ? `${parameterToSpecifyGenerateCode}=${!state.shouldGenerateCode}&` : '')}` +
7983
`e=${chosenIds.join('&e=')}`;
8084

8185
const buffer: Buffer = await tryGetProjectBuffer(qProjectUrl);
@@ -109,7 +113,7 @@ function promisify(api) {
109113
}
110114

111115
async function extract(content: Buffer, path: string): Promise<ZipFile> {
112-
const zipfile: ZipFile = (await yauzlFromBuffer(content, {lazyEntries: true})) as ZipFile;
116+
const zipfile: ZipFile = (await yauzlFromBuffer(content, { lazyEntries: true })) as ZipFile;
113117
const openReadStream = promisify(zipfile.openReadStream.bind(zipfile));
114118

115119
zipfile.readEntry();
@@ -121,7 +125,7 @@ async function extract(content: Buffer, path: string): Promise<ZipFile> {
121125
fs.mkdirSync(mappedPath);
122126
} else {
123127
const stream: Readable = (await openReadStream(entry)) as Readable;
124-
stream.pipe(fs.createWriteStream(mappedPath, {mode: entry.externalFileAttributes >>> 16}));
128+
stream.pipe(fs.createWriteStream(mappedPath, { mode: entry.externalFileAttributes >>> 16 }));
125129
}
126130
}
127131
zipfile.readEntry();

src/wizards/generateProject/generationWizard.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export async function generateProjectWizard() {
3333
}
3434

3535
const state: Partial<ProjectGenState> = {
36-
totalSteps: 7 + (apiCapabilities.canExcludeSampleCode ? 1 : 0)
36+
totalSteps: 7 + (apiCapabilities.supportsNoCodeParameter || apiCapabilities.supportsNoExamplesParameter ? 1 : 0)
3737
};
3838

3939
async function collectInputs(state: Partial<ProjectGenState>) {
@@ -149,18 +149,18 @@ export async function generateProjectWizard() {
149149
});
150150
return (input: MultiStepInput) => ExtensionsPicker.createExtensionsPicker(
151151
input, state, { showLastUsed: true, showRequiredExtensions: true, allowZeroExtensions: true },
152-
(apiCapabilities.canExcludeSampleCode ? inputGenerateSampleCode: undefined));
152+
(apiCapabilities.supportsNoCodeParameter || apiCapabilities.supportsNoExamplesParameter ? inputGenerateSampleCode : undefined));
153153
}
154154

155155
async function inputGenerateSampleCode(input: MultiStepInput, state: Partial<ProjectGenState>) {
156-
const YES: string = 'Include sample code';
157-
const NO: string = 'Do not include sample code';
156+
const YES: string = `Include ${apiCapabilities.supportsNoCodeParameter ? 'starter' : 'example'} code`;
157+
const NO: string = `Do not include ${apiCapabilities.supportsNoCodeParameter ? 'starter' : 'example'} code`;
158158
const quickPickItems: QuickPickItem[] = [
159-
{label: YES, picked: true},
160-
{label: NO}
159+
{ label: YES, picked: true },
160+
{ label: NO }
161161
];
162162

163-
state.isGenerateSampleCode = (await input.showQuickPick<QuickPickItem, QuickPickParameters<QuickPickItem>>({
163+
state.shouldGenerateCode = (await input.showQuickPick<QuickPickItem, QuickPickParameters<QuickPickItem>>({
164164
title: INPUT_TITLE,
165165
placeholder: 'Should sample code be included? Additional dependencies may be added along with the sample.',
166166
step: input.getStepNumber(),
@@ -176,7 +176,7 @@ export async function generateProjectWizard() {
176176
const projectGenState: ProjectGenState = state as ProjectGenState;
177177
saveDefaults(projectGenState);
178178
deleteFolderIfExists(getNewProjectDirectory(projectGenState));
179-
await downloadAndSetupProject(projectGenState);
179+
await downloadAndSetupProject(projectGenState, apiCapabilities);
180180
}
181181

182182
async function getTargetDirectory(projectName: string) {
@@ -249,9 +249,9 @@ function getNewProjectDirectory(state: ProjectGenState): Uri {
249249
return Uri.file(path.join(state.targetDir.fsPath, state.artifactId));
250250
}
251251

252-
async function downloadAndSetupProject(state: ProjectGenState): Promise<void> {
252+
async function downloadAndSetupProject(state: ProjectGenState, codeQuarkusFunctionality: CodeQuarkusFunctionality): Promise<void> {
253253
const projectDir: Uri = getNewProjectDirectory(state);
254-
const zip: ZipFile = await downloadProject(state);
254+
const zip: ZipFile = await downloadProject(state, codeQuarkusFunctionality);
255255
zip.on('end', () => {
256256
openProject(projectDir);
257257
});

0 commit comments

Comments
 (0)