ikhono-ikhono-desert 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/CHANGES +28 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +295 -0
  4. data/Rakefile +100 -0
  5. data/generators/desert_plugin/USAGE +14 -0
  6. data/generators/desert_plugin/desert_plugin_generator.rb +73 -0
  7. data/generators/desert_plugin/templates/empty_file +0 -0
  8. data/generators/desert_plugin/templates/plugin_migration.rb +11 -0
  9. data/generators/desert_plugin/templates/routes.rb +4 -0
  10. data/generators/desert_plugin/templates/spec_helper.rb +8 -0
  11. data/init.rb +0 -0
  12. data/lib/desert.rb +14 -0
  13. data/lib/desert/manager.rb +116 -0
  14. data/lib/desert/plugin.rb +74 -0
  15. data/lib/desert/plugin_migrations.rb +3 -0
  16. data/lib/desert/plugin_migrations/extensions/1.0/schema_statements.rb +34 -0
  17. data/lib/desert/plugin_migrations/extensions/2.1/schema_statements.rb +12 -0
  18. data/lib/desert/plugin_migrations/extensions/schema_statements.rb +6 -0
  19. data/lib/desert/plugin_migrations/migrator.rb +54 -0
  20. data/lib/desert/plugin_templates.rb +10 -0
  21. data/lib/desert/plugin_templates/1.2.0/action_mailer.rb +21 -0
  22. data/lib/desert/plugin_templates/1.2.0/action_view.rb +53 -0
  23. data/lib/desert/plugin_templates/1.99.0/action_mailer.rb +25 -0
  24. data/lib/desert/plugin_templates/1.99.0/action_view.rb +38 -0
  25. data/lib/desert/plugin_templates/2.0.0/action_mailer.rb +23 -0
  26. data/lib/desert/plugin_templates/2.0.2/action_view.rb +26 -0
  27. data/lib/desert/plugin_templates/2.1.0/action_view.rb +13 -0
  28. data/lib/desert/plugin_templates/action_controller.rb +14 -0
  29. data/lib/desert/plugin_templates/action_view.rb +16 -0
  30. data/lib/desert/plugin_templates/edge/action_view.rb +10 -0
  31. data/lib/desert/rails.rb +11 -0
  32. data/lib/desert/rails/1.2.0/initializer.rb +20 -0
  33. data/lib/desert/rails/2.0.0/plugin.rb +22 -0
  34. data/lib/desert/rails/dependencies.rb +87 -0
  35. data/lib/desert/rails/migration.rb +36 -0
  36. data/lib/desert/rails/observer.rb +22 -0
  37. data/lib/desert/rails/route_set.rb +23 -0
  38. data/lib/desert/ruby.rb +2 -0
  39. data/lib/desert/ruby/object.rb +34 -0
  40. data/lib/desert/supported_rails_versions.rb +9 -0
  41. data/lib/desert/version_checker.rb +26 -0
  42. metadata +95 -0
@@ -0,0 +1,73 @@
1
+ require 'rails_generator'
2
+ require 'rails_generator/commands'
3
+
4
+ class DesertPluginGenerator < Rails::Generator::NamedBase
5
+ def manifest
6
+ record do |m|
7
+ m.directory "vendor/plugins/#{file_name}"
8
+
9
+ m.directory "vendor/plugins/#{file_name}/app"
10
+ m.directory "vendor/plugins/#{file_name}/app/controllers"
11
+ m.directory "vendor/plugins/#{file_name}/app/helpers"
12
+ m.directory "vendor/plugins/#{file_name}/app/models"
13
+ m.directory "vendor/plugins/#{file_name}/app/views"
14
+
15
+ m.directory "vendor/plugins/#{file_name}/config"
16
+ m.template "routes.rb", "vendor/plugins/#{file_name}/config/routes.rb"
17
+ m.map_route_from_plugin
18
+
19
+ m.directory "vendor/plugins/#{file_name}/db"
20
+ m.directory "vendor/plugins/#{file_name}/db/migrate"
21
+ # m.template "plugin_migration.rb", "vendor/plugins/#{file_name}/db/migrate/001_init_#{file_name}_plugin.rb"
22
+
23
+ m.directory "vendor/plugins/#{file_name}/lib"
24
+
25
+ m.directory "vendor/plugins/#{file_name}/spec"
26
+ m.directory "vendor/plugins/#{file_name}/spec/controllers"
27
+ m.directory "vendor/plugins/#{file_name}/spec/fixtures"
28
+ m.directory "vendor/plugins/#{file_name}/spec/models"
29
+ m.directory "vendor/plugins/#{file_name}/spec/views"
30
+ m.file "spec_helper.rb", "vendor/plugins/#{file_name}/spec/spec_helper.rb"
31
+
32
+ m.directory "vendor/plugins/#{file_name}/tasks"
33
+
34
+ m.file "empty_file", "vendor/plugins/#{file_name}/init.rb"
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ module Desert #:nodoc:
41
+ module Generator #:nodoc:
42
+ module Commands #:nodoc:
43
+
44
+ module Create
45
+ def map_route_from_plugin
46
+ logger.route "adding map.routes_from_plugin(:#{file_name}) to top of routes.rb"
47
+ sentinel = 'ActionController::Routing::Routes.draw do |map|'
48
+ gsub_file('config/routes.rb', /(#{Regexp.escape(sentinel)})/mi) do |match|
49
+ "#{match}\n map.routes_from_plugin(:#{file_name})\n"
50
+ end
51
+ end
52
+ end
53
+
54
+ module Destroy
55
+ def map_route_from_plugin
56
+ look_for = "\n map.routes_from_plugin(:#{file_name})\n"
57
+ logger.route "removing map.routes_from_plugin(:#{file_name}) from routes.rb"
58
+ gsub_file 'config/routes.rb', /(#{Regexp.escape(look_for)})/mi, ''
59
+ end
60
+ end
61
+
62
+ module List
63
+ def map_route_from_plugin
64
+ logger.route "adding map.routes_from_plugin(:#{file_name}) to top of routes.rb"
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ Rails::Generator::Commands::Create.send :include, Desert::Generator::Commands::Create
72
+ Rails::Generator::Commands::Destroy.send :include, Desert::Generator::Commands::Destroy
73
+ Rails::Generator::Commands::List.send :include, Desert::Generator::Commands::List
File without changes
@@ -0,0 +1,11 @@
1
+ class Init<%= class_name %>Plugin < ActiveRecord::Migration
2
+ def self.up
3
+ create_table "<%= plural_name %>", :force => true do |t|
4
+ t.column "some_<%= file_name %>_column", :string
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ drop_table :<%= plural_name %>
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ # Add your custom routes here. If in config/routes.rb you would
2
+ # add <tt>map.resources</tt>, here you would add just <tt>resources</tt>
3
+
4
+ # resources :<%= plural_name %>
@@ -0,0 +1,8 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+ require File.expand_path(File.dirname(__FILE__) + "/../../../../config/environment")
3
+ require 'spec'
4
+ require 'spec/rails'
5
+
6
+ Spec::Runner.configure do |config|
7
+ config.fixture_path = "#{File.dirname(__FILE__)}/../spec/fixtures"
8
+ end
data/init.rb ADDED
File without changes
data/lib/desert.rb ADDED
@@ -0,0 +1,14 @@
1
+ require "active_support"
2
+ require "active_record"
3
+ require "action_controller"
4
+ require "action_mailer"
5
+
6
+ dir = File.dirname(__FILE__)
7
+ require "#{dir}/desert/supported_rails_versions"
8
+ require "#{dir}/desert/plugin"
9
+ require "#{dir}/desert/manager"
10
+ require "#{dir}/desert/version_checker"
11
+ require "#{dir}/desert/rails"
12
+ require "#{dir}/desert/ruby"
13
+ require "#{dir}/desert/plugin_migrations"
14
+ require "#{dir}/desert/plugin_templates"
@@ -0,0 +1,116 @@
1
+ module Desert
2
+ class Manager # nodoc
3
+ class << self
4
+ def instance
5
+ @instance ||= new
6
+ end
7
+ attr_writer :instance
8
+
9
+ protected
10
+ def method_missing(method_name, *args, &block)
11
+ instance.__send__(method_name, *args, &block)
12
+ end
13
+ end
14
+
15
+ attr_reader :loading_plugin, :plugins_in_registration
16
+
17
+ def initialize
18
+ @plugins = []
19
+ @plugins_in_registration = []
20
+ end
21
+
22
+ def plugins
23
+ @plugins.dup
24
+ end
25
+
26
+ def load_paths
27
+ paths = []
28
+ plugin_paths.each do |component_root|
29
+ paths << File.join(component_root, 'app')
30
+ paths << File.join(component_root, 'app','models')
31
+ paths << File.join(component_root, 'app','controllers')
32
+ paths << File.join(component_root, 'app','helpers')
33
+ paths << File.join(component_root, 'lib')
34
+ end
35
+ dependencies.load_paths.reverse.each do |path|
36
+ paths << File.expand_path(path) unless paths.include?(File.expand_path(path))
37
+ end
38
+ paths
39
+ end
40
+
41
+ def register_plugin(plugin_path)
42
+ plugin = Plugin.new(plugin_path)
43
+ @plugins_in_registration << plugin
44
+
45
+ yield if block_given?
46
+
47
+ dependencies.load_paths << plugin.models_path
48
+ dependencies.load_paths << plugin.controllers_path
49
+ dependencies.load_paths << plugin.helpers_path
50
+
51
+ @plugins_in_registration.pop
52
+
53
+ if existing_plugin = find_plugin(plugin.name)
54
+ return existing_plugin
55
+ end
56
+
57
+ @plugins << plugin
58
+ plugin
59
+ end
60
+
61
+ def find_plugin(name_or_directory)
62
+ name = File.basename(File.expand_path(name_or_directory))
63
+ plugins.find do |plugin|
64
+ plugin.name == name
65
+ end
66
+ end
67
+
68
+ def plugin_exists?(name_or_directory)
69
+ !find_plugin(name_or_directory).nil?
70
+ end
71
+
72
+ def plugin_path(name)
73
+ plugin = find_plugin(name)
74
+ return nil unless plugin
75
+ plugin.path
76
+ end
77
+
78
+ def files_on_load_path(file)
79
+ desert_file_exists = false
80
+ files = []
81
+ load_paths.each do |path|
82
+ full_path = File.join(path, file)
83
+ full_path << '.rb' unless File.extname(full_path) == '.rb'
84
+ files << full_path if File.exists?(full_path)
85
+ end
86
+ files
87
+ end
88
+
89
+ def directory_on_load_path?(dir_suffix)
90
+ Desert::Manager.load_paths.each do |path|
91
+ return true if File.directory?(File.join(path, dir_suffix))
92
+ end
93
+ return false
94
+ end
95
+
96
+ def layout_paths
97
+ layout_paths = plugins.reverse.collect do |plugin|
98
+ plugin.layouts_path
99
+ end
100
+ layout_paths
101
+ end
102
+
103
+ protected
104
+ def dependencies
105
+ @dependencies ||= ActiveSupport.const_defined?(:Dependencies) ? ActiveSupport::Dependencies : Dependencies
106
+ end
107
+
108
+ def plugin_paths
109
+ plugins_and_app.collect { |plugin| plugin.path }
110
+ end
111
+
112
+ def plugins_and_app
113
+ plugins + @plugins_in_registration + [Plugin.new(RAILS_ROOT)]
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,74 @@
1
+ module Desert
2
+ class Plugin # nodoc
3
+ attr_reader :name, :path
4
+ def initialize(path)
5
+ @path = File.expand_path(path)
6
+ @name = File.basename(@path)
7
+ end
8
+
9
+ def migration_path
10
+ "#{@path}/db/migrate"
11
+ end
12
+
13
+ # The path to the views for this plugin
14
+ def templates_path
15
+ "#{@path}/app/views"
16
+ end
17
+
18
+ def controllers_path
19
+ "#{@path}/app/controllers"
20
+ end
21
+
22
+ # TODO: Test me
23
+ def models_path
24
+ "#{@path}/app/models"
25
+ end
26
+
27
+ # TODO: Test me
28
+ def helpers_path
29
+ "#{@path}/app/helpers"
30
+ end
31
+
32
+ # The path to the layout for this plugin
33
+ def layouts_path
34
+ "#{templates_path}/layouts"
35
+ end
36
+
37
+ # Finds a template with the specified path
38
+ def find_template(template)
39
+ template_path = "#{templates_path}/#{template}"
40
+ File.exists?(template_path) ? template_path : nil
41
+ end
42
+
43
+ def framework_paths
44
+ # TODO: Don't include dirs for frameworks that are not used
45
+ %w(
46
+ railties
47
+ railties/lib
48
+ actionpack/lib
49
+ activesupport/lib
50
+ activerecord/lib
51
+ actionmailer/lib
52
+ actionwebservice/lib
53
+ ).map { |dir| "#{framework_root_path}/#{dir}" }.select { |dir| File.directory?(dir) }
54
+ end
55
+
56
+ def ==(other)
57
+ self.path == other.path
58
+ end
59
+
60
+ def migration
61
+ @migration ||= PluginMigrations::Migrator.new(:up, migration_path)
62
+ end
63
+
64
+ def with_current_plugin
65
+ old_plugin = PluginMigrations::Migrator.current_plugin
66
+ begin
67
+ PluginMigrations::Migrator.current_plugin = self
68
+ yield
69
+ ensure
70
+ PluginMigrations::Migrator.current_plugin = old_plugin
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ dir = File.dirname(__FILE__)
2
+ require "#{dir}/plugin_migrations/migrator"
3
+ require "#{dir}/plugin_migrations/extensions/schema_statements"
@@ -0,0 +1,34 @@
1
+ ActiveRecord::ConnectionAdapters::SchemaStatements.module_eval do
2
+ def initialize_schema_information_with_plugins
3
+ initialize_schema_information_without_plugins
4
+
5
+ begin
6
+ execute "CREATE TABLE #{Desert::PluginMigrations::Migrator.schema_info_table_name} (plugin_name #{type_to_sql(:string)}, version #{type_to_sql(:integer)})"
7
+ rescue ActiveRecord::StatementInvalid
8
+ # Schema has been initialized
9
+ end
10
+ end
11
+ alias_method_chain :initialize_schema_information, :plugins
12
+
13
+ def dump_schema_information_with_plugins
14
+ schema_information = []
15
+
16
+ dump = dump_schema_information_without_plugins
17
+ schema_information << dump if dump
18
+
19
+ begin
20
+ plugins = ActiveRecord::Base.connection.select_all("SELECT * FROM #{Desert::PluginMigrations::Migrator.schema_info_table_name}")
21
+ plugins.each do |plugin|
22
+ if (version = plugin['version'].to_i) > 0
23
+ plugin_name = ActiveRecord::Base.quote_value(plugin['plugin_name'])
24
+ schema_information << "INSERT INTO #{Desert::PluginMigrations::Migrator.schema_info_table_name} (plugin_name, version) VALUES (#{plugin_name}, #{version})"
25
+ end
26
+ end
27
+ rescue ActiveRecord::StatementInvalid
28
+ # No Schema Info
29
+ end
30
+
31
+ schema_information.join(";\n")
32
+ end
33
+ alias_method_chain :dump_schema_information, :plugins
34
+ end
@@ -0,0 +1,12 @@
1
+ ActiveRecord::ConnectionAdapters::SchemaStatements.module_eval do
2
+ def initialize_schema_migrations_table_with_plugins
3
+ initialize_schema_migrations_table_without_plugins
4
+
5
+ begin
6
+ execute "CREATE TABLE #{Desert::PluginMigrations::Migrator.schema_info_table_name} (plugin_name #{type_to_sql(:string)}, version #{type_to_sql(:integer)})"
7
+ rescue ActiveRecord::StatementInvalid
8
+ # Schema has been initialized
9
+ end
10
+ end
11
+ alias_method_chain :initialize_schema_migrations_table, :plugins
12
+ end
@@ -0,0 +1,6 @@
1
+ dir = File.dirname(__FILE__)
2
+ if ActiveRecord::ConnectionAdapters::SchemaStatements.instance_methods.include?('initialize_schema_information')
3
+ require "#{dir}/1.0/schema_statements"
4
+ else
5
+ require "#{dir}/2.1/schema_statements"
6
+ end
@@ -0,0 +1,54 @@
1
+ module Desert #:nodoc:
2
+ module PluginMigrations
3
+ # Responsible for migrating plugins. PluginMigrations.Migrator.current_plugin
4
+ # indicates which plugin is currently being migrated
5
+ class Migrator < ActiveRecord::Migrator
6
+ # We need to be able to set the current plugin being migrated.
7
+ cattr_accessor :current_plugin
8
+
9
+ class << self
10
+ # Runs the migrations from a plugin, up (or down) to the version given
11
+ def migrate_plugin(plugin, version = nil)
12
+ self.current_plugin = plugin
13
+ if ActiveRecord::Base.connection.respond_to?(:initialize_schema_migrations_table)
14
+ ActiveRecord::Base.connection.initialize_schema_migrations_table
15
+ end
16
+ migrate(plugin.migration_path, version)
17
+ end
18
+
19
+ def schema_info_table_name #:nodoc:
20
+ ActiveRecord::Base.table_name_prefix + 'plugin_schema_info' + ActiveRecord::Base.table_name_suffix
21
+ end
22
+ alias_method :schema_migrations_table_name, :schema_info_table_name
23
+
24
+ def current_version #:nodoc:
25
+ result = ActiveRecord::Base.connection.select_one("SELECT version FROM #{schema_info_table_name} WHERE plugin_name = '#{current_plugin.name}'")
26
+ if result
27
+ result['version'].to_i
28
+ else
29
+ # There probably isn't an entry for this plugin in the migration info table.
30
+ 0
31
+ end
32
+ end
33
+ end
34
+
35
+ def set_schema_version(version)
36
+ version = down? ? version.to_i - 1 : version.to_i
37
+
38
+ if ActiveRecord::Base.connection.select_one("SELECT version FROM #{self.class.schema_info_table_name} WHERE plugin_name = '#{current_plugin.name}'").nil?
39
+ # We need to create the entry since it doesn't exist
40
+ ActiveRecord::Base.connection.execute("INSERT INTO #{self.class.schema_info_table_name} (version, plugin_name) VALUES (#{version},'#{current_plugin.name}')")
41
+ else
42
+ ActiveRecord::Base.connection.update("UPDATE #{self.class.schema_info_table_name} SET version = #{version} WHERE plugin_name = '#{current_plugin.name}'")
43
+ end
44
+ end
45
+ alias_method :record_version_state_after_migrating, :set_schema_version
46
+
47
+
48
+ def migrated
49
+ current_plugin_version = self.class.current_version
50
+ (1..current_plugin_version).to_a
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,10 @@
1
+ dir = File.dirname(__FILE__)
2
+ require "#{dir}/plugin_templates/action_controller"
3
+ if Desert::VersionChecker.rails_version_is_below_1990?
4
+ require "#{dir}/plugin_templates/1.2.0/action_mailer"
5
+ elsif Desert::VersionChecker.rails_version_is_below_rc2?
6
+ require "#{dir}/plugin_templates/1.99.0/action_mailer"
7
+ else
8
+ require "#{dir}/plugin_templates/2.0.0/action_mailer"
9
+ end
10
+ require "#{dir}/plugin_templates/action_view"