Skip to content

Commit 5eee06c

Browse files
committed
fix: fix mvnw or gradlew no longer generated when creating simple projects
1 parent 4874959 commit 5eee06c

File tree

16 files changed

+671
-496
lines changed

16 files changed

+671
-496
lines changed

packages/nx-ktor/src/generators/preset/schema.json

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,7 @@
5858
"description": "Engine to use to serve the application.",
5959
"type": "string",
6060
"default": "NETTY",
61-
"enum": [
62-
"NETTY",
63-
"JETTY",
64-
"CIO",
65-
"TOMCAT"
66-
],
61+
"enum": ["NETTY", "JETTY", "CIO", "TOMCAT"],
6762
"x-prompt": {
6863
"message": "Which engine(application server) would you like to use?",
6964
"type": "list",
@@ -106,11 +101,7 @@
106101
"description": "Configuratin Location.",
107102
"type": "string",
108103
"default": "CODE",
109-
"enum": [
110-
"YAML",
111-
"HOCON",
112-
"CODE"
113-
],
104+
"enum": ["YAML", "HOCON", "CODE"],
114105
"x-prompt": {
115106
"message": "Where would you like to put the configuration in?",
116107
"type": "list",
@@ -163,7 +154,7 @@
163154
"keepProjectLevelWrapper": {
164155
"description": "Keep the `Maven` or `Gradle` wrapper files from child project (when generating a multi-module project). Follow this guide https://t.ly/dZelN for more information.",
165156
"type": "boolean",
166-
"default": false
157+
"default": true
167158
},
168159
"ktorInitializrUrl": {
169160
"type": "string",
@@ -185,13 +176,8 @@
185176
"projectNameAndRootFormat": {
186177
"description": "Whether to generate the project name and root directory as provided (`as-provided`) or generate them composing their values and taking the configured layout into account (`derived`).",
187178
"type": "string",
188-
"enum": [
189-
"as-provided",
190-
"derived"
191-
]
179+
"enum": ["as-provided", "derived"]
192180
}
193181
},
194-
"required": [
195-
"prjName"
196-
]
197-
}
182+
"required": ["prjName"]
183+
}

packages/nx-ktor/src/generators/project/lib/generate-ktor-project.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
import { Tree, joinPathFragments, logger, stripIndents, workspaceRoot } from '@nx/devkit';
1+
import {
2+
Tree,
3+
joinPathFragments,
4+
logger,
5+
stripIndents,
6+
workspaceRoot,
7+
} from '@nx/devkit';
28

39
import fetch from 'node-fetch';
410
import { NormalizedSchema } from '../schema';
511
import { buildKtorDownloadUrl } from '../../../utils/ktor-utils';
612
import { NX_KTOR_PKG } from '../../../index';
713
import {
814
extractFromZipStream,
9-
getCommonHttpHeaders,
15+
getCommonHttpHeaders,
1016
getGradleWrapperFiles,
1117
getMavenWrapperFiles,
1218
} from '@nxrocks/common-jvm';
@@ -35,7 +41,10 @@ export async function generateKtorProject(
3541
const response = await fetch(downloadUrl, downloadOptions);
3642

3743
logger.info(
38-
`📦 Extracting Ktor project zip to '${joinPathFragments(workspaceRoot, options.projectRoot)}'...`
44+
`📦 Extracting Ktor project zip to '${joinPathFragments(
45+
workspaceRoot,
46+
options.projectRoot
47+
)}'...`
3948
);
4049

4150
if (response.ok) {
@@ -44,7 +53,10 @@ export async function generateKtorProject(
4453
entryPath.endsWith('mvnw') || entryPath.endsWith('gradlew')
4554
? '755'
4655
: undefined;
47-
if (getMavenWrapperFiles().includes(entryPath) || getGradleWrapperFiles().includes(entryPath)) {
56+
if (
57+
getMavenWrapperFiles().includes(entryPath) ||
58+
getGradleWrapperFiles().includes(entryPath)
59+
) {
4860
if (options.transformIntoMultiModule) {
4961
tree.write(`${options.moduleRoot}/${entryPath}`, entryContent, {
5062
mode: execPermission,
@@ -55,18 +67,17 @@ export async function generateKtorProject(
5567
mode: execPermission,
5668
});
5769
}
58-
59-
}
60-
else {
70+
} else {
6171
tree.write(`${options.projectRoot}/${entryPath}`, entryContent, {
6272
mode: execPermission,
6373
});
6474
}
6575
});
6676
} else {
6777
throw new Error(stripIndents`
68-
❌ Error downloading Ktor project zip from '${options.ktorInitializrUrl
69-
}'
78+
❌ Error downloading Ktor project zip from '${
79+
options.ktorInitializrUrl
80+
}'
7081
If the problem persists, please open an issue at https://github.com/tinesoft/nxrocks/issues, with the following information:
7182
------------------------------------------------------
7283
Download URL: ${downloadUrl}
Lines changed: 134 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,151 @@
1-
import { logger, createProjectGraphAsync, ProjectGraph, Tree } from "@nx/devkit";
1+
import {
2+
logger,
3+
createProjectGraphAsync,
4+
ProjectGraph,
5+
Tree,
6+
} from '@nx/devkit';
27
import { prompt } from 'enquirer';
38

4-
import { NormalizedSchema } from "../schema";
5-
import { addGradleModule, addMavenModule, initGradleParentModule, initMavenParentModule, hasMultiModuleGradleProjectInTree, hasMultiModuleMavenProjectInTree, getAdjustedProjectAndModuleRoot } from "@nxrocks/common-jvm";
9+
import { NormalizedSchema } from '../schema';
10+
import {
11+
addGradleModule,
12+
addMavenModule,
13+
initGradleParentModule,
14+
initMavenParentModule,
15+
hasMultiModuleGradleProjectInTree,
16+
hasMultiModuleMavenProjectInTree,
17+
getAdjustedProjectAndModuleRoot,
18+
} from '@nxrocks/common-jvm';
619

7-
export async function promptForMultiModuleSupport(tree: Tree, options: NormalizedSchema) {
20+
export async function promptForMultiModuleSupport(
21+
tree: Tree,
22+
options: NormalizedSchema
23+
) {
24+
const buildSystemName = options.buildSystem === 'MAVEN' ? 'Maven' : 'Gradle';
825

9-
const buildSystemName = options.buildSystem === 'MAVEN' ? 'Maven' : 'Gradle';
26+
if (
27+
(options.transformIntoMultiModule === undefined ||
28+
options.addToExistingParentModule === undefined) &&
29+
options.parentModuleName === undefined &&
30+
process.env.NX_INTERACTIVE === 'true'
31+
) {
32+
logger.info(
33+
`⏳ Checking for existing multi-module projects. Please wait...`
34+
);
1035

11-
if (
12-
(options.transformIntoMultiModule === undefined || options.addToExistingParentModule === undefined) &&
13-
options.parentModuleName === undefined &&
14-
process.env.NX_INTERACTIVE === 'true'
15-
) {
16-
logger.info(`⏳ Checking for existing multi-module projects. Please wait...`);
36+
const projectGraph: ProjectGraph = await createProjectGraphAsync();
1737

18-
const projectGraph: ProjectGraph = await createProjectGraphAsync();
38+
const multiModuleProjects = Object.values(projectGraph.nodes)
39+
.map((n) => n.data)
40+
.filter((project) =>
41+
options.buildSystem === 'MAVEN'
42+
? hasMultiModuleMavenProjectInTree(tree, project.root)
43+
: hasMultiModuleGradleProjectInTree(tree, project.root)
44+
);
1945

20-
const multiModuleProjects = Object.values(projectGraph.nodes).map(n => n.data).filter(project => options.buildSystem === 'MAVEN' ? hasMultiModuleMavenProjectInTree(tree, project.root) : hasMultiModuleGradleProjectInTree(tree, project.root))
46+
if (multiModuleProjects.length === 0) {
47+
options.transformIntoMultiModule = await prompt({
48+
name: 'transformIntoMultiModule',
49+
message: `Would you like to transform the generated project into a ${buildSystemName} multi-module project?`,
50+
type: 'confirm',
51+
initial: false,
52+
}).then((a) => a['transformIntoMultiModule']);
2153

22-
if (multiModuleProjects.length === 0) {
23-
options.transformIntoMultiModule = await prompt({
24-
name: 'transformIntoMultiModule',
25-
message:
26-
`Would you like to transform the generated project into a ${buildSystemName} multi-module project?`,
27-
type: 'confirm',
28-
initial: false
29-
}).then((a) => a['transformIntoMultiModule']);
54+
if (options.transformIntoMultiModule) {
55+
options.parentModuleName = (
56+
await prompt({
57+
name: 'parentModuleName',
58+
message: `What name would you like to use for the ${buildSystemName} multi-module project?`,
59+
type: 'input',
60+
initial: `${options.projectName}-parent`,
61+
}).then((a) => a['parentModuleName'])
62+
).replace(/\//g, '-');
3063

31-
if (options.transformIntoMultiModule) {
32-
options.parentModuleName = (await prompt({
33-
name: 'parentModuleName',
34-
message:
35-
`What name would you like to use for the ${buildSystemName} multi-module project?`,
36-
type: 'input',
37-
initial: `${options.projectName}-parent`
38-
}).then((a) => a['parentModuleName'])).replace(/\//g, '-');
39-
40-
options.keepProjectLevelWrapper ??= false;
41-
}
42-
}
43-
else {
44-
options.addToExistingParentModule = await prompt({
45-
name: 'addToExistingParentModule',
46-
message:
47-
`We found ${multiModuleProjects.length} existing ${buildSystemName} multi-module projects in your workaspace${multiModuleProjects.length === 1 ? `('${multiModuleProjects[0].name}')` : ''}.\nWould you like to add this new project ${multiModuleProjects.length === 1 ? 'to it?' : 'into one of them?'}`,
48-
type: 'confirm',
49-
initial: false
50-
}).then((a) => a['addToExistingParentModule']);
51-
52-
if (options.addToExistingParentModule) {
53-
if (multiModuleProjects.length === 1) {
54-
options.parentModuleName = multiModuleProjects[0].name;
55-
}
56-
else {
57-
options.parentModuleName = await prompt({
58-
name: 'parentModuleName',
59-
message:
60-
'Which parent module would you like to add the new project into?',
61-
type: 'select',
62-
choices: multiModuleProjects.map(p => p.name),
63-
}).then((a) => a['parentModuleName']);
64-
}
65-
}
64+
options.keepProjectLevelWrapper ??= true;
65+
}
66+
} else {
67+
options.addToExistingParentModule = await prompt({
68+
name: 'addToExistingParentModule',
69+
message: `We found ${
70+
multiModuleProjects.length
71+
} existing ${buildSystemName} multi-module projects in your workaspace${
72+
multiModuleProjects.length === 1
73+
? `('${multiModuleProjects[0].name}')`
74+
: ''
75+
}.\nWould you like to add this new project ${
76+
multiModuleProjects.length === 1 ? 'to it?' : 'into one of them?'
77+
}`,
78+
type: 'confirm',
79+
initial: false,
80+
}).then((a) => a['addToExistingParentModule']);
6681

82+
if (options.addToExistingParentModule) {
83+
if (multiModuleProjects.length === 1) {
84+
options.parentModuleName = multiModuleProjects[0].name;
85+
} else {
86+
options.parentModuleName = await prompt({
87+
name: 'parentModuleName',
88+
message:
89+
'Which parent module would you like to add the new project into?',
90+
type: 'select',
91+
choices: multiModuleProjects.map((p) => p.name),
92+
}).then((a) => a['parentModuleName']);
6793
}
94+
}
6895
}
69-
if ((options.transformIntoMultiModule || options.addToExistingParentModule) && options.parentModuleName) {
70-
const isMavenProject = options.buildSystem === 'MAVEN';
71-
const helpComment = `For more information about ${buildSystemName} multi-modules projects, go to: ${isMavenProject ? 'https://maven.apache.org/guides/mini/guide-multiple-modules-4.html' : 'https://docs.gradle.org/current/userguide/intro_multi_project_builds.html'}`;
96+
}
97+
if (
98+
(options.transformIntoMultiModule || options.addToExistingParentModule) &&
99+
options.parentModuleName
100+
) {
101+
const isMavenProject = options.buildSystem === 'MAVEN';
102+
const helpComment = `For more information about ${buildSystemName} multi-modules projects, go to: ${
103+
isMavenProject
104+
? 'https://maven.apache.org/guides/mini/guide-multiple-modules-4.html'
105+
: 'https://docs.gradle.org/current/userguide/intro_multi_project_builds.html'
106+
}`;
72107

73-
const opts = await getAdjustedProjectAndModuleRoot(options, isMavenProject);
108+
const opts = await getAdjustedProjectAndModuleRoot(options, isMavenProject);
74109

75-
options.projectRoot = opts.projectRoot;
76-
options.moduleRoot = opts.moduleRoot;
110+
options.projectRoot = opts.projectRoot;
111+
options.moduleRoot = opts.moduleRoot;
77112

78-
if (options.transformIntoMultiModule) {
79-
// add the root module
80-
if (isMavenProject) {
81-
initMavenParentModule(tree, options.moduleRoot, options.groupId, options.parentModuleName, options.projectName, `<!-- ${helpComment} -->`);
82-
}
83-
else {
84-
initGradleParentModule(tree, options.moduleRoot, options.groupId, options.parentModuleName, options.projectName, opts.offsetFromRoot, options.buildSystem === 'GRADLE_KTS', `// ${helpComment}`);
85-
}
86-
}
87-
else if (options.addToExistingParentModule) {
88-
// add to the chosen root module
89-
if (isMavenProject) {
90-
addMavenModule(tree, options.moduleRoot, options.projectName);
91-
}
92-
else {
93-
addGradleModule(tree, options.moduleRoot, options.projectName, opts.offsetFromRoot, options.buildSystem === 'GRADLE_KTS');
94-
}
95-
}
113+
if (options.transformIntoMultiModule) {
114+
// add the root module
115+
if (isMavenProject) {
116+
initMavenParentModule(
117+
tree,
118+
options.moduleRoot,
119+
options.groupId,
120+
options.parentModuleName,
121+
options.projectName,
122+
`<!-- ${helpComment} -->`
123+
);
124+
} else {
125+
initGradleParentModule(
126+
tree,
127+
options.moduleRoot,
128+
options.groupId,
129+
options.parentModuleName,
130+
options.projectName,
131+
opts.offsetFromRoot,
132+
options.buildSystem === 'GRADLE_KTS',
133+
`// ${helpComment}`
134+
);
135+
}
136+
} else if (options.addToExistingParentModule) {
137+
// add to the chosen root module
138+
if (isMavenProject) {
139+
addMavenModule(tree, options.moduleRoot, options.projectName);
140+
} else {
141+
addGradleModule(
142+
tree,
143+
options.moduleRoot,
144+
options.projectName,
145+
opts.offsetFromRoot,
146+
options.buildSystem === 'GRADLE_KTS'
147+
);
148+
}
96149
}
150+
}
97151
}

packages/nx-ktor/src/generators/project/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@
158158
"keepProjectLevelWrapper": {
159159
"description": "Keep the `Maven` or `Gradle` wrapper files from child project (when generating a multi-module project). Follow this guide https://t.ly/dZelN for more information.",
160160
"type": "boolean",
161-
"default": false
161+
"default": true
162162
},
163163
"ktorInitializrUrl": {
164164
"type": "string",

0 commit comments

Comments
 (0)