Skip to content

Commit

Permalink
Merge pull request #42 from mloberg/feature/site-source-fix
Browse files Browse the repository at this point in the history
Allow Jekyll Source Directory
  • Loading branch information
courajs authored Sep 10, 2016
2 parents 8e1db4b + d467f20 commit 8100f7e
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 25 deletions.
11 changes: 8 additions & 3 deletions lib/jekyll-compose/arg_parser.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
class Jekyll::Compose::ArgParser
attr_reader :args, :options
attr_reader :args, :options, :config
def initialize(args, options)
@args = args
@options = options
@config = Jekyll.configuration(options)
end

def validate!
raise ArgumentError.new('You must specify a name.') if args.empty?
end

def type
type = options["extension"] || Jekyll::Compose::DEFAULT_TYPE
options["extension"] || Jekyll::Compose::DEFAULT_TYPE
end

def layout
layout = options["layout"] || Jekyll::Compose::DEFAULT_LAYOUT
options["layout"] || Jekyll::Compose::DEFAULT_LAYOUT
end

def title
Expand All @@ -24,4 +25,8 @@ def title
def force?
!!options["force"]
end

def source
config['source'].gsub(/^#{Regexp.quote(Dir.pwd)}/, '')
end
end
18 changes: 12 additions & 6 deletions lib/jekyll-compose/file_creator.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module Jekyll
module Compose
class FileCreator
attr_reader :file, :force
def initialize(fileInfo, force = false)
attr_reader :file, :force, :root
def initialize(fileInfo, force = false, root = nil)
@file = fileInfo
@force = force
@root = root
end

def create!
Expand All @@ -16,20 +17,25 @@ def create!
private

def validate_should_write!
raise ArgumentError.new("A #{file.resource_type} already exists at #{file.path}") if File.exist?(file.path) and !force
raise ArgumentError.new("A #{file.resource_type} already exists at #{file_path}") if File.exist?(file_path) and !force
end

def ensure_directory_exists
dir = File.dirname file.path
dir = File.dirname file_path
Dir.mkdir(dir) unless Dir.exist?(dir)
end

def write_file
File.open(file.path, "w") do |f|
File.open(file_path, "w") do |f|
f.puts(file.content)
end

puts "New #{file.resource_type} created at #{file.path}."
puts "New #{file.resource_type} created at #{file_path}."
end

def file_path
return file.path if root.nil? or root.empty?
return File.join(root, file.path)
end
end
end
Expand Down
27 changes: 21 additions & 6 deletions lib/jekyll-compose/file_mover.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module Jekyll
module Compose
class FileMover
attr_reader :movement
def initialize(movement)
attr_reader :movement, :root
def initialize(movement, root = nil)
@movement = movement
@root = root
end

def resource_type
Expand All @@ -17,17 +18,31 @@ def move
end

def validate_source
raise ArgumentError.new("There was no #{resource_type} found at '#{movement.from}'.") unless File.exist? movement.from
raise ArgumentError.new("There was no #{resource_type} found at '#{from}'.") unless File.exist? from
end

def ensure_directory_exists
dir = File.dirname movement.to
dir = File.dirname to
Dir.mkdir(dir) unless Dir.exist?(dir)
end

def move_file
FileUtils.mv(movement.from, movement.to)
puts "#{resource_type.capitalize} #{movement.from} was moved to #{movement.to}"
FileUtils.mv(from, to)
puts "#{resource_type.capitalize} #{from} was moved to #{to}"
end

private
def from
movement.from
end

def to
file_path(movement.to)
end

def file_path(path)
return path if root.nil? or root.empty?
return File.join(root, path)
end
end
end
Expand Down
7 changes: 6 additions & 1 deletion lib/jekyll-compose/movement_arg_parser.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module Jekyll
module Compose
class MovementArgParser
attr_reader :args, :options
attr_reader :args, :options, :config
def initialize(args, options)
@args = args
@options = options
@config = Jekyll.configuration(options)
end

def validate!
Expand All @@ -14,6 +15,10 @@ def validate!
def path
args.join ' '
end

def source
source = config['source'].gsub(/^#{Regexp.quote(Dir.pwd)}/, '')
end
end
end
end
6 changes: 4 additions & 2 deletions lib/jekyll/commands/draft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def self.options
[
['extension', '-x EXTENSION', '--extension EXTENSION', 'Specify the file extension'],
['layout', '-l LAYOUT', '--layout LAYOUT', "Specify the draft layout"],
['force', '-f', '--force', 'Overwrite a draft if it already exists']
['force', '-f', '--force', 'Overwrite a draft if it already exists'],
['config', '--config CONFIG_FILE[,CONFIG_FILE2,...]', Array, 'Custom configuration file'],
['source', '-s', '--source SOURCE', 'Custom source directory'],
]
end

Expand All @@ -27,7 +29,7 @@ def self.process(args = [], options = {})

draft = DraftFileInfo.new params

Compose::FileCreator.new(draft, params.force?).create!
Compose::FileCreator.new(draft, params.force?, params.source).create!
end

class DraftFileInfo < Compose::FileInfo
Expand Down
6 changes: 4 additions & 2 deletions lib/jekyll/commands/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def self.options
[
['extension', '-x EXTENSION', '--extension EXTENSION', 'Specify the file extension'],
['layout', '-l LAYOUT', '--layout LAYOUT', "Specify the page layout"],
['force', '-f', '--force', 'Overwrite a page if it already exists']
['force', '-f', '--force', 'Overwrite a page if it already exists'],
['config', '--config CONFIG_FILE[,CONFIG_FILE2,...]', Array, 'Custom configuration file'],
['source', '-s', '--source SOURCE', 'Custom source directory'],
]
end

Expand All @@ -26,7 +28,7 @@ def self.process(args = [], options = {})

page = PageFileInfo.new params

Compose::FileCreator.new(page, params.force?).create!
Compose::FileCreator.new(page, params.force?, params.source).create!
end

class PageArgParser < Compose::ArgParser
Expand Down
6 changes: 4 additions & 2 deletions lib/jekyll/commands/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ def self.options
['extension', '-x EXTENSION', '--extension EXTENSION', 'Specify the file extension'],
['layout', '-l LAYOUT', '--layout LAYOUT', "Specify the post layout"],
['force', '-f', '--force', 'Overwrite a post if it already exists'],
['date', '-d DATE', '--date DATE', 'Specify the post date']
['date', '-d DATE', '--date DATE', 'Specify the post date'],
['config', '--config CONFIG_FILE[,CONFIG_FILE2,...]', Array, 'Custom configuration file'],
['source', '-s', '--source SOURCE', 'Custom source directory'],
]
end

Expand All @@ -27,7 +29,7 @@ def self.process(args = [], options = {})

post = PostFileInfo.new params

Compose::FileCreator.new(post, params.force?).create!
Compose::FileCreator.new(post, params.force?, params.source).create!
end


Expand Down
4 changes: 3 additions & 1 deletion lib/jekyll/commands/publish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ def self.init_with_program(prog)
c.description 'Moves a draft into the _posts directory and sets the date'

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

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

movement = DraftMovementInfo.new params

mover = DraftMover.new movement
mover = DraftMover.new movement, params.source
mover.move
end

Expand Down
5 changes: 4 additions & 1 deletion lib/jekyll/commands/unpublish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ def self.init_with_program(prog)
c.syntax 'unpublish POST_PATH'
c.description 'Moves a post back into the _drafts directory'

c.option 'config', '--config CONFIG_FILE[,CONFIG_FILE2,...]', Array, 'Custom configuration file'
c.option 'source', '-s', '--source SOURCE', 'Custom source directory'

c.action do |args, options|
process(args, options)
end
Expand All @@ -18,7 +21,7 @@ def self.process(args = [], options = {})

movement = PostMovementInfo.new params

mover = PostMover.new movement
mover = PostMover.new movement, params.source
mover.move
end

Expand Down
33 changes: 33 additions & 0 deletions spec/draft_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,37 @@
expect(File.read(path)).to match(/layout: post/)
end
end

context 'when a configuration file exists' do
let(:config) { source_dir('_config.yml') }
let(:drafts_dir) { Pathname.new source_dir(File.join('site', '_drafts')) }

before(:each) do
File.open(config, 'w') do |f|
f.write(%{
source: site
})
end
end

after(:each) do
FileUtils.rm(config)
end

it 'should use source directory set by config' do
expect(path).not_to exist
capture_stdout { described_class.process(args) }
expect(path).to exist
end
end

context 'when source option is set' do
let(:drafts_dir) { Pathname.new source_dir(File.join('site', '_drafts')) }

it 'should use source directory set by command line option' do
expect(path).not_to exist
capture_stdout { described_class.process(args, 'source' => 'site') }
expect(path).to exist
end
end
end
33 changes: 33 additions & 0 deletions spec/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,37 @@
expect(File.read(path)).to match(/layout: page/)
end
end

context 'when a configuration file exists' do
let(:config) { source_dir('_config.yml') }
let(:path) { Pathname.new(source_dir).join('site', filename) }

before(:each) do
File.open(config, 'w') do |f|
f.write(%{
source: site
})
end
end

after(:each) do
FileUtils.rm(config)
end

it 'should use source directory set by config' do
expect(path).not_to exist
capture_stdout { described_class.process(args) }
expect(path).to exist
end
end

context 'when source option is set' do
let(:path) { Pathname.new(source_dir).join('site', filename) }

it 'should use source directory set by command line option' do
expect(path).not_to exist
capture_stdout { described_class.process(args, 'source' => 'site') }
expect(path).to exist
end
end
end
33 changes: 33 additions & 0 deletions spec/post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,37 @@
expect(File.read(path)).to match(/layout: post/)
end
end

context 'when a configuration file exists' do
let(:config) { source_dir('_config.yml') }
let(:posts_dir) { Pathname.new source_dir('site', '_posts') }

before(:each) do
File.open(config, 'w') do |f|
f.write(%{
source: site
})
end
end

after(:each) do
FileUtils.rm(config)
end

it 'should use source directory set by config' do
expect(path).not_to exist
capture_stdout { described_class.process(args) }
expect(path).to exist
end
end

context 'when source option is set' do
let(:posts_dir) { Pathname.new source_dir('site', '_posts') }

it 'should use source directory set by command line option' do
expect(path).not_to exist
capture_stdout { described_class.process(args, 'source' => 'site') }
expect(path).to exist
end
end
end
Loading

0 comments on commit 8100f7e

Please sign in to comment.