oliverdavies.uk/content/node.c61f3ce9-54c7-4b07-9e44-7affd7d16e5b.yml

108 lines
4.8 KiB
YAML

uuid:
- value: c61f3ce9-54c7-4b07-9e44-7affd7d16e5b
langcode:
- value: en
type:
- target_id: daily_email
target_type: node_type
target_uuid: 8bde1f2f-eef9-4f2d-ae9c-96921f8193d7
revision_timestamp:
- value: '2025-05-11T09:00:57+00:00'
revision_uid:
- target_type: user
target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849
revision_log: { }
status:
- value: true
uid:
- target_type: user
target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849
title:
- value: 'One more "run" command, for Git worktrees'
created:
- value: '2022-08-17T00:00:00+00:00'
changed:
- value: '2025-05-11T09:00:57+00:00'
promote:
- value: false
sticky:
- value: false
default_langcode:
- value: true
revision_translation_affected:
- value: true
path:
- alias: /daily/2022/08/17/one-more-run-command-git-worktrees
langcode: en
body:
- value: |
<p>Here's another <code>run</code> file example, this time relating to Git worktrees...</p>
<p>One project that I work on is a multilingual Drupal application that needs to work in both English and Welsh. As I'm cloning a fresh version today, I'm doing it as a bare repository so I can use worktrees.</p>
<p>To work on it locally, just like in production, I need to use a different URL for each language so that Drupal can identify it and load the correct content and configuration.</p>
<p>For fixed environments like production or staging, the URLs are set in configuration files, but for ad-hoc environments such as local worktrees, I thought that the best approach was to override them as needed per worktree using Drush (a Drupal CLI tool).</p>
<p>I could do this manually each time or I could automate it in a <code>run</code> command. :)</p>
<p>Here's the function that I came up with:</p>
<pre><code class="bash">function drupal:set-urls-for-worktree {
# Set the site URLs based on the current Git worktree name.
local worktree_name="$(basename $PWD)"
local cy_url="cy-projectname-${worktree_name}.docker.localhost"
local en_url="projectname-${worktree_name}.docker.localhost"
# Update the URLs.
drush config:set language.negotiation url.domains.cy -y $cy_url
drush config:set language.negotiation url.domains.en -y $en_url
# Display the domains configuration to ensure that they were set correctly.
drush config:get language.negotiation url.domains
}
</code></pre>
<p>It builds the worktree URL for each language based on the directory name, executes the configuration change, and finally displays the updated configuration so I can confirm that it's been set correctly.</p>
<p>This is a good example of why I like using <code>run</code> files and how I use them to automate and simplify parts of my workflow.</p>
format: full_html
processed: |
<p>Here's another <code>run</code> file example, this time relating to Git worktrees...</p>
<p>One project that I work on is a multilingual Drupal application that needs to work in both English and Welsh. As I'm cloning a fresh version today, I'm doing it as a bare repository so I can use worktrees.</p>
<p>To work on it locally, just like in production, I need to use a different URL for each language so that Drupal can identify it and load the correct content and configuration.</p>
<p>For fixed environments like production or staging, the URLs are set in configuration files, but for ad-hoc environments such as local worktrees, I thought that the best approach was to override them as needed per worktree using Drush (a Drupal CLI tool).</p>
<p>I could do this manually each time or I could automate it in a <code>run</code> command. :)</p>
<p>Here's the function that I came up with:</p>
<pre><code class="bash">function drupal:set-urls-for-worktree {
# Set the site URLs based on the current Git worktree name.
local worktree_name="$(basename $PWD)"
local cy_url="cy-projectname-${worktree_name}.docker.localhost"
local en_url="projectname-${worktree_name}.docker.localhost"
# Update the URLs.
drush config:set language.negotiation url.domains.cy -y $cy_url
drush config:set language.negotiation url.domains.en -y $en_url
# Display the domains configuration to ensure that they were set correctly.
drush config:get language.negotiation url.domains
}
</code></pre>
<p>It builds the worktree URL for each language based on the directory name, executes the configuration change, and finally displays the updated configuration so I can confirm that it's been set correctly.</p>
<p>This is a good example of why I like using <code>run</code> files and how I use them to automate and simplify parts of my workflow.</p>
summary: null
field_daily_email_cta: { }