Class: App2Engine::Rake::Tasks

Inherits:
Object
  • Object
show all
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.expand_path("../../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

Constructor Details

#initializeTasks

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.expand_path("."))
  @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



73
74
75
76
77
78
79
80
81
82
# File 'lib/app2engine/rake/tasks.rb', line 73

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



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/app2engine/rake/tasks.rb', line 111

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



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/app2engine/rake/tasks.rb', line 98

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

#comment_whole_file(file) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/app2engine/rake/tasks.rb', line 84

def comment_whole_file file
  file = resolve_name(file)
  lines = File.readlines(file)
  new_lines = lines.map { |line|
    (line =~ /^(\s*|\s*#.+)$/) ? line : '# '+line
  }
  if new_lines == lines
    already_done file
  else
    File.open(file, 'w') { |fh| fh.write(new_lines.join) }
    status "Comment #{file}".green
  end
end

#convert_tasksObject



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_tasksObject



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

#gemspecObject



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

#generatorsObject



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

#hierarchyObject



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

#initializersObject



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
    Dir['config/initializers/{secret_token,session_store}.rb'].each { |file| comment_whole_file(file) }
  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

#replace_line(file, line, by) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/app2engine/rake/tasks.rb', line 127

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

#routesObject



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

#sassObject



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