Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate-blueprint: improve dev-blueprint and ci workflow #27765

Merged
merged 4 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions generators/app/__snapshots__/generator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ exports[`generator - app with default config should match snapshot 1`] = `
"capitalizedBaseName": "Jhipster",
"caret": undefined,
"cjsExtension": ".cjs",
"cliName": undefined,
"clientBundler": "webpack",
"clientBundlerAny": true,
"clientBundlerVite": false,
Expand Down Expand Up @@ -907,6 +908,7 @@ exports[`generator - app with gateway should match snapshot 1`] = `
"capitalizedBaseName": "Jhipster",
"caret": undefined,
"cjsExtension": ".cjs",
"cliName": undefined,
"clientBundler": "webpack",
"clientBundlerAny": true,
"clientBundlerVite": false,
Expand Down Expand Up @@ -1554,6 +1556,7 @@ exports[`generator - app with microservice should match snapshot 1`] = `
"capitalizedBaseName": "Jhipster",
"caret": undefined,
"cjsExtension": ".cjs",
"cliName": undefined,
"clientBundler": undefined,
"clientBundlerAny": true,
"clientBundlerVite": false,
Expand Down
7 changes: 7 additions & 0 deletions generators/generate-blueprint/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ const command = {
},
scope: 'storage',
},
cliName: {
cli: {
description: 'CLI name',
type: String,
},
scope: 'storage',
},
recreatePackageLock: {
description: 'Recreate package lock',
cli: {
Expand Down
3 changes: 2 additions & 1 deletion generators/generate-blueprint/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ export const files = asWriteFilesSection<any>({
{
condition: ctx => !ctx[LOCAL_BLUEPRINT_OPTION] && ctx.githubWorkflows,
templates: [
'.blueprint/github-build-matrix/build-matrix.mjs',
'.blueprint/github-build-matrix/command.mjs',
'.blueprint/github-build-matrix/generator.mjs',
'.blueprint/github-build-matrix/generator.spec.mjs',
'.blueprint/github-build-matrix/index.mjs',
],
},
Expand Down
7 changes: 5 additions & 2 deletions generators/generate-blueprint/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export default class extends BaseGenerator {
await control.cleanupFiles({
'8.5.1': ['.eslintrc.json'],
'8.7.2': ['.eslintignore', 'vitest.test-setup.ts'],
'8.7.4': ['.blueprint/generate-sample/get-samples.mjs', '.blueprint/github-build-matrix/build-matrix.mjs'],
});
},
async writing({ application }) {
Expand Down Expand Up @@ -278,7 +279,7 @@ export default class extends BaseGenerator {
if (!this.isJhipsterVersionLessThan('8.7.2')) return;
for (const generator of Object.keys(this.application[GENERATORS])) {
const commandFile = `${this.application.blueprintsPath}${generator}/command.${application.blueprintMjsExtension}`;
this.editFile(commandFile, content =>
this.editFile(commandFile, { ignoreNonExisting: true }, content =>
content
.replace(
`/**
Expand All @@ -297,7 +298,9 @@ export default class extends BaseGenerator {
);

const generatorSpec = `${this.application.blueprintsPath}${generator}/generator.spec.${application.blueprintMjsExtension}`;
this.editFile(generatorSpec, content => content.replaceAll(/blueprint: '([\w-]*)'/g, "blueprint: ['$1']"));
this.editFile(generatorSpec, { ignoreNonExisting: true }, content =>
content.replaceAll(/blueprint: '([\w-]*)'/g, "blueprint: ['$1']"),
);
}
},
packageJson() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
import { readdir } from 'node:fs/promises';
import { GENERATOR_APP } from 'generator-jhipster/generators';
import { getGithubSamplesGroups } from 'generator-jhipster/testing';

/**
* @type {import('generator-jhipster').JHipsterCommandDefinition}
Expand All @@ -29,12 +30,20 @@ const command = {
},
},
configs: {
samplesFolder: {
description: 'Path to the samples folder',
cli: {
type: String,
},
default: 'samples',
scope: 'generator',
},
sampleName: {
prompt: gen => ({
when: !gen.all,
type: 'list',
message: 'which sample do you want to generate?',
choices: async () => readdir(gen.templatePath('samples')),
choices: async () => getGithubSamplesGroups(gen.templatePath(gen.samplesFolder)),
}),
scope: 'generator',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
import { readdir } from 'node:fs/promises';
import { readFileSync } from 'node:fs';
import { extname, join } from 'node:path';
import BaseGenerator from 'generator-jhipster/generators/base';
import { getGithubSamplesGroup } from 'generator-jhipster/testing';

export default class extends BaseGenerator {
sampleName;
all;
samplesFolder;

constructor(args, opts, features) {
super(args, opts, { ...features, jhipsterBootstrap: false });
}

get [BaseGenerator.INITIALIZING]() {
return this.asInitializingTaskGroup({
async initializeOptions() {
if (this.sampleName && !this.sampleName.endsWith('.jdl')) {
this.sampleName += '.jdl';
}
},
});
}

get [BaseGenerator.PROMPTING]() {
return this.asPromptingTaskGroup({
async askForSample() {
await this.promptCurrentJHipsterCommand();
},
});
super(args, opts, { ...features, queueCommandTasks: true, jhipsterBootstrap: false });
}

get [BaseGenerator.WRITING]() {
return this.asWritingTaskGroup({
async copySample() {
if (this.all) {
this.copyTemplate('samples/*.jdl', '');
const { samplesFolder, all, sampleName } = this;
if (all) {
this.copyTemplate(`${samplesFolder}/*.jdl`, '');
} else if (extname(sampleName) === '.jdl') {
this.copyTemplate(join(samplesFolder, sampleName), sampleName, { noGlob: true });
} else {
this.copyTemplate(`samples/${this.sampleName}`, this.sampleName, { noGlob: true });
const { samples } = await getGithubSamplesGroup(this.templatePath(), samplesFolder);
const { 'sample-type': sampleType } = samples[sampleName];
if (sampleType === 'jdl') {
const jdlFile = `${sampleName}.jdl`;
this.copyTemplate(join(samplesFolder, jdlFile), jdlFile, { noGlob: true });
} else if (sampleType === 'yo-rc') {
this.copyTemplate(join(samplesFolder, sampleName, '**'), '', {
fromBasePath: this.templatesPath(samplesFolder, sampleName),
});
}
}
},
});
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<%#
Copyright 2013-2024 the original author or authors from the JHipster project.

This file is part of the JHipster project, see https://www.jhipster.tech/
for more information.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-%>
/**
* @type {import('generator-jhipster').JHipsterCommandDefinition}
*/
const command = {
configs: {
samplesFolder: {
description: 'Samples folder',
cli: {
type: String,
},
scope: 'generator',
},
samplesGroup: {
description: 'Samples Group',
argument: {
type: String,
},
default: 'samples',
scope: 'generator',
},
},
options: {},
};

export default command;
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
import { existsSync, appendFileSync } from 'node:fs';
import os from 'node:os';
import { join } from 'node:path';
import BaseGenerator from 'generator-jhipster/generators/base';
import { setGithubTaskOutput } from 'generator-jhipster/testing';
import { buildMatrix } from './build-matrix.mjs';
import { convertToGitHubMatrix, getGithubOutputFile, getGithubSamplesGroup, setGithubTaskOutput } from 'generator-jhipster/testing';

export default class extends BaseGenerator {
/** @type {string} */
samplesFolder;
/** @type {string} */
samplesGroup;
/** @type {object} */
matrix;

constructor(args, opts, features) {
super(args, opts, { ...features, jhipsterBootstrap: false });
super(args, opts, { ...features, queueCommandTasks: true, jhipsterBootstrap: false });
}

get [BaseGenerator.WRITING]() {
return this.asWritingTaskGroup({
async buildMatrix() {
const matrix = await buildMatrix(this.templatePath('../../generate-sample/templates/samples'));
setGithubTaskOutput('matrix', matrix);
const { samplesGroup = 'samples' } = this;
const templatePath = this.templatePath('../../generate-sample/templates/');
const samplesFolder = this.samplesFolder ? join(templatePath, this.samplesFolder) : templatePath;
const { samples, warnings } = await getGithubSamplesGroup(samplesFolder, samplesGroup);
if (warnings.length > 0) {
this.log.info(warnings.join('\n'));
}
this.matrix = convertToGitHubMatrix(samples);
const githubOutputFile = getGithubOutputFile();
this.log.info('matrix', this.matrix);
if (githubOutputFile) {
setGithubTaskOutput('matrix', JSON.stringify(this.matrix));
}
},
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { basename, dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { beforeAll, describe, expect, it } from 'vitest';
import { getGithubSamplesGroups, defaultHelpers as helpers, runResult } from 'generator-jhipster/testing';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const generator = basename(__dirname);

describe(`generator - ${generator}`, async () => {
const groups = await getGithubSamplesGroups(join(__dirname, '../generate-sample/templates/'));
for (const workflow of groups.map(sample => sample.split('.')[0])) {
describe(`with ${workflow}`, () => {
beforeAll(async () => {
await helpers.runJHipster(join(__dirname, 'index.mjs'), { useEnvironmentBuilder: true }).withArguments(workflow);
});

it('should match matrix value', () => {
expect(runResult.generator.matrix).toMatchSnapshot();
});
});
}
});
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default } from './generator.mjs';
export { default as command } from './command.mjs';
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
npm install
./cli/cli.cjs github-build-matrix
samples:
name: ${{ matrix.job-name || matrix.sample-name }}
name: ${{ matrix.job-name || matrix.sample }}
runs-on: ubuntu-latest
needs: build-matrix
defaults:
Expand All @@ -50,22 +50,22 @@ jobs:
binary-dir: ${{ github.workspace }}/generator-jhipster-<%= baseName %>/cli/
- run: npm install
working-directory: ${{ github.workspace }}/generator-jhipster-<%= baseName %>
- run: cli.cjs generate-sample ${{ matrix.sample-name }} --skip-jhipster-dependencies ${{ matrix.extra-args }}
- run: cli.cjs generate-sample ${{ matrix.sample }} --skip-jhipster-dependencies --skip-install ${{ matrix.extra-args }}
- uses: jhipster/actions/compare-sample@v0
id: compare
if: >-
github.event.pull_request &&
!contains(github.event.pull_request.labels.*.name, 'pr: disable-compare')
with:
generator-path: generator-jhipster-<%= baseName %>
cmd: cli.cjs generate-sample ${{ matrix.sample-name }} --skip-jhipster-dependencies --skip-install ${{ matrix.extra-args }}
cmd: cli.cjs generate-sample ${{ matrix.sample }} --skip-jhipster-dependencies --skip-install ${{ matrix.extra-args }}
- run: npm run ci:backend:test
if: steps.compare.outputs.equals != 'true'
id: backend
- run: npm run ci:frontend:test --if-present
if: steps.compare.outputs.equals != 'true'
- run: npm run ci:e2e:package
if: steps.compare.outputs.equals != 'true'
- run: npm run ci:frontend:test --if-present
if: steps.compare.outputs.equals != 'true'
- run: npm run ci:e2e:prepare
if: steps.compare.outputs.equals != 'true'
- run: npm run ci:e2e:run --if-present
Expand All @@ -75,15 +75,15 @@ jobs:
uses: actions/upload-artifact@v4
if: always() && steps.backend.outcome == 'failure'
with:
name: log-${{ matrix.sample-name }}
name: log-${{ matrix.job-name || matrix.sample }}
path: |
${{ github.workspace }}/app/build/test-results/**/*.xml
${{ github.workspace }}/app/target/surefire-reports
- name: Store cypress screenshots
uses: actions/upload-artifact@v4
if: always() && steps.e2e.outcome == 'failure'
with:
name: screenshots-${{ matrix.sample-name }}
name: screenshots-${{ matrix.job-name || matrix.sample }}
path: ${{ github.workspace }}/app/**/cypress/screenshots
- name: Dump docker logs
if: always()
Expand Down
8 changes: 5 additions & 3 deletions lib/testing/github-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { extname, join } from 'node:path';
import type { GitHubMatrixGroup } from './github-matrix.js';
import { getUnknownGitHubMatrixGroupProperties } from './github-matrix.js';

export const getGithubSamplesGroups = async (samplesGroupFolder: string): Promise<string[]> => {
export const getGithubSamplesGroups = async (samplesGroupFolder: string, keepExtensions = false): Promise<string[]> => {
const samplesFolderContent = await readdir(samplesGroupFolder);
return samplesFolderContent.filter(sample => ['.json', '.js', '.ts', ''].includes(extname(sample)));
return samplesFolderContent
.filter(sample => ['.json', '.js', '.ts', ''].includes(extname(sample)))
.map(sample => (keepExtensions ? sample : sample.split('.')[0]));
};

export const getGithubSamplesGroup = async (
Expand All @@ -14,7 +16,7 @@ export const getGithubSamplesGroup = async (
): Promise<{ samples: GitHubMatrixGroup; warnings: string[] }> => {
const warnings: string[] = [];
let samples: GitHubMatrixGroup = {};
const samplesFolderContent = await getGithubSamplesGroups(samplesGroupFolder);
const samplesFolderContent = await getGithubSamplesGroups(samplesGroupFolder, true);
if (samplesFolderContent.includes(`${group}.js`) || samplesFolderContent.includes(`${group}.ts`)) {
const jsGroup: { default: GitHubMatrixGroup } = await import(join(samplesGroupFolder, `${group}.js`));
samples = Object.fromEntries(Object.entries(jsGroup.default).map(([sample, value]) => [sample, { ...value, 'samples-group': group }]));
Expand Down
Loading