Skip to content

Commit

Permalink
Require runtime_dependencies of a Gem-based theme from its `.gemspe…
Browse files Browse the repository at this point in the history
…c` file (jekyll#5914)

Merge pull request 5914
  • Loading branch information
ashmaroli authored and jekyllbot committed Mar 31, 2017
1 parent 7c49070 commit 0eb9379
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ group :test do
gem "rspec-mocks"
gem "rubocop", "~> 0.47.1"
gem "test-theme", :path => File.expand_path("./test/fixtures/test-theme", File.dirname(__FILE__))
gem "test-dependency-theme", :path => File.expand_path("./test/fixtures/test-dependency-theme", File.dirname(__FILE__))

gem "jruby-openssl" if RUBY_ENGINE == "jruby"
end
Expand Down
6 changes: 6 additions & 0 deletions docs/_docs/themes.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ Your theme's styles can be included in the user's stylesheet using the `@import`
{% raw %}@import "{{ site.theme }}";{% endraw %}
```
### Theme-gem dependencies
From `v3.5`, Jekyll will automatically require all whitelisted `runtime_dependencies` of your theme-gem even if they're not explicitly included under the `gems` array in the site's config file. (NOTE: whitelisting is only required when building or serving with the `--safe` option.)
With this, the end-user need not keep track of the plugins required to be included in their config file for their theme-gem to work as intended.
### Documenting your theme
Your theme should include a `/README.md` file, which explains how site authors can install and use your theme. What layouts are included? What includes? Do they need to add anything special to their site's configuration file?
Expand Down
7 changes: 7 additions & 0 deletions features/theme.feature
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ Feature: Writing themes
And I should see "default.html from test-theme: I'm content." in "_site/index.html"
And I should see "post.html from the project: I'm more content." in "_site/post.html"

Scenario: Requiring dependencies of a theme
Given I have a configuration file with "theme" set to "test-dependency-theme"
When I run jekyll build
Then I should get a zero exit status
And the _site directory should exist
And the "_site/test.txt" file should exist

Scenario: Complicated site that puts it all together
Given I have a configuration file with "theme" set to "test-theme"
And I have a _posts directory
Expand Down
12 changes: 12 additions & 0 deletions lib/jekyll/plugin_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def initialize(site)
#
# Returns nothing
def conscientious_require
require_theme_deps if site.theme
require_plugin_files
require_gems
deprecation_checks
Expand All @@ -29,6 +30,17 @@ def require_gems
)
end

# Require each of the runtime_dependencies specified by the theme's gemspec.
#
# Returns false only if no dependencies have been specified, otherwise nothing.
def require_theme_deps
return false unless site.theme.runtime_dependencies
site.theme.runtime_dependencies.each do |dep|
next if dep.name == "jekyll"
External.require_with_graceful_fail(dep.name) if plugin_allowed?(dep.name)
end
end

def self.require_from_bundler
if !ENV["JEKYLL_NO_BUNDLER_REQUIRE"] && File.file?("Gemfile")
require "bundler"
Expand Down
4 changes: 4 additions & 0 deletions lib/jekyll/theme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def configure_sass
Sass.load_paths << sass_path
end

def runtime_dependencies
gemspec.runtime_dependencies
end

private

def path_for(folder)
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/test-dependency-theme/test-dependency-theme.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Gem::Specification.new do |s|
s.name = 'test-dependency-theme'
s.version = '0.1.0'
s.licenses = ['MIT']
s.summary = "This is another theme used to test Jekyll"
s.authors = ["Jekyll"]
s.files = ["lib/example.rb"]
s.homepage = 'https://github.com/jekyll/jekyll'

s.add_runtime_dependency "jekyll_test_plugin"
end
16 changes: 14 additions & 2 deletions test/test_plugin_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,24 @@ def with_no_gemfile
end

should "conscientious require" do
site = double
site = double({
:config => { "theme" => "test-dependency-theme" },
:in_dest_dir => "/tmp/_site/",
})
plugin_manager = PluginManager.new(site)

expect(site).to receive(:theme).and_return(true)
expect(site).to receive(:process).and_return(true)
expect(plugin_manager).to(
receive_messages([:require_plugin_files, :require_gems, :deprecation_checks])
receive_messages([
:require_theme_deps,
:require_plugin_files,
:require_gems,
:deprecation_checks,
])
)
plugin_manager.conscientious_require
site.process
assert site.in_dest_dir("test.txt")
end
end

0 comments on commit 0eb9379

Please sign in to comment.