From ab72430abdab491227f7a460f21ea2825d1c9beb Mon Sep 17 00:00:00 2001 From: Facundo Farias Date: Fri, 12 May 2017 08:45:37 +0200 Subject: [PATCH] Adding namespace to rake tasks --- README.md | 4 +- .../jekyll/blog/templates/tasks/gen.rake.tt | 28 ++++---- .../blog/templates/tasks/new_post.rake.tt | 64 ++++++++++--------- 3 files changed, 52 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 3535f35..3a9056b 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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! diff --git a/lib/generators/jekyll/blog/templates/tasks/gen.rake.tt b/lib/generators/jekyll/blog/templates/tasks/gen.rake.tt index ea909ab..648d30d 100644 --- a/lib/generators/jekyll/blog/templates/tasks/gen.rake.tt +++ b/lib/generators/jekyll/blog/templates/tasks/gen.rake.tt @@ -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 \ No newline at end of file diff --git a/lib/generators/jekyll/blog/templates/tasks/new_post.rake.tt b/lib/generators/jekyll/blog/templates/tasks/new_post.rake.tt index 7eed6f1..04b0c26 100644 --- a/lib/generators/jekyll/blog/templates/tasks/new_post.rake.tt +++ b/lib/generators/jekyll/blog/templates/tasks/new_post.rake.tt @@ -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 \ No newline at end of file