|
| 1 | +.. examples_extensions_deployers_sources: |
| 2 | +
|
| 3 | +Copy sources from all your dependencies |
| 4 | +======================================= |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +Please, first of all, clone the sources to recreate this project. You can find them in the |
| 9 | +`examples2.0 repository <https://github.com/conan-io/examples2>`_ in GitHub: |
| 10 | + |
| 11 | +.. code-block:: bash |
| 12 | +
|
| 13 | + $ git clone https://github.com/conan-io/examples2.git |
| 14 | + $ cd examples2/examples/extensions/deployers/sources |
| 15 | +
|
| 16 | +
|
| 17 | +In this example we are going to see how to create and use a custom deployer. |
| 18 | +This deployer copies all the source files from your dependencies and puts them into a specific output folder |
| 19 | + |
| 20 | +.. note:: |
| 21 | + |
| 22 | + To better understand this example, it is highly recommended to have previously read the :ref:`Deployers <reference_extensions_deployer_direct_deploy>` reference. |
| 23 | + |
| 24 | + |
| 25 | +Locate the deployer |
| 26 | +------------------- |
| 27 | + |
| 28 | +In this case, the deployer is located in the same directory as our example conanfile, |
| 29 | +but as shown in :ref:`Deployers <reference_extensions_deployer_direct_deploy>` reference, |
| 30 | +Conan will look for the specified deployer in a few extra places in order, namely: |
| 31 | + |
| 32 | +#. Absolute paths |
| 33 | +#. Relative to cwd |
| 34 | +#. In the ``[CONAN_HOME]/extensions/deploy`` folder |
| 35 | +#. Built-in deployers |
| 36 | + |
| 37 | + |
| 38 | +Run it |
| 39 | +------ |
| 40 | + |
| 41 | +For our example, we have a simple recipe that lists both ``zlib`` and ``mcap`` as requirements. |
| 42 | +With the help of the ``tools.build:download_source=True`` conf, we can force the invocation of its ``source()`` method, |
| 43 | +which will ensure that sources are available even if no build needs to be carried out. |
| 44 | + |
| 45 | +Now, you should be able to use the new deployer in both ``conan install`` and ``conan graph`` commands for any given recipe: |
| 46 | + |
| 47 | +.. code-block:: bash |
| 48 | +
|
| 49 | + $ conan graph info . -c tools.build:download_source=True --deploy=sources_deploy |
| 50 | +
|
| 51 | +
|
| 52 | +Inspecting the command output we can see that it copied the sources of our direct dependencies ``zlib`` and ``mcap``, |
| 53 | +**plus** the sources of our transitive dependencies, ``zstd``and ``lz4`` to a ``dependencies_sources`` folder. |
| 54 | +After this is done, extra preprocessing could be done to accomplish more specific needs. |
| 55 | + |
| 56 | +Code tour |
| 57 | +--------- |
| 58 | + |
| 59 | +The **source_deploy.py** file has the following code: |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +.. code-block:: python |
| 64 | + :caption: **sources_deploy.py** |
| 65 | +
|
| 66 | + from conan.tools.files import copy |
| 67 | + import os |
| 68 | +
|
| 69 | +
|
| 70 | + def deploy(graph, output_folder): |
| 71 | + for name, dep in graph.root.conanfile.dependencies.items(): |
| 72 | + copy(graph.root.conanfile, "*", dep.folders.source_folder, os.path.join(output_folder, "dependency_sources", str(dep))) |
| 73 | +
|
| 74 | +
|
| 75 | +deploy() |
| 76 | +++++++++ |
| 77 | + |
| 78 | +The ``deploy()`` method is called by Conan, and gets both a dependency graph and an output folder path as arguments. |
| 79 | +It iterates all the dependencies of our recipe and copies every source file to their respective folders |
| 80 | +under ``dependencies_sources`` using :ref:`conan.tools.copy<conan_tools_files_copy>`. |
0 commit comments