Setting up your first Ruby on Rails app and fixing issues as they arise can pose a significant and time-consuming challenge. After all, it is easy to break something along the way, forget to run the rake routes command or forget to precompile assets, among other steps. Fortunately, Bash script automation can help. From enabling efficient development to showing exactly what you are installing and how it will behave, the advantages of Bash script automation are countless. Automating repetitive tasks can even help to reduce a development team’s human error rate, all while saving time and material resources.
To get started, some basic Bash knowledge as well as an understanding of RVM and Curl will be needed to install and manage Ruby. For this guide, Ruby 3.0.2 will be used on a classic Mac terminal. This tutorial will also require Yarn and Node. Installation of these via the terminal will be demonstrated in the code snippets below. In addition, a full step-by-step Ruby installation guide can be found here.
What is Ruby Automation?
In its most basic form, Ruby automation can be understood as writing scripts to automate tasks in Ruby. However, it is possible to substitute almost any other programming language name for “Ruby,” and this definition will hold true. Automation is a pretty standard and simple practice, but the name can sound intimidating. Let’s explore a simple example showing what automation of a Rails/Bash setup application script looks like.
There are a couple of things happening here. At first glance, we can see that from lines 6 to 9, we are installing Node, npm, and Yarn, all of which are libraries needed for Rails. On line 10, we install webpack as well. I always prefer to do this because Rails can be a little bit cheeky from time to time (as it is smart), and it may ask for those installations on every app created.
In line 12, we install a single controller intended for a landing page. This can be pretty much any controller; just keep in mind that if we want to set some of those views as root, we will need to do this by hand once we launch the server on the routes.rb file. We can automate this process, too, as we will discuss later on in this article.
Line 13 creates and migrates the database. At this point, since there is no database, it will just ignore the command. It is almost the same in line 14, with the assets and routes. As we have an “empty” app, there are no assets, but there are routes.
Finally, line 15 restarts the server and launches it again. I know this may look weird, but it is wise to do this step, as some of the processes we executed before might require it.
Automating a Ruby Environment From Zero
Now, let’s take a deeper look at the example code from above. Following coding best practices, a shebang is written on the first line to indicate to the program loader that a Bash script is being executed. The next two lines explain to the reader usage of the script and the command that calls it; the app_name parameter takes its part here on the script as $1.
Now, let’s modify the original code a bit by adding a scaffold. According to the Ruby on Rails Guides, “A scaffold in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above” (Source).
This code was intended to actually have something on the database to create and migrate with the rake command. As defined by the RoR Guides, rake is a task runner that is in this case used to create the database (since we have a new app) and then migrate it. When no elements on the app database are found and this command is applied, Rails creates an empty database which can be migrated later on. In the code snippet above, we create a table on the database called “Private” with some attributes, and then we rake it as usual on the database.
A Ruby/Bash Hybrid Can Dig Down Even Deeper
Sometimes we need to automate slightly more complex tasks, or maybe just write a chunk of code that can be called as a command from somewhere else. It is not a matter of if, but rather when it will be necessary to create separated Bash scripts, and possibly Ruby scripts as well. Keeping this in mind is necessary to maintain smooth automations. A good example on an app setup can be seen when there is a backup of databases to be placed somewhere else. Let’s say that for some reason, an XLSX file format backup of a small app needs to be moved. A simple Bash script to complete this task should look like this:
This example may look very simple, and it is indeed. But it is very useful to see how easy it is to mix Bash files when developing in Ruby on Rails. It is possible to use this simple script, for example, to add new assets to the assets directory and then precompile them via a single command using Bash code. This should look like this:
When used appropriately, this single line of code can save us tons of time tapping on the terminal, help us avoid human errors, and provide confirmation that the executed task has been performed properly. This particular example may look simple, but these kinds of scripts are very powerful. They make it possible to change CSS directives, set the right time zone, and set up internationalization, among other tasks like setting routes in the Bash setup script.
As said before, Rails-Bash automation can be as simple or as complex as we decide to make it. Regardless, we should not underestimate the power of this tool, as it can save us lots of time with certain tasks. Still, there are many apps that may demand long setups in order to be executed properly. We need to set a clear path to achieving what we want and take care not to “over-automate” our apps.
Bash automation with Ruby and Ruby on Rails must be practiced, and tasks to be automated must be carefully selected. But once we understand the basic concepts, they can then be applied to other programming languages and tasks. It may take some time to master the process, but ultimately, the payoff is well worth it.
Ready to join one of the fastest-growing software development companies? We’re hiring Ruby on Rails engineers — apply now!