Skip to content

Commit

Permalink
Merge pull request #23395 from mshima/skip_ci-dev-blueprint
Browse files Browse the repository at this point in the history
Expose dev blueprint to blueprints and improvements to argument parsing.
  • Loading branch information
DanielFran authored Sep 4, 2023
2 parents 2f121d5 + f2c2999 commit 1e94370
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 29 deletions.
28 changes: 13 additions & 15 deletions cli/environment-builder.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ import { parseBlueprintInfo, loadBlueprintsFromConfiguration, mergeBlueprints }
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

export const enableDevBlueprint = process.env.JHIPSTER_DEV_BLUEPRINT === 'true';
const devBlueprintPath = path.join(__dirname, '../.blueprint');
const jhipsterDevBlueprintPath = process.env.JHIPSTER_DEV_BLUEPRINT === 'true' ? path.join(__dirname, '../.blueprint') : undefined;
const devBlueprintNamespace = '@jhipster/jhipster-dev';

function loadYoRc(filePath = '.yo-rc.json') {
Expand All @@ -49,6 +48,8 @@ const createEnvironment = (options = {}) => {
};

export default class EnvironmentBuilder {
devBlueprintPath;

/**
* Creates a new EnvironmentBuilder with a new Environment.
*
Expand Down Expand Up @@ -99,12 +100,11 @@ export default class EnvironmentBuilder {
this.env = env;
}

async prepare({ blueprints, lookups } = {}) {
async prepare({ blueprints, lookups, devBlueprintPath = jhipsterDevBlueprintPath } = {}) {
this.devBlueprintPath = existsSync(devBlueprintPath) ? devBlueprintPath : undefined;
await this._lookupJHipster();
await this._lookupLocalBlueprint();
if (enableDevBlueprint) {
await this._lookupDevBlueprint();
}
await this._lookupDevBlueprint();
this._loadBlueprints(blueprints);
await this._lookups(lookups);
await this._lookupBlueprints();
Expand All @@ -116,7 +116,7 @@ export default class EnvironmentBuilder {
return [
...Object.keys(this._blueprintsWithVersion).map(packageName => packageNameToNamespace(packageName)),
'@jhipster/jhipster-local',
...(enableDevBlueprint ? [devBlueprintNamespace] : []),
...(this.devBlueprintPath ? [devBlueprintNamespace] : []),
];
}

Expand Down Expand Up @@ -173,12 +173,10 @@ export default class EnvironmentBuilder {
}

async _lookupDevBlueprint() {
if (existsSync(devBlueprintPath)) {
// Register jhipster generators.
const generators = await this.env.lookup({ packagePaths: [devBlueprintPath], lookups: ['.'] });
if (generators.length > 0) {
this.env.alias(/^@jhipster\/jhipster-dev(:(.*))?$/, '.blueprint$1');
}
// Register jhipster generators.
const generators = await this.env.lookup({ packagePaths: [this.devBlueprintPath], lookups: ['.'] });
if (generators.length > 0) {
this.env.alias(/^@jhipster\/jhipster-dev(:(.*))?$/, '.blueprint$1');
}
return this;
}
Expand Down Expand Up @@ -256,9 +254,9 @@ export default class EnvironmentBuilder {
*/
async getBlueprintCommands() {
let blueprintsPackagePath = await this._getBlueprintPackagePaths();
if (enableDevBlueprint) {
if (this.devBlueprintPath) {
blueprintsPackagePath = blueprintsPackagePath ?? [];
blueprintsPackagePath.push([devBlueprintNamespace, devBlueprintPath]);
blueprintsPackagePath.push([devBlueprintNamespace, this.devBlueprintPath]);
}
return this._getBlueprintCommands(blueprintsPackagePath);
}
Expand Down
4 changes: 3 additions & 1 deletion cli/program.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ export const buildJHipster = async ({
commands,
printLogo,
printBlueprintLogo,
devBlueprintPath,
env,
/* eslint-disable-next-line global-require, import/no-dynamic-require */
loadCommand = async key => {
Expand All @@ -302,7 +303,8 @@ export const buildJHipster = async ({
defaultCommand,
} = {}) => {
// eslint-disable-next-line chai-friendly/no-unused-expressions
createEnvBuilder = createEnvBuilder ?? (async options => EnvironmentBuilder.create(options).prepare({ blueprints, lookups }));
createEnvBuilder =
createEnvBuilder ?? (async options => EnvironmentBuilder.create(options).prepare({ blueprints, lookups, devBlueprintPath }));
envBuilder = envBuilder ?? (await createEnvBuilder());
env = env ?? envBuilder.getEnvironment();
commands = commands ?? { ...SUB_GENERATORS, ...(await envBuilder.getBlueprintCommands()) };
Expand Down
46 changes: 45 additions & 1 deletion generators/base-core/generator-core.spec.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-unused-expressions */
import { expect } from 'chai';
import { jestExpect } from 'esmocha';
import { basicHelpers as helpers, result as runResult } from '../../test/support/index.mjs';

import Base from './index.mjs';
Expand All @@ -15,7 +16,50 @@ BaseGenerator.log = msg => {

BaseGenerator.logger = createJHipsterLogger();

describe('generator - base', () => {
describe('generator - base-core', () => {
describe('passing arguments', () => {
let Dummy;
beforeEach(async () => {
await helpers.prepareTemporaryDir();
Dummy = helpers.createDummyGenerator(Base);
});

it('string arguments', async () => {
const base = new Dummy(['foo'], { sharedData: {}, env: await helpers.createTestEnv() });
base.parseJHipsterArguments({
jdlFiles: {
type: String,
},
});
jestExpect(base.jdlFiles).toBe('foo');
});
it('vararg arguments', async () => {
const base = new Dummy(['bar', 'foo'], { sharedData: {}, env: await helpers.createTestEnv() });
base.parseJHipsterArguments({
first: {
type: String,
},
jdlFiles: {
type: Array,
},
});
jestExpect(base.first).toBe('bar');
jestExpect(base.jdlFiles).toMatchObject(['foo']);
});
it('vararg arguments using positionalArguments', async () => {
const base = new Dummy({ positionalArguments: ['bar', ['foo']], sharedData: {}, env: await helpers.createTestEnv() });
base.parseJHipsterArguments({
first: {
type: String,
},
jdlFiles: {
type: Array,
},
});
jestExpect(base.first).toBe('bar');
jestExpect(base.jdlFiles).toMatchObject(['foo']);
});
});
describe('dateFormatForLiquibase', () => {
let base;
let options;
Expand Down
27 changes: 20 additions & 7 deletions generators/base-core/generator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -301,20 +301,33 @@ export default class CoreGenerator extends YeomanGenerator<JHipsterGeneratorOpti
}

parseJHipsterArguments(jhipsterArguments: JHipsterArguments = {}) {
const { positionalArguments = this._args ?? [] } = this.options;
const hasPositionalArguments = Boolean(this.options.positionalArguments);
let positionalArguments: unknown[] = hasPositionalArguments ? this.options.positionalArguments! : this._args;
const argumentEntries = Object.entries(jhipsterArguments);
if (positionalArguments.length > argumentEntries.length) {
if (hasPositionalArguments && positionalArguments.length > argumentEntries.length) {
throw new Error('More arguments than allowed');
}

argumentEntries.forEach(([argumentName, argumentDef], index) => {
if (positionalArguments.length > index) {
const argument = positionalArguments[index];
argumentEntries.find(([argumentName, argumentDef]) => {
if (positionalArguments.length > 0) {
let argument;
if (hasPositionalArguments || argumentDef.type !== Array) {
// Positional arguments already parsed or a single argument.
argument = positionalArguments.shift();
} else {
// Varags argument.
argument = positionalArguments;
positionalArguments = [];
}
const convertedValue = !argumentDef.type || argumentDef.type === Array ? argument : argumentDef.type(argument as any);
this[argumentName] = convertedValue;
} else if (argumentDef.required) {
throw new Error(`Missing required argument ${argumentName}`);
} else {
if (argumentDef.required) {
throw new Error(`Missing required argument ${argumentName}`);
}
return true;
}
return false;
});

// Arguments should only be parsed by the root generator, cleanup to don't be forwarded.
Expand Down
8 changes: 3 additions & 5 deletions generators/jdl/generator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,9 @@ export default class JdlGenerator extends BaseGenerator {
get initializing() {
return this.asInitializingTaskGroup({
loadArguments() {
if ((this.env.rootGenerator() as any) === this) {
this.parseJHipsterArguments(command.arguments);
if (this.jdlFiles) {
this.log.verboseInfo('Generating jdls', ...this.jdlFiles);
}
this.parseJHipsterArguments(command.arguments);
if (this.jdlFiles) {
this.log.verboseInfo('Generating jdls', ...this.jdlFiles);
}
},
loadOptions() {
Expand Down

0 comments on commit 1e94370

Please sign in to comment.