Class: CapsuleCD::Ruby::RubyEngine

Inherits:
Engine
  • Object
show all
Defined in:
lib/capsulecd/ruby/ruby_engine.rb

Instance Attribute Summary

Attributes inherited from Engine

#config

Instance Method Summary collapse

Methods inherited from Engine

#initialize, #post_build_step, #post_package_step, #post_release_step, #post_runner_retrieve_payload, #post_source_configure, #post_source_process_pull_request_payload, #post_source_process_push_payload, #post_source_release, #post_test_step, #pre_build_step, #pre_package_step, #pre_release_step, #pre_runner_retrieve_payload, #pre_source_configure, #pre_source_process_pull_request_payload, #pre_source_process_push_payload, #pre_source_release, #pre_test_step, #start

Constructor Details

This class inherits a constructor from CapsuleCD::Engine

Instance Method Details

#build_stepObject



10
11
12
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/capsulecd/ruby/ruby_engine.rb', line 10

def build_step
  super
  gemspec_path = CapsuleCD::Ruby::RubyHelper.get_gemspec_path(@source_git_local_path)
  gem_name = CapsuleCD::Ruby::RubyHelper.get_gem_name(@source_git_local_path)

  # bump up the version here.
  # since there's no standardized way to bump up the version in the *.gemspec file, we're going to assume that the version
  # is specified in a version file in the lib/<gem_name>/ directory, similar to how the bundler gem does it.
  # ie. bundle gem <gem_name> will create a file: my_gem/lib/my_gem/version.rb with the following contents
  # module MyGem
  #   VERSION = "0.1.0"
  # end
  #
  # Jeweler and Hoe both do something similar.
  # http://yehudakatz.com/2010/04/02/using-gemspecs-as-intended/
  # http://timelessrepo.com/making-ruby-gems
  # http://guides.rubygems.org/make-your-own-gem/

  gem_version = CapsuleCD::Ruby::RubyHelper.get_version(@source_git_local_path)
  next_version = bump_version(SemVer.parse(gem_version))
  CapsuleCD::Ruby::RubyHelper.set_version(@source_git_local_path, next_version.to_s)

  # check for/create any required missing folders/files
  unless File.exist?(@source_git_local_path + '/Gemfile')
    File.open(@source_git_local_path + '/Gemfile', 'w') { |file|
      file.puts("source 'https://rubygems.org'")
      file.puts('gemspec')
    }
  end
  unless File.exist?(@source_git_local_path + '/Rakefile')
    File.open(@source_git_local_path + '/Rakefile', 'w') { |file| file.write('task :default => :spec') }
  end
  unless File.exist?(@source_git_local_path + '/spec')
    FileUtils.mkdir(@source_git_local_path + '/spec')
  end
  unless File.exist?(@source_git_local_path + '/.gitignore')
    CapsuleCD::GitUtils.create_gitignore(@source_git_local_path, ['Ruby'])
  end

  # package the gem, make sure it builds correctly
  Bundler.with_clean_env do
    Open3.popen3('gem build '+ File.basename(gemspec_path), chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
      { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
        Thread.new do
          until (line = stream_buffer.gets).nil?
            puts "#{name} -> #{line}"
          end
        end
      end
      # wait for process
      external.join
      unless external.value.success?
        fail CapsuleCD::Error::BuildPackageFailed, 'gem build failed. Check gemspec file and dependencies'
      end
      unless File.exist?(@source_git_local_path + "/#{gem_name}-#{next_version.to_s}.gem")
        fail CapsuleCD::Error::BuildPackageFailed, "gem build failed. #{gem_name}-#{next_version.to_s}.gem not found"
      end
    end
  end
end

#package_stepObject

run npm publish



133
134
135
136
137
138
139
140
141
142
# File 'lib/capsulecd/ruby/ruby_engine.rb', line 133

def package_step
  super

  # commit changes to the cookbook. (test run occurs before this, and it should clean up any instrumentation files, created,
  # as they will be included in the commmit and any release artifacts)
  gem_version = CapsuleCD::Ruby::RubyHelper.get_version(@source_git_local_path)
  next_version = SemVer.parse(gem_version)
  CapsuleCD::GitUtils.commit(@source_git_local_path, "(v#{next_version}) Automated packaging of release by CapsuleCD")
  @source_release_commit = CapsuleCD::GitUtils.tag(@source_git_local_path, "v#{next_version}")
end

#release_stepObject

this step should push the release to the package repository (ie. npm, chef supermarket, rubygems)



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
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/capsulecd/ruby/ruby_engine.rb', line 145

def release_step
  super

  unless @config.rubygems_api_key
    fail CapsuleCD::Error::ReleaseCredentialsMissing, 'cannot deploy package to rubygems, credentials missing'
    return
  end

  # write the config file.
  rubygems_cred_path = File.expand_path('~/.gem')

  FileUtils.mkdir_p(rubygems_cred_path)
  File.open(rubygems_cred_path + '/credentials', 'w+', 0600) do |file|
    file.write(<<-EOT.gsub(/^\s+/, '')
      ---
      :rubygems_api_key: #{@config.rubygems_api_key}
      EOT
    )
  end

  # run gem push *.gem
  gems = Dir.glob(@source_git_local_path + '/*.gem')
  if gems.empty?
    fail CapsuleCD::Error::TestDependenciesError, 'Ruby gem file could not be found'
  end
  gem_path = gems.first
  Open3.popen3('gem push ' + File.basename(gem_path), chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
    { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
      Thread.new do
        until (line = stream_buffer.gets).nil?
          puts "#{name} -> #{line}"
        end
      end
    end
    # wait for process
    external.join
    unless external.value.success?
      fail CapsuleCD::Error::ReleasePackageError, 'Pushing gem to RubyGems.org using `gem push` failed. Check log for exact error'
    end
  end
end

#test_stepObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/capsulecd/ruby/ruby_engine.rb', line 71

def test_step
  super

  gems = Dir.glob(@source_git_local_path + '/*.gem')
  if gems.empty?
    fail CapsuleCD::Error::TestDependenciesError, 'Ruby gem file could not be found'
  end
  gem_path = gems.first

  # lets install the gem, and any dependencies
  # http://guides.rubygems.org/make-your-own-gem/
  Bundler.with_clean_env do
    Open3.popen3('gem install ./' + File.basename(gem_path) + ' --ignore-dependencies', chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
      { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
        Thread.new do
          until (line = stream_buffer.gets).nil?
            puts "#{name} -> #{line}"
          end
        end
      end
      # wait for process
      external.join
      unless external.value.success?
        fail CapsuleCD::Error::TestDependenciesError, 'gem install failed. Check gemspec and gem dependencies'
      end
    end

    Open3.popen3('bundle install', chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
      { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
        Thread.new do
          until (line = stream_buffer.gets).nil?
            puts "#{name} -> #{line}"
          end
        end
      end
      # wait for process
      external.join
      unless external.value.success?
        fail CapsuleCD::Error::TestDependenciesError, 'bundle install failed. Check Gemfile'
      end
    end

    # run test command
    test_cmd = @config.engine_cmd_test || 'rake spec'
    Open3.popen3(test_cmd, chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
      { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
        Thread.new do
          until (line = stream_buffer.gets).nil?
            puts "#{name} -> #{line}"
          end
        end
      end
      # wait for process
      external.join
      unless external.value.success?
        fail CapsuleCD::Error::TestRunnerError, test_cmd + ' failed. Check log for exact error'
      end
    end unless @config.engine_disable_test
  end
end