-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(gradle): add gradle init generator #22245
Merged
FrozenPandaz
merged 1 commit into
nrwl:master
from
xiongemi:feature/nxc-352-nx-init-works-in-a-gradle-generated-workspace
Mar 19, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "Nx Gradle", | ||
"version": "0.1", | ||
"generators": { | ||
"init": { | ||
"factory": "./src/generators/init/init#initGenerator", | ||
"schema": "./src/generators/init/schema.json", | ||
"description": "Initializes a Gradle project in the current workspace" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './plugin'; | ||
export { initGenerator } from './src/generators/init/init'; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,14 @@ | |
"url": "https://github.com/nrwl/nx/issues" | ||
}, | ||
"homepage": "https://nx.dev", | ||
"generators": "./generators.json", | ||
"exports": { | ||
".": "./index.js", | ||
"./package.json": "./package.json", | ||
"./migrations.json": "./migrations.json", | ||
"./generators.json": "./generators.json", | ||
"./plugin": "./plugin.js" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this. |
||
}, | ||
"nx-migrate": { | ||
"migrations": "./migrations.json" | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { readNxJson, Tree, updateNxJson } from '@nx/devkit'; | ||
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; | ||
|
||
import { initGenerator } from './init'; | ||
|
||
describe('@nx/gradle:init', () => { | ||
let tree: Tree; | ||
|
||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
tree.write('settings.gradle', ''); | ||
}); | ||
|
||
it('should add the plugin', async () => { | ||
await initGenerator(tree, { | ||
skipFormat: true, | ||
skipPackageJson: false, | ||
}); | ||
const nxJson = readNxJson(tree); | ||
expect(nxJson.plugins).toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"options": { | ||
"buildTargetName": "build", | ||
"classesTargetName": "classes", | ||
"testTargetName": "test", | ||
}, | ||
"plugin": "@nx/gradle/plugin", | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
it('should not overwrite existing plugins', async () => { | ||
updateNxJson(tree, { | ||
plugins: ['foo'], | ||
}); | ||
await initGenerator(tree, { | ||
skipFormat: true, | ||
skipPackageJson: false, | ||
}); | ||
const nxJson = readNxJson(tree); | ||
expect(nxJson.plugins).toMatchInlineSnapshot(` | ||
[ | ||
"foo", | ||
{ | ||
"options": { | ||
"buildTargetName": "build", | ||
"classesTargetName": "classes", | ||
"testTargetName": "test", | ||
}, | ||
"plugin": "@nx/gradle/plugin", | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
it('should not add plugin if already in array', async () => { | ||
updateNxJson(tree, { | ||
plugins: ['@nx/gradle/plugin'], | ||
}); | ||
await initGenerator(tree, { | ||
skipFormat: true, | ||
skipPackageJson: false, | ||
}); | ||
const nxJson = readNxJson(tree); | ||
expect(nxJson.plugins).toMatchInlineSnapshot(` | ||
[ | ||
"@nx/gradle/plugin", | ||
] | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { | ||
addDependenciesToPackageJson, | ||
formatFiles, | ||
GeneratorCallback, | ||
logger, | ||
readNxJson, | ||
runTasksInSerial, | ||
Tree, | ||
updateNxJson, | ||
} from '@nx/devkit'; | ||
import { updatePackageScripts } from '@nx/devkit/src/utils/update-package-scripts'; | ||
import { createNodes } from '../../plugin/nodes'; | ||
import { nxVersion } from '../../utils/versions'; | ||
import { InitGeneratorSchema } from './schema'; | ||
import { hasGradlePlugin } from '../../utils/has-gradle-plugin'; | ||
|
||
export async function initGenerator(tree: Tree, options: InitGeneratorSchema) { | ||
const tasks: GeneratorCallback[] = []; | ||
|
||
if (!options.skipPackageJson && tree.exists('package.json')) { | ||
tasks.push( | ||
addDependenciesToPackageJson( | ||
tree, | ||
{}, | ||
{ | ||
'@nx/gradle': nxVersion, | ||
}, | ||
undefined, | ||
options.keepExistingVersions | ||
) | ||
); | ||
} | ||
|
||
addPlugin(tree); | ||
addProjectReportToBuildGradle(tree); | ||
|
||
if (options.updatePackageScripts && tree.exists('package.json')) { | ||
await updatePackageScripts(tree, createNodes); | ||
} | ||
|
||
if (!options.skipFormat) { | ||
await formatFiles(tree); | ||
} | ||
|
||
return runTasksInSerial(...tasks); | ||
} | ||
|
||
function addPlugin(tree: Tree) { | ||
const nxJson = readNxJson(tree); | ||
|
||
if (!hasGradlePlugin(tree)) { | ||
nxJson.plugins ??= []; | ||
nxJson.plugins.push({ | ||
plugin: '@nx/gradle/plugin', | ||
options: { | ||
testTargetName: 'test', | ||
classesTargetName: 'classes', | ||
buildTargetName: 'build', | ||
}, | ||
}); | ||
updateNxJson(tree, nxJson); | ||
} | ||
} | ||
|
||
/** | ||
* This function adds the project-report plugin to the build.gradle or build.gradle.kts file | ||
*/ | ||
function addProjectReportToBuildGradle(tree: Tree) { | ||
let buildGradleFile: string; | ||
if (tree.exists('settings.gradle.kts')) { | ||
buildGradleFile = 'build.gradle.kts'; | ||
} else if (tree.exists('settings.gradle')) { | ||
buildGradleFile = 'build.gradle'; | ||
} else { | ||
throw new Error( | ||
'Could not find settings.gradle or settings.gradle.kts file in your gradle workspace.' | ||
); | ||
} | ||
let buildGradleContent = ''; | ||
if (tree.exists(buildGradleFile)) { | ||
buildGradleContent = tree.read(buildGradleFile).toString(); | ||
} | ||
if (buildGradleContent.includes('allprojects')) { | ||
if (!buildGradleContent.includes('"project-report')) { | ||
logger.warn(`Please add the project-report plugin to your ${buildGradleFile}: | ||
allprojects { | ||
apply { | ||
plugin("project-report") | ||
} | ||
}`); | ||
} | ||
} else { | ||
buildGradleContent += `\n\rallprojects { | ||
apply { | ||
plugin("project-report") | ||
} | ||
}`; | ||
tree.write(buildGradleFile, buildGradleContent); | ||
} | ||
} | ||
|
||
export default initGenerator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface InitGeneratorSchema { | ||
skipFormat?: boolean; | ||
skipPackageJson?: boolean; | ||
keepExistingVersions?: boolean; | ||
updatePackageScripts?: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"$schema": "https://json-schema.org/schema", | ||
"$id": "NxGradleInitSchema", | ||
"title": "Gradle Init Generator", | ||
"description": "Initializes a Gradle project in the current workspace.", | ||
"type": "object", | ||
"properties": { | ||
"skipFormat": { | ||
"description": "Skip formatting files.", | ||
"type": "boolean", | ||
"default": false, | ||
"x-priority": "internal" | ||
}, | ||
"skipPackageJson": { | ||
"type": "boolean", | ||
"default": false, | ||
"description": "Do not add dependencies to `package.json`.", | ||
"x-priority": "internal" | ||
}, | ||
"keepExistingVersions": { | ||
"type": "boolean", | ||
"x-priority": "internal", | ||
"description": "Keep existing dependencies versions", | ||
"default": false | ||
}, | ||
"updatePackageScripts": { | ||
"type": "boolean", | ||
"x-priority": "internal", | ||
"description": "Update `package.json` scripts with inferred targets", | ||
"default": false | ||
} | ||
}, | ||
"required": [] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either don't export this for now. Or, expose it under
/generators