Class: Drupal::Create_Module
- Inherits:
-
Object
- Object
- Drupal::Create_Module
- Defined in:
- lib/drupal/create_module.rb
Instance Method Summary collapse
-
#append_template(filepath, template, tokens = {}) ⇒ Object
Append a tokenized template template to a file.
-
#ask(question, list = false) ⇒ Object
Prompt user for input.
-
#check_module_name ⇒ Object
Ensure module name is supplied and that it is formatted correctly as module names must be alphanumeric and must begin with a letter.
-
#create_dir(dir) ⇒ Object
Create a new directory.
-
#create_file(filepath, contents = '') ⇒ Object
Create a new file.
-
#create_hook_weights ⇒ Object
Register hook weights.
-
#create_module ⇒ Object
Create module from wizard results.
-
#create_module_dirs ⇒ Object
Create directories.
-
#create_module_file ⇒ Object
Create .module file.
-
#create_module_files ⇒ Object
Create file templates.
-
#create_module_info_file ⇒ Object
Create info file.
-
#create_module_install_file ⇒ Object
Create .install file.
-
#create_tokens ⇒ Object
Create global tokens.
-
#get_templates(type) ⇒ Object
Get array of templates of a certain type.
-
#list_templates(title, type) ⇒ Object
List templates available of a certain type.
-
#run(arguments) ⇒ Object
Create a module using the module builing wizard.
-
#run_wizard ⇒ Object
Run module generation wizard.
Instance Method Details
#append_template(filepath, template, tokens = {}) ⇒ Object
Append a tokenized template template to a file.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/drupal/create_module.rb', line 158 def append_template(filepath, template, tokens = {}) # TODO: ensure template exists # TODO: is \n included with STDIN? _template = template filepath = "#{@dir}/#{@module}/#{filepath}" template = File.dirname(__FILE__) + "/templates/#{template}" puts "... Adding template '#{_template}' to '#{filepath}'" contents = File.read(template) tokens.each_pair do |token, value| if value.class == String && contents.include?("[#{token}]") contents.gsub!(/\[#{token}\]/, value) end end File.open(filepath, 'a') do |f| f.write contents end end |
#ask(question, list = false) ⇒ Object
Prompt user for input
177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/drupal/create_module.rb', line 177 def ask(question, list = false) puts "\n" << question # TODO: support 'all' # TODO: why is gets not working? # TODO: not catching exception when CTRL+C ? begin case list when true; STDIN.gets.split when false; STDIN.gets.gsub!(/\n/, '') end rescue => e puts ':)' end end |
#check_module_name ⇒ Object
Ensure module name is supplied and that it is formatted correctly as module names must be alphanumeric and must begin with a letter.
16 17 18 19 20 21 22 |
# File 'lib/drupal/create_module.rb', line 16 def check_module_name case when @arguments.empty?; puts 'Module name required.'; exit 3 when !@arguments[0].match(/^[a-z][\w]+/); puts 'Invalid module name.'; exit 4 else @module = @arguments[0] end end |
#create_dir(dir) ⇒ Object
Create a new directory.
142 143 144 145 146 |
# File 'lib/drupal/create_module.rb', line 142 def create_dir(dir) dir = "#{@dir}/#{dir}" puts "... Creating directory '#{dir}'" Dir.mkdir(dir) end |
#create_file(filepath, contents = '') ⇒ Object
Create a new file.
149 150 151 152 153 154 155 |
# File 'lib/drupal/create_module.rb', line 149 def create_file(filepath, contents = '') filepath = "#{@dir}/#{@module}/#{filepath}" puts "... Creating file '#{filepath}'" File.open(filepath, 'w') do |f| f.write contents end end |
#create_hook_weights ⇒ Object
Register hook weights
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/drupal/create_module.rb', line 64 def create_hook_weights @hook_weights = [ 'perm', 'cron', 'boot', 'init', 'menu', 'install', 'schema', 'theme', 'form_alter', 'block', ] end |
#create_module ⇒ Object
Create module from wizard results.
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/drupal/create_module.rb', line 80 def create_module puts "\n... Creating module '#{@module}' in '#{@dir}'" # Base directory create_dir("#{@module}") self.create_module_dirs self.create_module_files self.create_module_file self.create_module_install_file self.create_module_info_file puts 'Module created :)' end |
#create_module_dirs ⇒ Object
Create directories.
93 94 95 |
# File 'lib/drupal/create_module.rb', line 93 def create_module_dirs @dirs.each{ |dir| create_dir("#{@module}/#{dir}") } end |
#create_module_file ⇒ Object
Create .module file.
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/drupal/create_module.rb', line 107 def create_module_file create_file("#{@module}.module", "<?php\n") append_template("#{@module}.module", 'comments/file', @tokens) append_template("#{@module}.module", 'comments/large', {'title' => 'Hook Implementations'}) for hook in @hook_weights if @hooks.include?(hook) append_template("#{@module}.module", "hooks/#{hook}", @tokens) unless hook.match /^install|schema/ end end end |
#create_module_files ⇒ Object
Create file templates.
98 99 100 101 102 103 104 |
# File 'lib/drupal/create_module.rb', line 98 def create_module_files @files.each do |file| filepath = "#{file.upcase}.txt" create_file(filepath) append_template(filepath, "txt/#{file}", @tokens) end end |
#create_module_info_file ⇒ Object
Create info file.
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/drupal/create_module.rb', line 130 def create_module_info_file contents = '; $Id$' contents << "\nname = #{@module_name}" contents << "\ndescription = #{@module_description}" contents << "\ncore = 6.x" @module_dependencies.each do |dependency| contents << "\ndependencies[] = #{dependency}" end create_file("#{@module}.info", contents) end |
#create_module_install_file ⇒ Object
Create .install file.
119 120 121 122 123 124 125 126 127 |
# File 'lib/drupal/create_module.rb', line 119 def create_module_install_file if @hooks.include?('schema') || @hooks.include?('schema') create_file("#{@module}.install", "<?php\n") append_template("#{@module}.install", 'comments/file', @tokens) @hooks.each do |hook| append_template("#{@module}.install", "hooks/#{hook}", @tokens) if hook.match /^install|schema/ end end end |
#create_tokens ⇒ Object
Create global tokens.
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/drupal/create_module.rb', line 51 def create_tokens @tokens = { :module => @module, :link => @link, :email => @email, :author => @author, :module_name => @module_name, :module_description => @module_description, :module_dependencies => @module_dependencies, } end |
#get_templates(type) ⇒ Object
Get array of templates of a certain type.
198 199 200 |
# File 'lib/drupal/create_module.rb', line 198 def get_templates(type) Dir[File.dirname(__FILE__) + '/templates/' << type << '/*'] end |
#list_templates(title, type) ⇒ Object
List templates available of a certain type.
193 194 195 |
# File 'lib/drupal/create_module.rb', line 193 def list_templates(title, type) "\n" << title << self.get_templates(type).collect{ |t| "\n - " << File.basename(t) }.join end |
#run(arguments) ⇒ Object
Create a module using the module builing wizard.
6 7 8 9 10 11 |
# File 'lib/drupal/create_module.rb', line 6 def run(arguments) @arguments = arguments @dir = @arguments[1] || '.' # TODO remove trailing slash, check validity, and existance self.check_module_name self.run_wizard end |
#run_wizard ⇒ Object
Run module generation wizard.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/drupal/create_module.rb', line 25 def run_wizard # TODO create self.log() with padding to even output # Info @author = self.ask('What is your name?:') @link = self.ask('What is the URI to your companies website?:') @email = self.ask('What is your email?:') @module_name = self.ask('Enter a human readable name for your module:') @module_description = self.ask('Enter a short description of your module:') @module_dependencies = self.ask('Enter a list of dependencies for your module:', true) # Hooks puts self.list_templates('Hooks:', 'hooks') @hooks = self.ask('Which hooks would you like to implement?:', true) # Files puts self.list_templates('Files:', 'txt') @files = self.ask('Which additional files would you like to include?:', true) # Dirs puts "\nCommon directories:" puts ['js', 'images', 'css'].collect{ |d| " - " << d } @dirs = self.ask('Which directories would you like to create?:', true) # Finish self.create_tokens self.create_hook_weights self.create_module end |