Skip to content

fix custom generators #3781

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

Merged
merged 2 commits into from
Jul 2, 2024
Merged
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
18 changes: 14 additions & 4 deletions reference/extensions/custom_generators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ One way of having your own custom generators in Conan is by using them as
`python_requires` package:

.. code-block:: python
:caption: mygenerator/conanfile.py

from conan import ConanFile
from conan.tools.files import save
Expand All @@ -28,7 +29,7 @@ One way of having your own custom generators in Conan is by using them as
def generate(self):
deps_info = ""
for dep, _ in self._conanfile.dependencies.items():
deps_info = f"{dep.ref.name}, {dep.ref.version}"
deps_info += f"{dep.ref.name}, {dep.ref.version}\n"
save(self._conanfile, "deps.txt", deps_info)


Expand All @@ -38,9 +39,10 @@ One way of having your own custom generators in Conan is by using them as
package_type = "python-require"


And then use it in the generate method of your own packages like this:
And then ``conan create mygenerator`` and use it in the generate method of your own packages like this:

.. code-block:: python
:caption: pkg/conanfile.py

from conan import ConanFile

Expand All @@ -50,12 +52,20 @@ And then use it in the generate method of your own packages like this:
version = "1.0"

python_requires = "mygenerator/1.0"
requires = "zlib/1.2.11"
requires = "zlib/1.2.11", "bzip2/1.0.8"

def generate(self):
mygenerator = self.python_requires["mygenerator"].module.MyGenerator()
mygenerator = self.python_requires["mygenerator"].module.MyGenerator(self)
mygenerator.generate()

Then, doing a ``conan install pkg`` on this ``pkg`` recipe, will create a ``deps.txt`` text file containing:

.. code-block:: text

zlib, 1.2.11
bzip2, 1.0.8


This has the advantage that you can version your own custom generators as packages and
also that you can share those generators as Conan packages.

Expand Down