Write Jekyll::Utils::Exec.run for running shell commands.
This commit is contained in:
parent
4382b2467b
commit
6e2449b482
|
@ -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"
|
||||||
|
|
||||||
|
@ -106,21 +105,16 @@ end
|
||||||
|
|
||||||
# rubocop:disable Metrics/AbcSize
|
# 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
|
# rubocop:enable Metrics/AbcSize
|
||||||
|
|
||||||
|
|
|
@ -133,10 +133,9 @@ RUBY
|
||||||
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"]
|
_, output = Jekyll::Utils::Exec.run("bundle", "install", "--quiet")
|
||||||
system("bundle", "install", "--quiet")
|
output.to_s.each_line do |line|
|
||||||
else
|
Jekyll.logger.info(line)
|
||||||
system("bundle", "install")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -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"
|
||||||
|
|
||||||
# Constants for use in #slugify
|
# Constants for use in #slugify
|
||||||
SLUGIFY_MODES = %w(raw default pretty ascii).freeze
|
SLUGIFY_MODES = %w(raw default pretty ascii).freeze
|
||||||
|
|
|
@ -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
|
|
@ -159,11 +159,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
|
||||||
|
|
|
@ -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))
|
||||||
|
@ -54,7 +54,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 +76,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 +87,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 +95,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 +120,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
|
||||||
|
|
Loading…
Reference in New Issue