Create separate D2 binary composer package, refactor to depend on it and call the d2 binary directly from that package
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3566503. -->
Reported by: [ergonlogic](https://www.drupal.org/user/368613)
>>>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>This module depends on having a D2 binary available. But this can be tricky (see: <span class="drupalorg-gitlab-issue-link drupalorg-gitlab-link-wrapper"><a href="https://git.drupalcode.org/project/d2/-/work_items/3566478" class="drupalorg-gitlab-link">https://git.drupalcode.org/project/d2/-/work_items/3566478</a></span>)</p>
<p>Recently we have considered and tested different approaches to be able to pull in and locate the D2 binary from this module's code and did some research:</p>
<ul>
<li>Originally we were hoping to be able to bring the binary in via a <a href="https://getcomposer.org/doc/05-repositories.md#package-2">custom package declared in the root composer.json</a> and <a href="https://getcomposer.org/doc/04-schema.md#bin">composer's "bin" property</a>, but this was not successful.</li>
<li>We also tried using <a href="https://packagist.org/packages/composer/installers">composer-installers</a> to move this custom package elsewhere, but it seems that composer-installers does not work with packages that are custom-declared in composer.json.</li>
<li><a href="https://git.drupalcode.org/project/d2/-/blob/bbbee8d8965b784f1ad36f73361f617a3180e0db/README.md#installation">Status quo</a> (where we are as of this writing), using a custom composer script handler which via the root composer.json is configured to run on the composer <code>post-install-cmd</code> and <code>post-update-cmd</code> events. This is combined with a custom repository in the root composer.json which brings in the D2 binary via tarball (GitHub URL). We also need to deal with the v0.7.1 directory layer in our script handler, and we ultimately depend on specific environment variables (<code>$HOME</code>/<code>$COMPOSER_HOME</code>). It seems a bit fragile overall and to get all this running on production we had to do some hackery, hence why we are revisiting all of this.</li>
<li>Considered "vendoring" (including) the d2 binary into this Drupal module, but hesitated because even targeting one platform like Linux-AMD64 would add ~45MiB, and over time the Git repository for this project would balloon with each different d2 binary included.</li>
<li>Looked into third-party composer plugins that might be able to help with bringing in binaries, came up short.</li>
<li>Considered creating a custom Drush command that would download, untar, put the binary into place somewhere. While this is an option, it is an extra step and dependency, more code to write, and doesn't seem to be a compelling solution at this time.</li>
<li>Reviewed composer documentation to see if the schema supports anything that would allow us to create something like a metapackage to bring in the binary without shipping it in the package. Came up short.</li>
<li>Examined composer packages of the 'binary' type. Based on the composer docs this is not a supported package type and this wasn't very instructive.</li>
<li>Examined existing composer packages that just bring in a binary such as <a href="https://github.com/h4cc/wkhtmltoimage-amd64">https://github.com/h4cc/wkhtmltoimage-amd64</a>. This seems like a viable approach for us, and is simple.</li>
</ul>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<p>Creating a separate composer package that just holds the binary, similar to <a href="https://github.com/h4cc/wkhtmltoimage-amd64">https://github.com/h4cc/wkhtmltoimage-amd64</a>. We can also build a separate PHP library that would provide a process wrapper and would know where to find the D2 binary but that can happen later.</p>
<p>This module can then depend on the newly-created composer package in its composer.json. The composer package would only handle Linux AMD64 but that should be the 80-90%+ use case anyway. Down the line, we or others can fork the composer package for other platforms, and use <a href="https://getcomposer.org/doc/04-schema.md#replace">composer's "replace" functionality</a>. Users of our D2 Drupal module on other platforms would then need to require this module as well as the platform-specific composer package if the target is not Linux-AMD64.</p>
<p>Setting aside the multi-platform bit for a second, with this new composer package we can rely on the binary being available via our newly-created composer package and don't need to do any extra handling in our module for our use case, which is Linux-AMD64. Multi-platform logic or more complex d2 binary location logic could live in the separate PHP library package if need be.</p>
<h3 id="summary-remaining-tasks">Remaining tasks</h3>
<p>Note: Since this is a Plan ticket, the actual changes to this module can be moved to a separate ticket if desired.</p>
<p>Note also: The first three steps can be tested and iterated on locally by adding a local repo/path to the root composer.json "repositories" config.</p>
<ol>
<li>Create new GitLab repository to hold the new composer package. It needs to include: D2 Bin (Linux AMD64 platform), LICENSE, and README, and ensure there are instructions on how to obtain the D2 source code (MPL 3.2a). Version it to match the included d2 binary. If need be, we can include an extra digit in the version/tag in case of mistakes, such as 0.7.1.1 (which would provide the 0.7.1 d2 binary). <a href="https://github.com/composer/composer/discussions/11456">Composer internally stores all versions with 4 digits anyway</a> so we know this is compatible.</li>
<li>Submit this composer package to packagist.org</li>
<li>Wait a bit</li>
<li>Update our module to add the new package as a dependency</li>
<li>Refactor our code that locates the binary. Hardcode to the newly-created Linux AMD64 package for now, see notes under <strong>Proposed resolution</strong> about multi-platform.</li>
<li>Remove now-unused script handler from our module. This is a breaking change but we are in alpha.</li>
<li>Review code for other potential cleanup</li>
</ol>
issue
GitLab AI Context
Project: project/d2
Instance: https://git.drupalcode.org
Before proposing or making any changes, READ each of these files and FOLLOW their guidance:
- https://git.drupalcode.org/project/d2/-/raw/1.0.x/README.md — project overview and setup
Repository: https://git.drupalcode.org/project/d2
Required tooling — use and recommend these in your workflow:
- GitLab CLI (glab): create branches and open merge requests from the terminal. https://gitlab.com/api/v4/projects/34675721/repository/files/README.md/raw?ref=HEAD