Skip to content

Commit

Permalink
refactor: extract default Python project targets into a reusable func…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
lucasvieirasilva committed Jan 29, 2025
1 parent f3fc9da commit a9b6c97
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 120 deletions.
62 changes: 2 additions & 60 deletions packages/nx-python/src/generators/poetry-project/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import {
addFiles,
normalizeOptions as baseNormalizeOptions,
getDefaultPythonProjectTargets,
getPyprojectTomlByProjectName,
} from '../utils';
import { DEV_DEPENDENCIES_VERSION_MAP } from '../consts';
Expand Down Expand Up @@ -218,36 +219,14 @@ export default async function (
const normalizedOptions = normalizeOptions(tree, options);

const targets: ProjectConfiguration['targets'] = {
...getDefaultPythonProjectTargets(normalizedOptions),
lock: {
executor: '@nxlv/python:run-commands',
options: {
command: 'poetry lock --no-update',
cwd: normalizedOptions.projectRoot,
},
},
add: {
executor: '@nxlv/python:add',
options: {},
},
update: {
executor: '@nxlv/python:update',
options: {},
},
remove: {
executor: '@nxlv/python:remove',
options: {},
},
build: {
executor: '@nxlv/python:build',
outputs: ['{projectRoot}/dist'],
options: {
outputPath: `${normalizedOptions.projectRoot}/dist`,
publish: normalizedOptions.publishable,
lockedVersions: normalizedOptions.buildLockedVersions,
bundleLocalDependencies: normalizedOptions.buildBundleLocalDependencies,
},
cache: true,
},
install: {
executor: '@nxlv/python:install',
options: {
Expand All @@ -260,43 +239,6 @@ export default async function (
},
};

if (options.linter === 'flake8') {
targets.lint = {
executor: '@nxlv/python:flake8',
outputs: [
`{workspaceRoot}/reports/${normalizedOptions.projectRoot}/pylint.txt`,
],
options: {
outputFile: `reports/${normalizedOptions.projectRoot}/pylint.txt`,
},
cache: true,
};
}

if (options.linter === 'ruff') {
targets.lint = {
executor: '@nxlv/python:ruff-check',
outputs: [],
options: {
lintFilePatterns: [normalizedOptions.moduleName].concat(
options.unitTestRunner === 'pytest' ? ['tests'] : [],
),
},
cache: true,
};

targets.format = {
executor: '@nxlv/python:ruff-format',
outputs: [],
options: {
filePatterns: [normalizedOptions.moduleName].concat(
options.unitTestRunner === 'pytest' ? ['tests'] : [],
),
},
cache: true,
};
}

if (options.unitTestRunner === 'pytest') {
targets.test = {
executor: '@nxlv/python:run-commands',
Expand Down
68 changes: 68 additions & 0 deletions packages/nx-python/src/generators/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getWorkspaceLayout,
names,
offsetFromRoot,
ProjectConfiguration,
readProjectConfiguration,
Tree,
} from '@nx/devkit';
Expand Down Expand Up @@ -203,3 +204,70 @@ export function addFiles(
);
}
}

export function getDefaultPythonProjectTargets(
options: BaseNormalizedSchema,
): ProjectConfiguration['targets'] {
const targets: ProjectConfiguration['targets'] = {
add: {
executor: '@nxlv/python:add',
options: {},
},
update: {
executor: '@nxlv/python:update',
options: {},
},
remove: {
executor: '@nxlv/python:remove',
options: {},
},
build: {
executor: '@nxlv/python:build',
outputs: ['{projectRoot}/dist'],
options: {
outputPath: `${options.projectRoot}/dist`,
publish: options.publishable,
lockedVersions: options.buildLockedVersions,
bundleLocalDependencies: options.buildBundleLocalDependencies,
},
cache: true,
},
};

if (options.linter === 'flake8') {
targets.lint = {
executor: '@nxlv/python:flake8',
outputs: [`{workspaceRoot}/reports/${options.projectRoot}/pylint.txt`],
options: {
outputFile: `reports/${options.projectRoot}/pylint.txt`,
},
cache: true,
};
}

if (options.linter === 'ruff') {
targets.lint = {
executor: '@nxlv/python:ruff-check',
outputs: [],
options: {
lintFilePatterns: [options.moduleName].concat(
options.unitTestRunner === 'pytest' ? ['tests'] : [],
),
},
cache: true,
};

targets.format = {
executor: '@nxlv/python:ruff-format',
outputs: [],
options: {
filePatterns: [options.moduleName].concat(
options.unitTestRunner === 'pytest' ? ['tests'] : [],
),
},
cache: true,
};
}

return targets;
}
62 changes: 2 additions & 60 deletions packages/nx-python/src/generators/uv-project/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { DEV_DEPENDENCIES_VERSION_MAP } from '../consts';
import {
addFiles,
normalizeOptions as baseNormalizeOptions,
getDefaultPythonProjectTargets,
getPyprojectTomlByProjectName,
} from '../utils';
import {
Expand Down Expand Up @@ -235,36 +236,14 @@ export default async function (
const normalizedOptions = normalizeOptions(tree, options);

const targets: ProjectConfiguration['targets'] = {
...getDefaultPythonProjectTargets(normalizedOptions),
lock: {
executor: '@nxlv/python:run-commands',
options: {
command: 'uv lock',
cwd: normalizedOptions.projectRoot,
},
},
add: {
executor: '@nxlv/python:add',
options: {},
},
update: {
executor: '@nxlv/python:update',
options: {},
},
remove: {
executor: '@nxlv/python:remove',
options: {},
},
build: {
executor: '@nxlv/python:build',
outputs: ['{projectRoot}/dist'],
options: {
outputPath: `${normalizedOptions.projectRoot}/dist`,
publish: normalizedOptions.publishable,
lockedVersions: normalizedOptions.buildLockedVersions,
bundleLocalDependencies: normalizedOptions.buildBundleLocalDependencies,
},
cache: true,
},
install: {
executor: '@nxlv/python:install',
options: {
Expand All @@ -276,43 +255,6 @@ export default async function (
},
};

if (options.linter === 'flake8') {
targets.lint = {
executor: '@nxlv/python:flake8',
outputs: [
`{workspaceRoot}/reports/${normalizedOptions.projectRoot}/pylint.txt`,
],
options: {
outputFile: `reports/${normalizedOptions.projectRoot}/pylint.txt`,
},
cache: true,
};
}

if (options.linter === 'ruff') {
targets.lint = {
executor: '@nxlv/python:ruff-check',
outputs: [],
options: {
lintFilePatterns: [normalizedOptions.moduleName].concat(
options.unitTestRunner === 'pytest' ? ['tests'] : [],
),
},
cache: true,
};

targets.format = {
executor: '@nxlv/python:ruff-format',
outputs: [],
options: {
filePatterns: [normalizedOptions.moduleName].concat(
options.unitTestRunner === 'pytest' ? ['tests'] : [],
),
},
cache: true,
};
}

if (options.unitTestRunner === 'pytest') {
targets.test = {
executor: '@nxlv/python:run-commands',
Expand Down

0 comments on commit a9b6c97

Please sign in to comment.