Merge pull request #5640 from jekyll/jekyll-utils-exec

Merge pull request 5640
This commit is contained in:
jekyllbot 2017-01-18 14:15:11 -05:00 committed by GitHub
commit 477b2f0f3e
6 changed files with 56 additions and 40 deletions

View File

@ -1,6 +1,5 @@
require "fileutils" require "fileutils"
require "jekyll" require "jekyll"
require "open3"
require "time" require "time"
require "safe_yaml/load" require "safe_yaml/load"
@ -103,26 +102,18 @@ def run_jekyll(args)
end end
# #
# rubocop:disable Metrics/AbcSize
def run_in_shell(*args) def run_in_shell(*args)
i, o, e, p = Open3.popen3(*args) p, output = Jekyll::Utils::Exec.run(*args)
out = o.read.strip
err = e.read.strip
[i, o, e].each(&:close) File.write(Paths.status_file, p.exitstatus)
File.write(Paths.status_file, p.value.exitstatus)
File.open(Paths.output_file, "wb") do |f| File.open(Paths.output_file, "wb") do |f|
f.puts "$ " << args.join(" ") f.puts "$ " << args.join(" ")
f.puts out f.puts output
f.puts err f.puts "EXIT STATUS: #{p.exitstatus}"
f.puts "EXIT STATUS: #{p.value.exitstatus}"
end end
p.value p
end end
# rubocop:enable Metrics/AbcSize
# #

View File

@ -125,23 +125,23 @@ RUBY
# unless the user opts to generate a blank blog or skip 'bundle install'. # unless the user opts to generate a blank blog or skip 'bundle install'.
def after_install(path, options = {}) def after_install(path, options = {})
Jekyll.logger.info "New jekyll site installed in #{path.cyan}."
Jekyll.logger.info "Bundle install skipped." if options["skip-bundle"]
unless options["blank"] || options["skip-bundle"] unless options["blank"] || options["skip-bundle"]
bundle_install path bundle_install path
end end
Jekyll.logger.info "New jekyll site installed in #{path.cyan}."
Jekyll.logger.info "Bundle install skipped." if options["skip-bundle"]
end end
def bundle_install(path) def bundle_install(path)
Jekyll::External.require_with_graceful_fail "bundler" Jekyll::External.require_with_graceful_fail "bundler"
Jekyll.logger.info "Running bundle install in #{path.cyan}..." Jekyll.logger.info "Running bundle install in #{path.cyan}..."
Dir.chdir(path) do Dir.chdir(path) do
if ENV["CI"] process, output = Jekyll::Utils::Exec.run("bundle", "install")
system("bundle", "install", "--quiet") output.to_s.each_line do |line|
else Jekyll.logger.info("Bundler:".green, line.strip) unless line.to_s.empty?
system("bundle", "install")
end end
raise SystemExit unless process.success?
end end
end end
end end

View File

@ -2,8 +2,9 @@
module Jekyll module Jekyll
module Utils module Utils
extend self extend self
autoload :Platforms, "jekyll/utils/platforms"
autoload :Ansi, "jekyll/utils/ansi" autoload :Ansi, "jekyll/utils/ansi"
autoload :Exec, "jekyll/utils/exec"
autoload :Platforms, "jekyll/utils/platforms"
autoload :WinTZ, "jekyll/utils/win_tz" autoload :WinTZ, "jekyll/utils/win_tz"
# Constants for use in #slugify # Constants for use in #slugify

25
lib/jekyll/utils/exec.rb Normal file
View File

@ -0,0 +1,25 @@
require "open3"
module Jekyll
module Utils
module Exec
extend self
# Runs a program in a sub-shell.
#
# *args - a list of strings containing the program name and arguments
#
# Returns a Process::Status and a String of output in an array in
# that order.
def run(*args)
stdin, stdout, stderr, process = Open3.popen3(*args)
out = stdout.read.strip
err = stderr.read.strip
[stdin, stdout, stderr].each(&:close)
[process.value, out + err]
end
end
end
end

View File

@ -167,11 +167,11 @@ class JekyllUnitTest < Minitest::Test
end end
def capture_output def capture_output
stderr = StringIO.new buffer = StringIO.new
Jekyll.logger = Logger.new stderr Jekyll.logger = Logger.new(buffer)
yield yield
stderr.rewind buffer.rewind
return stderr.string.to_s buffer.string.to_s
end end
alias_method :capture_stdout, :capture_output alias_method :capture_stdout, :capture_output
alias_method :capture_stderr, :capture_output alias_method :capture_stderr, :capture_output

View File

@ -25,14 +25,14 @@ class TestNewCommand < JekyllUnitTest
should "create a new directory" do should "create a new directory" do
refute_exist @full_path refute_exist @full_path
Jekyll::Commands::New.process(@args) capture_output { Jekyll::Commands::New.process(@args) }
assert_exist @full_path assert_exist @full_path
end end
should "create a Gemfile" do should "create a Gemfile" do
gemfile = File.join(@full_path, "Gemfile") gemfile = File.join(@full_path, "Gemfile")
refute_exist @full_path refute_exist @full_path
capture_stdout { Jekyll::Commands::New.process(@args) } capture_output { Jekyll::Commands::New.process(@args) }
assert_exist gemfile assert_exist gemfile
assert_match(%r!gem "jekyll", "#{Jekyll::VERSION}"!, File.read(gemfile)) assert_match(%r!gem "jekyll", "#{Jekyll::VERSION}"!, File.read(gemfile))
assert_match(%r!gem "github-pages"!, File.read(gemfile)) assert_match(%r!gem "github-pages"!, File.read(gemfile))
@ -40,12 +40,11 @@ class TestNewCommand < JekyllUnitTest
should "display a success message" do should "display a success message" do
Jekyll::Commands::New.process(@args) Jekyll::Commands::New.process(@args)
output = Jekyll.logger.messages[-3] output = Jekyll.logger.messages
output_last = Jekyll.logger.messages.last success_message = "New jekyll site installed in #{@full_path.cyan}. "
success_message = "New jekyll site installed in #{@full_path.cyan}." bundle_message = "Running bundle install in #{@full_path.cyan}... "
bundle_message = "Running bundle install in #{@full_path.cyan}..."
assert_includes output, success_message assert_includes output, success_message
assert_includes output_last, bundle_message assert_includes output, bundle_message
end end
should "copy the static files in site template to the new directory" do should "copy the static files in site template to the new directory" do
@ -54,7 +53,7 @@ class TestNewCommand < JekyllUnitTest
end end
static_template_files << "/Gemfile" static_template_files << "/Gemfile"
capture_stdout { Jekyll::Commands::New.process(@args) } capture_output { Jekyll::Commands::New.process(@args) }
new_site_files = dir_contents(@full_path).reject do |f| new_site_files = dir_contents(@full_path).reject do |f|
File.extname(f) == ".markdown" File.extname(f) == ".markdown"
@ -76,7 +75,7 @@ class TestNewCommand < JekyllUnitTest
f.gsub! "0000-00-00", stubbed_date f.gsub! "0000-00-00", stubbed_date
end end
capture_stdout { Jekyll::Commands::New.process(@args) } capture_output { Jekyll::Commands::New.process(@args) }
new_site_files = dir_contents(@full_path).select do |f| new_site_files = dir_contents(@full_path).select do |f|
erb_template_files.include? f erb_template_files.include? f
@ -87,7 +86,7 @@ class TestNewCommand < JekyllUnitTest
should "create blank project" do should "create blank project" do
blank_contents = %w(/_drafts /_layouts /_posts /index.html) blank_contents = %w(/_drafts /_layouts /_posts /index.html)
capture_stdout { Jekyll::Commands::New.process(@args, "--blank") } capture_output { Jekyll::Commands::New.process(@args, "--blank") }
output = Jekyll.logger.messages.last output = Jekyll.logger.messages.last
bundle_message = "Running bundle install in #{@full_path.cyan}..." bundle_message = "Running bundle install in #{@full_path.cyan}..."
assert_same_elements blank_contents, dir_contents(@full_path) assert_same_elements blank_contents, dir_contents(@full_path)
@ -95,13 +94,13 @@ class TestNewCommand < JekyllUnitTest
end end
should "force created folder" do should "force created folder" do
capture_stdout { Jekyll::Commands::New.process(@args) } capture_output { Jekyll::Commands::New.process(@args) }
output = capture_stdout { Jekyll::Commands::New.process(@args, "--force") } output = capture_output { Jekyll::Commands::New.process(@args, "--force") }
assert_match(%r!New jekyll site installed in!, output) assert_match(%r!New jekyll site installed in!, output)
end end
should "skip bundle install when opted to" do should "skip bundle install when opted to" do
capture_stdout { Jekyll::Commands::New.process(@args, "--skip-bundle") } capture_output { Jekyll::Commands::New.process(@args, "--skip-bundle") }
output = Jekyll.logger.messages.last output = Jekyll.logger.messages.last
bundle_message = "Bundle install skipped." bundle_message = "Bundle install skipped."
assert_includes output, bundle_message assert_includes output, bundle_message
@ -120,7 +119,7 @@ class TestNewCommand < JekyllUnitTest
should "create a new directory" do should "create a new directory" do
refute_exist @site_name_with_spaces refute_exist @site_name_with_spaces
capture_stdout { Jekyll::Commands::New.process(@multiple_args) } capture_output { Jekyll::Commands::New.process(@multiple_args) }
assert_exist @site_name_with_spaces assert_exist @site_name_with_spaces
end end
end end