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

时间:2015-11-07 11:25:46

标签: ruby rake jekyll

我想创建一个构建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)

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

根据@DavidJacquel在下面评论中的要求,我在回购here中汇总了该问题的演示。

1 个答案:

答案 0 :(得分:1)

配置应该是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