@nx/gradle 19.0.0-canary.20240430-458f2cc → 19.0.0-canary.20240503-dbad02a

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/generators.json CHANGED
@@ -6,6 +6,11 @@
6
6
  "factory": "./src/generators/init/init#initGenerator",
7
7
  "schema": "./src/generators/init/schema.json",
8
8
  "description": "Initializes a Gradle project in the current workspace"
9
+ },
10
+ "ci-workflow": {
11
+ "factory": "./src/generators/ci-workflow/generator",
12
+ "schema": "./src/generators/ci-workflow/schema.json",
13
+ "description": "Setup a CI Workflow to run Nx in CI"
9
14
  }
10
15
  }
11
16
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nx/gradle",
3
- "version": "19.0.0-canary.20240430-458f2cc",
3
+ "version": "19.0.0-canary.20240503-dbad02a",
4
4
  "private": false,
5
5
  "description": "The Nx Plugin for Gradle allows Gradle tasks to be run through Nx",
6
6
  "repository": {
@@ -33,7 +33,7 @@
33
33
  "migrations": "./migrations.json"
34
34
  },
35
35
  "dependencies": {
36
- "@nx/devkit": "19.0.0-canary.20240430-458f2cc"
36
+ "@nx/devkit": "19.0.0-canary.20240503-dbad02a"
37
37
  },
38
38
  "publishConfig": {
39
39
  "access": "public"
@@ -0,0 +1,32 @@
1
+ version: 2.1
2
+
3
+ orbs:
4
+ nx: nrwl/nx@1.6.2
5
+
6
+ jobs:
7
+ main:
8
+ environment:
9
+ # Configure the JVM and Gradle to avoid OOM errors
10
+ _JAVA_OPTIONS: "-Xmx3g"
11
+ GRADLE_OPTS: "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=2"
12
+ docker:
13
+ - image: cimg/openjdk:17.0-node
14
+ steps:
15
+ - checkout
16
+
17
+ # Connect your workspace on <%= nxCloudHost %> and uncomment this to enable task distribution.
18
+ # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested
19
+ # - run: <%= packageManagerPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-jvm" --stop-agents-after="build"
20
+
21
+ - nx/set-shas:
22
+ main-branch-name: '<%= mainBranch %>'
23
+
24
+ <% for (const command of commands) { %>
25
+ - run: <%= command %><% } %>
26
+
27
+ workflows:
28
+ version: 2
29
+
30
+ <%= workflowFileName %>:
31
+ jobs:
32
+ - main
@@ -0,0 +1,38 @@
1
+ name: <%= workflowName %>
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - <%= mainBranch %>
7
+ pull_request:
8
+
9
+ permissions:
10
+ actions: read
11
+ contents: read
12
+
13
+ jobs:
14
+ main:
15
+ runs-on: ubuntu-latest
16
+ steps:
17
+ - uses: actions/checkout@v4
18
+ with:
19
+ fetch-depth: 0
20
+
21
+ # Connect your workspace on <%= nxCloudHost %> and uncomment this to enable task distribution.
22
+ # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested
23
+ # - run: <%= packageManagerPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-jvm" --stop-agents-after="build"
24
+
25
+ - name: Set up JDK 17 for x64
26
+ uses: actions/setup-java@v4
27
+ with:
28
+ java-version: '17'
29
+ distribution: 'temurin'
30
+ architecture: x64
31
+
32
+ - name: Setup Gradle
33
+ uses: gradle/gradle-build-action@v2
34
+
35
+ - uses: nrwl/nx-set-shas@v4
36
+
37
+ <% for (const command of commands) { %>
38
+ - run: <%= command %><% } %>
@@ -0,0 +1,9 @@
1
+ import { Tree } from '@nx/devkit';
2
+ export interface Schema {
3
+ name: string;
4
+ ci: 'github' | 'circleci';
5
+ packageManager?: null;
6
+ commands?: string[];
7
+ }
8
+ export declare function ciWorkflowGenerator(tree: Tree, schema: Schema): Promise<void>;
9
+ export default ciWorkflowGenerator;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ciWorkflowGenerator = void 0;
4
+ const devkit_1 = require("@nx/devkit");
5
+ const path_1 = require("path");
6
+ const nx_cloud_utils_1 = require("nx/src/utils/nx-cloud-utils");
7
+ const default_base_1 = require("nx/src/utils/default-base");
8
+ function getCiCommands(ci, mainBranch) {
9
+ switch (ci) {
10
+ case 'circleci': {
11
+ return [`./nx affected --base=$NX_BASE --head=$NX_HEAD -t test build`];
12
+ }
13
+ default: {
14
+ return [`./nx affected -t test build`];
15
+ }
16
+ }
17
+ }
18
+ async function ciWorkflowGenerator(tree, schema) {
19
+ const ci = schema.ci;
20
+ const nxJson = (0, devkit_1.readNxJson)(tree);
21
+ const nxCloudUsed = (0, nx_cloud_utils_1.isNxCloudUsed)(nxJson);
22
+ if (!nxCloudUsed) {
23
+ throw new Error('This workspace is not connected to Nx Cloud.');
24
+ }
25
+ const options = getTemplateData(schema, nxJson);
26
+ (0, devkit_1.generateFiles)(tree, (0, path_1.join)(__dirname, 'files', ci), '', options);
27
+ await (0, devkit_1.formatFiles)(tree);
28
+ }
29
+ exports.ciWorkflowGenerator = ciWorkflowGenerator;
30
+ function getTemplateData(options, nxJson) {
31
+ const { name: workflowName, fileName: workflowFileName } = (0, devkit_1.names)(options.name);
32
+ const packageManager = (0, devkit_1.detectPackageManager)();
33
+ const { exec: packageManagerPrefix } = (0, devkit_1.getPackageManagerCommand)(packageManager);
34
+ const nxCloudUrl = (0, nx_cloud_utils_1.getNxCloudUrl)(nxJson);
35
+ const nxCloudHost = new URL(nxCloudUrl).host;
36
+ const mainBranch = (0, default_base_1.deduceDefaultBase)();
37
+ const commands = options.commands ?? getCiCommands(options.ci, mainBranch);
38
+ return {
39
+ workflowName,
40
+ workflowFileName,
41
+ packageManager,
42
+ packageManagerPrefix,
43
+ commands,
44
+ mainBranch,
45
+ nxCloudHost,
46
+ };
47
+ }
48
+ exports.default = ciWorkflowGenerator;
@@ -0,0 +1,40 @@
1
+ {
2
+ "$schema": "https://json-schema.org/schema",
3
+ "$id": "NxGradleCiWorkflowSchema",
4
+ "title": "Gradle CI Workflow Generator",
5
+ "description": "Setup a CI Workflow to run Nx in CI.",
6
+ "type": "object",
7
+ "properties": {
8
+ "ci": {
9
+ "type": "string",
10
+ "description": "CI provider.",
11
+ "enum": ["github", "circleci"],
12
+ "x-prompt": {
13
+ "message": "What is your target CI provider?",
14
+ "type": "list",
15
+ "items": [
16
+ {
17
+ "value": "github",
18
+ "label": "GitHub Actions"
19
+ },
20
+ {
21
+ "value": "circleci",
22
+ "label": "Circle CI"
23
+ }
24
+ ]
25
+ }
26
+ },
27
+ "name": {
28
+ "type": "string",
29
+ "description": "Workflow name.",
30
+ "$default": {
31
+ "$source": "argv",
32
+ "index": 0
33
+ },
34
+ "default": "CI",
35
+ "x-prompt": "How should we name your workflow?",
36
+ "pattern": "^[a-zA-Z].*$"
37
+ }
38
+ },
39
+ "required": ["ci", "name"]
40
+ }
@@ -12,6 +12,7 @@ async function initGenerator(tree, options) {
12
12
  }, undefined, options.keepExistingVersions));
13
13
  }
14
14
  addPlugin(tree);
15
+ updateNxJsonConfiguration(tree);
15
16
  addProjectReportToBuildGradle(tree);
16
17
  if (!options.skipFormat) {
17
18
  await (0, devkit_1.formatFiles)(tree);
@@ -71,4 +72,15 @@ allprojects {
71
72
  tree.write(buildGradleFile, buildGradleContent);
72
73
  }
73
74
  }
75
+ function updateNxJsonConfiguration(tree) {
76
+ const nxJson = (0, devkit_1.readNxJson)(tree);
77
+ if (!nxJson.namedInputs) {
78
+ nxJson.namedInputs = {};
79
+ }
80
+ const defaultFilesSet = nxJson.namedInputs.default ?? [];
81
+ nxJson.namedInputs.default = Array.from(new Set([...defaultFilesSet, '{projectRoot}/**/*']));
82
+ const productionFileSet = nxJson.namedInputs.production ?? [];
83
+ nxJson.namedInputs.production = Array.from(new Set([...productionFileSet, 'default', '!{projectRoot}/test/**/*']));
84
+ (0, devkit_1.updateNxJson)(tree, nxJson);
85
+ }
74
86
  exports.default = initGenerator;
@@ -53,7 +53,7 @@ exports.createNodes = [
53
53
  });
54
54
  }
55
55
  const outputDirs = gradleFileToOutputDirsMap.get(gradleFilePath);
56
- const { targets, targetGroups } = createGradleTargets(tasks, projectRoot, options, context, outputDirs);
56
+ const { targets, targetGroups } = createGradleTargets(tasks, options, context, outputDirs, gradleProject);
57
57
  const project = {
58
58
  name: projectName,
59
59
  targets,
@@ -75,19 +75,15 @@ exports.createNodes = [
75
75
  }
76
76
  },
77
77
  ];
78
- function createGradleTargets(tasks, projectRoot, options, context, outputDirs) {
78
+ function createGradleTargets(tasks, options, context, outputDirs, gradleProject) {
79
79
  const inputsMap = createInputsMap(context);
80
80
  const targets = {};
81
81
  const targetGroups = {};
82
82
  for (const task of tasks) {
83
83
  const targetName = options?.[`${task.name}TargetName`] ?? task.name;
84
84
  const outputs = outputDirs.get(task.name);
85
- const path = (0, node_path_1.relative)((0, node_path_1.join)(context.workspaceRoot, projectRoot), (0, exec_gradle_1.getGradleBinaryPath)());
86
85
  targets[targetName] = {
87
- command: `${path} ${task.name}`,
88
- options: {
89
- cwd: projectRoot,
90
- },
86
+ command: `${(0, exec_gradle_1.getGradleExecFile)()} ${gradleProject ? gradleProject + ':' : ''}${task.name}`,
91
87
  cache: cacheableTaskType.has(task.type),
92
88
  inputs: inputsMap[task.name],
93
89
  outputs: outputs ? [outputs] : undefined,
@@ -110,6 +106,8 @@ function createInputsMap(context) {
110
106
  ? ['production', '^production']
111
107
  : ['default', '^default'],
112
108
  test: ['default', namedInputs?.production ? '^production' : '^default'],
113
- classes: ['default', '^default'],
109
+ classes: namedInputs?.production
110
+ ? ['production', '^production']
111
+ : ['default', '^default'],
114
112
  };
115
113
  }
@@ -4,4 +4,5 @@ import { ExecFileOptions } from 'child_process';
4
4
  import { ExecFileSyncOptionsWithBufferEncoding } from 'node:child_process';
5
5
  export declare function execGradle(args: string[], execOptions?: ExecFileSyncOptionsWithBufferEncoding): Buffer;
6
6
  export declare function getGradleBinaryPath(): string;
7
+ export declare function getGradleExecFile(): string;
7
8
  export declare function execGradleAsync(args: ReadonlyArray<string>, execOptions?: ExecFileOptions): Promise<Buffer>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.execGradleAsync = exports.getGradleBinaryPath = exports.execGradle = void 0;
3
+ exports.execGradleAsync = exports.getGradleExecFile = exports.getGradleBinaryPath = exports.execGradle = void 0;
4
4
  const devkit_1 = require("@nx/devkit");
5
5
  const node_child_process_1 = require("node:child_process");
6
6
  const node_fs_1 = require("node:fs");
@@ -21,6 +21,10 @@ function getGradleBinaryPath() {
21
21
  return gradleBinaryPath;
22
22
  }
23
23
  exports.getGradleBinaryPath = getGradleBinaryPath;
24
+ function getGradleExecFile() {
25
+ return process.platform.startsWith('win') ? '.\\gradlew.bat' : './gradlew';
26
+ }
27
+ exports.getGradleExecFile = getGradleExecFile;
24
28
  function execGradleAsync(args, execOptions) {
25
29
  const gradleBinaryPath = getGradleBinaryPath();
26
30
  return new Promise((res, rej) => {