Skip to content

Commit 9103444

Browse files
author
Matthew Loberg
committed
Add source and config options to publish command
Update Publish command to allow custom Jekyll source. Update MovementArgParser to generate Jekyll config like was done with ArgParser. Update FileMover to automatically use a root directory if given.
1 parent d9469c8 commit 9103444

File tree

4 files changed

+67
-9
lines changed

4 files changed

+67
-9
lines changed

lib/jekyll-compose/file_mover.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module Jekyll
22
module Compose
33
class FileMover
4-
attr_reader :movement
5-
def initialize(movement)
4+
attr_reader :movement, :root
5+
def initialize(movement, root = nil)
66
@movement = movement
7+
@root = root
78
end
89

910
def resource_type
@@ -17,17 +18,31 @@ def move
1718
end
1819

1920
def validate_source
20-
raise ArgumentError.new("There was no #{resource_type} found at '#{movement.from}'.") unless File.exist? movement.from
21+
raise ArgumentError.new("There was no #{resource_type} found at '#{from}'.") unless File.exist? from
2122
end
2223

2324
def ensure_directory_exists
24-
dir = File.dirname movement.to
25+
dir = File.dirname to
2526
Dir.mkdir(dir) unless Dir.exist?(dir)
2627
end
2728

2829
def move_file
29-
FileUtils.mv(movement.from, movement.to)
30-
puts "#{resource_type.capitalize} #{movement.from} was moved to #{movement.to}"
30+
FileUtils.mv(from, to)
31+
puts "#{resource_type.capitalize} #{from} was moved to #{to}"
32+
end
33+
34+
private
35+
def from
36+
file_path(movement.from)
37+
end
38+
39+
def to
40+
file_path(movement.to)
41+
end
42+
43+
def file_path(path)
44+
return path if root.nil? or root.empty?
45+
return File.join(root, path)
3146
end
3247
end
3348
end

lib/jekyll-compose/movement_arg_parser.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module Jekyll
22
module Compose
33
class MovementArgParser
4-
attr_reader :args, :options
4+
attr_reader :args, :options, :config
55
def initialize(args, options)
66
@args = args
77
@options = options
8+
@config = Jekyll.configuration(options)
89
end
910

1011
def validate!
@@ -14,6 +15,10 @@ def validate!
1415
def path
1516
args.join ' '
1617
end
18+
19+
def source
20+
source = config['source'].gsub(/^#{Regexp.quote(Dir.pwd)}/, '')
21+
end
1722
end
1823
end
1924
end

lib/jekyll/commands/publish.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ def self.init_with_program(prog)
77
c.description 'Moves a draft into the _posts directory and sets the date'
88

99
c.option 'date', '-d DATE', '--date DATE', 'Specify the post date'
10+
c.option 'config', '--config CONFIG_FILE[,CONFIG_FILE2,...]', Array, 'Custom configuration file'
11+
c.option 'source', '-s', '--source SOURCE', 'Custom source directory'
1012

1113
c.action do |args, options|
1214
Jekyll::Commands::Publish.process(args, options)
@@ -20,7 +22,7 @@ def self.process(args = [], options = {})
2022

2123
movement = DraftMovementInfo.new params
2224

23-
mover = DraftMover.new movement
25+
mover = DraftMover.new movement, params.source
2426
mover.move
2527
end
2628

spec/publish_spec.rb

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
output = capture_stdout { described_class.process(args) }
4747
expect(output).to eql("Draft _drafts/#{draft_to_publish} was moved to _posts/#{post_filename}\n")
4848
end
49-
49+
5050
it 'publishes a draft on the specified date' do
5151
path = posts_dir.join "2012-03-04-a-test-post.md"
5252
capture_stdout { described_class.process(args, {"date" => '2012-3-4'}) }
@@ -72,4 +72,40 @@
7272
}).to raise_error("There was no draft found at '_drafts/i-do-not-exist.markdown'.")
7373
end
7474

75+
context 'when a configuration file exists' do
76+
let(:config) { source_dir('_config.yml') }
77+
let(:drafts_dir) { Pathname.new source_dir('site', '_drafts') }
78+
let(:posts_dir) { Pathname.new source_dir('site', '_posts') }
79+
80+
before(:each) do
81+
File.open(config, 'w') do |f|
82+
f.write(%{
83+
source: site
84+
})
85+
end
86+
end
87+
88+
after(:each) do
89+
FileUtils.rm(config)
90+
end
91+
92+
it 'should use source directory set by config' do
93+
expect(post_path).not_to exist
94+
expect(draft_path).to exist
95+
capture_stdout { described_class.process(args) }
96+
expect(post_path).to exist
97+
end
98+
end
99+
100+
context 'when source option is set' do
101+
let(:drafts_dir) { Pathname.new source_dir('site', '_drafts') }
102+
let(:posts_dir) { Pathname.new source_dir('site', '_posts') }
103+
104+
it 'should use source directory set by command line option' do
105+
expect(post_path).not_to exist
106+
expect(draft_path).to exist
107+
capture_stdout { described_class.process(args, 'source' => 'site') }
108+
expect(post_path).to exist
109+
end
110+
end
75111
end

0 commit comments

Comments
 (0)