Skip to content

Commit

Permalink
Merge pull request #26078 from mshima/version-check
Browse files Browse the repository at this point in the history
Updates to generated blueprint.
  • Loading branch information
DanielFran authored May 9, 2024
2 parents 6a79111 + f29ebaf commit 611be68
Show file tree
Hide file tree
Showing 14 changed files with 154 additions and 40 deletions.
14 changes: 9 additions & 5 deletions generators/base-core/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,15 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`);
*/
isJhipsterVersionLessThan(version) {
const jhipsterOldVersion = this.sharedData.getControl().jhipsterOldVersion;
if (!jhipsterOldVersion) {
// if old version is unknown then can't compare (the project may be null) and return false
return false;
}
return semverLessThan(jhipsterOldVersion, version);
return this.isVersionLessThan(jhipsterOldVersion, version);
}

/**
* Wrapper for `semver.lt` to check if the oldVersion exists and is less than the newVersion.
* Can be used by blueprints.
*/
isVersionLessThan(oldVersion: string | null, newVersion: string) {
return oldVersion ? semverLessThan(oldVersion, newVersion) : false;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions generators/generate-blueprint/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ import {
} from './constants.js';

const command: JHipsterCommandDefinition = {
configs: {
githubRepository: {
cli: {
description: 'Github Repository',
type: String,
},
scope: 'storage',
},
githubWorkflows: {
cli: {
description: 'Generate github workflows',
type: Boolean,
},
scope: 'storage',
},
},
options: {
[GENERATE_SNAPSHOTS]: {
description: 'Generate test snapshots',
Expand Down
8 changes: 8 additions & 0 deletions generators/generate-blueprint/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ export const files = {
'.blueprint/generate-sample/index.mjs',
],
},
{
condition: ctx => !ctx[LOCAL_BLUEPRINT_OPTION] && ctx.githubWorkflows,
templates: [
'.blueprint/github-build-matrix/build-matrix.mjs',
'.blueprint/github-build-matrix/generator.mjs',
'.blueprint/github-build-matrix/index.mjs',
],
},
{
condition: ctx => !ctx[LOCAL_BLUEPRINT_OPTION] && !ctx.sampleWritten,
templates: ['.blueprint/generate-sample/templates/samples/sample.jdl'],
Expand Down
28 changes: 18 additions & 10 deletions generators/generate-blueprint/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import * as GENERATOR_LIST from '../generator-list.js';
import { files, generatorFiles } from './files.js';
import { packageJson } from '../../lib/index.js';
import { SKIP_COMMIT_HOOK } from '../init/constants.js';
import command from './command.js';
import { BLUEPRINT_API_VERSION, NODE_VERSION } from '../generator-constants.js';

const { GENERATOR_PROJECT_NAME, GENERATOR_INIT } = GENERATOR_LIST;
Expand All @@ -63,17 +62,18 @@ export default class extends BaseGenerator {
}

get initializing() {
return {
loadOptions() {
this.parseJHipsterOptions(command.options);
return this.asInitializingTaskGroup({
async loadOptions() {
await this.parseCurrentJHipsterCommand();

if (this[ALL_GENERATORS]) {
this.config.set(allGeneratorsConfig());
}
if (this.options.defaults) {
this.config.defaults(defaultConfig({ config: this.jhipsterConfig }));
}
},
};
});
}

get [BaseGenerator.INITIALIZING]() {
Expand Down Expand Up @@ -155,14 +155,15 @@ export default class extends BaseGenerator {
}

get loading() {
return {
createContext() {
return this.asLoadingTaskGroup({
async createContext() {
this.application = { ...defaultConfig(), ...this.config.getAll() };
await this.loadCurrentJHipsterCommandConfig(this.application);
},
async load() {
this.application.packagejs = packageJson;
},
};
});
}

get [BaseGenerator.LOADING]() {
Expand All @@ -185,6 +186,13 @@ export default class extends BaseGenerator {
preparePath() {
this.application.blueprintsPath = this.application[LOCAL_BLUEPRINT_OPTION] ? '.blueprint/' : 'generators/';
},
prepare() {
const { cli, cliName, baseName } = this.application;
this.application.githubRepository = this.jhipsterConfig.githubRepository ?? `jhipster/generator-jhipster-${baseName}`;
if (cli) {
this.application.cliName = cliName ?? `jhipster-${baseName}`;
}
},
};
}

Expand Down Expand Up @@ -279,7 +287,7 @@ export default class extends BaseGenerator {
* yeoman-test version is loaded through generator-jhipster peer dependency.
* generator-jhipster uses a fixed version, blueprints must set a compatible range.
*/
'yeoman-test': '>=8.0.0-rc.1',
'yeoman-test': '>=8.2.0',
},
engines: {
node: packagejs.engines.node,
Expand All @@ -288,7 +296,7 @@ export default class extends BaseGenerator {
},
addCliToPackageJson() {
if (!this.jhipsterConfig.cli || this.jhipsterConfig[LOCAL_BLUEPRINT_OPTION]) return;
const { baseName, cliName = `jhipster-${baseName}` } = this.application;
const { cliName } = this.application;
this.packageJson.merge({
bin: {
[cliName]: 'cli/cli.cjs',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ const defaultCommands = {
desc: 'Generate a test sample',
blueprint: '@jhipster/jhipster-dev',
},
<%_ if (githubWorkflows) { _%>
'github-build-matrix': {
desc: 'Build a matrix of jobs for github actions',
blueprint: '@jhipster/jhipster-dev',
},
<%_ } _%>
};

export default defaultCommands;
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { readdir } from 'node:fs/promises';
import { GENERATOR_APP } from 'generator-jhipster/generators';

/**
* @type {import('generator-jhipster').JHipsterCommandDefinition}
*/
Expand All @@ -26,6 +28,24 @@ const command = {
type: String,
},
},
configs: {
sampleName: {
prompt: gen => ({
when: !gen.all,
type: 'list',
message: 'which sample do you want to generate?',
choices: async () => readdir(gen.templatePath('samples')),
}),
scope: 'generator',
},
all: {
description: 'Generate every sample in a workspace',
cli: {
type: Boolean,
},
scope: 'generator',
},
},
options: {},
import: [GENERATOR_APP],
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { readdir } from 'node:fs/promises';
import { readFileSync } from 'node:fs';
import BaseGenerator from 'generator-jhipster/generators/base';
import command from './command.mjs';

export default class extends BaseGenerator {
sampleName;
all;

constructor(args, opts, features) {
super(args, opts, { ...features, jhipsterBootstrap: false });
Expand All @@ -26,23 +26,19 @@ export default class extends BaseGenerator {
get [BaseGenerator.PROMPTING]() {
return this.asPromptingTaskGroup({
async askForSample() {
if (!this.sampleName) {
const answers = await this.prompt({
type: 'list',
name: 'sampleName',
message: 'which sample do you want to generate?',
choices: async () => readdir(this.templatePath('samples')),
})
this.sampleName = answers.sampleName;
}
await this.promptCurrentJHipsterCommand();
},
});
}

get [BaseGenerator.WRITING]() {
return this.asWritingTaskGroup({
async copySample() {
this.copyTemplate(`samples/${this.sampleName}`, this.sampleName, { noGlob: true });
if (this.all) {
this.copyTemplate('samples/*.jdl', '');
} else {
this.copyTemplate(`samples/${this.sampleName}`, this.sampleName, { noGlob: true });
}
},
});
}
Expand All @@ -54,13 +50,13 @@ export default class extends BaseGenerator {
const projectVersion = `${packageJson.version}-git`;

await this.composeWithJHipster('jdl', {
generatorArgs: [this.sampleName],
generatorArgs: this.all ? await readdir(this.templatePath('samples')) : [this.sampleName],
generatorOptions: {
skipJhipsterDependencies: true,
insight: false,
skipChecks: true,
skipInstall: true,
projectVersion,
...(this.all ? { workspaces: true, monorepository: true } : { skipInstall: true }),
},
});
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { readdir } from 'fs/promises';
import { RECOMMENDED_JAVA_VERSION, RECOMMENDED_NODE_VERSION } from 'generator-jhipster';
import { fromMatrix } from 'generator-jhipster/testing';

const defaultMatrix = {
os: ['ubuntu-latest'],
'node-version': [RECOMMENDED_NODE_VERSION],
'java-version': [RECOMMENDED_JAVA_VERSION],
'default-environment': ['prod'],
};

export const buildMatrix = async samplesFolder => {
const samples = await readdir(samplesFolder);
return {
include: Object.values(
fromMatrix({
...defaultMatrix,
'sample-name': samples.filter(sample => !sample.includes('disabled')),
}),
),
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { existsSync, appendFileSync } from 'node:fs';
import os from 'node:os';
import BaseGenerator from 'generator-jhipster/generators/base';
import { buildMatrix } from './build-matrix.mjs';

export default class extends BaseGenerator {
constructor(args, opts, features) {
super(args, opts, { ...features, jhipsterBootstrap: false });
}

get [BaseGenerator.WRITING]() {
return this.asWritingTaskGroup({
async buildMatrix() {
const matrix = await buildMatrix(this.templatePath('../../generate-sample/templates/samples'));
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' });
}
},
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './generator.mjs';
4 changes: 2 additions & 2 deletions generators/generate-blueprint/templates/.eslintrc.json.ejs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"env": {
"node": true,
"es2020": true
"es2022": true
},
"extends": ["eslint:recommended", "plugin:prettier/recommended"],
"parserOptions": {
"ecmaVersion": 11,
"ecmaVersion": 13,
"sourceType": "module"
},
"overrides": [
Expand Down
24 changes: 16 additions & 8 deletions generators/generate-blueprint/templates/README.md.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@

[![NPM version][npm-image]][npm-url]
[![Generator][github-generator-image]][github-generator-url]
[![Integration Test][github-integration-image]][github-integration-url]
[![Samples][github-samples-image]][github-samples-url]

# Introduction

This is a [JHipster](https://www.jhipster.tech/) blueprint, that is meant to be used in a JHipster application.

# Prerequisites

As this is a [JHipster](https://www.jhipster.tech/) blueprint, we expect you have JHipster and its related tools already installed:
As this is a [JHipster](https://www.jhipster.tech/) blueprint, we expect you have JHipster basic knowledge:

- [Installing JHipster](https://www.jhipster.tech/installation/)
- [JHipster](https://www.jhipster.tech/)

# Installation

Expand All @@ -47,13 +47,21 @@ npm install -g generator-jhipster-<%= baseName %>
To use this blueprint, run the below command

```bash
<%_ if (cli) { _%>
<%= cliName %>
<%_ } else { _%>
jhipster --blueprints <%= baseName %>
<%_ } _%>
```

You can look for updated <%= baseName %> blueprint specific options by running

```bash
<%_ if (cli) { _%>
<%= cliName %> app --help
<%_ } else { _%>
jhipster app --blueprints <%= baseName %> --help
<%_ } _%>
```

And looking for `(blueprint option: <%= baseName %>)` like
Expand All @@ -63,13 +71,13 @@ And looking for `(blueprint option: <%= baseName %>)` like
To use an unreleased version, install it using git.

```bash
npm install -g jhipster/generator-jhipster-<%= baseName %>#main
npm install -g <%= githubRepository %>#main
jhipster --blueprints <%= baseName %> --skip-jhipster-dependencies
```

[npm-image]: https://img.shields.io/npm/v/generator-jhipster-<%= baseName %>.svg
[npm-url]: https://npmjs.org/package/generator-jhipster-<%= baseName %>
[github-generator-image]: https://github.com/jhipster/generator-jhipster-<%= baseName %>/actions/workflows/generator.yml/badge.svg
[github-generator-url]: https://github.com/jhipster/generator-jhipster-<%= baseName %>/actions/workflows/generator.yml
[github-integration-image]: https://github.com/jhipster/generator-jhipster-<%= baseName %>/actions/workflows/integration.yml/badge.svg
[github-integration-url]: https://github.com/jhipster/generator-jhipster-<%= baseName %>/actions/workflows/integration.yml
[github-generator-image]: https://github.com/<%= githubRepository %>/actions/workflows/generator.yml/badge.svg
[github-generator-url]: https://github.com/<%= githubRepository %>/actions/workflows/generator.yml
[github-samples-image]: https://github.com/<%= githubRepository %>/actions/workflows/samples.yml/badge.svg
[github-samples-url]: https://github.com/<%= githubRepository %>/actions/workflows/samples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export default defineConfig({
pool: 'forks',
hookTimeout: 20000,
exclude: [...defaultExclude.filter(val => val !== '**/cypress/**'), '**/templates/**', '**/resources/**'],
setupFiles: ['./vitest.test-setup.ts'],
},
});
4 changes: 2 additions & 2 deletions generators/init/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ export default class InitGenerator extends BaseApplicationGenerator {
addPrettierDependencies({ application }) {
this.packageJson.merge({
scripts: {
'prettier-check': 'prettier --check "{,**/}*.{md,json,yml,html,js,ts,tsx,css,scss,vue,java}"',
'prettier-format': 'prettier --write "{,**/}*.{md,json,yml,html,js,ts,tsx,css,scss,vue,java}"',
'prettier-check': 'prettier --check "{,**/}*.{md,json,yml,html,cjs,mjs,js,cts,mts,ts,tsx,css,scss,vue,java}"',
'prettier-format': 'prettier --write "{,**/}*.{md,json,yml,html,cjs,mjs,js,cts,mts,ts,tsx,css,scss,vue,java}"',
},
devDependencies: {
prettier: application.nodeDependencies.prettier,
Expand Down

0 comments on commit 611be68

Please sign in to comment.