diff --git a/lib/jarvis/commands/publish.rb b/lib/jarvis/commands/publish.rb index fedb2e9..8333658 100644 --- a/lib/jarvis/commands/publish.rb +++ b/lib/jarvis/commands/publish.rb @@ -7,6 +7,7 @@ require "jarvis/logstash_helper" require "jarvis/github/project" require "jarvis/patches/i18n" +require "fileutils" require "gems" module Jarvis module Command class Publish < Clamp::Command @@ -30,9 +31,9 @@ class Bug < ::Jarvis::Error; end ALWAYS_RUN = lambda { |workdir| true } - TASKS = { "bundle install" => ALWAYS_RUN, - "ruby -rbundler/setup -S rake vendor" => ALWAYS_RUN, - "ruby -rbundler/setup -S rake publish_gem" => ALWAYS_RUN }.freeze + TASKS_RUBY = { "bundle install" => ALWAYS_RUN, + "ruby -rbundler/setup -S rake vendor" => ALWAYS_RUN, + "ruby -rbundler/setup -S rake publish_gem" => ALWAYS_RUN }.freeze NO_PREVIOUS_GEMS_PUBLISHED = "This rubygem could not be found." @@ -105,21 +106,37 @@ def execute end end - TASKS.each do |command, condition| - if condition.call(workdir) - context[:command] = command - puts I18n.t("lita.handlers.jarvis.publish command", :command => command) - Jarvis.execute(command, git.dir, env, logger) - - # Clear the logs if it was successful - logs.clear unless logger.debug? + Dir.chdir(git.dir.path) do + if Dir.glob("*.gemspec").empty? # it's a java plugin + #logstash_core_branch = IO.read("PLUGIN_API_VERSION") + logstash_core_branch = "7.x" # TODO read this from the plugin's repository + clone_logstash = "git clone https://github.com/elastic/logstash/ --branch #{logstash_core_branch} --single-branch ../logstash" + execute_command(context, clone_logstash, logger, env) + build_logstash_jar = "./gradlew assemble" # build the core jar + Dir.chdir("../logstash") { execute_command(context, build_logstash_jar, logger, env) } + IO.write("gradle.properties", "LOGSTASH_CORE_PATH=../logstash/logstash-core") + build_gem = "./gradlew gem" + execute_command(context, build_gem, logger, env) + FileUtils.rm_rf('../logstash') # we no longer need this + gem_push = "vendor/jruby/bin/jruby -S jgem push *.gem" + execute_command(context, gem_push, logger, env) + _, local_version = gem_specification + tag_repository = "git tag v#{local_version} && git push origin v#{local_version}" + execute_command(context, tag_repository, logger, env) + else # it's a ruby plugin + TASKS_RUBY.each do |command, condition| + next unless condition.call(workdir) + execute_command(context, command, logger, env, git.dir) + end end end + # Clear the logs if it was successful + logs.clear unless logger.debug? context.clear() git.reset git.clean(force: true) - verify_publish + Dir.chdir(git.dir.path) { |_| verify_publish } puts I18n.t("lita.handlers.jarvis.publish success", :organization => project.organization, @@ -135,6 +152,12 @@ def execute Thread.current[:logger] = nil end + def execute_command(context, command, logger, env, dir = nil) + context[:command] = command + puts I18n.t("lita.handlers.jarvis.publish command", :command => command) + Jarvis.execute(command, dir, env, logger) + end + def verify_publish name, local_version = gem_specification published_gems = Gems.versions(name) diff --git a/lib/jarvis/exec.rb b/lib/jarvis/exec.rb index cc25ece..64136d5 100644 --- a/lib/jarvis/exec.rb +++ b/lib/jarvis/exec.rb @@ -1,8 +1,6 @@ require "jarvis/error" require "jarvis/env_utils" -require "bundler" require "open4" -require "tmpdir" module Jarvis extend EnvUtils @@ -13,29 +11,36 @@ class SubprocessFailure < ::Jarvis::Error ; end def self.execute(args, directory = nil, env = {}, logger = self.logger) logger.info("Running command", :args => args) if logger env = parse_env_string(env) if env.is_a?(String) + + wrap_args_with_env = lambda do |args| + if env.any? + wrapped_args = [ 'env', '-' ] + wrapped_args.concat env_to_shell_lines(execute_env.merge(env)) + wrapped_args.concat [ 'bash', '-c', args.join('; ') ] + wrapped_args + else + args + end + end + # We have to wrap the command into this block to make sure the current command use his # defined set of gems and not jarvis gems. - with_dir(directory) do # Bundler.with_clean_env do - pid, stdin, stdout, stderr = if directory || env.any? - cd_rvm_args = [ - "cd #{shell_escape(directory)}", - ". #{rvm_path}/scripts/rvm", - "echo PWD; pwd", - "rvm use #{JRUBY_VERSION}; rvm use" - ] - cd_rvm_args << Array(args).join(' ') - wrapped = [ 'env', '-' ] - wrapped.concat env_to_shell_lines(execute_env.merge(env)) - wrapped.concat [ 'bash', '-c', cd_rvm_args.join('; ') ] - Open4::popen4(*wrapped) - else - Open4::popen4(*args) - end - stdin.close - logger.pipe(stdout => :info, stderr => :error) if logger - _, status = Process::waitpid2(pid) - raise SubprocessFailure, "subprocess failed with code #{status.exitstatus}" unless status.success? - end + pid, stdin, stdout, stderr = if directory + cd_rvm_args = [ + "cd #{shell_escape(directory)}", + ". #{rvm_path}/scripts/rvm", + "echo PWD; pwd", + "rvm use #{JRUBY_VERSION}; rvm use" + ] + cd_rvm_args << Array(args).join(' ') + Open4::popen4 *wrap_args_with_env.(cd_rvm_args) + else + Open4::popen4 *wrap_args_with_env.(args) + end + stdin.close + logger.pipe(stdout => :info, stderr => :error) if logger + _, status = Process::waitpid2(pid) + raise SubprocessFailure, "subprocess failed with code #{status.exitstatus}" unless status.success? end def self.logger @@ -46,14 +51,6 @@ class << self private - def with_dir(directory, &block) - if directory.nil? - Dir.mktmpdir(&block) - else - yield directory - end - end - def rvm_path ENV['rvm_path'] || '~/.rvm' end