Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding namespace to rake tasks #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ you will find the file structure you are used to seeing for creating blog posts,

I have provided a rake task to name and open your new blog post for editing. To use it just run

$ bundle exec rake np post-title
$ bundle exec rake bloggy:np post-title

By default, your post will open in textmate, but you can override this by creating a .bloggyrc file in your home directory, containing the command line invocation of your editor.

Expand All @@ -39,7 +39,7 @@ By default, your post will open in textmate, but you can override this by creati

Your posts will be served from the public/blog directory inside of your rails application. After you write a new blog post simply run

$ bundle exec rake generate
$ bundle exec rake bloggy:generate

And the new static files will be generated and ready to be re-deployed and served as static assets by your server!

Expand Down
28 changes: 16 additions & 12 deletions lib/generators/jekyll/blog/templates/tasks/gen.rake.tt
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
desc 'Run Jekyll in config/jekyll directory without having to cd there'
task :generate do
Dir.chdir("config/jekyll") do
system('bundle exec jekyll build')
end
end
desc 'Run Jekyll in config/jekyll directory with --watch'
task :autogenerate do
Dir.chdir("config/jekyll") do
system('bundle exec jekyll build --watch')
end
end
namespace :bloggy do

desc 'Run Jekyll in config/jekyll directory without having to cd there'
task :generate do
Dir.chdir("config/jekyll") do
system('bundle exec jekyll build')
end
end
desc 'Run Jekyll in config/jekyll directory with --watch'
task :autogenerate do
Dir.chdir("config/jekyll") do
system('bundle exec jekyll build --watch')
end
end

end
64 changes: 34 additions & 30 deletions lib/generators/jekyll/blog/templates/tasks/new_post.rake.tt
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,47 @@ require 'rubygems'
require 'optparse'
require 'yaml'

desc "create new post with textmate"
task :np do
OptionParser.new.parse!
ARGV.shift
title = ARGV.join(' ')
namespace :bloggy do

path = "config/jekyll/_posts/#{Date.today}-#{title.downcase.gsub(/[^[:alnum:]]+/, '-')}.markdown"
home_dir = Dir.respond_to?(:home) ? Dir.home : ENV['HOME']
desc "create new post with textmate"
task :np do
OptionParser.new.parse!
ARGV.shift
title = ARGV.join(' ')

if File.exist?(path)
puts "[WARN] File exists - skipping create"
else
File.open(path, "w") do |file|
file.puts YAML.dump({'layout' => 'post', 'published' => false, 'title' => title})
file.puts "---"
end
path = "config/jekyll/_posts/#{Date.today}-#{title.downcase.gsub(/[^[:alnum:]]+/, '-')}.markdown"
home_dir = Dir.respond_to?(:home) ? Dir.home : ENV['HOME']

begin
config = {'editor' => 'mate'}
if File.exist?("#{home_dir}/.bloggyrc")
config.merge!(YAML.load_file("#{home_dir}/.bloggyrc"))
if File.exist?(path)
puts "[WARN] File exists - skipping create"
else
File.open(path, "w") do |file|
file.puts YAML.dump({'layout' => 'post', 'published' => false, 'title' => title})
file.puts "---"
end
rescue TypeError
puts "[WARN] Failed to parse editor from .bloggyrc"
end

file = `which #{config['editor']} 2> /dev/null`.chomp
if $?.to_i == 0 and File.exists?(file)

begin
`#{config['editor']} #{path}`
rescue Exception
config = {'editor' => 'mate'}
if File.exist?("#{home_dir}/.bloggyrc")
config.merge!(YAML.load_file("#{home_dir}/.bloggyrc"))
end
rescue TypeError
puts "[WARN] Failed to parse editor from .bloggyrc"
end

file = `which #{config['editor']} 2> /dev/null`.chomp
if $?.to_i == 0 and File.exists?(file)
begin
`#{config['editor']} #{path}`
rescue Exception
puts "[WARN] Could not find editor #{config['editor']} - please edit #{path} manually"
end
else
puts "[WARN] Could not find editor #{config['editor']} - please edit #{path} manually"
end
else
puts "[WARN] Could not find editor #{config['editor']} - please edit #{path} manually"
end

exit 1
end

exit 1
end
end