Skip to content

Commit

Permalink
generate-blueprint: dev blueprints adjusts
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Sep 26, 2024
1 parent 5f649e3 commit da7d1c3
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ exports[`generator - generate-blueprint with all option should match snapshot 1`
".blueprint/generate-sample/generator.mjs": {
"stateCleared": "modified",
},
".blueprint/generate-sample/get-samples.mjs": {
"stateCleared": "modified",
},
".blueprint/generate-sample/index.mjs": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -877,6 +880,9 @@ exports[`generator - generate-blueprint with default config should write files a
".blueprint/generate-sample/generator.mjs": {
"stateCleared": "modified",
},
".blueprint/generate-sample/get-samples.mjs": {
"stateCleared": "modified",
},
".blueprint/generate-sample/index.mjs": {
"stateCleared": "modified",
},
Expand Down
2 changes: 2 additions & 0 deletions generators/generate-blueprint/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const files = asWriteFilesSection<any>({
'.blueprint/cli/commands.mjs',
'.blueprint/generate-sample/command.mjs',
'.blueprint/generate-sample/generator.mjs',
'.blueprint/generate-sample/get-samples.mjs',
'.blueprint/generate-sample/index.mjs',
// Always write cli for devBlueprint usage
'cli/cli.cjs',
Expand All @@ -43,6 +44,7 @@ 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/index.mjs',
],
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 { getSamples } from './get-samples.mjs';

/**
* @type {import('generator-jhipster').JHipsterCommandDefinition}
Expand All @@ -34,7 +35,7 @@ const command = {
when: !gen.all,
type: 'list',
message: 'which sample do you want to generate?',
choices: async () => readdir(gen.templatePath('samples')),
choices: async () => getSamples(gen.templatePath(gen.samplesFolder)),
}),
scope: 'generator',
},
Expand All @@ -45,6 +46,14 @@ const command = {
},
scope: 'generator',
},
samplesFolder: {
description: 'Path to the samples folder',
cli: {
type: String,
},
default: 'samples',
scope: 'generator',
},
},
options: {},
import: [GENERATOR_APP],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,20 @@ import BaseGenerator from 'generator-jhipster/generators/base';
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() {
const samplesFolder = `${this.samplesFolder ?? 'samples'}/`;
if (this.all) {
this.copyTemplate('samples/*.jdl', '');
this.copyTemplate(`${samplesFolder}*.jdl`, '');
} else {
this.copyTemplate(`samples/${this.sampleName}`, this.sampleName, { noGlob: true });
this.copyTemplate(`${samplesFolder}${this.sampleName}`, this.sampleName, { noGlob: true });
}
},
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<%#
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.
-%>
import { readdir, stat } from 'node:fs/promises';
import { extname } from 'path';

export const getSamples = async samplesFolder => {
const filenames = await readdir(samplesFolder);
const entries = await Promise.all(filenames.map(async filename => [filename, await stat(`${samplesFolder}/${filename}`)]));
return entries
.filter(([filename, statResult]) => extname(filename) === '.jdl' || statResult.isDirectory())
.map(([filename]) => filename)
.filter(filename => !filename.includes('disabled'));
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ const defaultMatrix = {
'default-environment': ['prod'],
};

export const buildMatrix = async samplesFolder => {
const samples = await readdir(samplesFolder);
export const buildMatrix = ({ samples, samplesFolder }) => {
return {
include: Object.values(
fromMatrix({
...defaultMatrix,
'sample-name': samples.filter(sample => !sample.includes('disabled')),
'sample-name': samples,
}),
),
).map(sample => ({
...sample,
'job-name': sample['sample-name'],
'extra-args': `--samples-folder ${samplesFolder}`,
})),
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<%#
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.
-%>
import { readdir } from 'node:fs/promises';

/**
* @type {import('generator-jhipster').JHipsterCommandDefinition}
*/
const command = {
configs: {
samplesFolder: {
description: 'Samples folder',
cli: {
type: String,
},
default: 'samples',
scope: 'generator',
},
},
options: {},
};

export default command;
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import { existsSync, appendFileSync } from 'node:fs';
import os from 'node:os';
import BaseGenerator from 'generator-jhipster/generators/base';
import { setGithubTaskOutput } from 'generator-jhipster/testing';
import { buildMatrix } from './build-matrix.mjs';
import { getSamples } from '../generate-sample/get-samples.mjs';

export default class extends BaseGenerator {
samplesFolder;

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 samples = await getSamples(this.templatePath(`../../generate-sample/templates/${this.samplesFolder}`));
const matrix = buildMatrix({ samples, samplesFolder: this.samplesFolder });
const matrixoutput = `matrix<<EOF${os.EOL}${JSON.stringify(matrix)}${os.EOL}EOF${os.EOL}`;
const filePath = process.env['GITHUB_OUTPUT'];
console.log(matrixoutput);
if (filePath && existsSync(filePath)) {
appendFileSync(filePath, matrixoutput, { encoding: 'utf8' });
}
},
});
}
Expand Down
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';

0 comments on commit da7d1c3

Please sign in to comment.