feat(nx-python): Add new project generator #123
Merged
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.
This PR adds a new project generator (
@nxlv/python:poetry-project
), more flexible and extendable.Current Behavior
The
@nxlv/python:project
is based on personal assumptions and is not flexible enough for different workspace needs.Expected Behavior
This PR deprecates the
@nxlv/python:project
generator and adds a new one@nxlv/python:poetry-project
that contains much more options to customize the project.Add a new Python Project
Options
--directory
string
false
--tags
string
false
--projectType
string
application
orlibrary
true
application
--packageName
string
false
name
property (provided in the CLI)--moduleName
string
false
name
property using_
instead of-
--description
string
false
--pyprojectPythonDependency
string
pyproject.toml
false
>=3.9,<3.11
(Poetry syntax)--pyenvPythonVersion
string
.python-version
pyenv file contentfalse
3.9.5
--publishable
boolean
false
true
--buildLockedVersions
boolean
false
true
--buildBundleLocalDependencies
boolean
false
true
--linter
string
flake8
ornone
)false
flake8
--unitTestRunner
string
pytest
ornone
)false
pytest
--unitTestHtmlReport
boolean
false
true
--unitTestJUnitReport
boolean
false
true
--codeCoverage
boolean
false
true
--codeCoverageHtmlReport
boolean
false
true
--codeCoverageXmlReport
boolean
false
true
--codeCoverageThreshold
number
false
rootPyprojectDependencyGroup
When the workspace is configured to use a shared virtual environment (see below), the
rootPyprojectDependencyGroup
option specifies the dependency group to be used in the rootpyproject.toml
file, by default, the main dependency group is used.Shared Virtual Environment
By default, the
@nxlv/python
manages the projects individually, so, all the projects have their one set of dependencies and virtual environments.However, In some cases, we want to use a shared virtual environment for the entire workspace to save some installation time in your local environment and CI tool, we use this mode when the workspace contains many projects with the same dependencies and versions that don't conflict in the workspace level.
To migrate to this mode, run the following command:
Options:
--moveDevDependencies
boolean
pyproject.toml
true
true
After the migration is completed, the workspace does not have the
pyproject.toml
in the root directory, and all the local projects are referencing the rootpyproject.toml
file.Benefits:
Cons:
pyproject.toml
file to resolve the packages, so, it will use the first module found in thepyproject.toml
file.devDependenciesProject
This approach consists of moving all the dev dependencies from the projects to separate projects, this project is referenced in the root
pyproject.toml
and all the local projects as a dev dependency.Benefits:
templateDir
The
templateDir
option specifies a custom directory to be used as a template for the project, by default, the@nxlv/python
has a built-in template folder that is used to generate the project.However, there are some cases where the developer wants to use different files or customize the way the project is generated without the need to create a custom generator based on the
@nxlv/python
generator.The files in the
templateDir
needs to follow the Nx generator convention by using EJS to customize the files based on the options provided by the@nxlv/python
.Template variables
All the options listed above are available as variables in the template files, for example, to use the
packageName
option in the template file, use<%= packageName %>
.Additional variables are available in the template files:
offsetFromRoot
: This variable contains the relative path from the project to the root directory. (e.g. for the project folderapps/my-project
theoffsetFromRoot
value will be../../
)projectRoot
: This variable contains the relative path of the project.individualPackage
: This variable is a boolean that indicates if the workspace is using a shared virtual environment or not.dot
: This variable is a string that contains a dot (.
), it is used to create files that start with a dot (e.g.__dot__gitignore.template
).template
: This variable is an empty string, this variable is usually combined with thedot
variable, because the Nx generator file function only generates files with extensions, however, there are some cases where the file doesn't have an extension (e.g..gitignore
), so, thetemplate
variable is used to create files without an extension (e.g.__dot__gitignore.template
).Global Default Options
By default,
@nxlv/python:poetry-project
generator defines linter and unit test runner with all reports enabled by default, however, those default options can be globally changed by using the following configuration in thenx.json
config file.Related Issue(s)
Fixes #111, #117