Ruby bundler: Difference between revisions

From wikinotes
 
No edit summary
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
Commandline Tool to manage a set of [[ruby gems|ruby gem]] requirements. <br>
Commandline Tool to manage a set of [[ruby gems|ruby gem]] requirements using a [[ruby gemfile]]. <br>
This is similar to python-pip's <code>requirements.txt</code> file.
This is similar to python-pip's <code>requirements.txt</code> file.


Line 38: Line 38:
== Usage ==
== Usage ==
<blockquote>
<blockquote>
<source lang="bash">
=== Create Project ===
<blockquote>
<syntaxhighlight lang="bash">
# generate gemfile using 'rubygems.org' as source
# generate gemfile using 'rubygems.org' as source
bundle init
bundle init
bundle update --bundler     # update bundler version in Gemfile.lock
bundle update --bundler                   # update bundler version in Gemfile.lock
 
bundle config set --local path '~/.bundle'  # use user bundle path
</syntaxhighlight>
</blockquote><!-- Create Project -->


=== Project Management ===
<blockquote>
<source lang="bash">
# install all gemfile requirements
# install all gemfile requirements
bundle list                  # list installed
bundle install
bundle install
bundle install --path .gems  # install to {cwd}/.gems (and remember for future bundle commands)
bundle install --path .gems  # install to {cwd}/.gems (and remember for future bundle commands)
Line 52: Line 60:
# install gemfile requirements for specific groups 'cucumber', 'development'
# install gemfile requirements for specific groups 'cucumber', 'development'
bundle install --without cucumber development
bundle install --without cucumber development
</source>
</blockquote><!-- Project Management -->
== Gem Build Arguments ==
<blockquote>
You can configure compile-time arguments for use when a gem with C-extensions is built.<br>
This can be stored in your project-local, or global bundle config.
<syntaxhighlight lang="bash">
# writes ${CWD}/.bundle/config
bundle config --local build.ffi --with-cflags="-Wno-error=implicit-function-declaration" --enable-system-libffi


# configuring gem build params (for C extensions etc.
# writes ~/.bundle/config
# bundle config --global build.{yourpackage} [your gem build params]
bundle config --global build.snappy --with-opt-dir="/usr/local/opt/snappy"
bundle config --global build.snappy --with-opt-dir="/usr/local/opt/snappy"
</source>
</syntaxhighlight>
 
</blockquote><!-- Gem Build Arguments -->
</blockquote><!-- usage -->


== Updating Gems ==
== Updating Gems ==
Line 75: Line 92:
Note that if you are writing a gem for rubygems, requirements should be defined in the gemspec file.
Note that if you are writing a gem for rubygems, requirements should be defined in the gemspec file.


</blockquote><!-- components -->
</blockquote><!-- gemfiles -->
 
== Personal/Dev Gemfiles ==
<blockquote>
If using an LSP, your work Gemfile may not have all of the requirements that you need.<br>
You can work around this by defining an alternate Gemfile that sources the real one and adds requirements.
 
<syntaxhighlight lang="ruby">
# ${PROJECT}/Gemfile
 
source "https://rubygems.org"
gem "pry"
</syntaxhighlight>
 
<syntaxhighlight lang="ruby">
# ${PROJECT}/Gemfile.pers
 
gemfile = File.realpath("Gemfile")
eval(File.read(gemfile), nil, "Gemfile.pers")
gem "solargraph"
gem "solargraph-rails"
</syntaxhighlight>
 
Set Gemfile for bundle install, and applications you'd like to use this Gemfile for.
<syntaxhighlight lang="bash">
# install gems
BUNDLE_GEMFILE=Gemfile.pers bundle install  # (may need to bundle update)
 
# runs nvim, instructing it to use the alternate gemfile
BUNDLE_GEMFILE=Gemfile.pers nvim
</syntaxhighlight>
 
You probably want to git ignore this file so it is never committed.
<syntaxhighlight lang="bash">
# ~/.gitignore
**/Gemfile.pers
**/Gemfile.pers.lock
</syntaxhighlight>
</blockquote><!-- Personal/Dev Gemfiles -->
</blockquote><!-- usage -->
</blockquote><!-- consuming gems -->
</blockquote><!-- consuming gems -->


= Publishing Gems =
= Publishing Gems =
<blockquote>
<blockquote>
== Gemspec File ==
Create a [[ruby gemspec]] file, build a gem, and push it to rubygems.org
Your gemspec file defines requirements, version, author etc. <br>
A gemspec file is required to publish to rubygems.<br>
You can reference your gemspec requirements within a Gemfile by calling the function <code>gemspec</code>.
 
See https://guides.rubygems.org/specification-reference/


== Build/Release ==
== Build/Release ==

Latest revision as of 19:51, 4 November 2023

Commandline Tool to manage a set of ruby gem requirements using a ruby gemfile.
This is similar to python-pip's requirements.txt file.

Documentation

bundler quickstart https://bundler.io/
bundler config options https://bundler.io/v2.2/man/bundle-config.1.html#LIST-OF-AVAILABLE-KEYS
bundler creating gems https://bundler.io/v2.0/guides/creating_gem.html
rubygems creatin gems https://guides.rubygems.org/make-your-own-gem/

Locations

${project}/.bundle/config bundle config (Remembers things like gem path)
~/.bundle/config<?code> global config

Install

gem install bundler

Consuming Gems

Usage

Create Project

# generate gemfile using 'rubygems.org' as source
bundle init
bundle update --bundler                    # update bundler version in Gemfile.lock
bundle config set --local path '~/.bundle'  # use user bundle path

Project Management

# install all gemfile requirements
bundle list                  # list installed
bundle install
bundle install --path .gems  # install to {cwd}/.gems (and remember for future bundle commands)

bundle lock --add-platform x86_64-linux

# install gemfile requirements for specific groups 'cucumber', 'development'
bundle install --without cucumber development

Gem Build Arguments

You can configure compile-time arguments for use when a gem with C-extensions is built.
This can be stored in your project-local, or global bundle config.

# writes ${CWD}/.bundle/config
bundle config --local build.ffi --with-cflags="-Wno-error=implicit-function-declaration" --enable-system-libffi

# writes ~/.bundle/config
bundle config --global build.snappy --with-opt-dir="/usr/local/opt/snappy"

Updating Gems

bundle update rails                   # update rails to latest only
bundle update --conservative sidekiq  # update this gem only, ignoring shared dependencies
bundle lock --update=foo              # update 'foo' only to latest in Gemfile.lock
bundle update                         # try to update everything

Gemfiles

See ruby gemfile (outlines gem requirements for project).
Note that if you are writing a gem for rubygems, requirements should be defined in the gemspec file.

Personal/Dev Gemfiles

If using an LSP, your work Gemfile may not have all of the requirements that you need.
You can work around this by defining an alternate Gemfile that sources the real one and adds requirements.

# ${PROJECT}/Gemfile

source "https://rubygems.org"
gem "pry"
# ${PROJECT}/Gemfile.pers

gemfile = File.realpath("Gemfile")
eval(File.read(gemfile), nil, "Gemfile.pers")
gem "solargraph"
gem "solargraph-rails"

Set Gemfile for bundle install, and applications you'd like to use this Gemfile for.

# install gems
BUNDLE_GEMFILE=Gemfile.pers bundle install  # (may need to bundle update)

# runs nvim, instructing it to use the alternate gemfile
BUNDLE_GEMFILE=Gemfile.pers nvim

You probably want to git ignore this file so it is never committed.

# ~/.gitignore
**/Gemfile.pers
**/Gemfile.pers.lock

Publishing Gems

Create a ruby gemspec file, build a gem, and push it to rubygems.org

Build/Release

gem build foo.gemspec
gem push foo-0.0.0.gem