Skip to content
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(nx-python): add support for nx releases #246

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
}
]
}
],
"@typescript-eslint/no-unused-vars": [
"error",
{
"ignoreRestSiblings": true,
"varsIgnorePattern": "^_"
}
]
}
},
Expand Down
23 changes: 13 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
"@commitlint/config-conventional": "^19.2.2",
"@commitlint/cz-commitlint": "^19.4.0",
"@commitlint/types": "^19.0.3",
"@nx/devkit": "19.5.7",
"@nx/eslint": "19.5.7",
"@nx/eslint-plugin": "19.5.7",
"@nx/js": "19.5.7",
"@nx/plugin": "19.5.7",
"@nx/vite": "19.5.7",
"@nx/web": "19.5.7",
"@nx/workspace": "19.5.7",
"@nx/devkit": "19.7.3",
"@nx/eslint": "19.7.3",
"@nx/eslint-plugin": "19.7.3",
"@nx/js": "19.7.3",
"@nx/plugin": "19.7.3",
"@nx/vite": "19.7.3",
"@nx/web": "19.7.3",
"@nx/workspace": "19.7.3",
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/exec": "^6.0.3",
"@semantic-release/git": "^10.0.1",
Expand All @@ -46,8 +46,8 @@
"jsonc-eslint-parser": "^2.4.0",
"lint-staged": "^15.2.2",
"memfs": "^4.11.1",
"nx": "19.5.7",
"nx-cloud": "19.0.0",
"nx": "19.7.3",
"nx-cloud": "19.1.0",
"prettier": "^3.2.5",
"semantic-release-npm": "^0.0.5",
"semantic-release-plus": "^20.0.0",
Expand All @@ -73,6 +73,7 @@
"@swc/helpers": "0.5.12",
"archiver": "^7.0.0",
"aws-lambda": "^1.0.7",
"axios": "^1.7.7",
"chalk": "^4.1.1",
"command-exists": "^1.2.9",
"cross-spawn": "^7.0.3",
Expand All @@ -82,7 +83,9 @@
"fs-extra": "^11.2.0",
"glob": "^10.3.10",
"lodash": "^4.17.21",
"ora": "5.3.0",
"prompts": "^2.4.2",
"semver": "^7.5.3",
"tslib": "^2.3.0",
"uuid": "^9.0.1"
},
Expand Down
10 changes: 10 additions & 0 deletions packages/nx-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -548,3 +548,13 @@ autoActivate = true
The options and behavior are the same as the `nx:run-commands` executor.

[See the Nx documentation for more information](https://nx.dev/packages/nx/executors/run-commands)

#### Releases

This plugin supports the [Nx releases](https://nx.dev/features/manage-releases) feature.

If you are already using the `@nxlv/python` plugin and want to enable the releases feature, please run the following command:

```bash
nx generate @nxlv/python:enable-releases
```
12 changes: 12 additions & 0 deletions packages/nx-python/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
"factory": "./src/generators/poetry-project/generator",
"schema": "./src/generators/poetry-project/schema.json",
"description": "Python Poetry Project"
},
"release-version": {
"factory": "./src/generators/release-version/release-version",
"schema": "./src/generators/release-version/schema.json",
"description": "DO NOT INVOKE DIRECTLY WITH `nx generate`. Use `nx release version` instead.",
"hidden": true
},
"enable-releases": {
"factory": "./src/generators/enable-releases/generator",
"schema": "./src/generators/enable-releases/schema.json",
"description": "Enable Releases for Python projects",
"hidden": true
}
}
}
4 changes: 3 additions & 1 deletion packages/nx-python/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"command-exists": "^1.2.9",
"lodash": "^4.17.21",
"@nx/devkit": "^19.0.0",
"nx": "^19.0.0"
"nx": "^19.0.0",
"ora": "5.3.0",
"semver": "^7.5.3"
},
"nx-migrations": {
"migrations": "./migrations.json"
Expand Down
19 changes: 18 additions & 1 deletion packages/nx-python/src/executors/utils/poetry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExecutorContext, ProjectConfiguration } from '@nx/devkit';
import { ExecutorContext, ProjectConfiguration, Tree } from '@nx/devkit';
import chalk from 'chalk';
import spawn from 'cross-spawn';
import path from 'path';
Expand Down Expand Up @@ -84,6 +84,23 @@ export function parseToml(tomlFile: string) {
return toml.parse(fs.readFileSync(tomlFile, 'utf-8')) as PyprojectToml;
}

export function readPyprojectToml(tree: Tree, tomlFile: string) {
const content = tree.read(tomlFile, 'utf-8');
if (!content) {
return null;
}

return toml.parse(content) as PyprojectToml;
}

export function writePyprojectToml(
tree: Tree,
tomlFile: string,
data: PyprojectToml,
) {
tree.write(tomlFile, toml.stringify(data));
}

export function getLocalDependencyConfig(
context: ExecutorContext,
dependencyName: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { vi, MockInstance } from 'vitest';
import '../../utils/mocks/cross-spawn.mock';
import * as poetryUtils from '../../executors/utils/poetry';
import { readJson, Tree } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import generator from './generator';
import projectGenerator from '../poetry-project/generator';
import spawn from 'cross-spawn';

describe('nx-python enable-releases', () => {
let checkPoetryExecutableMock: MockInstance;
let appTree: Tree;

beforeEach(() => {
appTree = createTreeWithEmptyWorkspace({});
checkPoetryExecutableMock = vi.spyOn(poetryUtils, 'checkPoetryExecutable');
checkPoetryExecutableMock.mockResolvedValue(undefined);
vi.mocked(spawn.sync).mockReturnValue({
status: 0,
output: [''],
pid: 0,
signal: null,
stderr: null,
stdout: null,
});
});

it('should add release version generator', async () => {
await projectGenerator(appTree, {
name: 'proj1',
projectType: 'application',
pyprojectPythonDependency: '',
pyenvPythonVersion: '',
publishable: false,
buildLockedVersions: false,
buildBundleLocalDependencies: false,
linter: 'none',
unitTestRunner: 'none',
rootPyprojectDependencyGroup: 'main',
unitTestHtmlReport: false,
unitTestJUnitReport: false,
codeCoverage: false,
codeCoverageHtmlReport: false,
codeCoverageXmlReport: false,
projectNameAndRootFormat: 'derived',
});

await generator(appTree);

expect(readJson(appTree, 'proj1/project.json').release).toEqual({
version: {
generator: '@nxlv/python:release-version',
},
});
});
});
20 changes: 20 additions & 0 deletions packages/nx-python/src/generators/enable-releases/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { getProjects, Tree, updateProjectConfiguration } from '@nx/devkit';
import path from 'path';

async function generator(host: Tree) {
for (const project of getProjects(host)) {
const [projectName, projectConfig] = project;
const pyprojectTomlPath = path.join(projectConfig.root, 'pyproject.toml');
if (host.exists(pyprojectTomlPath)) {
projectConfig.release = projectConfig.release || {
version: {
generator: '@nxlv/python:release-version',
},
};

updateProjectConfiguration(host, projectName, projectConfig);
}
}
}

export default generator;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Schema = object;
8 changes: 8 additions & 0 deletions packages/nx-python/src/generators/enable-releases/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "http://json-schema.org/schema",
"$id": "NxEnableReleases",
"title": "Enable Releases for Python projects",
"type": "object",
"properties": {},
"required": []
}
Loading
Loading