Module: DevopsAssist::Gem

Includes:
Gemspec, TR::CondUtils
Included in:
VersionManager
Defined in:
lib/devops_assist/gem/gem.rb,
lib/devops_assist/gem/gemspec.rb

Defined Under Namespace

Modules: Gemspec Classes: GemError

Instance Method Summary collapse

Methods included from Gemspec

#find_gem_name

Instance Method Details

#find_gem_version_file(root) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/devops_assist/gem/gem.rb', line 49

def find_gem_version_file(root)
  if is_empty?(root)
     raise GemError, "Root path '#{root}' to find_gem_version_file is empty"
  else
    Dir.glob(File.join(root,"**/version.rb"))
  end
end

#publish_gem(version, opts = { }, &block) ⇒ Object



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
88
89
90
91
# File 'lib/devops_assist/gem/gem.rb', line 58

def publish_gem(version, opts = { }, &block)

  cred = find_rubygems_api_key

  selAcct = cred.keys.first
  if cred.keys.length > 1
    logger.tdebug :pubgem, "Multiple rubygems account detected."
    # multiple account configured...
    selAcct = block.call(:multiple_rubygems_account, cred)
    return :skipped if selAcct == :skip
    raise GemError, "No rubygems account is selected." if is_empty?(selAcct)
  end

  # find the package
  root = opts[:root] || Dir.getwd
  foundGem = Dir.glob("**/*-#{version}*.gem")
  if foundGem.length == 0
    raise GemError, "No built gem found."
  elsif foundGem.length > 1
    if block
      targetGem = block.call(:multiple_built_gems, foundGem)
    else
      raise GemError, "Multiple versions of gem found : #{foundGem}. Please provide a block for selection"
    end
  else
    targetGem = foundGem.first
  end

  cmd = "cd #{root} && gem push #{targetGem} -k #{selAcct}"
  logger.tdebug :pubgem, "Command to publish gem : #{cmd}"  
  res = `#{cmd}`
  [$?, targetGem, res]

end

#publish_gem_file(gemfile, opts = { }, &block) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/devops_assist/gem/gem.rb', line 93

def publish_gem_file(gemfile, opts = { }, &block)

  cred = find_rubygems_api_key

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

  if File.exist?(gemfile)
    root = File.dirname(gemfile)
    targetGem = File.basename(gemfile)

    cmd = "cd #{root} && gem push #{targetGem} -k #{selAcct}"
    logger.tdebug :pubgemfile, "Command to publish gem : #{cmd}"  
    res = `#{cmd}`
    [$?, res, targetGem]
  else
    raise GemError, "Given Gemfile '#{gemfile}' not found"
  end

end

#update_gem_version(root, newVersion, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/devops_assist/gem/gem.rb', line 13

def update_gem_version(root, newVersion, &block)

  version_file = find_gem_version_file(root)

  if version_file.length > 1
    if block
      selVerFile = block.call(:select_version_file, version_file)
      raise GemError, "Multiple version files found (#{version_file.join(", ")}) and user not selected any" if is_empty?(selVerFile)
    else
      raise GemError, "Multiple version files found (#{version_file.join(", ")}). Please provide a block to select version file or make sure there is no other file named version.rb"
    end
  else
    selVerFile = version_file.first
  end

  tmpFile = File.join(File.dirname(selVerFile),"version.rb.tmp")
  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