diff --git a/.rubocop.yml b/.rubocop.yml index 65e92f6..03092af 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,30 +1,6 @@ +inherit_from: .rubocop_todo.yml + require: rubocop-jekyll inherit_gem: rubocop-jekyll: .rubocop.yml - -Lint/AmbiguousOperator: - Exclude: - - lib/jekyll/commands/draft.rb - - lib/jekyll/commands/page.rb - - lib/jekyll/commands/post.rb - -Metrics/LineLength: - Exclude: - - jekyll-compose.gemspec - - lib/jekyll-compose/file_creator.rb - - lib/jekyll-compose/file_mover.rb - - lib/jekyll/commands/draft.rb - - lib/jekyll/commands/page.rb - - lib/jekyll/commands/post.rb - - lib/jekyll/commands/publish.rb - - lib/jekyll/commands/unpublish.rb - - spec/**/* - -Metrics/BlockLength: - Exclude: - - spec/**/* - -Style/GlobalVars: - Exclude: - - spec/spec_helper.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..f9182d8 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,24 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2018-10-26 17:05:15 +0200 using RuboCop version 0.60.0. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 3 +Lint/AmbiguousOperator: + Exclude: + - 'lib/jekyll/commands/draft.rb' + - 'lib/jekyll/commands/page.rb' + - 'lib/jekyll/commands/post.rb' + +# Offense count: 5 +# Configuration parameters: Max, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. +# URISchemes: http, https +Metrics/LineLength: + Exclude: + - 'lib/jekyll-compose/file_creator.rb' + - 'lib/jekyll-compose/file_mover.rb' + - 'lib/jekyll/commands/publish.rb' + - 'lib/jekyll/commands/unpublish.rb' diff --git a/History.markdown b/History.markdown index 5fc8fa4..1d8b439 100644 --- a/History.markdown +++ b/History.markdown @@ -5,11 +5,14 @@ * Replace `puts` calls with `Jekyll.logger.info` (#69) * Formatting of dates and times in a DRY manner (#60) * Appease newest Rubocop (#71) + * chore(deps): rubocop-jekyll 0.3 (#80) ### Minor Enhancements * Add some color to the success msg like jekyll new (#75) * Allow additional front matter for Post (#41) + * Mirror `draft` command to current `post` command (#79) + * Generate configuration from CLI options (#76) ## 0.8.0 / 2018-03-24 diff --git a/README.md b/README.md index 785f66e..3dbb87f 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,11 @@ Streamline your writing in Jekyll with some commands. -[![Build Status](https://travis-ci.org/jekyll/jekyll-compose.svg?branch=master)](https://travis-ci.org/jekyll/jekyll-compose) +[![Linux Build Status](https://img.shields.io/travis/jekyll/jekyll-compose/master.svg?label=Linux%20build)][travis] +[![Windows Build status](https://img.shields.io/appveyor/ci/jekyll/jekyll-compose/master.svg?label=Windows%20build)][appveyor] + +[travis]: https://travis-ci.org/jekyll/jekyll-compose +[appveyor]: https://ci.appveyor.com/project/jekyll/jekyll-compose ## Installation diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..6e25691 --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,28 @@ +version: "{build}" +clone_depth: 5 +branches: + only: + - master + +build: off + +environment: + matrix: + - RUBY_FOLDER_VER: "25-x64" + - RUBY_FOLDER_VER: "25" + - RUBY_FOLDER_VER: "24" + - RUBY_FOLDER_VER: "23" + +install: + - SET PATH=C:\Ruby%RUBY_FOLDER_VER%\bin;%PATH% + - bundle install --retry 5 --jobs=%NUMBER_OF_PROCESSORS% --clean --path vendor\bundle + +test_script: + - ruby --version + - gem --version + - bundler --version + - bash ./script/cibuild + +cache: + # If one of the files after the right arrow changes, cache will be invalidated + - 'vendor\bundle -> appveyor.yml,Gemfile,jekyll-compose.gemspec' diff --git a/jekyll-compose.gemspec b/jekyll-compose.gemspec index 7022157..48c9a1c 100644 --- a/jekyll-compose.gemspec +++ b/jekyll-compose.gemspec @@ -25,5 +25,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler", "~> 1.5" spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rspec", "~> 3.0" - spec.add_development_dependency "rubocop-jekyll", "~> 0.2" + spec.add_development_dependency "rubocop-jekyll", "~> 0.3" end diff --git a/lib/jekyll-compose/arg_parser.rb b/lib/jekyll-compose/arg_parser.rb index 9a65c89..3b338ce 100644 --- a/lib/jekyll-compose/arg_parser.rb +++ b/lib/jekyll-compose/arg_parser.rb @@ -4,10 +4,12 @@ module Jekyll module Compose class ArgParser attr_reader :args, :options, :config - def initialize(args, options) + + # TODO: Remove `nil` parameter in v1.0 + def initialize(args, options, config = nil) @args = args @options = options - @config = Jekyll.configuration(options) + @config = config || Jekyll.configuration(options) end def validate! diff --git a/lib/jekyll-compose/file_editor.rb b/lib/jekyll-compose/file_editor.rb index 6b48455..c05f004 100644 --- a/lib/jekyll-compose/file_editor.rb +++ b/lib/jekyll-compose/file_editor.rb @@ -16,6 +16,13 @@ module Jekyll module Compose class FileEditor class << self + attr_reader :compose_config + alias_method :jekyll_compose_config, :compose_config + + def bootstrap(config) + @compose_config = config["jekyll_compose"] || {} + end + def open_editor(filepath) run_editor(post_editor, File.expand_path(filepath)) if post_editor end @@ -31,11 +38,7 @@ def post_editor end def auto_open? - jekyll_compose_config && jekyll_compose_config["auto_open"] - end - - def jekyll_compose_config - @jekyll_compose_config ||= Jekyll.configuration["jekyll_compose"] + compose_config["auto_open"] end end end diff --git a/lib/jekyll/commands/draft.rb b/lib/jekyll/commands/draft.rb index d146039..59a7cf2 100644 --- a/lib/jekyll/commands/draft.rb +++ b/lib/jekyll/commands/draft.rb @@ -25,12 +25,17 @@ def self.options end def self.process(args = [], options = {}) - params = Compose::ArgParser.new args, options + config = configuration_from_options(options) + params = Compose::ArgParser.new args, options, config params.validate! draft = DraftFileInfo.new params - Compose::FileCreator.new(draft, params.force?, params.source).create! + file_creator = Compose::FileCreator.new(draft, params.force?, params.source) + file_creator.create! + + Compose::FileEditor.bootstrap(config) + Compose::FileEditor.open_editor(file_creator.file_path) end class DraftFileInfo < Compose::FileInfo @@ -41,6 +46,13 @@ def resource_type def path "_drafts/#{file_name}" end + + def content(custom_front_matter = {}) + default_front_matter = params.config.dig("jekyll_compose", "draft_default_front_matter") + custom_front_matter.merge!(default_front_matter) if default_front_matter.is_a?(Hash) + + super(custom_front_matter) + end end end end diff --git a/lib/jekyll/commands/page.rb b/lib/jekyll/commands/page.rb index 2b18149..cd60396 100644 --- a/lib/jekyll/commands/page.rb +++ b/lib/jekyll/commands/page.rb @@ -25,7 +25,8 @@ def self.options end def self.process(args = [], options = {}) - params = PageArgParser.new args, options + config = configuration_from_options(options) + params = PageArgParser.new args, options, config params.validate! page = PageFileInfo.new params diff --git a/lib/jekyll/commands/post.rb b/lib/jekyll/commands/post.rb index 5279e03..06d6b63 100644 --- a/lib/jekyll/commands/post.rb +++ b/lib/jekyll/commands/post.rb @@ -26,13 +26,16 @@ def self.options end def self.process(args = [], options = {}) - params = PostArgParser.new args, options + config = configuration_from_options(options) + params = PostArgParser.new args, options, config params.validate! post = PostFileInfo.new params file_creator = Compose::FileCreator.new(post, params.force?, params.source) file_creator.create! + + Compose::FileEditor.bootstrap(config) Compose::FileEditor.open_editor(file_creator.file_path) end @@ -64,17 +67,11 @@ def _time_stamp end def content(custom_front_matter = {}) - default_front_matter = compose_config["post_default_front_matter"] + default_front_matter = params.config.dig("jekyll_compose", "post_default_front_matter") custom_front_matter.merge!(default_front_matter) if default_front_matter.is_a?(Hash) super({ "date" => _time_stamp }.merge(custom_front_matter)) end - - private - - def compose_config - @compose_config ||= Jekyll.configuration["jekyll_compose"] || {} - end end end end diff --git a/lib/jekyll/commands/publish.rb b/lib/jekyll/commands/publish.rb index 2ddedff..3aa7ec7 100644 --- a/lib/jekyll/commands/publish.rb +++ b/lib/jekyll/commands/publish.rb @@ -20,7 +20,8 @@ def self.init_with_program(prog) end def self.process(args = [], options = {}) - params = PublishArgParser.new args, options + config = configuration_from_options(options) + params = PublishArgParser.new args, options, config params.validate! movement = DraftMovementInfo.new params diff --git a/lib/jekyll/commands/unpublish.rb b/lib/jekyll/commands/unpublish.rb index fb67993..e5c7416 100644 --- a/lib/jekyll/commands/unpublish.rb +++ b/lib/jekyll/commands/unpublish.rb @@ -19,7 +19,8 @@ def self.init_with_program(prog) end def self.process(args = [], options = {}) - params = UnpublishArgParser.new args, options + config = configuration_from_options(options) + params = UnpublishArgParser.new args, options, config params.validate! movement = PostMovementInfo.new params diff --git a/spec/draft_spec.rb b/spec/draft_spec.rb index f2db8cf..908c785 100644 --- a/spec/draft_spec.rb +++ b/spec/draft_spec.rb @@ -13,6 +13,7 @@ before(:each) do FileUtils.mkdir_p drafts_dir unless File.directory? drafts_dir + allow(Jekyll::Compose::FileEditor).to receive(:system) end after(:each) do @@ -101,6 +102,44 @@ expect(path).to exist end + context "configuration is set" do + let(:drafts_dir) { Pathname.new source_dir("_drafts") } + let(:config_data) do + %( + jekyll_compose: + auto_open: true + draft_default_front_matter: + description: my description + category: + ) + end + + it "creates post with the specified config" do + capture_stdout { described_class.process(args) } + post = File.read(path) + expect(post).to match(%r!description: my description!) + expect(post).to match(%r!category: !) + end + + context "env variable EDITOR is set up" do + before { ENV["EDITOR"] = "nano" } + + it "opens post in default editor" do + expect(Jekyll::Compose::FileEditor).to receive(:run_editor).with("nano", path.to_s) + capture_stdout { described_class.process(args) } + end + + context "env variable JEKYLL_EDITOR is set up" do + before { ENV["JEKYLL_EDITOR"] = "nano" } + + it "opens post in jekyll editor" do + expect(Jekyll::Compose::FileEditor).to receive(:run_editor).with("nano", path.to_s) + capture_stdout { described_class.process(args) } + end + end + end + end + context "and collections_dir is set" do let(:collections_dir) { "my_collections" } let(:drafts_dir) { Pathname.new source_dir("site", collections_dir, "_drafts") } diff --git a/spec/post_spec.rb b/spec/post_spec.rb index 07d3b70..7f5d4fa 100644 --- a/spec/post_spec.rb +++ b/spec/post_spec.rb @@ -133,10 +133,10 @@ end context "env variable EDITOR is set up" do - before { ENV["EDITOR"] = "vim" } + before { ENV["EDITOR"] = "nano" } it "opens post in default editor" do - expect(Jekyll::Compose::FileEditor).to receive(:run_editor).with("vim", path.to_s) + expect(Jekyll::Compose::FileEditor).to receive(:run_editor).with("nano", path.to_s) capture_stdout { described_class.process(args) } end