mack 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,4 +1,6 @@
1
- ===0.0.5
1
+ ===0.0.6
2
+ * rake generate:<generator_name>
3
+ * rake generate:plugin - will generate the stub of a plugin in the vendor/plugins directory.
2
4
  * rake dump:config - prints out the configuration information for the specified environment.
3
5
  * redirect_to now takes an optional Hash as a second parameter, instead of a fixnum.
4
6
  * redirect_to will now do server side redirects if passed :server_side => true as part of the optional second argument Hash.
data/errors/errors.rb CHANGED
@@ -75,5 +75,13 @@ module Mack
75
75
  end
76
76
  end
77
77
 
78
+ # Raised if a Mack::Generator::Base required parameter is not supplied.
79
+ class RequiredGeneratorParameterMissing < StandardError
80
+ # Takes the name of the missing parameter.
81
+ def initialize(name)
82
+ super("The required parameter '#{name.to_s.upcase}' is missing for this generator!")
83
+ end
84
+ end
85
+
78
86
  end # Errors
79
87
  end # Mack
@@ -4,6 +4,10 @@ Dir.glob(File.join(MACK_PLUGINS, "*")).each do |d|
4
4
  $: << File.join(d, "lib") # add the lib for this plugin to the global load path
5
5
  end
6
6
  plugins.sort.each do |plug|
7
- require File.join(plug, "init.rb") # load the init.rb for each plugin.
7
+ begin
8
+ require File.join(plug, "init.rb") # load the init.rb for each plugin.
9
+ rescue Exception => e
10
+ puts e.message
11
+ end
8
12
  $:.delete(File.join(plug, "lib")) # remove the plugins lib directory from the global load path.
9
13
  end
@@ -0,0 +1,83 @@
1
+ module Mack
2
+ module Generator # :nodoc:
3
+ # All generator classes should extend this class if they're expected to be used by the rake generate:<generator_name> task.
4
+ # A generator must by name in the following style: <name>Generator.
5
+ #
6
+ # Example:
7
+ # class MyCoolGenerator < Mack::Generator::Base
8
+ # require_param :name, :foo
9
+ #
10
+ # def generate
11
+ # # do work...
12
+ # end
13
+ # end
14
+ #
15
+ # rake generate:my_cool # => calls MyCoolGenerator
16
+ class Base
17
+
18
+ include FileUtils
19
+
20
+ # Used to define arguments that are required by the generator.
21
+ def self.require_param(*args)
22
+ required_params << args
23
+ required_params.flatten!
24
+ end
25
+
26
+ # Returns the required_params array.
27
+ def self.required_params
28
+ @required_params ||= []
29
+ end
30
+
31
+ # Takes a Hash of parameters.
32
+ # Raise Mack::Errors::RequiredGeneratorParameterMissing if a required parameter
33
+ # is missing.
34
+ def initialize(env = {})
35
+ @env = env
36
+ self.class.required_params.each do |p|
37
+ raise Mack::Errors::RequiredGeneratorParameterMissing.new(p) unless param(p)
38
+ end
39
+ end
40
+
41
+ # Runs the generate method.
42
+ def run
43
+ generate
44
+ end
45
+
46
+ # Returns a parameter from the initial Hash of parameters.
47
+ def param(key)
48
+ (@env[key.to_s.downcase] ||= @env[key.to_s.upcase])
49
+ end
50
+
51
+ # Needs to be implemented by the subclass.
52
+ def generate
53
+ raise MethodNotImplemented.new("generate")
54
+ end
55
+
56
+ # Takes an input_file runs it through ERB and
57
+ # saves it to the specified output_file. If the output_file exists it will
58
+ # be skipped. If you would like to force the writing of the file, use the
59
+ # :force => true option.
60
+ def template(input_file, output_file, options = {})
61
+ if File.exists?(output_file)
62
+ unless options[:force]
63
+ puts "Skipped: #{output_file}"
64
+ return
65
+ end
66
+ end
67
+ File.open(output_file, "w") {|f| f.puts ERB.new(File.open(input_file).read).result(binding)}
68
+ puts "Wrote: #{output_file}"
69
+ end
70
+
71
+ # Creates the specified directory.
72
+ def directory(output_dir, options = {})
73
+ if File.exists?(output_dir)
74
+ puts "Exists: #{output_dir}"
75
+ return
76
+ end
77
+ mkdir_p(output_dir)
78
+ puts "Created: #{output_dir}"
79
+ end
80
+
81
+ end # Base
82
+ end # Generator
83
+ end # Mack
@@ -0,0 +1,30 @@
1
+ # Generates plugins for Mack applications.
2
+ #
3
+ # Example:
4
+ # rake generate:plugin name=my_cool_plugin
5
+ # This will generate the following in your mack application:
6
+ # vendor/plugins/my_cool_plugin
7
+ # vendor/plugins/my_cool_plugin/init.rb
8
+ # vendor/plugins/my_cool_plugin/lib
9
+ # vendor/plugins/my_cool_plugin/lib/my_cool_plugin.rb
10
+ class PluginGenerator < Mack::Generator::Base
11
+
12
+ require_param :name
13
+
14
+ def generate
15
+ plugin_dir = File.join(MACK_ROOT, "vendor", "plugins", param(:name).downcase)
16
+ template_dir = File.join(File.dirname(__FILE__), "templates")
17
+
18
+ # create vendor/plugins/<name>
19
+ directory(plugin_dir)
20
+ # create vendor/plugins/<name>/lib
21
+ directory(File.join(plugin_dir, "lib"))
22
+
23
+ # create vendor/plugins/<name>/init.rb
24
+ template(File.join(template_dir, "init.rb.template"), File.join(plugin_dir, "init.rb"))
25
+ # create vendor/plugins/<name>/lib/<name>.rb
26
+ template(File.join(template_dir, "lib", "plugin.rb.template"), File.join(plugin_dir, "lib", "#{param(:name).downcase}.rb"))
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1 @@
1
+ require '<%= param(:name) %>'
@@ -0,0 +1 @@
1
+ # Do work here for <%= param(:name) %>
@@ -16,4 +16,12 @@ rule /^cachetastic:/ do |t|
16
16
  ENV['cache_name'] = cache_name
17
17
  ENV['cache_action'] = cache_action
18
18
  Rake::Task["cachetastic:manipulate_caches"].invoke
19
+ end
20
+
21
+ rule /^generate:/ do |t|
22
+ klass = t.name.gsub("generate:", '')
23
+ Rake::Task["environment"].invoke
24
+ klass = "#{klass.camelcase}Generator"
25
+ gen = klass.constantize.new(ENV)
26
+ gen.run
19
27
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - markbates
@@ -106,6 +106,10 @@ files:
106
106
  - initialization/initializers/orm_support.rb
107
107
  - initialization/initializers/plugins.rb
108
108
  - initialization/server/simple_server.rb
109
+ - lib/generators/generator_base.rb
110
+ - lib/generators/plugin_generator/plugin_generator.rb
111
+ - lib/generators/plugin_generator/templates/init.rb.template
112
+ - lib/generators/plugin_generator/templates/lib/plugin.rb.template
109
113
  - lib/utils/html.rb
110
114
  - lib/utils/server.rb
111
115
  - mack.rb