-
Notifications
You must be signed in to change notification settings - Fork 1k
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
custom global generators in cache extensions/generators folder #13718
Merged
memsharded
merged 2 commits into
conan-io:release/2.0
from
memsharded:feature/global_generators
Apr 20, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
conans/test/integration/generators/test_custom_global_generators.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import os | ||
import textwrap | ||
|
||
from conans.test.assets.genconanfile import GenConanfile | ||
from conans.test.utils.tools import TestClient | ||
from conans.util.files import save | ||
|
||
|
||
def test_custom_global_generator(): | ||
c = TestClient() | ||
generator = textwrap.dedent(""" | ||
class MyCustomGenerator: | ||
def __init__(self, conanfile): | ||
self._conanfile = conanfile | ||
def generate(self): | ||
pkg = self._conanfile.dependencies["pkg"].ref | ||
self._conanfile.output.info(f"DEP: {pkg}!!") | ||
""") | ||
save(os.path.join(c.cache.custom_generators_path, "mygen.py"), generator) | ||
conanfile = textwrap.dedent(""" | ||
[requires] | ||
pkg/0.1 | ||
|
||
[generators] | ||
MyCustomGenerator | ||
""") | ||
c.save({"pkg/conanfile.py": GenConanfile("pkg", "0.1"), | ||
"conanfile.txt": conanfile}) | ||
c.run("create pkg") | ||
c.run("install .") | ||
assert "conanfile.txt: Generator 'MyCustomGenerator' calling 'generate()'" in c.out | ||
assert "conanfile.txt: DEP: pkg/0.1!!" in c.out | ||
|
||
# By CLI also works | ||
conanfile = textwrap.dedent(""" | ||
[requires] | ||
pkg/0.1 | ||
""") | ||
c.save({"conanfile.txt": conanfile}) | ||
c.run("install . -g MyCustomGenerator") | ||
memsharded marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert "conanfile.txt: Generator 'MyCustomGenerator' calling 'generate()'" in c.out | ||
assert "conanfile.txt: DEP: pkg/0.1!!" in c.out | ||
|
||
# In conanfile.py also works | ||
conanfile = textwrap.dedent(""" | ||
from conan import ConanFile | ||
class MyPkg(ConanFile): | ||
requires = "pkg/0.1" | ||
generators = "MyCustomGenerator" | ||
""") | ||
c.save({"conanfile.py": conanfile}, clean_first=True) | ||
c.run("install . ") | ||
assert "conanfile.py: Generator 'MyCustomGenerator' calling 'generate()'" in c.out | ||
assert "conanfile.py: DEP: pkg/0.1!!" in c.out | ||
|
||
|
||
def test_custom_global_generator_imports(): | ||
""" | ||
our custom generator can use python imports | ||
""" | ||
c = TestClient() | ||
generator = textwrap.dedent(""" | ||
from _myfunc import mygenerate | ||
|
||
class MyCustomGenerator: | ||
def __init__(self, conanfile): | ||
self._conanfile = conanfile | ||
def generate(self): | ||
mygenerate(self._conanfile) | ||
""") | ||
myaux = textwrap.dedent(""" | ||
def mygenerate(conanfile): | ||
conanfile.output.info("MYGENERATE WORKS!!") | ||
""") | ||
save(os.path.join(c.cache.custom_generators_path, "mygen.py"), generator) | ||
save(os.path.join(c.cache.custom_generators_path, "_myfunc.py"), myaux) | ||
|
||
c.save({"conanfile.txt": ""}) | ||
c.run("install . -g MyCustomGenerator") | ||
assert "conanfile.txt: Generator 'MyCustomGenerator' calling 'generate()'" in c.out | ||
assert "conanfile.txt: MYGENERATE WORKS!!" in c.out |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider defining GENERATORS_EXTENSION_FOLDER analogous to DEPLOYERS_EXTENSION_FOLDER at line 30. Also, if it is not too late, it would read better if DEPLOYERS_EXTENSION_FOLDER was defined to be "deployers" rather than "deploy". Lastly, this code might be more logically placed at line 211.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am afraid that it would be better not to change the
deploy
folders, as we are aware of some users already using them, and it would be breaking, probably not worth it.Regarding
GENERATORS_EXTENSION_FOLDER
I am tempted to go in the other direction, if no one else in the whole codebase uses the constant, then, tit is better encapsulated in thecustom_generators_path
, so I was tempted to remove theCUSTOM_COMMANDS_FOLDER
global const, and make it an implementation detail overcustom_commands_path
, which is the abstraction to use, not the constant. I will double check that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, most of those consts, defined as public in the global module scope are unused in the rest of the codebase. I typically prefer to provide the less possible scope and indirection when it is artificial/unused/premature, so I'd probably propose an internal simplification PR for that (but I'll do it in another PR to keep this one minimal).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @memsharded,
Thanks for the additional insights. Regarding "deploy" vs. "deployers", would it be possible to look in both "deployers" and "deploy" but issues a deprecation warning for any files found in the older one? Thanks