Let Gemfile be machine-writable

I really love Bundler that it lets us write Ruby for its configuration. We don’t have to learn new language — you should writing Ruby when you are using Bundler! If you need, you can write any valid Ruby code in the configuration.

However, it also involves a difficulty. It is not very easy to develop some program which writes Gemfile. Parsing Gemfile is easy. We today have several gems to parse Ruby program. We can use one of them to understand what is happening in our Gemfiles. Or, monkey patching may be a good idea to inspect things like which gems Bundler will install from the Gemfile.

Okay, but how about writing Gemfile? It’s a Ruby program. As of 2017, we don’t have any good option for rewriting Ruby code programmatically. (We can off course use Ruby or some other text based tools. But I don’t like that.)

When do we need to do that? Bundler provides a good set of commands. The separation of Gemfile and Gemfile.lock ensures maximum flexibility. We rarely need to edit Gemfiles!

One example is using private gems. When you want to share some piece of code as a gem in your company, but don’t want to share it with others, you have several options. The best one should be running your private gem server. Does it sound too much hassle? I agree. The easiest one should be using Bundler’s git: syntax.

Put your gem in Git repository and let Bundler download the source code from the repository. Keep the repository private. Nothing goes wrong. Except updating the gem.

You would specify the version of the gem by git tags. Something like the following:

gem "foobar", git: "git@github.com:mycompany/private_library.git", tag: "v0.4.1"

Unfortunately, you cannot use `bundle update` command to update the library, because it’s not under the control of gem mechanism. And every time you release a new version, you have to edit Gemfiles of all your dependent programs. I don’t think it makes you feel good.

So, this is the problem what my small gem, called Bundly, will solve.

With that tool, you can write your dependency as YAML file. As you know, reading and writing YAML file using programs is almost trivial. And in fact, Bundly ships with a built-in command to update YAML file.

Upgrading gem version in tens of dependent programs? Just a few lines of script would do for you now.

$ for dir in `ls -d`
cd $dir
git checkout -b update_library
bundly gem private_library tag=v0.4.2
bundle install
git add -u
git commit -m "update library"
git push -u origin update_library
cd -

The bundly gem private_library tag=v0.4.2 in the shell script do the magic. It changes YAML file as you told. No editors required now.