Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WiP] support for Java plugins publishing #101

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions lib/jarvis/commands/publish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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."

Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
59 changes: 28 additions & 31 deletions lib/jarvis/exec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require "jarvis/error"
require "jarvis/env_utils"
require "bundler"
require "open4"
require "tmpdir"

module Jarvis
extend EnvUtils
Expand All @@ -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
Expand All @@ -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
Expand Down