【问题标题】:Unable to programmatically run a Jekyll build无法以编程方式运行 Jekyll 构建
【发布时间】:2013-11-18 02:20:06
【问题描述】:

我正在尝试以编程方式运行 Jekyll 构建,但在尝试加载 Jekyll 站点插件使用的一些 gem 时,我遇到了构建崩溃的问题。

我的代码如下所示(为便于阅读添加了换行符):

%x(cd #{@config['input_directory']} &&
  bundle install &&
  bundle exec jekyll build --config #{File.join(@config['input_directory'], "_config.yml")} --source #{@config['input_directory']} --destination #{@config['output_directory']} --trace)

当它运行时,我得到一个如下所示的堆栈跟踪:

/_plugins/jekyll_lunr_js_search.rb:80:in `require': cannot load such file -- nokogiri (LoadError)
    from /_plugins/jekyll_lunr_js_search.rb:80:in `<top (required)>'
    from /ruby-1.9.3-p448/gems/jekyll-1.3.0/lib/jekyll/site.rb:77:in `require'
    from /ruby-1.9.3-p448/gems/jekyll-1.3.0/lib/jekyll/site.rb:77:in `block (2 levels) in setup'
    from /ruby-1.9.3-p448/gems/jekyll-1.3.0/lib/jekyll/site.rb:76:in `each'
    from /ruby-1.9.3-p448/gems/jekyll-1.3.0/lib/jekyll/site.rb:76:in `block in setup'
    from /ruby-1.9.3-p448/gems/jekyll-1.3.0/lib/jekyll/site.rb:75:in `each'
    from /ruby-1.9.3-p448/gems/jekyll-1.3.0/lib/jekyll/site.rb:75:in `setup'
    from /ruby-1.9.3-p448/gems/jekyll-1.3.0/lib/jekyll/site.rb:29:in `initialize'
    from /ruby-1.9.3-p448/gems/jekyll-1.3.0/lib/jekyll/commands/build.rb:5:in `new'
    from /ruby-1.9.3-p448/gems/jekyll-1.3.0/lib/jekyll/commands/build.rb:5:in `process'
    from /ruby-1.9.3-p448/gems/jekyll-1.3.0/bin/jekyll:77:in `block (2 levels) in <top (required)>'
    from /ruby-1.9.3-p448/gems/commander-4.1.5/lib/commander/command.rb:180:in `call'
    from /ruby-1.9.3-p448/gems/commander-4.1.5/lib/commander/command.rb:180:in `call'
    from /ruby-1.9.3-p448/gems/commander-4.1.5/lib/commander/command.rb:155:in `run'
    from /ruby-1.9.3-p448/gems/commander-4.1.5/lib/commander/runner.rb:402:in `run_active_command'
    from /ruby-1.9.3-p448/gems/commander-4.1.5/lib/commander/runner.rb:78:in `run!'
    from /ruby-1.9.3-p448/gems/commander-4.1.5/lib/commander/delegates.rb:11:in `run!'
    from /ruby-1.9.3-p448/gems/commander-4.1.5/lib/commander/import.rb:10:in `block in <top (required)>'

input_directory 的 Gemfile 看起来是这样的,所以我不知道为什么它无法加载 nokogiri。

source 'https://rubygems.org'

gem 'directory_watcher', '= 1.4.1' # http://stackoverflow.com/questions/15591000/jekylls-auto-doesnt-work
gem 'kramdown'
gem 'nokogiri'
gem 'yui-compressor'
gem "jekyll"

group :development do
  gem 'capistrano'
  gem 'rvm-capistrano'
end

欢迎任何指点。我感觉有更好的方法可以通过编程方式构建 Jekyll,这对我来说似乎是最简单的。

【问题讨论】:

    标签: ruby bundler jekyll


    【解决方案1】:

    运行bundle exec jekyll 以确保它为 Jekyll 正确设置 gem 路径。如果您从使用 Bundler 本身的应用程序调用程序,您可能还需要将执行包装在对 Bundler.with_clean_env 的调用中。

    完整的命令应该是这样的:

    Bundler.with_clean_env do
      %x(cd #{@config['input_directory']} &&
        bundle install &&
        bundle exec jekyll build --config #{File.join(@config['input_directory'], "_config.yml")} --source #{@config['input_directory']} --destination #{@config['output_directory']} --trace)
    end
    

    【讨论】:

    • 我也尝试过 - 令人讨厌的是,我遇到了同样的错误(我会更新问题以表明这一点)。
    • 我已经用另一个建议更新了答案以使用Bundler.with_clean_env
    • 还有一个问题。在构建期间没有实际输出到 STDOUT。我应该做些什么让它显示出来(仅出于开发目的)?
    • 使用 %x 捕获子进程的标准输出并将其作为字符串返回。由于您没有将其分配给任何东西,因此它会丢失。您可以使用 system 来让子进程直接使用 STDOUT。详情请见stackoverflow.com/a/18623297/29470
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-07
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 2011-12-27
    • 2023-03-14
    相关资源
    最近更新 更多