Skip to content

Commit 7e43295

Browse files
authored
post: The better way ruby gems (#70)
* chore: add .bundle/config and .asset_pipeline to .gitignore * chore: correct author's role * post: The Better Way with Ruby Gems * chore: adjust sentence to please linter * chore: correct typo
1 parent b612cd9 commit 7e43295

File tree

4 files changed

+204
-1
lines changed

4 files changed

+204
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ _site
44
.jekyll-metadata
55
vendor
66
.DS_Store
7+
.bundle/config
8+
.asset_pipeline

_data/authors.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Robinson Zimmermann:
88
Arthur Alves:
99
name : "Arthur Alves"
1010
avatar : "assets/images/avatars/arthur.jpg"
11-
bio : "Principal Mobile Engineer - R&D"
11+
bio : "Principal Mobile Engineer - Customer Success"
1212

1313
Andrés Torres:
1414
name : "Andrés Torres García"
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
---
2+
title: "The Better Way with Ruby Gems"
3+
excerpt: "Using Ruby Gems to ensure the everyone in the project shares the same setup."
4+
tags: Mobile Android iOS Ruby
5+
authors:
6+
- Arthur Alves
7+
header:
8+
teaser: /assets/images/post/ruby-gems/the_better_way_ruby_gems.png
9+
teaser_alt: The Better Way with Ruby Gems
10+
category: mobile
11+
---
12+
13+
![](/assets/images/post/ruby-gems/the_better_way_ruby_gems.png)
14+
15+
# Introduction
16+
17+
Working with an iOS project involves the usage of tools such as Cocoapods, Slather, SwiftLint, Fastlane and more. Ideally, all engineers working on a particular codebase - as well as the CI machine it runs on - should maintain the same versions of these tools.
18+
19+
Some of these tools also use Ruby, and the version of Ruby installed in each one's machine might lead to strange behaviours.
20+
21+
Consider, for a moment, you’ve aligned the versions of all the tools above, including Ruby itself, between everyone working on `CodebaseA`. What if you need to also work on `CodebaseB`, owned by another team possibly in another department? The chances of them using the exact same versions for all this tooling is very minimal.
22+
23+
# Solution
24+
25+
There is a way to maintain all these alignments, regardless of how many projects you’re working on, without changes to your global setup. A solution where, for every project, you can make use of different versions of Ruby, Cocoapods, Fastlane etc. Only by making use of:
26+
27+
* Ruby Gems; and
28+
* [rbenv](https://github.com/rbenv/rbenv) - A Ruby version manager tool.
29+
30+
# Steps
31+
32+
**_Versions specified here are illustrative. Choose your versions carefully in accordance with your team._**
33+
34+
### **1.** Multiple Ruby versions.
35+
36+
**1.1** Install RBENV
37+
38+
RBENV is a Ruby version manager tool that allows us to install multiple versions of Ruby and set a specific version to a particular folder/repo/codebase. This way multiple versions can exist and multiple projects can use their own version if necessary.
39+
40+
If you don't have RBENV installed, ensure the following:
41+
42+
**_We recommend using homebrew, as it’s a package manager commonly used._**
43+
44+
Simply run the following command in your Terminal:
45+
46+
```bash
47+
brew install rbenv
48+
```
49+
50+
Once installed, we have to ensure it will be initiated properly. Add the following line to your `~/.zshrc` or `~/.bashrc` file:
51+
52+
```bash
53+
eval "$(rbenv init - )"
54+
```
55+
56+
Save the file, restart your Terminal or run source `~/.zshrc` or source `~/.bashrc` (depending on which Shell you’re using).
57+
58+
Once you first install `rbenv`, the only version of Ruby available is the `system` version, which is whatever Ruby has been installed as system default. You can see the currently installed and available versions by running:
59+
60+
```bash
61+
rbenv versions
62+
```
63+
64+
**1.2** Installing and Locking Ruby versions
65+
66+
A Ruby version is locked for a particular folder/repository with the presence of a file named `.ruby-version`, which contains solely the version of Ruby expected to be used, as in:
67+
68+
```bash
69+
2.7.6
70+
```
71+
72+
**_Simply stating a Ruby version in this file will require it to be installed, but won’t install it for you. Creating or updating this file doesn’t have to be done manually. The following steps will guide you on how to set it up._**
73+
74+
As an example, we’ll lock the Ruby version for an iOS repository, called `PeachTreeBank-iOS`. Assume there is no current version lock for Ruby, and there is only the system `version` installed.
75+
76+
You can check the versions available for install with:
77+
78+
```bash
79+
rbenv install --list
80+
```
81+
82+
For me, the output of this command is:
83+
84+
```bash
85+
2.6.10
86+
2.7.6
87+
3.0.4
88+
3.1.2
89+
jruby-9.3.6.0
90+
mruby-3.1.0
91+
picoruby-3.0.0
92+
rbx-5.0
93+
truffleruby-22.2.0
94+
truffleruby+graalvm-22.2.0
95+
96+
Only latest stable releases for each Ruby implementation are shown.
97+
Use 'rbenv install --list-all / -L' to show all local versions.
98+
```
99+
100+
**_Notice the message at the bottom. These are stable releases and we recommend you choose one of them. For a bigger list, containing previews and unstable versions, use --list-all._**
101+
102+
We can proceed to install version `2.7.6`:
103+
104+
```bash
105+
rbenv install 2.7.6
106+
```
107+
108+
**_Installing a Ruby version is only needed once. It is now available in your machine through rbenv, it will now appear when you run the command rbenv versions._**
109+
110+
Now, with multiple versions available, you can specify which version this local folder/repository should use. Inside the folder, in Terminal, run:
111+
112+
```bash
113+
rbenv local <VERSION>
114+
```
115+
116+
In this case:
117+
118+
```bash
119+
rbenv local 2.7.6
120+
```
121+
122+
This will create the file `.ruby-version`. This file should be committed, so that it enforces this version for everyone else, including your CI machine.
123+
124+
**_Later, if you want to change the version, make sure it’s installed with `rbenv` and run the `local` command again, i.e: `rbenv local 3.1.2`._**
125+
126+
### **2.** Using Ruby Gems.
127+
128+
You’ve managed to have multiple versions of Ruby and set different versions for different repositories. Now, we’re set to use Ruby Gems.
129+
130+
Ruby Gems is a package manager, responsible for distributing Ruby programs and libraries. Programs such as Cocoapods and Fastlane.
131+
132+
It’ll facilitate our work by not relying on the versions of these tools we’ve installed in our machines directly, but have Ruby Gems handle it for us. This comes with the same benefit from the section above, the ability to maintain specific versions of it for different repositories.
133+
134+
**2.1** Your repository already has a `Gemfile`.
135+
136+
If your repository already has a file named `Gemfile`, it already uses Ruby Gems. You can simply specify the Gems you want to use in this file.
137+
138+
**2.2.** Your repository doesn’t have a `Gemfile`.
139+
140+
Your repository doesn’t currently have a file named `Gemfile`. We’ll create one and proceed to install these gems.
141+
142+
It’s similar to a `Podfile`, where you’ll specify the dependencies and, optionally, their versions, with a certain constraint.
143+
144+
```bash
145+
source 'https://rubygems.org'
146+
147+
gem 'cocoapods', '1.11.3'
148+
gem 'cocoapods-art'
149+
gem 'fastlane'
150+
```
151+
152+
The following will install `cocoapods` version `1.11.3` and the latest version of `cocoapods-art` that is compatible with cocoapods.
153+
154+
Save your file and run:
155+
156+
```bash
157+
bundle install
158+
```
159+
160+
**_You might have troubles with this installation. If that happens, you can specify a local path for bundler to install with: `bundle config set --local path 'vendor/bundle'`
161+
Then proceed to run `bundle install` again._**
162+
163+
**_Once Ruby Gems are installed you’ll also have a `Gemfile.lock`, this file specifies the versions used during the latest installation/update and makes sure it locks to those versions for whoever is installing those dependencies, as long as they match the version constraints specified in the `Gemfile`._**
164+
165+
**_`Gemfile.lock` should also be committed._**
166+
167+
168+
#### **3.** Running your tools.
169+
170+
By now your project has a locked version of Ruby, which everyone can install without conflicting with other setups/projects. And you have most of the tools you need, such as Cocoapods and Fastlane, installed in a similar manner, as Ruby Gems.
171+
172+
You can run these tools, within the folder/repository of your project, with `bundle exec`.
173+
174+
Instead of running:
175+
176+
```bash
177+
pod install
178+
fastlane <YOUR-LANE>
179+
```
180+
181+
Run:
182+
183+
```bash
184+
bundle exec pod install
185+
bundle exec fastlane <YOUR-LANE>
186+
```
187+
188+
--
189+
190+
You can make this easier by creating an alias in your `~/.zshrc` or `~/.bashrc` file:
191+
192+
```bash
193+
alias bx='bundle exec'
194+
```
195+
196+
From now on, the commands above could be used as:
197+
198+
```bash
199+
bx pod install
200+
bx fastlane <YOUR-LANE>
201+
```
Loading

0 commit comments

Comments
 (0)