-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrails_gem_bundle_notes.txt
400 lines (243 loc) · 9.86 KB
/
rails_gem_bundle_notes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
Gem
========================================================================
# https://guides.rubygems.org/make-your-own-gem/
My First Gem
------------------------------------------------------------------------
> mkdir hola
> cd hola
> tree
.
├── hola.gemspec
└── lib
└── hola.rb
hola.rb:
class Hola
def self.hi
puts "Hello world!"
end
end
hola.gemspec:
Gem::Specification.new do |s|
s.name = 'hola'
s.version = '0.0.0'
s.summary = "Hola!"
s.description = "A simple hello world gem"
s.authors = ["Nick Quaranto"]
s.email = '[email protected]'
s.files = ["lib/hola.rb"]
s.homepage = 'https://rubygems.org/gems/hola'
s.license = 'MIT'
s.default_executable = "hola"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.date = %q{2010-04-03}
s.test_files = ["test/test_hola.rb"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.6.2}
if s.respond_to? :specification_version then
s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
else
end
else
end
end
> gem build hola.gemspec
Successfully built RubyGem
Name: hola
Version: 0.0.0
File: hola-0.0.0.gem
> gem install ./hola-0.0.0.gem
> irb
> require 'hola'
> Hola.hi
- Publish your gem
------------------------------------------------------------------------
- Requiring more files
------------------------------------------------------------------------
% tree
.
├── hola.gemspec
└── lib
├── hola
│ └── translator.rb
└── hola.rb
> vim lib/hola.rb
class Hola
def self.hi(language = "english")
translator = Translator.new(language)
translator.hi
end
end
require 'hola/translator'
> vim lib/hola/translator.rb
class Hola::Translator
def initialize(language)
@language = language
end
def hi
case @language
when "spanish"
"hola mundo"
else
"hello world"
end
end
end
> vim hola.gemspec
s.files = ["lib/hola.rb", "lib/hola/translator.rb"]
> irb -Ilib -rhola # -I : $LOAD_PATH
> Hola.hi("english")
- Adding an executable
------------------------------------------------------------------------
% mkdir bin
% touch bin/hola
% chmod a+x bin/hola
bin/hola:
#!/usr/bin/env ruby
require 'hola'
puts Hola.hi(ARGV[0])
> ruby -Ilib ./bin/hola
hello world
- Finally, to get Hola’s executable included when you push the gem, you’ll need to add it in the gemspec.
s.version = '0.0.1'
s.executables << 'hola'
- Adding test
------------------------------------------------------------------------
- Using Minitest
> vim Rakefile
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << 'test'
end
desc "Run tests"
task :default => :test
> mkdir test
> vim test/test_hola.rb
require 'minitest/autorun'
require 'hola'
class HolaTest < Minitest::Test
def test_hello
assert_equal "Hello world!", Hola.hi
end
end
> rake test
Run options: --seed 37872
# Running:
.
Finished in 0.003306s, 302.4847 runs/s, 302.4847 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
- Documentation
# https://yardoc.org/
- By default most gems use RDoc to generate docs.
# The main Hola driver
class Hola
# Say hi to the world!
#
# Example:
# >> Hola.hi("spanish")
# => hola mundo
#
# Arguments:
# language: (String)
def self.hi(language = "english")
translator = Translator.new(language)
puts translator.hi
end
end
- Add external dependency
s.add_dependency "activesupport"
Bundler
========================================================================
Bundler:
- Bundler is a dependency management tool available as a gem.
> gem install bundler
- specify your dependencies in a Gemfile\
source 'https://rubygems.org'
gem 'nokogiri'
gem 'rack', '~> 2.0.1'
gem 'rspec'
> bundle install
> git add Gemfile Gemfile.lock
- You can install it through RubyGems which comes built-in from Ruby 1.9 with gem install bundler.
- Bundler will read the Gemfile for the list of gems need to be installed, fetches metadata from the source provided, resolves the dependencies of each gem, installs them and requires them while booting.
- Without bundler, we need to handle the installation and manage the dependencies manually.
Gemfile:
- Gemfile is a ruby file.
- You can specify the gem dependencies of the project in Gemfile.
- During the gem installation and while auto requiring the gems, bundler will search for the Gemfile under the directory mentioned by the environment variable BUNDLE_GEMFILE or at the root of the project directory.
- Bundler provides a utility called bundle init which creates a simple Gemfile on the current working directory with default source as RubyGems.
- By default a new rails application ships with Gemfile along with the default gems.
Gemfile.lock:
- Bundler will create a file called Gemfile.lock with the list of the gems installed along with versions of the installed gems.
- Next time when you run bundle install, bundler will read the Gemfile.lock and install the exact same versions of the gems listed on Gemfile.lock.
- This makes sure that the application runs with the same version of the gems in all the environments.
Groups:
- By default, bundler will install the gems listed in all the groups.
- You can override it by passing the option without.
- It accepts the list of groups to exclude from the installation.
- Please note that bundler will only skip the installation of the gems.
- It will still download the gems to exactly resolve the dependencies of the gems listed in the Gemfile.
group :development do
gem 'spring'
end
gem 'rails', '5.2.3'
- For example, bundle install –without test will exclude installing the gems from the test group.
- It will create a file called config under the directory root of the app/.bundle to remember the option.
- The next time when you bundle install without the without option, bundler will still remember the option from the config file.
.bundle/config
---
BUNDLE_WITHOUT: "test"
Versioning gems:
Patch – 0.0.X – Includes bug fixes, All the features worked on v0.0.1 will also work on v0.0.2
Minor – 0.X.0 – Includes additional features, All the features worked on v0.1.0 will also work on v0.2.0
Major – X.0.0 – Includes breaking changes, Some of the features worked on v1.0.0 may break on version v2.0.0
gem ‘factory_bot_rails‘, ‘>= 4.0‘ – Specifies any of the gems greater than or equal to version 4.0 can be installed.
gem ‘rails‘, ‘~> 4.2.0‘ allows installation of rails gem with version ‘>= 4.2.0‘ and ‘< 4.3.x‘.
require:
- The require method allows the file to be executed only once.
- It works with the help of global variables $LOAD_PATH(simply $:) and $LOADED_FEATURES.
$LOAD_PATH => ["/home/isalem/.rvm/rubies/ruby-2.6.5/lib/ruby/gems/2.6.0/gems/did_you_mean-1.3.0/lib", "/home/isalem/.rvm/rubies/ruby-2.6.5/lib/ruby/site_ruby/2.6.0", "/home/isalem/.rvm/rubies/ruby-2.6.5/lib/ruby/site_ruby/2.6.0/x86_64-linux", "/home/isalem/.rvm/rubies/ruby-2.6.5/lib/ruby/site_ruby", "/home/isalem/.rvm/rubies/ruby-2.6.5/lib/ruby/vendor_ruby/2.6.0", "/home/isalem/.rvm/rubies/ruby-2.6.5/lib/ruby/vendor_ruby/2.6.0/x86_64-linux", "/home/isalem/.rvm/rubies/ruby-2.6.5/lib/ruby/vendor_ruby", "/home/isalem/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0", "/home/isalem/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/x86_64-linux"]
- You can always push a new folder for searching
$LOAD_PATH << '/Users/gomathi/Documents/Projects/Project/TestApp/'
$LOADED_FEATURES => ["enumerator.so", "thread.rb", "rational.so", "complex.so" ....
test.rb:
p 'Hello World'
> irb
> require './test'
"hello world"
=> true
> $LOADED_FEATURES
$LOADED_FEATURES => ["enumerator.so", ........ , "/home/isalem/Projects/bookshelf/test.rb"]
- Bundler uses the same process for requiring the gems.
- During the initialisation process of Rails app bundler loads boot.rb and application.rb.
boot.rb:
require 'bundler/setup' # adds the paths provided on require_paths option of the gemspec(which defaults to lib directory) of gems listed in the Gemfile to $LOAD_PATH.
# Hence the gem files can be required without the need for specifying the absolute path of each file.
#
application.rb:
Bundler.require(:default, Rails.env)
- The next step Bundler.require accepts an array of groups for auto requiring.
It then requires the gems along with their dependencies on the specified groups using ruby’s built-in require method.
It eliminates the need for manually requiring the gems.
What does require: false do?
gem ‘rubocop’, require: false
The require: false after the gem name instructs bundler to not to auto require the gem. Please note that the bundler will still add the gem to $LOAD_PATH. You can manually require the gem whenever required using require ‘gem_name’.
Create a gem via bunlder
--------------------------------------------------------------------------------
# https://bundler.io/v2.3/guides/creating_gem.html
> bundle gem foodie
Stuff
------------------------------------------------------------------------
rails:freeze:gems (don't update gems)
bundle package # This locks and then caches the gems into ./vendor/cache.
When installing the system first time ..
> bundler install
Ops
> gem install bundler --default -v "2.1.2"
* to upgrade to bundler 2
https://bundler.io/guides/bundler_2_upgrade.html
> bundle exec rails db:migrate RAILS_ENV=production
- Now, To Check the code of any gem,
> bundle open jquery-rails
To open a bundled gem, set $EDITOR or $BUNDLER_EDITOR
> bundle exec rake routes