Repository: conan-io/conan-center-index

Issue:
[package] tree-sitter-c/*: tree-sitter-c needs to be updated to conan v2
### Description

tree-sitter-c needs to wrok well with conan v2.

### Package and Environment Details

* Package Name/Version: **tree-sitter-c/***
* Operating System+version: **Arch Linux**
* Compiler+version: **GCC 13**
* Docker image: **conanio/gcc8**
* Conan version: **conan 2.0.4**
* Python version: **Python 3.11.3**


### Conan profile

```shell
Configuration for profile default:

[settings]
os=Linux
os_build=Linux
arch=x86_64
arch_build=x86_64
compiler=gcc
compiler.version=13
compiler.libcxx=libstdc++11
build_type=Release
[options]
[conf]
[build_requires]
[env]
```


### Steps to reproduce

```shell
conan create all/conanfile.py --version 0.20.2 -pr:b=default -pr:h=default -s build_type=Release -o "tree-sitter-c/0.20.2:shared=False"
```

### Logs

<details><summary>Click to expand log</summary>

```shell
ERROR: Error loading conanfile at '/home/whalien/codebase/misc/conan-center-index/recipes/tree-sitter-c/all/conanfile.py': Unable to load conanfile in /home/whalien/codebase/misc/conan-center-index/recipes/tree-sitter-c/all/conanfile.py
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/whalien/codebase/misc/conan-center-index/recipes/tree-sitter-c/all/conanfile.py", line 1, in <module>
    from conans import CMake, ConanFile, tools
ImportError: cannot import name 'CMake' from 'conans' (/usr/lib/python3.11/site-packages/conans/__init__.py)
```

</details>



Discussion:

Resolving patch:
diff --git a/recipes/tree-sitter-c/all/conanfile.py b/recipes/tree-sitter-c/all/conanfile.py
--- a/recipes/tree-sitter-c/all/conanfile.py
+++ b/recipes/tree-sitter-c/all/conanfile.py
@@ -1,8 +1,10 @@
-from conans import CMake, ConanFile, tools
-import functools
+from conan import ConanFile
+from conan.tools.cmake import CMake
+from conan.tools.files import get, replace_in_file, copy
+from conan.tools.layout import basic_layout
 import os
 
-required_conan_version = ">=1.33.0"
+required_conan_version = ">=1.53.0"
 
 
 class TreeSitterCConan(ConanFile):
@@ -13,21 +15,22 @@ class TreeSitterCConan(ConanFile):
     homepage = "https://github.com/tree-sitter/tree-sitter-c"
     license = "MIT"
     settings = "os", "arch", "compiler", "build_type"
+    package_type = "library"
+    generators = "CMakeToolchain", "CMakeDeps"
     options = {
-        "fPIC": [True, False],
         "shared": [True, False],
+        "fPIC": [True, False],
     }
     default_options = {
-        "fPIC": True,
         "shared": False,
+        "fPIC": True,
     }
 
-    generators = "cmake", "cmake_find_package_multi"
+    generators = "CMakeToolchain", "CMakeDeps"
     exports_sources = "CMakeLists.txt"
 
-    @property
-    def _source_subfolder(self):
-        return "source_subfolder"
+    def layout(self):
+        basic_layout(self, src_folder="src")
 
     def config_options(self):
         if self.settings.os == "Windows":
@@ -35,38 +38,38 @@ def config_options(self):
 
     def configure(self):
         if self.options.shared:
-            del self.options.fPIC
-        del self.settings.compiler.libcxx
-        del self.settings.compiler.cppstd
-
-    def requirements(self):
-        self.requires("tree-sitter/0.20.0")
+            self.options.rm_safe("fPIC")
+        self.settings.rm_safe("compiler.cppstd")
+        self.settings.rm_safe("compiler.libcxx")
 
     def source(self):
-        tools.get(**self.conan_data["sources"][self.version],
-                  destination=self._source_subfolder, strip_root=True)
+        get(self, **self.conan_data["sources"][self.version], strip_root=True)
 
-    @functools.lru_cache(1)
-    def _configure_cmake(self):
-        cmake = CMake(self)
-        cmake.configure()
-        return cmake
+    def requirements(self):
+        self.requires("tree-sitter/0.20.8", transitive_headers=True, transitive_libs=True)
 
     def _patch_sources(self):
         if not self.options.shared:
-            tools.replace_in_file(
-                os.path.join(self._source_subfolder, "src", "parser.c"),
+            replace_in_file(
+                self,
+                os.path.join(self.source_folder, "src", "parser.c"),
                 "__declspec(dllexport)", ""
             )
 
     def build(self):
         self._patch_sources()
-        cmake = self._configure_cmake()
+        cmake = CMake(self)
+        cmake.configure(build_script_folder=os.path.join(self.source_folder, os.pardir))
         cmake.build()
 
     def package(self):
-        self.copy("LICENSE", src=self._source_subfolder, dst="licenses")
-        cmake = self._configure_cmake()
+        copy(
+            self,
+            "LICENSE",
+            src=self.source_folder,
+            dst=os.path.join(self.package_folder, "licenses"),
+        )
+        cmake = CMake(self)
         cmake.install()
 
     def package_info(self):


Test patch:
diff --git a/recipes/tree-sitter-c/all/test_package/CMakeLists.txt b/recipes/tree-sitter-c/all/test_package/CMakeLists.txt
--- a/recipes/tree-sitter-c/all/test_package/CMakeLists.txt
+++ b/recipes/tree-sitter-c/all/test_package/CMakeLists.txt
@@ -1,9 +1,6 @@
 cmake_minimum_required(VERSION 3.1)
 project(test_package C)
 
-include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
-conan_basic_setup(TARGETS)
-
 find_package(tree-sitter-c REQUIRED CONFIG)
 
 add_executable(${PROJECT_NAME} test_package.c)
diff --git a/recipes/tree-sitter-c/all/test_package/conanfile.py b/recipes/tree-sitter-c/all/test_package/conanfile.py
--- a/recipes/tree-sitter-c/all/test_package/conanfile.py
+++ b/recipes/tree-sitter-c/all/test_package/conanfile.py
@@ -1,10 +1,20 @@
-from conans import ConanFile, CMake, tools
+from conan import ConanFile
+from conan.tools.build import can_run
+from conan.tools.cmake import CMake, cmake_layout
+
 import os
 
 
 class TestPackageConan(ConanFile):
     settings = "os", "arch", "compiler", "build_type"
-    generators = "cmake", "cmake_find_package_multi"
+    generators = "CMakeToolchain", "CMakeDeps", "VirtualRunEnv"
+    test_type = "explicit"
+
+    def layout(self):
+        cmake_layout(self)
+
+    def requirements(self):
+        self.requires(self.tested_reference_str)
 
     def build(self):
         cmake = CMake(self)
@@ -12,6 +22,6 @@ def build(self):
         cmake.build()
 
     def test(self):
-        if not tools.cross_building(self):
-            bin_path = os.path.join("bin", "test_package")
-            self.run(bin_path, run_environment=True)
+        if can_run(self):
+            cmd = os.path.join(self.cpp.build.bindir, "test_package")
+            self.run(cmd, env="conanrun")