Class: Release::Gem::ReleaseInfector

Inherits:
Object
  • Object
show all
Includes:
TR::CondUtils, TR::TerminalUtils
Defined in:
lib/release/gem/release_infector.rb

Overview

Setup the release-gem into other gem project automatically!

Instance Method Summary collapse

Constructor Details

#initialize(root, name) ⇒ ReleaseInfector

Returns a new instance of ReleaseInfector.



15
16
17
18
19
# File 'lib/release/gem/release_infector.rb', line 15

def initialize(root, name)
  @root = root
  @name = name
  @backupFiles = {}
end

Instance Method Details

#activate_rakefile(&block) ⇒ Object

add_to_gemspec



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
# File 'lib/release/gem/release_infector.rb', line 138

def activate_rakefile(&block)

  rf = File.join(@root,"Rakefile")
  if not File.exist?(rf)
    rfCont <<-END
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rspec/core/rake_task"

require "release/gem"

RSpec::Core::RakeTask.new(:spec)

task default: :spec
    END
    block.call(:creating_new_rakefile, rakefile: rf ) if block
    File.open(rf,"w") do |f|
      f.write rfCont
    end
  else
    
    block.call(:adding_to_rakefile, rakefile: rf ) if block
   
    cont = File.read(rf)
    FileUtils.mv(rf, "#{rf}.bak")
    @backupFiles[rf] = "#{rf}.bak"

    File.open(rf,"w") do |f|
      f.puts cont
      f.puts "require 'release/gem'"
    end

    block.call(:rakefile_updated, name: @name, rakefile: rf) if block

  end
end

#add_to_gemspec(&block) ⇒ Object

is_release_gem_installed?



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
# File 'lib/release/gem/release_infector.rb', line 98

def add_to_gemspec(&block)
  

  gs = Dir.glob(File.join(@root,"*.gemspec"))
  raise ReleaseInfectorError, "gemspec not found at '#{@root}'" if is_empty?(gs)
  
  block.call(:adding_to_gemspec, gemspec: gs) if block

  if gs.length > 1
    if block
      gs = block.call(:multiple_gemspecs, gs)
    else
      raise ReleaseInfectorError, "There are more than 1 gemspec file found (#{gs.length} found). Since no block to filter out the required one, this will be an error condition"
    end

  else
    gs = gs.first
  end

  cont = File.read(gs) 

  if (cont =~ /spec.add_development_dependency 'release-gem'/) == nil
    lastEnd = cont.rindex("end")

    FileUtils.mv(gs, "#{gs}.bak")
    @backupFiles[gs] = "#{gs}.bak"

    File.open(gs, "w") do |f|
      f.write cont[0...lastEnd]
      f.puts "  spec.add_development_dependency 'release-gem', \"~> #{Release::Gem::VERSION}\" "
      f.puts "end"
    end
    block.call(:gemspec_updated, name: @name, gemspec: gs ) if block

  else 
    block.call(:gemspec_already_has_gem, name: @name, gemspec: gs) if block
  end
  
end

#infect(&block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/release/gem/release_infector.rb', line 21

def infect(&block)
  if not is_release_gem_installed?
    add_to_gemspec(&block)
    #Bundler.with_clean_env do
    # Or the warning suggest Bundler.with_original_env
    # not sure which one is correct
    Bundler.with_unbundled_env do
      res = `cd #{@root} && bundle update 2>&1`
      puts res
    end
  end

  if not is_rakefile_activated?
    activate_rakefile(&block)
  end
end

#is_rakefile_activated?Boolean

Returns:

  • (Boolean)


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/release/gem/release_infector.rb', line 176

def is_rakefile_activated?
  rf = File.join(@root,"Rakefile")
  if not File.exist?(rf)
    false
  else
    cont = File.read(rf)
    found = false
    cont.each_line do |l|
      if l =~ /require ('|")(release\/gem)("|')/
        found = true
        break
      end
    end
    found
  end
end

#is_release_gem_installed?Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/release/gem/release_infector.rb', line 73

def is_release_gem_installed?
  #Bundler.with_clean_env do
  Bundler.with_unbundled_env do

    res = `cd #{@root} && bundle 2>&1`

    puts res

    if $?.success?
      found = false
      res.each_line do |l|
        if l =~ / release-gem /
          found = true
          break
        end
      end

      found
    else
      raise ReleaseInfectorError, "Error running bundle in '#{@root}'. Error was :\n#{res}"
    end

  end
end

#trigger_release_gem(&block) ⇒ Object



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
# File 'lib/release/gem/release_infector.rb', line 38

def trigger_release_gem(&block)

  block.call(:triggering_release_gem) if block

  poss = tu_possible_terminal
  raise ReleaseInfectorError, "No possible terminal found" if is_empty?(poss)

  terminal = ""
  #Bundler.with_clean_env do
  Bundler.with_unbundled_env do

    cmd = "cd #{@root} && bundle update 2>&1 && rake gem:release" 
    if block
      terminal = block.call(:select_terminal, name: @name, options: poss) if block
      if terminal != :skip
        terminal = poss.first if is_empty?(terminal)

        block.call(:new_terminal_launching, name: @name, terminal: terminal) if block
        tu_new_terminal(terminal, cmd)
      end

    else
      terminal = poss.first
      block.call(:new_terminal_launching, name: @name, terminal: terminal) if block
      tu_new_terminal(terminal, cmd)
    end

    block.call(:new_terminal_launched, name: @name, terminal: terminal) if block

  end

  terminal

end