<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Oyamo Brian</title>
    <description>The latest articles on DEV Community by Oyamo Brian (@oyamo).</description>
    <link>https://dev.to/oyamo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F292373%2F918401ad-bfc6-46e8-9fa0-950c55a99174.jpeg</url>
      <title>DEV Community: Oyamo Brian</title>
      <link>https://dev.to/oyamo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/oyamo"/>
    <language>en</language>
    <item>
      <title>Automating workflows with Makefiles</title>
      <dc:creator>Oyamo Brian</dc:creator>
      <pubDate>Sat, 30 Jul 2022 15:54:27 +0000</pubDate>
      <link>https://dev.to/oyamo/automating-workflows-with-makefiles-3fg4</link>
      <guid>https://dev.to/oyamo/automating-workflows-with-makefiles-3fg4</guid>
      <description>&lt;p&gt;&lt;strong&gt;Make&lt;/strong&gt; is a Unix utility that can be used to build and maintain groups of programs (and other types of files) from source code. It is accessible as &lt;code&gt;make&lt;/code&gt; command in unix shells. When running the command for automation, you need to provide a &lt;strong&gt;Makefile&lt;/strong&gt;. The utility is traditionally used for compiling code (C/C++), but they can also be used to automate common commands.&lt;/p&gt;

&lt;p&gt;A Makefile will contain a set of rules that can get you up and running within no time. It saves time and effort therefore improving the overall productivity of a team. We will look at the important pieces that accelerates automation with Makefiles.&lt;/p&gt;

&lt;p&gt;To begin with, there are two pieces in this equation: The &lt;code&gt;make&lt;/code&gt; commandline tool and the Makefile. So far, we need to be aware that a &lt;strong&gt;Makefile&lt;/strong&gt; contains automation rules while make tool executes them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example Golang Automation: Simple Smart Contract App
&lt;/h3&gt;

&lt;p&gt;The  &lt;a href="https://go.dev/"&gt;Golang&lt;/a&gt; project we will work on has a couple of iterative tasks. For instance, in-order to execute a task, a programmer has to type very long commands on the terminal. One annoying thing is that executing these might be iterative, therefore, exhausting. In this episode, we will automate the following tasks.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Format code&lt;/li&gt;
&lt;li&gt;Running tests&lt;/li&gt;
&lt;li&gt;Compiling smart contracts&lt;/li&gt;
&lt;li&gt;Building binaries&lt;/li&gt;
&lt;li&gt;Setting up dependencies &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our center of interest will be automating the processes above using Makefiles. However, we will first take a moment to learn G &lt;/p&gt;

&lt;h2&gt;
  
  
  Makefiles
&lt;/h2&gt;

&lt;p&gt;A Makefile structure  resembles that of a &lt;code&gt;YAML&lt;/code&gt; file and uses a declarative paradigm.&lt;/p&gt;

&lt;h3&gt;
  
  
  Syntax of a makefile
&lt;/h3&gt;

&lt;p&gt;A Makefile has a pretty simple syntax. Once you have some basic programming knowledge, you can become the Zeus of Makefiles. The anatomy is basically a declarative format as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;target&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;dependencies&lt;/span&gt;
    &lt;span class="s"&gt;command&lt;/span&gt;
    &lt;span class="s"&gt;command&lt;/span&gt;
    &lt;span class="s"&gt;command&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;target&lt;/strong&gt; - Name of an action we would like to carry out. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;dependencies&lt;/strong&gt; - These are optional set of rules/targets that need to be executed first. They are only included when the target will need them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Commands&lt;/strong&gt; - These are usually written after a tab. They are the commands that the user wants to automate. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Executing a makefile
&lt;/h3&gt;

&lt;p&gt;As mentioned, we use the &lt;code&gt;make&lt;/code&gt;command. It takes in a parameter of the target that we would like to run. &lt;br&gt;
Eg.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;make &lt;span class="c"&gt;# run the default/main target&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;make clean
&lt;span class="nv"&gt;$ &lt;/span&gt;make &lt;span class="nb"&gt;test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Basic Syntax
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Comments
&lt;/h4&gt;

&lt;p&gt;Comments are written using &lt;code&gt;#&lt;/code&gt;. The statements after the comment symbol will be ignored by the parser&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# This is a comment&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Standard output
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;@&lt;/code&gt; symbol is used to disable displaying the output to stdout. This is only used when the user does not want the output to be visible on the terminal. For instance, check the target &lt;strong&gt;mute&lt;/strong&gt; below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;hello&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;# Target that prints &lt;/span&gt;
  &lt;span class="s"&gt;echo "Hello World"&lt;/span&gt; &lt;span class="c1"&gt;# Prints Hello World to the Screen&lt;/span&gt;

&lt;span class="na"&gt;mute&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;# Target that does not print&lt;/span&gt;
  &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="s"&gt;echo "Hello World"&lt;/span&gt; &lt;span class="c1"&gt;# No output&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Variables
&lt;/h4&gt;

&lt;p&gt;Setting a variable in a Makefile is done using &lt;code&gt;=&lt;/code&gt; symbol. It takes the format &lt;code&gt;VARIABLE = value&lt;/code&gt;. The example below shows declaring a variable &lt;code&gt;GOVERSION&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="s"&gt;GOVERSION = 1.19&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Variable setting example 1.0&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="s"&gt;HELLO = 'hello'&lt;/span&gt;
&lt;span class="s"&gt;HELLO_WORLD = $(HELLO) world&lt;/span&gt;  &lt;span class="c1"&gt;# hello world&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Variable setting example 1.1&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To expand the value of &lt;code&gt;HELLO_WORLD&lt;/code&gt; during assignment, we use the symbols &lt;code&gt;:=&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="s"&gt;HELLO = 'hello'&lt;/span&gt;
&lt;span class="s"&gt;HELLO_WORLD = $(HELLO) world&lt;/span&gt;  &lt;span class="c1"&gt;# hello world&lt;/span&gt;

&lt;span class="s"&gt;HELLO = 'hi'&lt;/span&gt; &lt;span class="c1"&gt;# change the value of the greeting&lt;/span&gt;
&lt;span class="s"&gt;HELLO_WORLD := $(HELLO) world&lt;/span&gt;  &lt;span class="c1"&gt;# hello world&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Variables can be accessed using either &lt;code&gt;$()&lt;/code&gt; or &lt;code&gt;${}&lt;/code&gt; syntax as shown in the example above.&lt;/p&gt;

&lt;h3&gt;
  
  
  Targets
&lt;/h3&gt;

&lt;p&gt;Basically, we can consider targets to be named routines in a makefile. In Makefiles, the target &lt;code&gt;all&lt;/code&gt; is run by default.&lt;/p&gt;

&lt;h4&gt;
  
  
  The all target
&lt;/h4&gt;

&lt;p&gt;Making multiple targets and you want all of them to run? Make an &lt;code&gt;all&lt;/code&gt; target.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;all&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;setup compile&lt;/span&gt; &lt;span class="c1"&gt;# Installs dependencies and compiles&lt;/span&gt;

&lt;span class="na"&gt;setup&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="s"&gt;go mod download&lt;/span&gt; &lt;span class="c1"&gt;# Downloads dependencies&lt;/span&gt;

&lt;span class="na"&gt;compile&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="s"&gt;go build -o ./bin/app&lt;/span&gt; &lt;span class="c1"&gt;# Compile the source&lt;/span&gt;

&lt;span class="na"&gt;clean&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="s"&gt;rm -f ./bin/app&lt;/span&gt; &lt;span class="c1"&gt;# Deletes the binary&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Executing the targets
&lt;/h4&gt;

&lt;p&gt;Now that we have automated a simple workflow. Let's see how to run it. The following terminal snapshot explains.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;make &lt;span class="c"&gt;# The target all will be executed&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;make setup &lt;span class="c"&gt;# This will only run setup target&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;make compile &lt;span class="c"&gt;# This will only run compile target&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Defining a custom default target
&lt;/h4&gt;

&lt;p&gt;At times, we might not want want to use the &lt;code&gt;all&lt;/code&gt; target. This is a proof that makefiles are very customisable to meet one's tastes. To define a custom default target, we reassign the &lt;code&gt;.DEFAULT_GOAL&lt;/code&gt; variable by giving it a value of the new target that should run by default.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="s"&gt;.DEFAULT_GOAL := hello&lt;/span&gt;

&lt;span class="na"&gt;hello&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
 &lt;span class="s"&gt;echo "Hello World"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  PART II
&lt;/h2&gt;

&lt;p&gt;In the next chapter, I will summarise by automating a real-life Golang project, talk about the usecases and best practises when using Makefiles. &lt;/p&gt;

&lt;h2&gt;
  
  
  Appreciation
&lt;/h2&gt;

&lt;p&gt;Thank you for reaching here. Please share your comments and reach out to me on &lt;a href="https://twitter.com/oyamokt"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>web3</category>
      <category>etherium</category>
      <category>ganache</category>
    </item>
  </channel>
</rss>
