Class: Bun::Gemfile

Inherits:
Object
  • Object
show all
Defined in:
lib/bun/gemfile.rb

Constant Summary collapse

PATH =
"Gemfile"

Instance Method Summary collapse

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

#initObject



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

#with_group(group) ⇒ Object



32
33
34
35
36
# File 'lib/bun/gemfile.rb', line 32

def with_group(group)
  File.write(PATH, "\ngroup :#{group} do\n", mode: "a") if group
  yield
  File.write(PATH, "end\n", mode: "a") if group
end