Class: Release::Gem::Action::GemAction

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, opts = { }) ⇒ GemAction

Returns a new instance of GemAction.

Raises:



20
21
22
23
24
25
26
# File 'lib/release/gem/gem_action.rb', line 20

def initialize(root, opts = {  })
  raise GemActionError, "root is not given" if is_empty?(root)
  @root = root
  oopts = opts || {}
  @ui = oopts[:ui]
  @engine = oopts[:engine]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mtd, *args, &block) ⇒ Object (private)



396
397
398
399
400
401
402
# File 'lib/release/gem/gem_action.rb', line 396

def method_missing(mtd, *args, &block)
  if not @engine.nil? 
    @engine.send(mtd, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#uiObject

Returns the value of attribute ui.



18
19
20
# File 'lib/release/gem/gem_action.rb', line 18

def ui
  @ui
end

Instance Method Details

#build(*args, &block) ⇒ Object

Raises:



94
95
96
97
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/release/gem/gem_action.rb', line 94

def build(*args, &block)

  block.call(:action_start, :build) if block
  cp "Action 'build' starting at #{Gem.format_dt(Time.now)}"

  verfile = find_version_file(&block)
  curVer = find_gem_version(verfile) 
  possVer = possible_versions(curVer)

  cp  "Selection of version number. Current version : #{curVer}"
  if block
    @selVersion = block.call(:select_version, { current_version: curVer, proposed_next: possVer }) 

  else
    @selVersion = possVer[2] # increase last number
    cp "System selected version '#{@selVersion}' since no block is given"
  end

  raise GemActionError, "Version number cannot be blank" if is_empty?(@selVersion)

  cp  "Given version number : #{@selVersion}. Updating gem version file"
  update_gem_version(@selVersion) do |ops, *args|
    if block
      block.call(ops,*args)
    else
      # no block
      case ops
      when :multiple_version_files_found
        sel = args.first.first
        cp  "System selected version file '#{sel}' since no block is given"
        sel
      end
    end
  end

  cp "Gem version updated. Start building the gem"
  begin
  
    cmd = "cd #{@root} && rake build"
    Gem.logger.debug "Command to build gem : #{cmd}"  
    res = `#{cmd} 2>&1`
    block.call(:gem_building, $?, res) if block

    #Dir.chdir(@root)
    #Rake::Task["build"].execute

    if $?
      cp "Gem build successfully at #{Gem.format_dt(Time.now)}"

      register(:version_file_path, verfile)

      if block
        block.call(:gem_build_successfully, @selVersion)
      else
        [true, @selVersion]
      end
    else
      cp "Gem build failed"
      block.call(:gem_build_failed) if block
      [false, ""]
    end
  rescue Exception => ex
    cp "Gem build failed with message : #{ex.message}"
    block.call(:gem_build_failed, ex) if block
  end

end

#dependency_restoreObject



89
90
91
92
# File 'lib/release/gem/gem_action.rb', line 89

def dependency_restore
  #puts "restoring dependency"
  gemdepInst.restore_dev_gem 
end

#exec(&block) ⇒ Object



28
29
30
# File 'lib/release/gem/gem_action.rb', line 28

def exec(&block)
  instance_eval(&block) if block
end

#find_gem_version(file = nil, &block) ⇒ Object

have to resort to manual way since the version file has frozen_string_literal which makes the Gem::Specification always keep the version that it is first loaded



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/release/gem/gem_action.rb', line 318

def find_gem_version(file = nil, &block)

  if is_empty?(file)
    raise GemActionError, "Version file not given to extract version" if not block
    file = find_version_file(&block)
  end

  version = nil
  cont = File.read(file)
  if cont =~ /VERSION/ 
    val = $'
    if not_empty?(val)
      version = val.gsub("=","").gsub("\"","").gsub("end","").strip
    end
  end
  version

end

#install(*args, &block) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/release/gem/gem_action.rb', line 223

def install(*args, &block)
  opts = args.first || {}
  gemVer = ""
  if is_empty?(opts)
    if is_empty?(@selVersion)
      raise GemActionError, "No version info is available and no block given" if not block
      gemVer = block.call(:gem_version_to_install) 
      raise GemActionError, "No version info is available" if is_empty?(gemVer)
    else
      gemVer = @selVersion
    end

  else
    gemVer = opts[:version] 
  end

  cp "Given gem version '#{gemVer}' to install"
  target = Dir.glob(File.join(Dir.getwd,"**/*.gem")).select { |sf|
    sf =~ /\w*-#{gemVer}\w*/
  }

  if not_empty?(target)

    if target.length > 1
      if block
        st = {}
        target.each do |t|
          st[Pathname.new(t).relative_path_from(@root)] = t
        end
        selGemFile = block.call(:multiple_target_gems_found, st)
      else
        raise GemActionError, "Multiple target gem found. Please provide a block for gem selection"
      end
    else
      selGemFile = target.first
    end

    #res = TTY::Command.new.run!("gem install pkg/#{gemspec.name}-#{gemVer}.gem") do |out, err|
    res = TTY::Command.new.run!("gem install #{selGemFile}") do |out, err|
      cp out if not_empty?(out)
      ce err if not_empty?(err)
    end
    block.call(:gem_file_installed, Pathname.new(selGemFile).relative_path_from(@root))

  else
    if block
      block.call(:target_gem_not_found, gemVer)
    else
      raise GemActionError, "Target gem carry the version '#{gemVer}' not found"
    end
  end
  
  #res = TTY::Command.new.run!("gem install pkg/#{gemspec.name}-#{gemVer}.gem") do |out, err|
  #  cp out if not_empty?(out)
  #  ce err if not_empty?(err)
  #end

end

#push(*args, &block) ⇒ Object

build()

Raises:



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/release/gem/gem_action.rb', line 162

def push(*args, &block)

  opts = args.first || {}

  name = opts[:name]
  name = gemspec.name if is_empty?(name)
  version = opts[:version]
  version = @selVersion if is_empty?(version)

  raise GemActionError, "Version not given and no block" if not block and is_empty?(version)
  if is_empty?(version) and block
    version = block.call(:push_gem_version)
  end

  raise GemActionError, "No version given to push. Push aborted" if is_empty?(version)

  proceed = block.call(:confirm_proceed, name, version)
  if proceed

    cred = find_rubygems_api_key

    selAcct = cred.keys.first
    if cred.keys.length > 1
      Gem.logger.debug "Multiple rubygems account detected."
      # multiple account configured...
      if block
        selAcct = block.call(:multiple_rubygems_account, cred)
        raise GemActionError, "No rubygems account is selected." if is_empty?(selAcct)
      end
    end


    # find the package
    foundGem = Dir.glob(File.join(@root,"**/#{name}-#{version}*.gem"))
    if foundGem.length == 0
      raise GemActionError, "No built gem with version '#{version}' found."
    elsif foundGem.length > 1
      if block
        targetGem = block.call(:multiple_built_gems, foundGem)
      else
        raise GemActionError, "#{foundGem.length} versions of gem found. Please provide a block for selection"
      end
    else
      targetGem = foundGem.first
    end

    if cred.length > 1
      cmd = "cd #{@root} && gem push #{targetGem} -k #{selAcct}"
    else
      cmd = "cd #{@root} && gem push #{targetGem} "
    end
    Gem.logger.debug "Command to publish gem : #{cmd}"  
    res = `#{cmd} 2>&1`
    block.call(:gem_push_output, $?, res) if block
    [$?, res]

  else
    block.call(:gem_push_skipped, name, version)
  end
end

#release_dependencies(*args, &block) ⇒ Object



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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/release/gem/gem_action.rb', line 32

def release_dependencies(*args, &block)

  if gemdepInst.has_development_gem?

    if block

      block.call(:action_start, :release_dependencies)

      gemdepInst.development_gem.each do |k,v|
        res = gemdepInst.infect_gem(v, k, &block)
        if res != :skip
          block.call(:block_until_dev_gem_done, { name: k, path: v })
        end
      end
      

      #keys = gemdepInst.development_gem.keys
      keys = gemdepInst.development_gem
      loop do
        begin
          conf = block.call(:define_gem_prod_config, { gems: keys })
          if conf.is_a?(Hash)
            conf.each do |k,v|
              gemdepInst.configure_gem(k,v)
            end
            break if gemdepInst.all_dev_gems_has_config? 
            keys = gemdepInst.not_configured_gem
          else
            block.call(:invalid_gem_prod_config, "Expected return from :define_gem_prod_config is a hash of \"gem name\" => { type: [:runtime | :dev], version: \">= 1.2.0\" }. Note version can be empty")
          end
        rescue GemDepError => ex
          block.call(:invalid_gem_prod_config, ex.message)
        end
      end

      Gem.logger.debug "About to transfer gem"
      gemdepInst.transfer_gem
      Gem.logger.debug "GEM transfer!"

      Bundler.with_unbundled_env do
        Gem.logger.debug "Updating bundle!"
        # since there is a new version after release!
        puts `cd #{@root} && bundle update 2>&1`
      end

      block.call(:development_gem_temporary_promoted)
    end

  else
    if block
      block.call(:no_development_gems_found)
    end

  end

end

#update_gem_version(newVersion, version_file = nil, &block) ⇒ Object

Raises:



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/release/gem/gem_action.rb', line 283

def update_gem_version(newVersion, version_file = nil, &block)

  raise GemActionError, "block is required" if not block

  if is_empty?(version_file)
    selVerFile = find_version_file(&block)
    raise GemActionError, "Cannot find version file from #{@root}" if is_empty?(selVerFile)
  else
    selVerFile = version_file
  end

  tmpFile = File.join(File.dirname(selVerFile),"version-da.bak")
  FileUtils.mv(selVerFile,tmpFile)

  File.open(selVerFile,"w") do |f|
    File.open(tmpFile,"r").each_line do |l|
      if l =~ /VERSION/
        indx = (l =~ /=/)
        ll = "#{l[0..indx]} \"#{newVersion}\""
        f.puts ll
      else
        f.write l
      end
    end
  end

  FileUtils.rm(tmpFile)

  selVerFile

end