Class: App2Engine::Rake::Tasks
- Inherits:
-
Object
- Object
- App2Engine::Rake::Tasks
- Defined in:
- lib/app2engine/rake/tasks.rb,
lib/app2engine/rake/extra_tasks.rb,
lib/app2engine/rake/convert_tasks.rb
Constant Summary collapse
- FILES_PATH =
File.("../../files", __FILE__)
- SASS =
<<SASS initializer "sass" do |app| require 'sass/plugin/rack' template_location = __PROJECT__::Engine.root.join('public/stylesheets/sass').to_s css_location = __PROJECT__::Engine.root.join('public/stylesheets').to_s Sass::Plugin.add_template_location(template_location, css_location) end SASS
Instance Method Summary collapse
- #add_file(file) ⇒ Object
- #already_done(what) ⇒ Object
- #append_in_class(file, what) ⇒ Object
- #append_to_file(file, contents) ⇒ Object
- #convert_tasks ⇒ Object
- #define_task(name, description, &block) ⇒ Object
- #extra_tasks ⇒ Object
- #file_contents(file) ⇒ Object
- #gemspec ⇒ Object
- #generators ⇒ Object
- #hierarchy ⇒ Object
-
#initialize ⇒ Tasks
constructor
A new instance of Tasks.
- #initializers ⇒ Object
- #mkdir(dir) ⇒ Object
- #move_dir(dir, to) ⇒ Object
- #replace_line(file, line, by) ⇒ Object
-
#resolve_contents(contents) ⇒ Object
Templates conventions.
- #resolve_name(name) ⇒ Object
- #routes ⇒ Object
- #sass ⇒ Object
- #status(status) ⇒ Object
Constructor Details
#initialize ⇒ Tasks
Returns a new instance of Tasks.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/app2engine/rake/tasks.rb', line 22 def initialize @dir = File.basename(File.(".")) @project = @dir.split(/[^A-Za-z0-9]/).map(&:capitalize).join # Camelize namespace :engine do convert_tasks extra_tasks end desc "Alias for engine:convert" task :engine => "engine:convert" end |
Instance Method Details
#add_file(file) ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/app2engine/rake/tasks.rb', line 82 def add_file file contents = file_contents(file) file = resolve_name(file) if File.exist? file already_done file else File.open(file, 'w') { |fh| fh.write(contents) } status "Create #{file}".green end end |
#already_done(what) ⇒ Object
55 56 57 |
# File 'lib/app2engine/rake/tasks.rb', line 55 def already_done what status "already done (#{what})".black # yellow end |
#append_in_class(file, what) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/app2engine/rake/tasks.rb', line 106 def append_in_class(file, what) file = resolve_name(file) what = resolve_contents(what) if File.read(file).include?(what) already_done file else lines = File.readlines(file) class_indent = lines.find { |line| line =~ /^\s*class .+$/ }.split(//).index('c') class_end = lines.rindex { |line| line =~ /^\s{#{class_indent}}end\s*$/ } what = what.split("\n").map { |line| line.chomp + "\n" } lines = lines[0...class_end] + ["\n"] + what + lines[class_end..-1] File.open(file, 'w') { |fh| fh.write(lines.join) } status "Append #{file}".green end end |
#append_to_file(file, contents) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/app2engine/rake/tasks.rb', line 93 def append_to_file file, contents file = resolve_name(file) if File.read(file).include?(contents) already_done file else File.open(file, 'a') { |fh| fh.puts fh.puts contents } status "Append #{file}".green end end |
#convert_tasks ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/app2engine/rake/convert_tasks.rb', line 4 def convert_tasks tasks = %w[gemspec routes hierarchy initializers generators] task :convert => tasks.map { |t| "engine:convert:" << t } do puts puts "Now your app should be ready to be used as an Engine".blue puts "You have to add this to you main app's Gemfile:".red puts "gem '#{@dir}', :path => 'path/to/#{@dir}'" puts "You may want to remove the dependency to app2engine in the Engine's Gemfile".blue end namespace :convert do tasks.each { |t| send(t) } end end |
#define_task(name, description, &block) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/app2engine/rake/tasks.rb', line 34 def define_task(name, description, &block) desc description task(name) { puts name.to_s.capitalize.blue block.call } end |
#extra_tasks ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/app2engine/rake/extra_tasks.rb', line 13 def extra_tasks tasks = %w[sass] task :extra => tasks.map { |t| "engine:extra:" << t } namespace :extra do tasks.each { |t| send(t) } end end |
#file_contents(file) ⇒ Object
59 60 61 |
# File 'lib/app2engine/rake/tasks.rb', line 59 def file_contents file resolve_contents File.read(File.join(FILES_PATH, file)) end |
#gemspec ⇒ Object
18 19 20 21 22 |
# File 'lib/app2engine/rake/convert_tasks.rb', line 18 def gemspec define_task(:gemspec, "add Jeweler to the Rakefile to allow to build a gem which can be referenced from the main app") do add_file('__project__.gemspec') end end |
#generators ⇒ Object
48 49 50 51 52 53 |
# File 'lib/app2engine/rake/convert_tasks.rb', line 48 def generators define_task(:generators, "add the basic code for generators (needed for migrations)") do mkdir('lib/generators/__project__/migrations/templates') add_file('lib/generators/__project__/migrations/migrations_generator.rb') end end |
#hierarchy ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/app2engine/rake/convert_tasks.rb', line 30 def hierarchy define_task(:hierarchy, "add the basic hierarchy for the Engine") do add_file('lib/__project__.rb') mkdir("lib/#{@dir}") add_file('lib/__project__/engine.rb') mkdir("app/controllers/__project__") mkdir("app/models/__project__") mkdir("app/views/__project__") end end |
#initializers ⇒ Object
42 43 44 45 46 |
# File 'lib/app2engine/rake/convert_tasks.rb', line 42 def initializers define_task(:initializers, "remove initializers as they would conflict and create NameError") do move_dir('config/initializers', 'config/org_initializers') end end |
#mkdir(dir) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/app2engine/rake/tasks.rb', line 63 def mkdir dir dir = resolve_name(dir) if File.directory? dir already_done dir else FileUtils.mkdir_p(dir) status "Create #{dir}/".green end end |
#move_dir(dir, to) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/app2engine/rake/tasks.rb', line 73 def move_dir dir, to if File.directory? to already_done dir else FileUtils.mv(dir, to) status "Move #{dir} to #{to}".green end end |
#replace_line(file, line, by) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/app2engine/rake/tasks.rb', line 122 def replace_line(file, line, by) line = line.chomp + "\n" by = by.chomp + "\n" lines = File.readlines(file) if lines.include? by already_done(file) else if i = lines.index(line) lines[i] = by File.open(file, 'w') { |fh| fh.write(lines.join) } status "Edit #{file}".green else status "#{file}: line '#{line}' not found".red end end end |
#resolve_contents(contents) ⇒ Object
Templates conventions
43 44 45 |
# File 'lib/app2engine/rake/tasks.rb', line 43 def resolve_contents(contents) contents.gsub("__PROJECT__", @project).gsub("__DIR__", @dir) end |
#resolve_name(name) ⇒ Object
47 48 49 |
# File 'lib/app2engine/rake/tasks.rb', line 47 def resolve_name(name) name.gsub("__project__", @dir) end |
#routes ⇒ Object
24 25 26 27 28 |
# File 'lib/app2engine/rake/convert_tasks.rb', line 24 def routes define_task(:routes, "Change routes to not have a reference to the application, but to the main app") do replace_line('config/routes.rb', "#{@project}::Application.routes.draw do", "Rails.application.routes.draw do") end end |
#sass ⇒ Object
21 22 23 24 25 |
# File 'lib/app2engine/rake/extra_tasks.rb', line 21 def sass define_task(:sass, "configure the project to be used with Sass") do append_in_class('lib/__project__/engine.rb', SASS) end end |
#status(status) ⇒ Object
51 52 53 |
# File 'lib/app2engine/rake/tasks.rb', line 51 def status status puts " #{status}" end |