简体   繁体   English

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

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

I want to create a Rake task that builds a Jekyll site, then runs tests on the generated site, similar to the following: 我想创建一个构建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

I'm relatively new to Ruby and I'm keen to gain more experience. 我是Ruby的新手,我渴望获得更多经验。 Using system 'bundle exec jekyll build' in the build task seems a bit of a shortcut to me, so as an exercise I wanted to refactor this rake task to build the site using Jekyll::Commands::Build and therefore not calling a command line executable, as the above example does. 在构建任务中使用system 'bundle exec jekyll build'对我来说似乎有点捷径,因此,作为练习,我想重构此rake任务以使用Jekyll::Commands::Build来构建站点,因此不调用命令行可执行文件,如上面的示例所示。 I was hoping something like this would suffice: 我希望这样的事情就足够了:

# 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

However, I can't build the site using this task: 但是,我无法使用此任务来构建站点:

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)

How do I build an existing site from a Rake task without using the command line and instead use the Jekyll library directly? 如何通过Rake任务构建现有站点而不使用命令行,而是直接使用Jekyll库?

As requested by @DavidJacquel in the comments below, I've thrown together a demo of the issue in a repo here . 按照@DavidJacquel在以下评论中的要求,我在此处的回购中汇总了该问题的演示。

The configuration should be a Jekyll.configuration instance: 该配置应为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM