Class: Bun::Gemfile
- Inherits:
-
Object
- Object
- Bun::Gemfile
- Defined in:
- lib/bun/gemfile.rb
Constant Summary collapse
- PATH =
"Gemfile"
Instance Method Summary collapse
- #add(gem, version_string, group) ⇒ Object
- #init ⇒ Object
- #remove(gem) ⇒ Object
- #verify_unique!(gem) ⇒ Object
- #with_group(group) ⇒ Object
Instance Method Details
#add(gem, version_string, group) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/bun/gemfile.rb', line 13 def add(gem, version_string, group) file = File.read(PATH) if file =~ /gem "#{gem}"/ raise ::Bun::Errors::DuplicateGemError.new("Aborting. Gem already present in the Gemfile: #{gem}") end if file =~ /group :#{group} do/ # appends gem to the end of the group block file.gsub!(%r{(group :#{group} do\n.*?)(end)}m, "\\1 gem \"#{gem}\", \"#{version_string}\"\n\\2") File.write(PATH, file, mode: "w") else with_group(group) do File.write(PATH, "#{" " if group }gem \"#{gem}\", \"#{version_string}\"\n", mode: "a") end end end |
#init ⇒ Object
7 8 9 10 11 |
# File 'lib/bun/gemfile.rb', line 7 def init return if File.file?(PATH) File.write(PATH, %Q{source "https://rubygems.org"\n\n}, mode: "w") end |
#remove(gem) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/bun/gemfile.rb', line 38 def remove(gem) output = Tempfile.new(PATH) File.foreach(PATH) do |line| if line !~ /gem "#{gem}"/ output.write(line) end end FileUtils.mv(output.path, PATH) end |
#verify_unique!(gem) ⇒ Object
50 51 52 53 54 |
# File 'lib/bun/gemfile.rb', line 50 def verify_unique!(gem) if File.read(PATH) =~ /gem "#{gem}"/ raise ::Bun::Errors::DuplicateGemError.new("Aborting. Gem already present in the Gemfile: #{gem}") end end |