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

small example how to use template file in custom generators #1043

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 29 additions & 7 deletions howtos/custom_generators.rst
@@ -1,6 +1,5 @@
.. _dyn_generators:


How to create and share a custom generator with generator packages
==================================================================

Expand All @@ -18,11 +17,9 @@ doesn't satisfy your needs. There are several options:
to your own server and share with your team, or share with the world uploading it to bintray.
You can manage it as a package, you can version it, overwrite it, delete it, create channels (testing/stable...),
and the most important: bring it to your projects as a regular dependency.



This **how to** will show you how to do the latest one. We will build a generator for **premake** (https://premake.github.io/)
build system:

build system:

Creating a custom generator
---------------------------
Expand Down Expand Up @@ -82,7 +79,6 @@ You have access to the ``conanfile`` instance at ``self.conanfile`` and get info
| self.conanfile.env | dict with the applied env vars declared in the requirements |
+-----------------------------------------+------------------------------------------------------------------------------------------------+


Premake generator example
-------------------------

Expand Down Expand Up @@ -244,7 +240,6 @@ reference in conanfile.txt.

Let's install the requirements and build the project:


.. code-block:: bash

$ conan install . -s compiler=gcc -s compiler.version=4.9 -s compiler.libcxx=libstdc++ --build
Expand All @@ -263,3 +258,30 @@ Now, everything works, so you might want to share your generator:

This is a regular conan package. You could for example embed this example in a *test_package* folder, create a *conanfile.py* that
invokes premake4 in the build() method, and use :command:`conan test` to automatically test your custom generator with a real project.

Using template files for custom generators
------------------------------------------

If your generator has a lot of common, non-parameterized text, you might want to use files that contain the template.
It is possible to do this as long as the template file is exported in the recipe. The following example uses a simple text file,
but you could use other templating formats:

.. code-block:: python

import os
from conans import ConanFile, load
from conans.model import Generator

class MyCustomGenerator(Generator):
@property
def filename(self):
return "customfile.gen"
@property
def content(self):
template = load(os.path.join(os.path.dirname(__file__), "mytemplate.txt"))
return template % "Hello"

class MyCustomGeneratorPackage(ConanFile):
name = "custom"
version = "0.1"
exports = "mytemplate.txt"