Skip to content

Commit

Permalink
Merge pull request #10163 from mshima/use_existing_data
Browse files Browse the repository at this point in the history
Entity blueprint: allow blueprints to create custom configs.
  • Loading branch information
murdos committed Sep 20, 2019
2 parents 063553c + b724cef commit 7a43da8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 22 deletions.
57 changes: 37 additions & 20 deletions generators/entity/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,34 +622,48 @@ class EntityGenerator extends BaseBlueprintGenerator {
if (_.isUndefined(context.changelogDate) && ['sql', 'cassandra'].includes(context.databaseType)) {
context.changelogDate = this.dateFormatForLiquibase();
}
this.data = {};
this.data.fluentMethods = context.fluentMethods;
this.data.clientRootFolder = context.clientRootFolder;
this.data.relationships = context.relationships;
this.data.fields = context.fields;
this.data.changelogDate = context.changelogDate;
this.data.dto = context.dto;
this.data.searchEngine = context.searchEngine;
this.data.service = context.service;
this.data.entityTableName = context.entityTableName;
this.data.databaseType = context.databaseType;
this.copyFilteringFlag(context, this.data, context);

// Keep existing config by cloning fileData
const storageData = this.context.fileData ? { ...this.context.fileData } : {};
storageData.fluentMethods = context.fluentMethods;
storageData.clientRootFolder = context.clientRootFolder;
storageData.relationships = context.relationships;
storageData.fields = context.fields;
storageData.changelogDate = context.changelogDate;
storageData.dto = context.dto;
storageData.searchEngine = context.searchEngine;
storageData.service = context.service;
storageData.entityTableName = context.entityTableName;
storageData.databaseType = context.databaseType;
this.copyFilteringFlag(context, storageData, context);
if (['sql', 'mongodb', 'couchbase'].includes(context.databaseType)) {
this.data.pagination = context.pagination;
storageData.pagination = context.pagination;
} else {
this.data.pagination = 'no';
storageData.pagination = 'no';
}
this.data.javadoc = context.javadoc;
storageData.javadoc = context.javadoc;
if (context.entityAngularJSSuffix) {
this.data.angularJSSuffix = context.entityAngularJSSuffix;
storageData.angularJSSuffix = context.entityAngularJSSuffix;
}
if (context.applicationType === 'microservice' || context.applicationType === 'uaa') {
this.data.microserviceName = context.baseName;
storageData.microserviceName = context.baseName;
}
if (context.applicationType === 'gateway' && context.useMicroserviceJson) {
this.data.microserviceName = context.microserviceName;
storageData.microserviceName = context.microserviceName;
}

if (this.storageData) {
// Override storageData configs with existing this.storageData
// So that blueprints can create it and override fields.
this.storageData = { ...storageData, ...this.storageData };
} else {
this.storageData = storageData;
}
this.fs.writeJSON(context.filename, this.data, null, 4);

this.fs.writeJSON(context.filename, this.storageData, null, 4);

// Keep this.data for compatibility with existing blueprints
this.data = this.storageData;
},

loadInMemoryData() {
Expand Down Expand Up @@ -1151,7 +1165,10 @@ class EntityGenerator extends BaseBlueprintGenerator {
this.log(`\n${chalk.bold.green('Running post run module hooks\n')}`);
// form the data to be passed to modules
const context = this.context;
context.data = context.data || context.fileData;

// Keep context.data for unexpected compatibility issue.
context.data = context.data || this.storageData || context.fileData;

// run through all post entity creation module hooks
this.callHooks(
'entity',
Expand Down
27 changes: 25 additions & 2 deletions test/blueprint/entity-blueprint.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,29 @@ const mockBlueprintSubGen = class extends EntityGenerator {

get initializing() {
const phaseFromJHipster = super._initializing();
const customPhaseSteps = {
const customPrePhaseSteps = {
// Create a custom persistent entity config.
createCustomConfig() {
// Simulate data loaded from file
this.context.fileData = this.context.fileData || {};
this.context.fileData.customPreConfigKey = 'customPreConfigValue';
this.context.fileData.customBlueprintConfigKey = 'customPreConfigValue';

// Override with new value
this.storageData = {
customBlueprintConfigKey: 'customBlueprintConfigValue'
};
}
};
const customPostPhaseSteps = {
changeProperty() {
this.context.angularAppName = 'awesomeAngularAppName';
}
};
return {
...customPrePhaseSteps,
...phaseFromJHipster,
...customPhaseSteps
...customPostPhaseSteps
};
}

Expand Down Expand Up @@ -99,6 +114,14 @@ describe('JHipster entity generator with blueprint', () => {
it('contains the specific change added by the blueprint', () => {
assert.fileContent(`${CLIENT_MAIN_SRC_DIR}i18n/en/foo.json`, /awesomeAngularAppName/);
});

// Verify if the custom entity config is persisted.
it('contains the specific config added', () => {
assert.fileContent('.jhipster/Foo.json', /"customPreConfigKey": "customPreConfigValue"/);
});
it('contains the specific config added by the blueprint', () => {
assert.fileContent('.jhipster/Foo.json', /"customBlueprintConfigKey": "customBlueprintConfigValue"/);
});
});
});

Expand Down

0 comments on commit 7a43da8

Please sign in to comment.