Skip to content

Commit

Permalink
fix: fix mvnw or gradlew no longer generated when creating simple…
Browse files Browse the repository at this point in the history
… projects
  • Loading branch information
tinesoft committed Jun 23, 2024
1 parent 4874959 commit 2869654
Show file tree
Hide file tree
Showing 19 changed files with 652 additions and 404 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ jobs:
main-branch-name: 'develop'

- run: pnpm exec nx-cloud record -- nx format:check
- run: pnpm exec nx affected -t lint test build e2e --exclude=smoke --codeCoverage
- run: pnpm exec nx affected -t lint test build e2e-ci --parallel=6 --exclude=smoke --codeCoverage
2 changes: 2 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

pnpm affected:format
pnpm affected:lint
pnpm affected:test
pnpm sync-preset-schemas
git add -u
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
"affected:e2e": "nx affected --target e2e --exclude=smoke",
"affected:test": "nx affected --target test --exclude=smoke",
"affected:lint": "nx affected --target lint --fix",
"affected:format": "nx affected --target format",
"affected:graph": "nx affected --graph",
"format": "nx format ",
"format:write": "nx format:write",
"format:check": "nx format:check",
"format": "nx format --base head~1",
"migrate": "nx migrate latest",
"run-migrate": "nx migrate --run-migrations --if-exists",
"graph": "nx graph",
Expand Down
2 changes: 1 addition & 1 deletion packages/nx-ktor/src/generators/preset/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
"keepProjectLevelWrapper": {
"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.",
"type": "boolean",
"default": false
"default": true
},
"ktorInitializrUrl": {
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { Tree, joinPathFragments, logger, stripIndents, workspaceRoot } from '@nx/devkit';
import {
Tree,
joinPathFragments,
logger,
stripIndents,
workspaceRoot,
} from '@nx/devkit';

import fetch from 'node-fetch';
import { NormalizedSchema } from '../schema';
import { buildKtorDownloadUrl } from '../../../utils/ktor-utils';
import { NX_KTOR_PKG } from '../../../index';
import {
extractFromZipStream,
getCommonHttpHeaders,
getCommonHttpHeaders,
getGradleWrapperFiles,
getMavenWrapperFiles,
} from '@nxrocks/common-jvm';
Expand Down Expand Up @@ -35,7 +41,10 @@ export async function generateKtorProject(
const response = await fetch(downloadUrl, downloadOptions);

logger.info(
`📦 Extracting Ktor project zip to '${joinPathFragments(workspaceRoot, options.projectRoot)}'...`
`📦 Extracting Ktor project zip to '${joinPathFragments(
workspaceRoot,
options.projectRoot
)}'...`
);

if (response.ok) {
Expand All @@ -44,7 +53,10 @@ export async function generateKtorProject(
entryPath.endsWith('mvnw') || entryPath.endsWith('gradlew')
? '755'
: undefined;
if (getMavenWrapperFiles().includes(entryPath) || getGradleWrapperFiles().includes(entryPath)) {
if (
getMavenWrapperFiles().includes(entryPath) ||
getGradleWrapperFiles().includes(entryPath)
) {
if (options.transformIntoMultiModule) {
tree.write(`${options.moduleRoot}/${entryPath}`, entryContent, {
mode: execPermission,
Expand All @@ -55,18 +67,17 @@ export async function generateKtorProject(
mode: execPermission,
});
}

}
else {
} else {
tree.write(`${options.projectRoot}/${entryPath}`, entryContent, {
mode: execPermission,
});
}
});
} else {
throw new Error(stripIndents`
❌ Error downloading Ktor project zip from '${options.ktorInitializrUrl
}'
❌ Error downloading Ktor project zip from '${
options.ktorInitializrUrl
}'
If the problem persists, please open an issue at https://github.com/tinesoft/nxrocks/issues, with the following information:
------------------------------------------------------
Download URL: ${downloadUrl}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,97 +1,151 @@
import { logger, createProjectGraphAsync, ProjectGraph, Tree } from "@nx/devkit";
import {
logger,
createProjectGraphAsync,
ProjectGraph,
Tree,
} from '@nx/devkit';
import { prompt } from 'enquirer';

import { NormalizedSchema } from "../schema";
import { addGradleModule, addMavenModule, initGradleParentModule, initMavenParentModule, hasMultiModuleGradleProjectInTree, hasMultiModuleMavenProjectInTree, getAdjustedProjectAndModuleRoot } from "@nxrocks/common-jvm";
import { NormalizedSchema } from '../schema';
import {
addGradleModule,
addMavenModule,
initGradleParentModule,
initMavenParentModule,
hasMultiModuleGradleProjectInTree,
hasMultiModuleMavenProjectInTree,
getAdjustedProjectAndModuleRoot,
} from '@nxrocks/common-jvm';

export async function promptForMultiModuleSupport(tree: Tree, options: NormalizedSchema) {
export async function promptForMultiModuleSupport(
tree: Tree,
options: NormalizedSchema
) {
const buildSystemName = options.buildSystem === 'MAVEN' ? 'Maven' : 'Gradle';

const buildSystemName = options.buildSystem === 'MAVEN' ? 'Maven' : 'Gradle';
if (
(options.transformIntoMultiModule === undefined ||
options.addToExistingParentModule === undefined) &&
options.parentModuleName === undefined &&
process.env.NX_INTERACTIVE === 'true'
) {
logger.info(
`⏳ Checking for existing multi-module projects. Please wait...`
);

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

const projectGraph: ProjectGraph = await createProjectGraphAsync();
const multiModuleProjects = Object.values(projectGraph.nodes)
.map((n) => n.data)
.filter((project) =>
options.buildSystem === 'MAVEN'
? hasMultiModuleMavenProjectInTree(tree, project.root)
: hasMultiModuleGradleProjectInTree(tree, project.root)
);

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

if (multiModuleProjects.length === 0) {
options.transformIntoMultiModule = await prompt({
name: 'transformIntoMultiModule',
message:
`Would you like to transform the generated project into a ${buildSystemName} multi-module project?`,
type: 'confirm',
initial: false
}).then((a) => a['transformIntoMultiModule']);
if (options.transformIntoMultiModule) {
options.parentModuleName = (
await prompt({
name: 'parentModuleName',
message: `What name would you like to use for the ${buildSystemName} multi-module project?`,
type: 'input',
initial: `${options.projectName}-parent`,
}).then((a) => a['parentModuleName'])
).replace(/\//g, '-');

if (options.transformIntoMultiModule) {
options.parentModuleName = (await prompt({
name: 'parentModuleName',
message:
`What name would you like to use for the ${buildSystemName} multi-module project?`,
type: 'input',
initial: `${options.projectName}-parent`
}).then((a) => a['parentModuleName'])).replace(/\//g, '-');

options.keepProjectLevelWrapper ??= false;
}
}
else {
options.addToExistingParentModule = await prompt({
name: 'addToExistingParentModule',
message:
`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?'}`,
type: 'confirm',
initial: false
}).then((a) => a['addToExistingParentModule']);

if (options.addToExistingParentModule) {
if (multiModuleProjects.length === 1) {
options.parentModuleName = multiModuleProjects[0].name;
}
else {
options.parentModuleName = await prompt({
name: 'parentModuleName',
message:
'Which parent module would you like to add the new project into?',
type: 'select',
choices: multiModuleProjects.map(p => p.name),
}).then((a) => a['parentModuleName']);
}
}
options.keepProjectLevelWrapper ??= true;
}
} else {
options.addToExistingParentModule = await prompt({
name: 'addToExistingParentModule',
message: `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?'
}`,
type: 'confirm',
initial: false,
}).then((a) => a['addToExistingParentModule']);

if (options.addToExistingParentModule) {
if (multiModuleProjects.length === 1) {
options.parentModuleName = multiModuleProjects[0].name;
} else {
options.parentModuleName = await prompt({
name: 'parentModuleName',
message:
'Which parent module would you like to add the new project into?',
type: 'select',
choices: multiModuleProjects.map((p) => p.name),
}).then((a) => a['parentModuleName']);
}
}
}
if ((options.transformIntoMultiModule || options.addToExistingParentModule) && options.parentModuleName) {
const isMavenProject = options.buildSystem === 'MAVEN';
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'}`;
}
if (
(options.transformIntoMultiModule || options.addToExistingParentModule) &&
options.parentModuleName
) {
const isMavenProject = options.buildSystem === 'MAVEN';
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'
}`;

const opts = await getAdjustedProjectAndModuleRoot(options, isMavenProject);
const opts = await getAdjustedProjectAndModuleRoot(options, isMavenProject);

options.projectRoot = opts.projectRoot;
options.moduleRoot = opts.moduleRoot;
options.projectRoot = opts.projectRoot;
options.moduleRoot = opts.moduleRoot;

if (options.transformIntoMultiModule) {
// add the root module
if (isMavenProject) {
initMavenParentModule(tree, options.moduleRoot, options.groupId, options.parentModuleName, options.projectName, `<!-- ${helpComment} -->`);
}
else {
initGradleParentModule(tree, options.moduleRoot, options.groupId, options.parentModuleName, options.projectName, opts.offsetFromRoot, options.buildSystem === 'GRADLE_KTS', `// ${helpComment}`);
}
}
else if (options.addToExistingParentModule) {
// add to the chosen root module
if (isMavenProject) {
addMavenModule(tree, options.moduleRoot, options.projectName);
}
else {
addGradleModule(tree, options.moduleRoot, options.projectName, opts.offsetFromRoot, options.buildSystem === 'GRADLE_KTS');
}
}
if (options.transformIntoMultiModule) {
// add the root module
if (isMavenProject) {
initMavenParentModule(
tree,
options.moduleRoot,
options.groupId,
options.parentModuleName,
options.projectName,
`<!-- ${helpComment} -->`
);
} else {
initGradleParentModule(
tree,
options.moduleRoot,
options.groupId,
options.parentModuleName,
options.projectName,
opts.offsetFromRoot,
options.buildSystem === 'GRADLE_KTS',
`// ${helpComment}`
);
}
} else if (options.addToExistingParentModule) {
// add to the chosen root module
if (isMavenProject) {
addMavenModule(tree, options.moduleRoot, options.projectName);
} else {
addGradleModule(
tree,
options.moduleRoot,
options.projectName,
opts.offsetFromRoot,
options.buildSystem === 'GRADLE_KTS'
);
}
}
}
}
2 changes: 1 addition & 1 deletion packages/nx-ktor/src/generators/project/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
"keepProjectLevelWrapper": {
"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.",
"type": "boolean",
"default": false
"default": true
},
"ktorInitializrUrl": {
"type": "string",
Expand Down
2 changes: 1 addition & 1 deletion packages/nx-micronaut/src/generators/preset/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
"keepProjectLevelWrapper": {
"description": "Keep the `Maven` or `Gradle` wrapper files from child project (when generating a multi-module project). Follow this guide https://t.ly/ov0Y9 for more information.",
"type": "boolean",
"default": false
"default": true
},
"micronautLaunchUrl": {
"type": "string",
Expand Down
Loading

0 comments on commit 2869654

Please sign in to comment.