<?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>Forem: Alexander Ulyev</title>
    <description>The latest articles on Forem by Alexander Ulyev (@alexander_ulyev).</description>
    <link>https://forem.com/alexander_ulyev</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%2F379940%2Fb5ed795f-67a5-45d1-bde8-7422c73e9b0c.jpeg</url>
      <title>Forem: Alexander Ulyev</title>
      <link>https://forem.com/alexander_ulyev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/alexander_ulyev"/>
    <language>en</language>
    <item>
      <title>Mono repository pattern in Elixir</title>
      <dc:creator>Alexander Ulyev</dc:creator>
      <pubDate>Tue, 05 May 2020 12:00:30 +0000</pubDate>
      <link>https://forem.com/alexander_ulyev/mono-repository-pattern-in-elixir-7lk</link>
      <guid>https://forem.com/alexander_ulyev/mono-repository-pattern-in-elixir-7lk</guid>
      <description>&lt;p&gt;Hello everyone! My name is Alexander Ulyev, I am an Erlang/Elixir enthusiast.&lt;/p&gt;

&lt;p&gt;Recently while building a chat application, I ran into a situation when I felt like my application needs to nest other applications in it. &lt;em&gt;Mix&lt;/em&gt; tool gives a solution for that - umbrella applications. Great! I was happy with that until I discovered another umbrella application inside my project. There's no out-of-the-box solution for that case, so I stopped working on my chat app and switched to reading &lt;em&gt;mix&lt;/em&gt;'s documentation and source codes. In the end I developed a pattern for building complex applications and today I want to share it with you. There will be some useful insights about mix application in general as well as mix tasks and tuning MixProject configuration along the way. Let's dive in!&lt;/p&gt;

&lt;h2&gt;
  
  
  Going beyond umbrella application
&lt;/h2&gt;

&lt;p&gt;Imagine you've got a bunch of applications that don't make sense without each other, yet must be separated to satisfy design requirements. Perhaps you want to run one of those apps on separate or/and multiple nodes or it's just a feeling that the app will have it's own life. And then it gets children apps itself! Or even more! You can end up having applications deeply nested in your main app. Should we avoid that? If apps are related, then they are related and must be developed together. Breaking designs and concepts in favour of ease isn't a good thing, so let's find a proper solution for the case.&lt;/p&gt;

&lt;p&gt;While reading umbrella applications documentation, I came across a term - "mono repository". It sounded like a solution for my case, but there were no ready-to-use modules and tasks for convenient configuring and managing such a repository. I'm not a member of mix core team, so I decided to go through all related mix's features to find ways of solving my problem without changing the way mix works. Let's see what mix has to offer!&lt;/p&gt;

&lt;h2&gt;
  
  
  Mix
&lt;/h2&gt;

&lt;p&gt;First of all, let's talk a bit about umbrella applications. I'll skip basic info as it can be easily found in &lt;code&gt;mix help new&lt;/code&gt; output.&lt;/p&gt;

&lt;p&gt;First question I had to answer was: what makes umbrella application an umbrella? &lt;em&gt;:apps_path&lt;/em&gt; - that's it. If there's such a key with a string (can be empty) value, the project is considered to be umbrella and all applications in &lt;em&gt;:apps_path&lt;/em&gt; - to be it's dependencies.&lt;/p&gt;

&lt;p&gt;A word on dependencies. Dependencies can be conveniently compiled and released with single line each, just like any application. Later we'll make a use of that. Watch out: you can't define a dependency for your umbrella app if the dependency is in your &lt;em&gt;:apps_path&lt;/em&gt; folder. This results in an unresolvable dependency definition conflict, which can't be fixed with &lt;em&gt;:override&lt;/em&gt; key (not sure if it's a bug or a feature).&lt;/p&gt;

&lt;p&gt;Umbrella project ignores any test task commands as it is not supposed to (but can) have any source code files, therefore - tests... Until you comment out that line and add &lt;em&gt;:app&lt;/em&gt; key.&lt;/p&gt;

&lt;h2&gt;
  
  
  Discovering scopes
&lt;/h2&gt;

&lt;p&gt;Imagine we've got a following applications' tree:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app
   /app0
   /app1
        /app00
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;app&lt;/em&gt; will be our main project - the root, &lt;em&gt;app0&lt;/em&gt; and &lt;em&gt;app1&lt;/em&gt; - tier 1 children and one tier 2 child - &lt;em&gt;app00&lt;/em&gt;. &lt;em&gt;Root&lt;/em&gt;, &lt;em&gt;parent&lt;/em&gt;, &lt;em&gt;child&lt;/em&gt;, &lt;em&gt;leaf&lt;/em&gt; - tree structures terminology will be used further in this article.&lt;/p&gt;

&lt;p&gt;Looking at this structure, let's think:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;how can we test all applications in the tree at once?&lt;/li&gt;
&lt;li&gt;how can we keep all dependencies source, build and lock files at one place?&lt;/li&gt;
&lt;li&gt;how can we make a release of any set of those apps?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And we must consider application tree of any complexity.&lt;/p&gt;

&lt;p&gt;Mix projects tend to define a single configuration for most of project's needs. This approach suits well until project starts getting complex. Just like with any other task - when it starts getting complicated, we break it in smaller pieces, focusing and processing them one by one. Let's follow that and break the idea of mix project into mix project scopes: building, testing and releasing an application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing
&lt;/h2&gt;

&lt;p&gt;Of course individual applications get tested during their development process (assuming TDD). However it can be a good idea to test a subtree or entire tree of applications before making a release. There are things to take care of to make it work:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You must have all the configuration required by all the applications in a single file mapped on &lt;em&gt;:config_path&lt;/em&gt; key in your root or parent application's project declaration.&lt;/li&gt;
&lt;li&gt;You must start all applications before running tests. This can be achieved by declaring your children apps to be your dependencies through &lt;em&gt;:deps&lt;/em&gt; key.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;:test_paths&lt;/em&gt; key must be mapped to a list of paths of your applications' &lt;em&gt;test_helper.exs&lt;/em&gt; (&lt;em&gt;test&lt;/em&gt; folder by default).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And again, the presence of &lt;em&gt;:apps_path&lt;/em&gt; key denies all test task commands, absence of &lt;em&gt;:app&lt;/em&gt; key raises an exception.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building
&lt;/h2&gt;

&lt;p&gt;It makes sense to keep all dependencies source and built beam files at one place, so we don't duplicate any of them. And it makes sense to keep them all at the application root's folder.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;mix.lock&lt;/em&gt; file tracks fetching dependencies, and it is checked with by &lt;em&gt;mix&lt;/em&gt; every time you issue &lt;code&gt;mix deps.get&lt;/code&gt; command. The default path for lock file is ".", but we'll change it using &lt;em&gt;:lockfile&lt;/em&gt; key. &lt;em&gt;:deps_path&lt;/em&gt; points to dependencies source codes path, &lt;em&gt;:build_path&lt;/em&gt; - to built beam files, &lt;em&gt;:config_path&lt;/em&gt; - to application's configuration. I find it more convenient to have application's configuration at the same folder with application instead of having a single cumbersome file for all children apps. If you think opposite, you can map all four mentioned keys to the main application root's folder. If not - just three of those.&lt;/p&gt;

&lt;h2&gt;
  
  
  Releasing
&lt;/h2&gt;

&lt;p&gt;Both the main application and it's dependencies get released in the same way, so it's not that complicated to setup a proper project's configuration. However, release scope is too different from testing and building, so we must use a different &lt;em&gt;mix.exs&lt;/em&gt; and pass a path to it through &lt;em&gt;MIX_EXS&lt;/em&gt; environment variable. Since there are more than one mix project files, we must take care of &lt;em&gt;:version&lt;/em&gt; value - it must be same in both files. So, what must be configured for a release of mono repository?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;:deps&lt;/em&gt; must be a list of applications to release.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;:config_path&lt;/em&gt; must be a path to release-related configuration file. 3. &lt;em&gt;:releases&lt;/em&gt; key to be defined as instructed in &lt;a href="https://hexdocs.pm/mix/Mix.Tasks.Release.html"&gt;official documentation&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It makes sense to have an individual configuration for each release instead of pushing unrelated parameters to various sets of applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  mono_repo
&lt;/h2&gt;

&lt;p&gt;For now we have talked about all the configuration we need to setup in order to keep our repository testable, releasable and free from duplications. It can be tedious and error-prone to define all the paths (that mostly consist of dots and slashes) and dependencies in every project's &lt;em&gt;mix.exs&lt;/em&gt;. That's why I developed &lt;em&gt;mono_repo&lt;/em&gt; - a library that removes necessity of literal definitions and substitutes them with function calls. The library is split in three modules named after scopes they focus: Test, Build and Release. I named functions after the keys they are meant to be used for, prefixed with &lt;em&gt;build_&lt;/em&gt;. For example, &lt;em&gt;:config_path&lt;/em&gt; can be defined with &lt;em&gt;build_config_path()&lt;/em&gt;. Let's make a short stop-by at every module.&lt;/p&gt;

&lt;h3&gt;
  
  
  MonoRepo.Test
&lt;/h3&gt;

&lt;p&gt;The main task here is to build test folders paths and dependencies lists, but you can also use &lt;code&gt;build_config_files/0&lt;/code&gt; to let MonoRepo assemble all children's &lt;em&gt;config.exs&lt;/em&gt; and &lt;em&gt;test.exs&lt;/em&gt; into corresponding files in your root's &lt;em&gt;config&lt;/em&gt; folder. The module's functions do not require arguments. To be used in root's/parent's &lt;em&gt;mix.exs&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  MonoRepo.Build
&lt;/h3&gt;

&lt;p&gt;Using this module's functions saves time and nerves by building paths all the way up from a child to root/parent. Zero-arity functions assume the target parent to be root, one-arity functions need a target application name as an argument. To be used in children's &lt;em&gt;mix.exs&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  MonoRepo.Release
&lt;/h3&gt;

&lt;p&gt;This module needs some configuration in order to do it's job. A guide can be found in &lt;code&gt;MonoRepo&lt;/code&gt; and &lt;code&gt;MonoRepo.Release&lt;/code&gt; documentation. As soon as you setup the configuration, the rest will be taken care of this module's functions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Epilogue
&lt;/h2&gt;

&lt;p&gt;That's all we need to setup a mono repository in Elixir. As we saw, it takes some efforts, but it saves time afterwards. And, in case of using &lt;em&gt;mono_repo&lt;/em&gt; library, adds couple of new features like testing entire application tree and building cleaner releases.&lt;/p&gt;

&lt;p&gt;I found it interesting to focus mix scopes and what it takes to make mix work the way you need it to. If you want to learn more about &lt;em&gt;mix&lt;/em&gt;, I recommend reading it's source codes and using &lt;em&gt;MIX_DEBUG&lt;/em&gt; environment variable set to &lt;em&gt;true&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Thank you for reading!&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>erlang</category>
      <category>repository</category>
      <category>mix</category>
    </item>
  </channel>
</rss>
