Skip to content

Commit 5d647b4

Browse files
committed
bootstrap: add option to remove needles
1 parent d9e7786 commit 5d647b4

File tree

8 files changed

+72
-3
lines changed

8 files changed

+72
-3
lines changed

.blueprint/generate-sample/generator.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default class extends BaseGenerator {
1616
sampleYorcFolder;
1717

1818
constructor(args, options, features) {
19-
super(args, options, { queueCommandTasks: true, ...features });
19+
super(args, options, { jhipsterBootstrap: false, queueCommandTasks: true, ...features });
2020
}
2121

2222
get [BaseGenerator.INITIALIZING]() {
@@ -92,7 +92,7 @@ export default class extends BaseGenerator {
9292
if (sample.jdlFiles) {
9393
await this.composeWithJHipster(GENERATOR_JDL, {
9494
generatorArgs: sample.jdlFiles,
95-
generatorOptions: { jsonOnly: true, destinationRoot: this.projectFolder },
95+
generatorOptions: { jsonOnly: true, destinationRoot: this.projectFolder, ...generatorOptions },
9696
});
9797
}
9898
await this.composeWithJHipster(GENERATOR_APP, { generatorOptions });

.blueprint/github-build-matrix/__snapshots__/generator.spec.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ exports[`generator - github-build-matrix with angular should match matrix value
4141
"app-sample": "ng-default",
4242
"jdl-entity": "*",
4343
"generatorOptions": {
44+
"removeNeedles": true,
4445
"clientBundler": "experimentalEsbuild"
4546
},
4647
"workspaces": "false",

generators/app/__snapshots__/generator.spec.ts.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ exports[`generator - app help should print expected information 1`] = `
77
88
Options:
99
--auto-crlf Detect line endings
10+
--remove-needles Remove needles, needles are used by entity and language generators to inject code
1011
--defaults Execute jhipster with default config
1112
--skip-client Skip the client-side application generation
1213
--skip-server Skip the server-side application generation
@@ -813,6 +814,7 @@ exports[`generator - app with default config should match snapshot 1`] = `
813814
"reactorBlock": "",
814815
"reactorBlockOptional": "",
815816
"rememberMeKey": undefined,
817+
"removeNeedles": undefined,
816818
"requiresDeleteAllUsers": false,
817819
"routes": undefined,
818820
"searchEngine": "no",
@@ -1482,6 +1484,7 @@ exports[`generator - app with gateway should match snapshot 1`] = `
14821484
"reactorBlock": ".block()",
14831485
"reactorBlockOptional": ".blockOptional()",
14841486
"rememberMeKey": undefined,
1487+
"removeNeedles": undefined,
14851488
"requiresDeleteAllUsers": true,
14861489
"routes": undefined,
14871490
"searchEngine": "no",
@@ -2082,6 +2085,7 @@ exports[`generator - app with microservice should match snapshot 1`] = `
20822085
"reactorBlock": "",
20832086
"reactorBlockOptional": "",
20842087
"rememberMeKey": undefined,
2088+
"removeNeedles": undefined,
20852089
"requiresDeleteAllUsers": false,
20862090
"routes": undefined,
20872091
"searchEngine": "no",

generators/base/support/needles.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@
1818
*/
1919
import assert from 'assert';
2020
import { escapeRegExp, kebabCase } from 'lodash-es';
21+
import { transformContents } from '@yeoman/transform';
22+
import type { MemFsEditorFile } from 'mem-fs-editor';
2123
import type CoreGenerator from '../../base-core/index.js';
2224
import type { CascatedEditFileCallback, EditFileCallback, NeedleCallback } from '../api.js';
2325
import { joinCallbacks } from './write-files.js';
2426

27+
const needlesWhiteList = [
28+
'liquibase-add-incremental-changelog', // mandatory for incremental changelogs
29+
];
30+
2531
type NeedleContentToAddCallback = {
2632
/**
2733
* Position of the needle start.
@@ -81,6 +87,27 @@ export const convertToPrettierExpressions = (str: string): string =>
8187
.replace(/(?! )(>|\\\))/g, ',?\\n?[\\s]*$1')
8288
.replace(/\s+/g, '[\\s\\n]*');
8389

90+
export const createNeedleRegexp = (needle: string): RegExp => new RegExp(`(?://|<!--|/\\*|#) ${needle}(?: [^$\\n]*)?(?:$|\\n)`, 'g');
91+
92+
type NeedleLinePosition = {
93+
start: number;
94+
end: number;
95+
};
96+
97+
export const getNeedlesPositions = (content: string, needle = 'jhipster-needle-(?:[-\\w]*)'): NeedleLinePosition[] => {
98+
const regexp = createNeedleRegexp(needle);
99+
const positions: NeedleLinePosition[] = [];
100+
let match: RegExpExecArray | null;
101+
while ((match = regexp.exec(content))) {
102+
if (needlesWhiteList.some(whileList => match![0].includes(whileList))) {
103+
continue;
104+
}
105+
const needleLineIndex = content.lastIndexOf('\n', match.index) + 1;
106+
positions.unshift({ start: needleLineIndex, end: regexp.lastIndex });
107+
}
108+
return positions;
109+
};
110+
84111
/**
85112
* Check if contentToCheck existing in content
86113
*
@@ -120,7 +147,7 @@ export const insertContentBeforeNeedle = ({ content, contentToAdd, needle, autoI
120147

121148
needle = needle.includes('jhipster-needle-') ? needle : `jhipster-needle-${needle}`;
122149

123-
const regexp = new RegExp(`(?://|<!--|/\\*|#) ${needle}(?:$|\n| )`, 'g');
150+
const regexp = createNeedleRegexp(needle);
124151
const firstMatch = regexp.exec(content);
125152
if (!firstMatch) {
126153
return null;
@@ -279,3 +306,18 @@ export function createBaseNeedle<Generator extends CoreGenerator = CoreGenerator
279306

280307
return callback;
281308
}
309+
310+
export const createNeedleTransform = () =>
311+
transformContents<MemFsEditorFile>(content => {
312+
if (content) {
313+
let contentAsString = content.toString();
314+
const positions = getNeedlesPositions(contentAsString);
315+
if (positions.length > 0) {
316+
for (const position of positions) {
317+
contentAsString = contentAsString.slice(0, position.start) + contentAsString.slice(position.end);
318+
}
319+
return Buffer.from(contentAsString);
320+
}
321+
}
322+
return content;
323+
});

generators/bootstrap/command.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ const command: JHipsterCommandDefinition = {
5050
scope: 'generator',
5151
},
5252
},
53+
configs: {
54+
removeNeedles: {
55+
description: 'Remove needles, needles are used by entity and language generators to inject code',
56+
cli: {
57+
type: Boolean,
58+
},
59+
jdl: {
60+
tokenType: 'BOOLEAN',
61+
type: 'boolean',
62+
},
63+
scope: 'storage',
64+
},
65+
},
5366
};
5467

5568
export default command;

generators/bootstrap/generator.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { GENERATOR_UPGRADE } from '../generator-list.js';
3131
import { PRIORITY_NAMES, QUEUES } from '../base-application/priorities.js';
3232
import { loadStoredAppOptions } from '../app/support/index.js';
3333
import type { GenericTaskGroup, TaskParamWithControl } from '../../lib/types/base/tasks.js';
34+
import { createNeedleTransform } from '../base/support/needles.js';
3435
import {
3536
autoCrlfTransform,
3637
createESLintTransform,
@@ -259,11 +260,17 @@ export default class BootstrapGenerator extends BaseGenerator {
259260
};
260261
}
261262

263+
const removeNeedlesTransforms: FileTransform<MemFsEditorFile>[] = [];
264+
if (this.jhipsterConfig.removeNeedles) {
265+
removeNeedlesTransforms.push(createNeedleTransform());
266+
}
267+
262268
const transformStreams = [
263269
...skipYoResolveTransforms,
264270
forceYoFiles(),
265271
createSortConfigFilesTransform(),
266272
createForceWriteConfigFilesTransform(),
273+
...removeNeedlesTransforms,
267274
...prettierTransforms,
268275
...autoCrlfTransforms,
269276
createConflicterTransform(this.env.adapter, { ...(this.env as any).conflicterOptions, customizeActions }),

generators/jdl/__snapshots__/generator.spec.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ Create entities from the JDL file/URL/content passed in argument.
159159
160160
Options:
161161
--auto-crlf Detect line endings
162+
--remove-needles Remove needles, needles are used by entity and language generators to inject code
162163
--interactive Generate multiple applications in series so that questions can be interacted with. This is the default when there is an existing application configuration in any of the folders
163164
--json-only Generate only the JSON files and skip entity regeneration
164165
--ignore-application Ignores application generation

test-integration/workflow-samples/angular.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"app-sample": "ng-default",
1414
"jdl-entity": "*",
1515
"generatorOptions": {
16+
"removeNeedles": true,
1617
"clientBundler": "experimentalEsbuild"
1718
}
1819
},

0 commit comments

Comments
 (0)