如何在不使用命令行的情况下从 Rake 任务构建 Jekyll 站点?

How do I build a Jekyll site from Rake task without using the command line?

我想创建一个构建 Jekyll 站点的 Rake 任务,然后在生成的站点上运行测试,类似于以下内容:

require 'html/proofer'

task :test => [:build] do
  HTML::Proofer.new('./_site',{
                                 :only_4xx => true,
                                 :check_favicon => true,
                                 :check_html => true
                             }).run
end

task :build do
  system 'bundle exec jekyll build'
end

我是 Ruby 的新手,我渴望获得更多经验。在构建任务中使用 system 'bundle exec jekyll build' 对我来说似乎有点捷径,所以作为练习,我想重构这个 rake 任务以使用 Jekyll::Commands::Build 构建站点,因此不调用命令行可执行文件,因为上面的例子确实如此。我希望这样的事情就足够了:

# Including only the changed build task
require 'jekyll'

task :build do
  config = { 'source' => './', 'destination' => './_site' }
  site = Jekyll::Site.new(config)
  Jekyll::Commands::Build.build site, config
end

但是,我无法使用此任务构建站点:

joenyland@Joes-MBP ~/Documents/masterroot24.github.io $ bundle exec rake build
rake aborted!
NoMethodError: undefined method `to_sym' for nil:NilClass
/Users/joenyland/.rvm/gems/ruby-2.2.1@masterroot24.github.io/gems/jekyll-2.4.0/lib/jekyll/site.rb:27:in `initialize'
/Users/joenyland/Documents/masterroot24.github.io/Rakefile:14:in `new'
/Users/joenyland/Documents/masterroot24.github.io/Rakefile:14:in `block in <top (required)>'
Tasks: TOP => build
(See full trace by running task with --trace)

如何在不使用命令行的情况下直接使用 Jekyll 库从 Rake 任务构建现有站点?

应@DavidJacquel 在下面评论中的要求,我在回购 here.

中汇总了该问题的演示

配置应该是一个Jekyll.configuration实例:

# Including only the changed build task
require 'jekyll'

task :build do
  config = Jekyll.configuration({ 
    'source' => './', 
    'destination' => './_site' 
  })
  site = Jekyll::Site.new(config)
  Jekyll::Commands::Build.build site, config
end