oliverdavies.uk/content/node.cdfbc891-17fa-45e3-9ea6-b0b1bc6defd2.json
2025-05-30 02:34:52 +01:00

100 lines
No EOL
10 KiB
JSON

{
"uuid": [
{
"value": "cdfbc891-17fa-45e3-9ea6-b0b1bc6defd2"
}
],
"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": "Using a \"run\" file to simplify project tasks"
}
],
"created": [
{
"value": "2022-08-15T00: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\/15\/using-run-file-simplify-project-tasks",
"langcode": "en"
}
],
"body": [
{
"value": "\n <p>Every project has its own set of commands that need to be run regularly.<\/p>\n\n<p>From starting a local server or the project's containers with Docker or Docker Compose, running tests or clearing a cache, or generating the CSS and JavaScript assets, these commands can get quite complicated and time-consuming and error-prone to type over and over again.<\/p>\n\n<p>One common way to simplify these commands is using a <code>Makefile<\/code>.<\/p>\n\n<p>A Makefile contains a number of named targets that you can reference, and each has one or more commands that it executes.<\/p>\n\n<p>For example:<\/p>\n\n<pre><code class=\"language-language-yaml\"># Start the project.\nstart:\n docker-compose up -d\n\n# Stop the project.\nstop:\n docker-compose down\n\n# Run a Drush command.\ndrush:\n docker-compose exec php-fpm drush $(ARGS)\n<\/code><\/pre>\n\n<p>With this Makefile, I can run <code>make start<\/code> to start the project, and <code>make stop<\/code> to stop it.<\/p>\n\n<p>Makefiles work well, but I don't use the full functionality that they offer, such as dependencies for targets, and passing arguments to a command - like arguments for a Drush, Symfony Console, or Artisan command, doesn't work as I originally expected.<\/p>\n\n<p>In the example, to pass arguments to the <code>drush<\/code> command, I'd have to type <code>ARGS=\"cache:rebuild\" make drush<\/code> for them to get added and the command to work as expected.<\/p>\n\n<p>An agency that I worked for created and open-sourced their own Makefile-like tool, written in PHP and built on Symfony Console. I gave a talk on it called <a href=\"\/presentations\/working-with-workspace\">Working with Workspace<\/a> and used it on some of my own personal and client projects.<\/p>\n\n<h2 id=\"what-i%27m-using-now\">What I'm using now<\/h2>\n\n<p>The solution that I'm using now is a <code>run<\/code> file, which is something that I learned from Nick Janetakis' blog and YouTube channel.<\/p>\n\n<p>It's a simple Bash file where you define your commands (or tasks) as functions, and then execute them by typing <code>.\/run test<\/code> or <code>.\/run composer require something<\/code>.<\/p>\n\n<p>Here's the Makefile example, but as a <code>run<\/code> script:<\/p>\n\n<pre><code class=\"bash\">#!\/usr\/bin\/env bash\n\nfunction help() {\n # Display some default help text.\n # See examples on GitHub of how to list the available tasks.\n}\n\nfunction start {\n # Start the project.\n docker-compose up -d\n}\n\nfunction stop {\n # Stop the project.\n docker-compose down\n}\n\nfunction drush {\n # Run a Drush command with any additional arguments.\n # e.g. \".\/run drush cache:rebuild\"\n docker-compose exec php-fpm drush \"${@}\"\n}\n\n# Execute the command, or run \"help\".\neval \"${@:-help}\"\n<\/code><\/pre>\n\n<p>As it's Bash, I can just use <code>$1<\/code>, <code>$2<\/code> etc to get specific arguments, or <code>$@<\/code> to get them all, so <code>.\/run drush cache:rebuild<\/code> works as expected and any additional arguments are included.<\/p>\n\n<p>You can group tasks by having functions like <code>test:unit<\/code> and <code>test:commit<\/code>, and tasks can run other tasks. I use this for running groups of commands within a CI pipeline, and to extract helper functions for tasks like running <code>docker-compose exec<\/code> within the PHP container that other commands like <code>drush<\/code>, <code>console<\/code> or <code>composer<\/code> could re-use.<\/p>\n\n<p>As well as running ad-hoc commands during development, I also use the run file to create functions that run Git pre-commit or pre-push hooks, deploy code with Ansible, or build, push or pull the project's latest Docker images.<\/p>\n\n<p>I also use one within my Talks repository to generate PDF files using rst2pdf, present them using phdpc, and generate thumbnail images.<\/p>\n\n<p>For examples of <code>run<\/code> files that I use in my open-source code, <a href=\"https:\/\/github.com\/search?l=Shell&amp;q=user%3Aopdavies+filename%3Arun&amp;type=Code\">you can look in my public GitHub repositories<\/a>, and for more information, here is <a href=\"https:\/\/nickjanetakis.com\/blog\/replacing-make-with-a-shell-script-for-running-your-projects-tasks\">Nick's blog post where I first found the idea<\/a>.<\/p>\n\n ",
"format": "full_html",
"processed": "\n <p>Every project has its own set of commands that need to be run regularly.<\/p>\n\n<p>From starting a local server or the project's containers with Docker or Docker Compose, running tests or clearing a cache, or generating the CSS and JavaScript assets, these commands can get quite complicated and time-consuming and error-prone to type over and over again.<\/p>\n\n<p>One common way to simplify these commands is using a <code>Makefile<\/code>.<\/p>\n\n<p>A Makefile contains a number of named targets that you can reference, and each has one or more commands that it executes.<\/p>\n\n<p>For example:<\/p>\n\n<pre><code class=\"language-language-yaml\"># Start the project.\nstart:\n docker-compose up -d\n\n# Stop the project.\nstop:\n docker-compose down\n\n# Run a Drush command.\ndrush:\n docker-compose exec php-fpm drush $(ARGS)\n<\/code><\/pre>\n\n<p>With this Makefile, I can run <code>make start<\/code> to start the project, and <code>make stop<\/code> to stop it.<\/p>\n\n<p>Makefiles work well, but I don't use the full functionality that they offer, such as dependencies for targets, and passing arguments to a command - like arguments for a Drush, Symfony Console, or Artisan command, doesn't work as I originally expected.<\/p>\n\n<p>In the example, to pass arguments to the <code>drush<\/code> command, I'd have to type <code>ARGS=\"cache:rebuild\" make drush<\/code> for them to get added and the command to work as expected.<\/p>\n\n<p>An agency that I worked for created and open-sourced their own Makefile-like tool, written in PHP and built on Symfony Console. I gave a talk on it called <a href=\"http:\/\/default\/presentations\/working-with-workspace\">Working with Workspace<\/a> and used it on some of my own personal and client projects.<\/p>\n\n<h2 id=\"what-i%27m-using-now\">What I'm using now<\/h2>\n\n<p>The solution that I'm using now is a <code>run<\/code> file, which is something that I learned from Nick Janetakis' blog and YouTube channel.<\/p>\n\n<p>It's a simple Bash file where you define your commands (or tasks) as functions, and then execute them by typing <code>.\/run test<\/code> or <code>.\/run composer require something<\/code>.<\/p>\n\n<p>Here's the Makefile example, but as a <code>run<\/code> script:<\/p>\n\n<pre><code class=\"bash\">#!\/usr\/bin\/env bash\n\nfunction help() {\n # Display some default help text.\n # See examples on GitHub of how to list the available tasks.\n}\n\nfunction start {\n # Start the project.\n docker-compose up -d\n}\n\nfunction stop {\n # Stop the project.\n docker-compose down\n}\n\nfunction drush {\n # Run a Drush command with any additional arguments.\n # e.g. \".\/run drush cache:rebuild\"\n docker-compose exec php-fpm drush \"${@}\"\n}\n\n# Execute the command, or run \"help\".\neval \"${@:-help}\"\n<\/code><\/pre>\n\n<p>As it's Bash, I can just use <code>$1<\/code>, <code>$2<\/code> etc to get specific arguments, or <code>$@<\/code> to get them all, so <code>.\/run drush cache:rebuild<\/code> works as expected and any additional arguments are included.<\/p>\n\n<p>You can group tasks by having functions like <code>test:unit<\/code> and <code>test:commit<\/code>, and tasks can run other tasks. I use this for running groups of commands within a CI pipeline, and to extract helper functions for tasks like running <code>docker-compose exec<\/code> within the PHP container that other commands like <code>drush<\/code>, <code>console<\/code> or <code>composer<\/code> could re-use.<\/p>\n\n<p>As well as running ad-hoc commands during development, I also use the run file to create functions that run Git pre-commit or pre-push hooks, deploy code with Ansible, or build, push or pull the project's latest Docker images.<\/p>\n\n<p>I also use one within my Talks repository to generate PDF files using rst2pdf, present them using phdpc, and generate thumbnail images.<\/p>\n\n<p>For examples of <code>run<\/code> files that I use in my open-source code, <a href=\"https:\/\/github.com\/search?l=Shell&amp;q=user%3Aopdavies+filename%3Arun&amp;type=Code\">you can look in my public GitHub repositories<\/a>, and for more information, here is <a href=\"https:\/\/nickjanetakis.com\/blog\/replacing-make-with-a-shell-script-for-running-your-projects-tasks\">Nick's blog post where I first found the idea<\/a>.<\/p>\n\n ",
"summary": null
}
],
"feeds_item": [
{
"imported": "1970-01-01T00:32:50+00:00",
"guid": null,
"hash": "15a1f14b232babef6136f4cc242315c0",
"target_type": "feeds_feed",
"target_uuid": "90c85284-7ca8-4074-9178-97ff8384fe76"
}
]
}