bborn-desert 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/CHANGES +50 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +316 -0
  4. data/Rakefile +77 -0
  5. data/VERSION.yml +2 -0
  6. data/generators/desert_plugin/USAGE +14 -0
  7. data/generators/desert_plugin/desert_plugin_generator.rb +73 -0
  8. data/generators/desert_plugin/templates/desert_routes.rb +4 -0
  9. data/generators/desert_plugin/templates/empty_file +0 -0
  10. data/generators/desert_plugin/templates/plugin_migration.rb +11 -0
  11. data/generators/desert_plugin/templates/spec_helper.rb +8 -0
  12. data/init.rb +0 -0
  13. data/lib/desert.rb +14 -0
  14. data/lib/desert/manager.rb +131 -0
  15. data/lib/desert/plugin.rb +74 -0
  16. data/lib/desert/plugin_migrations.rb +11 -0
  17. data/lib/desert/plugin_migrations/1.2/extensions/schema_statements.rb +34 -0
  18. data/lib/desert/plugin_migrations/1.2/migrator.rb +33 -0
  19. data/lib/desert/plugin_migrations/2.1/extensions/schema_statements.rb +39 -0
  20. data/lib/desert/plugin_migrations/2.1/migrator.rb +35 -0
  21. data/lib/desert/plugin_migrations/migrator.rb +29 -0
  22. data/lib/desert/plugin_templates.rb +13 -0
  23. data/lib/desert/plugin_templates/1.2.0/action_mailer.rb +21 -0
  24. data/lib/desert/plugin_templates/1.2.0/action_view.rb +53 -0
  25. data/lib/desert/plugin_templates/1.99.0/action_mailer.rb +25 -0
  26. data/lib/desert/plugin_templates/1.99.0/action_view.rb +38 -0
  27. data/lib/desert/plugin_templates/2.0.0/action_mailer.rb +23 -0
  28. data/lib/desert/plugin_templates/2.0.2/action_view.rb +26 -0
  29. data/lib/desert/plugin_templates/2.1.0/action_view.rb +15 -0
  30. data/lib/desert/plugin_templates/2.2.0/action_mailer.rb +23 -0
  31. data/lib/desert/plugin_templates/2.2.0/action_view.rb +10 -0
  32. data/lib/desert/plugin_templates/action_controller.rb +12 -0
  33. data/lib/desert/plugin_templates/action_view.rb +17 -0
  34. data/lib/desert/rails.rb +10 -0
  35. data/lib/desert/rails/1.2.0/initializer.rb +20 -0
  36. data/lib/desert/rails/2.0.0/plugin.rb +22 -0
  37. data/lib/desert/rails/dependencies.rb +87 -0
  38. data/lib/desert/rails/migration.rb +36 -0
  39. data/lib/desert/rails/route_set.rb +23 -0
  40. data/lib/desert/ruby.rb +2 -0
  41. data/lib/desert/ruby/object.rb +34 -0
  42. data/lib/desert/supported_rails_versions.rb +12 -0
  43. data/lib/desert/tasks.rb +4 -0
  44. data/lib/desert/version_checker.rb +34 -0
  45. metadata +105 -0
@@ -0,0 +1,2 @@
1
+ ---
2
+ :version: 0.5.3
@@ -0,0 +1,14 @@
1
+ NAME
2
+ desert_plugin - creates a directory structure and starter files for a new desert plugin
3
+
4
+ SYNOPSIS
5
+ desert_plugin [plugin name]
6
+
7
+ DESCRIPTION
8
+ |-- vendor
9
+ `-- plugins
10
+ `-- [plugin name]
11
+
12
+ EXAMPLE
13
+ ./script/generate desert_plugin spiffy
14
+
@@ -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
@@ -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,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,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
@@ -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,131 @@
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, 'app','sweepers')
34
+ paths << File.join(component_root, 'lib')
35
+ end
36
+ dependencies.load_paths.reverse_each do |path|
37
+ paths << File.expand_path(path)
38
+ end
39
+ paths.uniq!
40
+ paths
41
+ end
42
+
43
+ def register_plugin(plugin_path)
44
+ plugin = Plugin.new(plugin_path)
45
+ @plugins_in_registration << plugin
46
+
47
+ yield if block_given?
48
+
49
+ dependencies.load_paths << plugin.models_path
50
+ dependencies.load_paths << plugin.controllers_path
51
+ dependencies.load_paths << plugin.helpers_path
52
+
53
+ @plugins_in_registration.pop
54
+
55
+ if existing_plugin = find_plugin(plugin.name)
56
+ return existing_plugin
57
+ end
58
+
59
+ @plugins << plugin
60
+ plugin
61
+ end
62
+
63
+ def find_plugin(name_or_directory)
64
+ name = File.basename(File.expand_path(name_or_directory))
65
+ plugins.find do |plugin|
66
+ plugin.name == name
67
+ end
68
+ end
69
+
70
+ def plugin_exists?(name_or_directory)
71
+ !find_plugin(name_or_directory).nil?
72
+ end
73
+
74
+ def plugin_path(name)
75
+ plugin = find_plugin(name)
76
+ return nil unless plugin
77
+ plugin.path
78
+ end
79
+
80
+ def files_on_load_path(file)
81
+ desert_file_exists = false
82
+ files = []
83
+ load_paths.each do |path|
84
+ full_path = File.join(path, file)
85
+ full_path << '.rb' unless File.extname(full_path) == '.rb'
86
+ files << full_path if File.exists?(full_path)
87
+ end
88
+ files
89
+ end
90
+
91
+ def directory_on_load_path?(dir_suffix)
92
+ Desert::Manager.load_paths.each do |path|
93
+ return true if File.directory?(File.join(path, dir_suffix))
94
+ end
95
+ return false
96
+ end
97
+
98
+ def layout_paths
99
+ layout_paths = plugins.reverse.collect do |plugin|
100
+ plugin.layouts_path
101
+ end
102
+ layout_paths
103
+ end
104
+
105
+ def require_all_files
106
+ all_files.each do |file|
107
+ require file
108
+ end
109
+ end
110
+
111
+ def all_files
112
+ Desert::Manager.load_paths.inject([]) do |all, load_path|
113
+ all |= Dir["#{load_path}/**/*.rb"]
114
+ all
115
+ end
116
+ end
117
+
118
+ protected
119
+ def dependencies
120
+ @dependencies ||= ActiveSupport.const_defined?(:Dependencies) ? ActiveSupport::Dependencies : Dependencies
121
+ end
122
+
123
+ def plugin_paths
124
+ plugins_and_app.collect { |plugin| plugin.path }
125
+ end
126
+
127
+ def plugins_and_app
128
+ plugins + @plugins_in_registration + [Plugin.new(RAILS_ROOT)]
129
+ end
130
+ end
131
+ 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,11 @@
1
+ dir = File.dirname(__FILE__)
2
+
3
+ dir = File.dirname(__FILE__)
4
+ require "#{dir}/plugin_migrations/migrator"
5
+ if ActiveRecord::ConnectionAdapters::SchemaStatements.instance_methods.include?('initialize_schema_information')
6
+ require "#{dir}/plugin_migrations/1.2/migrator"
7
+ require "#{dir}/plugin_migrations/1.2/extensions/schema_statements"
8
+ else
9
+ require "#{dir}/plugin_migrations/2.1/migrator"
10
+ require "#{dir}/plugin_migrations/2.1/extensions/schema_statements"
11
+ end
@@ -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,33 @@
1
+ module Desert #:nodoc:
2
+ module PluginMigrations
3
+ class Migrator < ActiveRecord::Migrator
4
+ class << self
5
+ def current_version #:nodoc:
6
+ result = ActiveRecord::Base.connection.select_one("SELECT version FROM #{schema_info_table_name} WHERE plugin_name = '#{current_plugin.name}'")
7
+ if result
8
+ result['version'].to_i
9
+ else
10
+ # There probably isn't an entry for this plugin in the migration info table.
11
+ 0
12
+ end
13
+ end
14
+ end
15
+
16
+ def set_schema_version(version)
17
+ version = down? ? version.to_i - 1 : version.to_i
18
+
19
+ if ActiveRecord::Base.connection.select_one("SELECT version FROM #{self.class.schema_info_table_name} WHERE plugin_name = '#{current_plugin.name}'").nil?
20
+ # We need to create the entry since it doesn't exist
21
+ ActiveRecord::Base.connection.execute("INSERT INTO #{self.class.schema_info_table_name} (version, plugin_name) VALUES (#{version},'#{current_plugin.name}')")
22
+ else
23
+ ActiveRecord::Base.connection.update("UPDATE #{self.class.schema_info_table_name} SET version = #{version} WHERE plugin_name = '#{current_plugin.name}'")
24
+ end
25
+ end
26
+
27
+ def migrated
28
+ current_plugin_version = self.class.current_version
29
+ (1..current_plugin_version).to_a
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,39 @@
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
+ smt = Desert::PluginMigrations::Migrator.schema_migrations_table_name
7
+ unless ActiveRecord::Base.connection.tables.include?(smt)
8
+ execute "CREATE TABLE #{smt} (plugin_name #{type_to_sql(:string)}, version #{type_to_sql(:string)})"
9
+ end
10
+ old_smt = Desert::PluginMigrations::Migrator.schema_info_table_name
11
+ if ActiveRecord::Base.connection.tables.include?(old_smt)
12
+ plugins_and_versions = select_all("SELECT plugin_name, version from #{old_smt}")
13
+ plugins_and_versions.each do |plugin_data|
14
+ plugin_name, version = plugin_data["plugin_name"], plugin_data["version"]
15
+ plugin = Desert::Manager.find_plugin(plugin_name)
16
+ migration_versions = Dir["#{plugin.migration_path}/*.rb"].map do |path|
17
+ File.basename(path, ".rb")
18
+ end.select do |migration|
19
+ # Make sure versions don't start with zero, or Integer will interpret them as octal
20
+ version_from_table_stripped = version.sub(/^0*/, '')
21
+ migration_version_stripped = migration.split("_").first.sub(/^0*/, '')
22
+ Integer(migration_version_stripped) <= Integer(version_from_table_stripped)
23
+ end
24
+ migration_versions.each do |migration_version|
25
+ insert_sql = ActiveRecord::Base.send(:sanitize_sql, [
26
+ "INSERT INTO #{Desert::PluginMigrations::Migrator.schema_migrations_table_name}(plugin_name, version) VALUES(?, ?)",
27
+ plugin_name,
28
+ Integer(migration_version.split("_").first.sub(/^0*/, ''))
29
+ ])
30
+ execute insert_sql
31
+ end
32
+ end
33
+ end
34
+ rescue ActiveRecord::StatementInvalid
35
+ # Schema has been initialized
36
+ end
37
+ end
38
+ alias_method_chain :initialize_schema_migrations_table, :plugins
39
+ end