Class: FixMahGemfile::Processor

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Processor

Returns a new instance of Processor.



9
10
11
12
13
14
# File 'lib/fix_mah_gemfile.rb', line 9

def initialize &block
  @gemfile      = File.readlines("Gemfile")
  @gemfile_orig = @gemfile.dup
  instance_eval &block if block_given?
  write_to_file
end

Instance Attribute Details

#gemfileObject

Returns the value of attribute gemfile.



7
8
9
# File 'lib/fix_mah_gemfile.rb', line 7

def gemfile
  @gemfile
end

Class Method Details

.generate_sample_rcObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/fix_mah_gemfile.rb', line 84

def self.generate_sample_rc
  outfile = "./fixgemfile_rc"
  file = <<-END
# example .fixgemfile_rc
FixMahGemfile::Processor.new do
  remove_gem 'looksee'
  change_gem_version 'libxml-ruby', :to => '~> 2.7'
  #add_gem 'therubyracer', "'~> 0.12', :require=>false", :above_gem => "guard"
  remove_gem 'therubyracer'
  remove_gem 'guard-less'
end

FixMahGemfile::Processor.run "bundle"
#FixMahGemfile::Processor.run "bundle update libxml-ruby"
#FixMahGemfile::Processor.run "bundle update guard guard-less therubyracer"
END
end

.run(str) ⇒ Object



101
102
103
104
# File 'lib/fix_mah_gemfile.rb', line 101

def self.run str
  puts str
  system str
end

.usageObject



76
77
78
79
80
81
82
83
# File 'lib/fix_mah_gemfile.rb', line 76

def self.usage
  puts "Usage: #{Pathname.new($0).basename} [--help] [--generate_rc] [path/to/rc_file]"
  puts "   --help          : this help"
  puts "   --generate_rc   : creates a sample .fixgemfile_rc in the current directory"
  puts "   path/to/rc_file : [optional]\n"
  puts "This utility will look for a .fixgemfile_rc which consists of directives to modify the Gemfile"
  puts "in the current directory.  An option argument can specify the path to an alternate rc file.\n\n"
end

Instance Method Details

#add_gem(gemname, *args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fix_mah_gemfile.rb', line 19

def add_gem gemname, *args
  log "adding #{gemname} #{args.inspect}... "
  where = args[1].keys.first
  puts "where #{where} #{args[1][where]}"
  newline = ([" gem '#{gemname}'", args[0]].join(", ")) << "\n"
  case where.to_s
  when 'above_gem'
    linenum = gem_at_line(args[1][where])
    gemfile.insert(linenum, newline)
  when 'below_gem'
    linenum = gem_at_line(args[1][where])
  else
    puts "don't know how to handle directive #{where}"
    exit 1
  end
end

#change_gem_version(*args) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fix_mah_gemfile.rb', line 46

def change_gem_version *args
  log "changing gem: args #{args.inspect}... "
  gemname = args[0]
  options = args[1]
  if linenum = gem_at_line(gemname)
    line = gemfile[linenum]
    lineparts = line.split(',')
    if lineparts[1] # replace
      lineparts[1] = " '#{options[:to]}'"
    else # append
      lineparts << " '#{options[:to]}'"
    end
    gemfile[linenum] = lineparts.join(",")
  else
    puts "cant find gem to modify"
  end
end

#gem_at_line(gemname) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/fix_mah_gemfile.rb', line 63

def gem_at_line gemname
  found = gemfile.index { |line| line =~ /^\s*gem ['|"]#{gemname}/ }
  puts "found at #{found}"
  if found
    puts gemfile[found]
  end
  found
end

#log(str, color_code = 32) ⇒ Object



15
16
17
18
# File 'lib/fix_mah_gemfile.rb', line 15

def log str, color_code=32
  # green = 32 , red 31, yellow = 33
  print "\e[#{color_code}m#{str}\e[0m"
end

#remove_gem(gemname) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/fix_mah_gemfile.rb', line 36

def remove_gem gemname
  log "removing #{gemname}... "
  if linenum = gem_at_line(gemname)
    line = gemfile[linenum]
    line = "##{line}"
    gemfile[linenum] = line
  else
    puts "Not found"
  end
end

#write_to_file(filename = 'Gemfile') ⇒ Object



71
72
73
74
75
# File 'lib/fix_mah_gemfile.rb', line 71

def write_to_file filename = 'Gemfile'
  # write out original to backup, overwrite Gemfile with modified version
  File.open("Gemfile.orig",'w') { |f| f.write(@gemfile_orig.join("")) }
  File.open("Gemfile",'w') { |f| f.write(@gemfile.join("")) }
end