app2engine 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.md +55 -0
- data/bin/app2engine +10 -0
- data/lib/app2engine.rb +20 -0
- data/lib/app2engine/files/__project__.gemspec +7 -0
- data/lib/app2engine/files/lib/__project__.rb +3 -0
- data/lib/app2engine/files/lib/__project__/engine.rb +10 -0
- data/lib/app2engine/files/lib/generators/__project__/migrations/migrations_generator.rb +23 -0
- data/lib/app2engine/rake/convert_tasks.rb +56 -0
- data/lib/app2engine/rake/extra_tasks.rb +29 -0
- data/lib/app2engine/rake/tasks.rb +140 -0
- metadata +91 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 eregon
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# app2engine
|
2
|
+
|
3
|
+
Convert a Rails 3 app to an Engine.
|
4
|
+
|
5
|
+
The structure of a standard Rails application is very similar to what you need for an Engine.
|
6
|
+
|
7
|
+
But a few details need to be changed.
|
8
|
+
|
9
|
+
This tool intend to do most of them for you.
|
10
|
+
|
11
|
+
## Install
|
12
|
+
|
13
|
+
gem install app2engine
|
14
|
+
|
15
|
+
## Synopsis
|
16
|
+
|
17
|
+
Create an new app to convert it to an Engine (avoid using underscores):
|
18
|
+
|
19
|
+
rails new myengine #
|
20
|
+
|
21
|
+
Run `app2engine` in the root directory of this app you want to make an Engine:
|
22
|
+
|
23
|
+
app2engine
|
24
|
+
|
25
|
+
Then convert it with Rake:
|
26
|
+
|
27
|
+
rake engine:convert # or simply rake engine
|
28
|
+
|
29
|
+
Follow the instructions: To the main app's Gemfile, add
|
30
|
+
|
31
|
+
gem 'myengine', :path => 'path/to/myengine'
|
32
|
+
|
33
|
+
Use extras if you want:
|
34
|
+
|
35
|
+
rake engine:extra
|
36
|
+
|
37
|
+
## You are done setting up your engine
|
38
|
+
|
39
|
+
If you want a little test:
|
40
|
+
|
41
|
+
In your engine's dir:
|
42
|
+
|
43
|
+
rails g controller engine myaction
|
44
|
+
|
45
|
+
In your main app's dir:
|
46
|
+
|
47
|
+
rails g controller base myaction
|
48
|
+
|
49
|
+
`rails s` and surf on `/engine/myaction` and `/base/myaction` !
|
50
|
+
|
51
|
+
(You can also verify routing is fine with `rake routes`)
|
52
|
+
|
53
|
+
## Author
|
54
|
+
|
55
|
+
Benoit Daloze
|
data/bin/app2engine
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
3
|
+
require 'app2engine'
|
4
|
+
|
5
|
+
if ARGV.delete("-h") or ARGV.delete("--help")
|
6
|
+
puts "#{File.basename($0)} [dir=.]: install the Rake tasks to the Rakefile (or create it)"
|
7
|
+
puts " The App name is given from the directory. It will also be use in namespaces."
|
8
|
+
else
|
9
|
+
App2Engine.install(ARGV.shift || ".")
|
10
|
+
end
|
data/lib/app2engine.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'app2engine/rake/tasks'
|
2
|
+
|
3
|
+
module App2Engine
|
4
|
+
def install(dir)
|
5
|
+
unless File.directory?(dir) and
|
6
|
+
rakefile = File.join(dir, 'Rakefile') and File.exist?(rakefile) and
|
7
|
+
gemfile = File.join(dir, 'Gemfile') and File.exist?(gemfile)
|
8
|
+
raise "#{dir} is not a rails app"
|
9
|
+
end
|
10
|
+
|
11
|
+
tasks = App2Engine::Rake::Tasks.new
|
12
|
+
|
13
|
+
puts "Appending the App2Engine tasks to the Rakefile"
|
14
|
+
tasks.append_to_file(rakefile, "require 'app2engine/rake/tasks'\nApp2Engine::Rake::Tasks.new")
|
15
|
+
|
16
|
+
puts "Adding app2engine in the Gemfile (necessary for Rake tasks to run)"
|
17
|
+
tasks.append_to_file(gemfile, "gem 'app2engine'")
|
18
|
+
end
|
19
|
+
module_function :install
|
20
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
class MigrationsGenerator < Rails::Generators::Base
|
4
|
+
include Rails::Generators::Migration
|
5
|
+
|
6
|
+
def self.source_root
|
7
|
+
@source_root ||= File.expand_path('../../templates', __FILE__)
|
8
|
+
end
|
9
|
+
|
10
|
+
# Implement the required interface for Rails::Generators::Migration.
|
11
|
+
def self.next_migration_number(dirname)
|
12
|
+
if ActiveRecord::Base.timestamped_migrations
|
13
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
14
|
+
else
|
15
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def create_migration_file
|
20
|
+
# migration_template 'create_posts.rb', 'db/migrate/create_blog_posts.rb'
|
21
|
+
# migration_template '__DIR___create_posts.rb', 'db/migrate/__DIR___create_posts.rb'
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module App2Engine
|
2
|
+
module Rake
|
3
|
+
class Tasks
|
4
|
+
def convert_tasks
|
5
|
+
tasks = %w[gemspec routes hierarchy initializers generators]
|
6
|
+
task :convert => tasks.map { |t| "engine:convert:" << t } do
|
7
|
+
puts
|
8
|
+
puts "Now your app should be ready to be used as an Engine".blue
|
9
|
+
puts "You have to add this to you main app's Gemfile:".red
|
10
|
+
puts "gem '#{@dir}', :path => 'relative/path/to/#{@dir}'"
|
11
|
+
puts "You may want to remove the dependency to app2engine in the Engine's Gemfile".blue
|
12
|
+
end
|
13
|
+
namespace :convert do
|
14
|
+
tasks.each { |t| send(t) }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def gemspec
|
19
|
+
define_task(:gemspec, "add Jeweler to the Rakefile to allow to build a gem which can be referenced from the main app") do
|
20
|
+
add_file('__project__.gemspec')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def routes
|
25
|
+
define_task(:routes, "Change routes to not have a reference to the application, but to the main app") do
|
26
|
+
replace_line('config/routes.rb', "#{@project}::Application.routes.draw do", "Rails.application.routes.draw do")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def hierarchy
|
31
|
+
define_task(:hierarchy, "add the basic hierarchy for the Engine") do
|
32
|
+
add_file('lib/__project__.rb')
|
33
|
+
mkdir("lib/#{@dir}")
|
34
|
+
add_file('lib/__project__/engine.rb')
|
35
|
+
|
36
|
+
mkdir("app/controllers/__project__")
|
37
|
+
mkdir("app/models/__project__")
|
38
|
+
mkdir("app/views/__project__")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def initializers
|
43
|
+
define_task(:initializers, "remove initializers as they would conflict and create NameError") do
|
44
|
+
move_dir('config/initializers', 'config/org_initializers')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def generators
|
49
|
+
define_task(:generators, "add the basic code for generators (needed for migrations)") do
|
50
|
+
mkdir('lib/generators/__project__/migrations/templates')
|
51
|
+
add_file('lib/generators/__project__/migrations/migrations_generator.rb')
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end # Tasks
|
55
|
+
end # Rake
|
56
|
+
end # App2Engine
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module App2Engine
|
2
|
+
module Rake
|
3
|
+
class Tasks
|
4
|
+
SASS = <<SASS
|
5
|
+
initializer "sass" do |app|
|
6
|
+
require 'sass/plugin/rack'
|
7
|
+
template_location = __PROJECT__::Engine.root.join('public/stylesheets/sass').to_s
|
8
|
+
css_location = __PROJECT__::Engine.root.join('public/stylesheets').to_s
|
9
|
+
Sass::Plugin.add_template_location(template_location, css_location)
|
10
|
+
end
|
11
|
+
SASS
|
12
|
+
|
13
|
+
def extra_tasks
|
14
|
+
tasks = %w[sass]
|
15
|
+
task :extra => tasks.map { |t| "engine:extra:" << t }
|
16
|
+
namespace :extra do
|
17
|
+
tasks.each { |t| send(t) }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def sass
|
22
|
+
define_task(:sass, "configure the project to be used with Sass") do
|
23
|
+
append_in_class('lib/__project__/engine.rb', SASS)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end # Tasks
|
28
|
+
end # Rake
|
29
|
+
end # App2Engine
|
@@ -0,0 +1,140 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
require 'rake'
|
4
|
+
|
5
|
+
require 'app2engine/rake/convert_tasks'
|
6
|
+
require 'app2engine/rake/extra_tasks'
|
7
|
+
|
8
|
+
require 'term/ansicolor'
|
9
|
+
class String
|
10
|
+
[:green, :red, :black, :blue].each { |method|
|
11
|
+
define_method(method) {
|
12
|
+
Term::ANSIColor.send(method, self)
|
13
|
+
}
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
module App2Engine
|
18
|
+
module Rake
|
19
|
+
class Tasks
|
20
|
+
FILES_PATH = File.expand_path("../../files", __FILE__)
|
21
|
+
|
22
|
+
def initialize
|
23
|
+
@dir = File.basename(File.expand_path("."))
|
24
|
+
@project = @dir.split(/[^A-Za-z0-9]/).map(&:capitalize).join # Camelize
|
25
|
+
|
26
|
+
namespace :engine do
|
27
|
+
convert_tasks
|
28
|
+
extra_tasks
|
29
|
+
end
|
30
|
+
desc "Alias for engine:convert"
|
31
|
+
task :engine => "engine:convert"
|
32
|
+
end
|
33
|
+
|
34
|
+
def define_task(name, description, &block)
|
35
|
+
desc description
|
36
|
+
task(name) {
|
37
|
+
puts name.to_s.capitalize.blue
|
38
|
+
block.call
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
# Templates conventions
|
43
|
+
def resolve_contents(contents)
|
44
|
+
contents.gsub("__PROJECT__", @project).gsub("__DIR__", @dir)
|
45
|
+
end
|
46
|
+
|
47
|
+
def resolve_name(name)
|
48
|
+
name.gsub("__project__", @dir)
|
49
|
+
end
|
50
|
+
|
51
|
+
def status status
|
52
|
+
puts " #{status}"
|
53
|
+
end
|
54
|
+
|
55
|
+
def already_done what
|
56
|
+
status "already done (#{what})".black
|
57
|
+
end
|
58
|
+
|
59
|
+
def file_contents file
|
60
|
+
resolve_contents File.read(File.join(FILES_PATH, file))
|
61
|
+
end
|
62
|
+
|
63
|
+
def mkdir dir
|
64
|
+
dir = resolve_name(dir)
|
65
|
+
if File.directory? dir
|
66
|
+
already_done dir
|
67
|
+
else
|
68
|
+
FileUtils.mkdir_p(dir)
|
69
|
+
status "Create #{dir}/".green
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def move_dir dir, to
|
74
|
+
if File.directory? to
|
75
|
+
already_done dir
|
76
|
+
else
|
77
|
+
FileUtils.mv(dir, to)
|
78
|
+
status "Move #{dir} to #{to}".green
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def add_file file
|
83
|
+
contents = file_contents(file)
|
84
|
+
file = resolve_name(file)
|
85
|
+
if File.exist? file
|
86
|
+
already_done file
|
87
|
+
else
|
88
|
+
File.open(file, 'w') { |fh| fh.write(contents) }
|
89
|
+
status "Create #{file}".green
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def append_to_file file, contents
|
94
|
+
file = resolve_name(file)
|
95
|
+
if File.read(file).include?(contents)
|
96
|
+
already_done file
|
97
|
+
else
|
98
|
+
File.open(file, 'a') { |fh|
|
99
|
+
fh.puts
|
100
|
+
fh.puts contents
|
101
|
+
}
|
102
|
+
status "Append #{file}".green
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def append_in_class(file, what)
|
107
|
+
file = resolve_name(file)
|
108
|
+
what = resolve_contents(what)
|
109
|
+
if File.read(file).include?(what)
|
110
|
+
already_done file
|
111
|
+
else
|
112
|
+
lines = File.readlines(file)
|
113
|
+
class_indent = lines.find { |line| line =~ /^\s*class .+$/ }.split(//).index('c')
|
114
|
+
class_end = lines.rindex { |line| line =~ /^\s{#{class_indent}}end\s*$/ }
|
115
|
+
what = what.split("\n").map { |line| line.chomp + "\n" }
|
116
|
+
lines = lines[0...class_end] + ["\n"] + what + lines[class_end..-1]
|
117
|
+
File.open(file, 'w') { |fh| fh.write(lines.join) }
|
118
|
+
status "Append #{file}".green
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def replace_line(file, line, by)
|
123
|
+
line = line.chomp + "\n"
|
124
|
+
by = by.chomp + "\n"
|
125
|
+
lines = File.readlines(file)
|
126
|
+
if lines.include? by
|
127
|
+
already_done(file)
|
128
|
+
else
|
129
|
+
if i = lines.index(line)
|
130
|
+
lines[i] = by
|
131
|
+
File.open(file, 'w') { |fh| fh.write(lines.join) }
|
132
|
+
status "Edit #{file}".green
|
133
|
+
else
|
134
|
+
status "#{file}: line '#{line}' not found".red
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end # Tasks
|
139
|
+
end # Rake
|
140
|
+
end # App2Engine
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: app2engine
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- eregon
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-08-04 00:00:00 +02:00
|
19
|
+
default_executable: app2engine
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: term-ansicolor
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
description: Ease the convertion of a Rails 3 app in an Engine
|
36
|
+
email: eregontp@gmail.com
|
37
|
+
executables:
|
38
|
+
- app2engine
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.md
|
44
|
+
files:
|
45
|
+
- bin/app2engine
|
46
|
+
- lib/app2engine.rb
|
47
|
+
- lib/app2engine/files/__project__.gemspec
|
48
|
+
- lib/app2engine/files/lib/__project__.rb
|
49
|
+
- lib/app2engine/files/lib/__project__/engine.rb
|
50
|
+
- lib/app2engine/files/lib/generators/__project__/migrations/migrations_generator.rb
|
51
|
+
- lib/app2engine/rake/convert_tasks.rb
|
52
|
+
- lib/app2engine/rake/extra_tasks.rb
|
53
|
+
- lib/app2engine/rake/tasks.rb
|
54
|
+
- LICENSE
|
55
|
+
- README.md
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: http://github.com/eregon/app2engine
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options:
|
62
|
+
- --charset=UTF-8
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
hash: 3
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.3.7
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: Convert a Rails 3 app to an Engine
|
90
|
+
test_files: []
|
91
|
+
|