saruman 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ app/*
6
+ *.DS_STORE
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in saruman.gemspec
4
+ gemspec
data/README ADDED
File without changes
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # What is Saruman?
2
+
3
+ A ruby gem developed by jasonayre of bravenewwebdesign.com (site down currently), to make super complicated, ridiculous, time consuming things in Magento, e.g. creating a new extension, easy.
4
+
5
+ ## NOTE: NOT EVERYTHING HAS BEEN TESTED, USE AT YOUR OWN RISK
6
+
7
+ ### IF YOUR COMPUTER BLOWS UP, NOT MY FAULT. Probably make a new branch before attempting to use, ESPECIALLY if not creating a new extension.
8
+
9
+ ## Installing
10
+
11
+ gem install saruman
12
+
13
+ Creating a new extension should be working. To do so, navigate to the root directory of your magento installation and call
14
+
15
+ saruman extension
16
+
17
+ The wizard should take you through the rest of it. I would recommend adding an observer, choose event #3 (checkout_cart_update), and then after it's finished, add an item to your cart and watch the system log, to verify everything is working correctly (well not EVERYTHING, just the observer and general installation of the extension really)
18
+
19
+ ### A picture is worth a thousand words (or is it a million?)
20
+
21
+ However the phrase goes, its a lot of words, so here is an example of what my terminal looks like after creating a new extension via saruman, along with tailing the magento system log in bottom portion of terminal screen. Oh and I didnt see my itunes open in back when I took screenshot, and too lazy to take a new one, so I would like to state for the record that I have no idea how Katy Perry got into my itunes library, though I suspect it was the work of gremlins, or George W Bush and is part of some 9/11 cover up conspiracy...
22
+
23
+ ![Saruman Image example](/jasonayre/saruman/raw/master/doc_assets/saruman_extension_example.jpg)
24
+
25
+ ### Creating the models
26
+
27
+ You are able to create an unlimited number of models for your extension, and it is done in rails fashion. E.g, when you get to models part of wizard:
28
+
29
+ title:string content:text active:boolean
30
+
31
+ Will create the magento installer sql using the rails friendly syntax.
32
+
33
+ ### Future stuff
34
+
35
+ Technically I believe the model command is working, honestly haven't really tested it so I dont recommend using it. The idea behind this whole library is you will be able to do things like
36
+
37
+ saruman model
38
+
39
+ And then the wizard will let you create any number of models, using a railsish syntax, which will create magento models for a specific extension. It will also create a new version upgrade, along with the resource models and what not.
40
+
41
+ The coolest part and hardest part to get right, is I'm actually reading the config files of the magento extension, and appending the new data declarations for the models and what not, in an attempt to really simplify the creation of magento extensions. (work in progress, you can try it for now but no promises nothing will break)
42
+
43
+
44
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/saruman ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'saruman/cli'
3
+ Saruman::CLI.start
@@ -0,0 +1,89 @@
1
+ require 'thor'
2
+ require 'saruman'
3
+ require "highline/import"
4
+ require 'saruman/generators/saruman'
5
+ module Saruman
6
+ class CLI < Thor
7
+
8
+ desc "extension", "Creates a new magento extension"
9
+ def extension
10
+ options = Hash.new
11
+
12
+ options[:namespace] = ask("Enter extension namespace:") { |q| q.default = "Saruman" }
13
+ options[:name] = ask("Enter extension name:") { |q| q.default = "Wizard" }
14
+ options[:author] = ask("Author of extension:") { |q| q.default = "Jason Ayre www.bravenewwebdesign.com" }
15
+ options[:version] = ask("Version number (default - 0.0.1):") { |q| q.default = "0.0.1" }
16
+
17
+ say("Would you like me to create an observer?")
18
+ choose do |menu|
19
+ menu.choice(:yes) { options[:observer] = true }
20
+ menu.choice(:no) { options[:observer] = false }
21
+ end
22
+
23
+ if(options[:observer])
24
+ say("Choose the events you would like to observe")
25
+ begin
26
+ choose do |menu|
27
+ if(options[:observer_events]).nil?
28
+ options[:observer_events] = Array.new
29
+ end
30
+ menu.choice(:catalogrule_before_apply) { options[:observer_events].push(:catalog_before_apply) }
31
+ menu.choice(:catalogrule_after_apply) { options[:observer_events].push(:catalog_after_apply) }
32
+ menu.choice(:checkout_cart_save_after) { options[:observer_events].push(:checkout_cart_save_after) }
33
+ end
34
+ end while agree("Observe another event?")
35
+
36
+ end
37
+
38
+ say("Would you like me to create a model?")
39
+ choose do |menu|
40
+ menu.choice(:yes) { options[:model] = true }
41
+ menu.choice(:no) { options[:model] = false }
42
+ end
43
+
44
+ if(options[:model])
45
+
46
+ if(options[:models]).nil?
47
+ options[:models] = Array.new
48
+ end
49
+
50
+ begin
51
+ question = Saruman::ModelBuilder.new
52
+ options[:models] << question.output
53
+ end while agree("Create another model?")
54
+
55
+ end
56
+
57
+ say("Would you like me to create a helper?")
58
+ choose do |menu|
59
+ menu.choice(:yes) { options[:helper] = true }
60
+ menu.choice(:no) { options[:helper] = false }
61
+ end
62
+
63
+ Saruman::Generators::Extension.start([options])
64
+
65
+ end
66
+
67
+ desc "model", "Creates a new magento model"
68
+ def model
69
+ options = Hash.new
70
+ options[:namespace] = ask("Enter extension namespace:") { |q| q.default = "Saruman" }
71
+ options[:name] = ask("Enter extension name:") { |q| q.default = "Wizard" }
72
+
73
+ if(options[:models]).nil?
74
+ options[:models] = Array.new
75
+ end
76
+
77
+ begin
78
+ question = Saruman::ModelBuilder.new
79
+ options[:models] << question.output
80
+ end while agree("Create another model?")
81
+
82
+
83
+ Saruman::Generators::Model.start([options])
84
+
85
+ end
86
+
87
+ end
88
+
89
+ end
@@ -0,0 +1,6 @@
1
+ <?php
2
+
3
+ class <%= combined_namespace %>_Helper_Data extends Mage_Core_Helper_Abstract
4
+ {
5
+
6
+ }
@@ -0,0 +1,11 @@
1
+ <?php
2
+
3
+ class <%= @model_klass_name %> extends Mage_Core_Model_Abstract
4
+ {
5
+ public function _construct()
6
+ {
7
+ parent::_construct();
8
+ $this->_init('<%= name_lower %>/<%= @model_name_lower %>');
9
+ }
10
+
11
+ }
@@ -0,0 +1,13 @@
1
+ <?php
2
+
3
+ class <%= combined_namespace %>_Model_Observer
4
+ {
5
+ <% observers.each do |event| %>
6
+ public function <%= event %>($observer)
7
+ {
8
+ $event = $observer->getEvent();
9
+ Mage::log("I put on my robe and wizard hat");
10
+ Mage::log($event->getEventName().' was called. Winning!');
11
+ }
12
+ <% end %>
13
+ }
@@ -0,0 +1,10 @@
1
+ <?php
2
+
3
+ class <%= @resource_model_klass_name %> extends Mage_Core_Model_Mysql4_Abstract
4
+ {
5
+ public function _construct()
6
+ {
7
+ $this->_init('<%= name.downcase %>/<%= @model_name.downcase %>', '<%= @table_name %>_id');
8
+ }
9
+
10
+ }
@@ -0,0 +1,81 @@
1
+ <?xml version="1.0"?>
2
+ <!--
3
+ /**
4
+ * @category <%= namespace %>
5
+ * @package <%= combined_namespace %>
6
+ * @author <%= author %>
7
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
8
+ */
9
+ -->
10
+ <config>
11
+ <modules>
12
+ <<%= combined_namespace %>>
13
+ <version>0.0.1</version>
14
+ </<%= combined_namespace %>>
15
+ </modules>
16
+
17
+ <frontend>
18
+ <% if observer? %>
19
+ <events>
20
+ <% observers.each do |event| %>
21
+ <<%= event %>>
22
+ <observers>
23
+ <<%= combined_namespace %>_Model_Observer>
24
+ <type>singleton</type>
25
+ <class><%= combined_namespace %>_Model_Observer</class>
26
+ <method><%= event %></method>
27
+ </<%= combined_namespace %>_Model_Observer>
28
+ </observers>
29
+ </<%= event %>>
30
+ <% end %>
31
+ </events>
32
+ <% end %>
33
+ </frontend>
34
+
35
+ <global>
36
+ <% if model? %>
37
+ <models>
38
+ <<%= extension_name_lower %>>
39
+ <class><%= model_klass_name %></class>
40
+ <resourceModel><%= resource_model_name_lower %></resourceModel>
41
+ </<%= extension_name_lower %>>
42
+ <<%= resource_model_name_lower %>>
43
+ <class><%= resource_model_klass_name %></class>
44
+ <entities>
45
+ <% models.each do |model| %>
46
+ <<%= model[:model_name_lower] %>><table><%= model[:model_table_name] %></table></<%= model[:model_name_lower] %>>
47
+ <% end %>
48
+ </entities>
49
+ </<%= resource_model_name_lower %>>
50
+ </models>
51
+ <resources>
52
+ <<%= extension_name_lower %>_setup>
53
+ <setup>
54
+ <module><%= combined_namespace %></module>
55
+ </setup>
56
+ <connection>
57
+ <use>core_setup</use>
58
+ </connection>
59
+ </<%= extension_name_lower %>_setup>
60
+ <<%= extension_name_lower %>_write>
61
+ <connection>
62
+ <use>core_write</use>
63
+ </connection>
64
+ </<%= extension_name_lower %>_write>
65
+ <<%= extension_name_lower %>_read>
66
+ <connection>
67
+ <use>core_read</use>
68
+ </connection>
69
+ </<%= extension_name_lower %>_read>
70
+ </resources>
71
+ <% end %>
72
+ <% if helper? %>
73
+ <helpers>
74
+ <<%= name_lower %>>
75
+ <class><%= combined_namespace %>_Helper</class>
76
+ </<%= name_lower %>>
77
+ </helpers>
78
+ <% end %>
79
+ </global>
80
+
81
+ </config>
@@ -0,0 +1,17 @@
1
+ <?xml version="1.0"?>
2
+ <!--
3
+ /**
4
+ * @category <%= namespace %>
5
+ * @package <%= combined_namespace %>
6
+ * @author <%= author %>
7
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
8
+ */
9
+ -->
10
+ <config>
11
+ <modules>
12
+ <<%= namespace %>_<%= name %>>
13
+ <active>true</active>
14
+ <codePool>local</codePool>
15
+ </<%= namespace %>_<%= name %>>
16
+ </modules>
17
+ </config>
@@ -0,0 +1,19 @@
1
+ <?php
2
+
3
+ $installer = $this;
4
+
5
+ $installer->startSetup();
6
+
7
+ $installer->run("
8
+ <% models.each do |model| %>
9
+ -- DROP TABLE IF EXISTS {$this->getTable('<%= model[:model_table_name] %>')};
10
+ CREATE TABLE {$this->getTable('<%= model[:model_table_name] %>')} (
11
+ `id` int(11) unsigned NOT NULL auto_increment,
12
+ <%= model[:sql] %>
13
+ PRIMARY KEY (`id`)
14
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
15
+ <% end %>
16
+
17
+ ");
18
+
19
+ $installer->endSetup();
@@ -0,0 +1,167 @@
1
+ # require 'saruman/generators/saruman'
2
+ require 'thor/group'
3
+
4
+ module Saruman
5
+ module Generators
6
+ class Extension < Thor::Group
7
+ include Thor::Actions
8
+ argument :arguments, :type => :hash
9
+
10
+ def self.source_root
11
+ File.dirname(__FILE__) + "/extension/templates"
12
+ end
13
+
14
+ def copy_global_config
15
+ template("module.xml", "#{global_config_basepath}#{arguments[:namespace]}_#{arguments[:name]}.xml")
16
+ end
17
+
18
+ def copy_extension_config
19
+ template("extension_config.xml", "#{extension_config_path}/config.xml")
20
+ end
21
+
22
+ def create_model_directory
23
+ empty_directory(model_path)
24
+ end
25
+
26
+ def create_observers
27
+ if observer?
28
+ template("Observer.php", "#{model_path}Observer.php")
29
+ end
30
+ end
31
+
32
+ def create_helper
33
+ if helper?
34
+ template("Helper.php", "#{helper_path}Data.php")
35
+ end
36
+ end
37
+
38
+ def create_models
39
+ if model?
40
+
41
+ models.each do |model|
42
+ @model_name = model[:model_name]
43
+ @model_klass_name = "#{namespace}_#{name}_Model_#{@model_name}"
44
+ @model_name_lower = model[:model_name_lower]
45
+
46
+ @resource_model_klass_name = "#{namespace}_#{name}_Model_Mysql4_#{@model_name}"
47
+ @table_name = model[:model_table_name]
48
+ template("Model.php", "#{model_path}#{@model_name}.php")
49
+ template("Resource_Model.php", "#{resource_model_path}#{@model_name}.php")
50
+ end
51
+
52
+ @setup_path = "#{setup_base_path}#{name_lower}_setup/"
53
+ empty_directory(@setup_path)
54
+ template("mysql4-install.php", "#{@setup_path}mysql4-install-#{version}.php")
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ def namespace
61
+ arguments[:namespace]
62
+ end
63
+
64
+ def name
65
+ arguments[:name]
66
+ end
67
+
68
+ def combined_namespace
69
+ "#{arguments[:namespace]}_#{arguments[:name]}"
70
+ end
71
+
72
+ def namespace_lower
73
+ namespace.downcase
74
+ end
75
+
76
+ def name_lower
77
+ name.downcase
78
+ end
79
+
80
+ #alias for name
81
+ def extension_name_lower
82
+ name.downcase
83
+ end
84
+
85
+ def author
86
+ arguments[:author]
87
+ end
88
+
89
+ def version
90
+ arguments[:version]
91
+ end
92
+
93
+ def global_config_basepath
94
+ "app/etc/modules/"
95
+ end
96
+
97
+ def extension_base_path
98
+ "app/code/local/#{namespace}/#{name}/"
99
+ end
100
+
101
+ def extension_config_path
102
+ "#{extension_base_path}etc/"
103
+ end
104
+
105
+ def model_path
106
+ "#{extension_base_path}Model/"
107
+ end
108
+
109
+ def resource_model_path
110
+ "#{model_path}Mysql4/"
111
+ end
112
+
113
+ def helper_path
114
+ "#{extension_base_path}Helper/"
115
+ end
116
+
117
+ def setup_base_path
118
+ "#{extension_base_path}/sql/"
119
+ end
120
+
121
+ def model_klass_name
122
+ "#{combined_namespace}_Model"
123
+ end
124
+
125
+ def resource_model_name_lower
126
+ "#{name_lower}_mysql4"
127
+ end
128
+
129
+ def resource_model_klass_name
130
+ "#{combined_namespace}_Model_Mysql4"
131
+ end
132
+
133
+ def observer?
134
+ if arguments[:observer] == true
135
+ return true
136
+ else
137
+ return false
138
+ end
139
+ end
140
+
141
+ def observers
142
+ arguments[:observer_events]
143
+ end
144
+
145
+ def helper?
146
+ if arguments[:helper] == true
147
+ return true
148
+ else
149
+ return false
150
+ end
151
+ end
152
+
153
+ def model?
154
+ if arguments[:model] == true
155
+ return true
156
+ else
157
+ return false
158
+ end
159
+ end
160
+
161
+ def models
162
+ arguments[:models]
163
+ end
164
+
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,10 @@
1
+ <?php
2
+
3
+ class <%= @model_klass_name %> extends Mage_Core_Model_Abstract
4
+ {
5
+ public function _construct()
6
+ {
7
+ parent::_construct();
8
+ $this->_init('<%= name_lower %>/<%= @model_name_lower %>');
9
+ }
10
+ }
@@ -0,0 +1,10 @@
1
+ <?php
2
+
3
+ class <%= @resource_model_klass_name %> extends Mage_Core_Model_Mysql4_Abstract
4
+ {
5
+ public function _construct()
6
+ {
7
+ $this->_init('<%= name.downcase %>/<%= @model_name.downcase %>', 'id');
8
+ }
9
+
10
+ }
@@ -0,0 +1,4 @@
1
+ <<%= extension_name_lower %>>
2
+ <class><%= model_klass_name %></class>
3
+ <resourceModel><%= resource_model_name %></resourceModel>
4
+ </<%= extension_name_lower %>>
@@ -0,0 +1,19 @@
1
+ <?php
2
+
3
+ $installer = $this;
4
+
5
+ $installer->startSetup();
6
+
7
+ $installer->run("
8
+ <% models.each do |model| %>
9
+ -- DROP TABLE IF EXISTS {$this->getTable('<%= model[:model_table_name] %>')};
10
+ CREATE TABLE {$this->getTable('<%= model[:model_table_name] %>')} (
11
+ `id` int(11) unsigned NOT NULL auto_increment,
12
+ <%= model[:sql] %>
13
+ PRIMARY KEY (`id`)
14
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
15
+ <% end %>
16
+
17
+ ");
18
+
19
+ $installer->endSetup();
@@ -0,0 +1,8 @@
1
+ <<%= resource_model_name %>>
2
+ <class><%= resource_model_klass_name %></class>
3
+ <entities>
4
+ <% models.each do |model| %>
5
+ <<%= model[:model_name_lower] %>><table><%= model[:model_table_name] %></table></<%= model[:model_name_lower] %>>
6
+ <% end %>
7
+ </entities>
8
+ </<%= resource_model_name %>>