Repository: conan-io/conan

Issue:
install_folder usage in CMakeDefinitionsBuilder can break custom generators
To help us debug your issue please explain:

- [x] I've read the [CONTRIBUTING guide](https://github.com/conan-io/conan/blob/develop/.github/CONTRIBUTING.md).
- [x] I've specified the Conan version, operating system version and any tool that can be relevant.
Conan 1.18
Windows 10 x86_64
Custom generator such as https://github.com/pepe82sh/ConanCmakeToolchainGenerator
- [x] I've explained the steps to reproduce the error or the motivation/use case of the question/suggestion.

#5021 moves install_folder outside the check for the generators. When we use a custom generator such as https://github.com/pepe82sh/ConanCmakeToolchainGenerator the CMakeDefinitionsBuilder cannot be used to get the cmake definitions.

This results in:
conanfile.txt: ERROR: Generator CmakeToolchain(file:conan_toolchain.cmake) failed
'ConanFile' object has no attribute 'install_folder'
ERROR: 'ConanFile' object has no attribute 'install_folder'

I'm not sure if this is the custom generator misusing CMakeDefininitionsBuilder, or the CMakeDefininitionsBuilder using install_folder when it should not.

I think the functionality this custom generator brings are great and I am willing to have a look to fix this in a good way (on conan or CmakeToolchain), but for now I am clueless :-).




Discussion:
Able to reproduce it with:

```
git clone  https://github.com/pepe82sh/ConanCmakeToolchainGenerator
cd ConanCmakeToolchainGenerator
conan create . user/testing
```

Resolving patch:
diff --git a/conans/client/manager.py b/conans/client/manager.py
--- a/conans/client/manager.py
+++ b/conans/client/manager.py
@@ -20,6 +20,7 @@ def deps_install(app, ref_or_path, install_folder, graph_info, remotes=None, bui
                  manifest_interactive=False, generators=None, no_imports=False,
                  create_reference=None, keep_build=False, use_lock=False, recorder=None):
     """ Fetch and build all dependencies for the given reference
+    :param app: The ConanApp instance with all collaborators
     @param ref_or_path: ConanFileReference or path to user space conanfile
     @param install_folder: where the output files will be saved
     @param build_modes: List of build_modes specified
@@ -31,6 +32,7 @@ def deps_install(app, ref_or_path, install_folder, graph_info, remotes=None, bui
     @param generators: List of generators from command line. If False, no generator will be
     written
     @param no_imports: Install specified packages but avoid running imports
+
     """
     out, user_io, graph_manager, cache = app.out, app.user_io, app.graph_manager, app.cache
     remote_manager, hook_manager = app.remote_manager, app.hook_manager
@@ -77,6 +79,7 @@ def deps_install(app, ref_or_path, install_folder, graph_info, remotes=None, bui
         manifest_manager.print_log()
 
     if install_folder:
+        conanfile.install_folder = install_folder
         # Write generators
         output = conanfile.output if conanfile.display_name != "virtual" else out
         if generators is not False:


Test patch:
diff --git a/conans/test/functional/generators/custom_generator_test.py b/conans/test/functional/generators/custom_generator_test.py
--- a/conans/test/functional/generators/custom_generator_test.py
+++ b/conans/test/functional/generators/custom_generator_test.py
@@ -1,4 +1,5 @@
 import os
+import textwrap
 import unittest
 
 from conans.model.ref import ConanFileReference
@@ -147,3 +148,27 @@ class MyCustomGeneratorWithTemplatePackage(ConanFile):
         client.run("install gen/0.1@user/stable -g=MyCustomTemplateGenerator")
         generated = load(os.path.join(client.current_folder, "customfile.gen"))
         self.assertEqual(generated, "Template: Hello")
+
+    def install_folder_test(self):
+        # https://github.com/conan-io/conan/issues/5568
+        templated_generator = textwrap.dedent("""
+            from conans import ConanFile
+            from conans.model import Generator
+            class MyGenerator(Generator):
+                @property
+                def filename(self):
+                    return "customfile.gen"
+                @property
+                def content(self):
+                    return self.conanfile.install_folder
+
+            class MyCustomGenerator(ConanFile):
+                pass
+            """)
+        client = TestClient()
+        client.save({CONANFILE: templated_generator, "mytemplate.txt": "Template: %s"})
+        client.run("create . gen/0.1@user/stable")
+        client.run("install gen/0.1@user/stable -g=MyGenerator")
+        generated = load(os.path.join(client.current_folder, "customfile.gen"))
+        self.assertEqual(generated, client.current_folder)
+