Module: AgapeRedRecipes::Gemfile

Included in:
RecipeGenerator
Defined in:
lib/agape-red-recipes/modules/gemfile.rb

Instance Method Summary collapse

Instance Method Details

#gem(*args) ⇒ Object



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
# File 'lib/agape-red-recipes/modules/gemfile.rb', line 25

def gem(*args)
  options = args.extract_options!
  name, version = args

  # Set the message to be shown in logs. Uses the git repo if one is given,
  # otherwise use name (version).
  parts, message = [ name.inspect ], name
  if version ||= options.delete(:version)
    parts   << version.inspect
    message << " (#{version})"
  end
  message = options[:git] if options[:git]

  log :gemfile, message

  options.each do |option, value|
    parts << "#{option}: #{value.inspect}"
  end

  @@installed_gem_names ||= Gem::Specification.map &:name
  return if @@installed_gem_names.include? name

  in_root do
    str = "gem#{parts.join(", ")}"
    str = "  " + str if @in_group
    str = "\n" + str

    if @current_group
      insert_at_end_of_line_containing 'Gemfile', str, "group #{@current_group} do"
    else
      append_file 'Gemfile', str, verbose: false
    end

  end
end

#gem_group(*names, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/agape-red-recipes/modules/gemfile.rb', line 11

def gem_group(*names, &block)
  name = names.map(&:inspect).join(", ")

  if gem_group_exists?(*names)
    @in_group = true
    @current_group = name
    instance_eval(&block)
    @current_group = nil
    @in_group = false
  else
    super(*names, &block)
  end
end

#gem_group_exists?(*names) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
6
7
8
9
# File 'lib/agape-red-recipes/modules/gemfile.rb', line 3

def gem_group_exists?(*names)
  name = names.map(&:inspect).join(", ")

  File.open("Gemfile") do |file|
    file.read.include? "\ngroup #{name} do"
  end
end

#insert_at_end_of_line_containing(file_name, insert, contents) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/agape-red-recipes/modules/gemfile.rb', line 61

def insert_at_end_of_line_containing(file_name, insert, contents)
  open(file_name, 'r+') do |file|

    while !file.eof?
      break if file.readline.include? contents
    end

    # Move back to the end of the previous line
    file.seek file.pos - 1

    position = file.pos
    rest_of_file = file.read
    file.seek position

    file.write insert
    file.write rest_of_file
  end
end