Class: Drupal::Create_Module

Inherits:
Object
  • Object
show all
Defined in:
lib/drupal/create_module.rb

Instance Method Summary collapse

Instance Method Details

#append_template(filepath, template, tokens = {}) ⇒ Object

Append a tokenized template template to a file.



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/drupal/create_module.rb', line 188

def append_template(filepath, template, tokens = {})
  # TODO: ensure template exists
  # TODO: is \n included with STDIN?
  _template = template
  filepath = "#{@dir}/#{@module}/#{filepath}"
  template = get_template_location << 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, default = '', list = false) ⇒ Object

Prompt user for input



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/drupal/create_module.rb', line 207

def ask(question, default = '', list = false)
  if not default.to_s.empty? then question = question << " (#{default})" end
  puts "\n" << question

  # TODO: support 'all'
  # TODO: why is gets not working?
  # TODO: not catching exception when CTRL+C ?
  begin
    case list
      when true; input = STDIN.gets.split
      when false; input = STDIN.gets.gsub!(/\n/, '')
    end
  rescue => e
    puts ':)'
  end

  if input.empty?
    return default
  else
    return input
  end
end

#check_module_nameObject

Ensure module name is supplied and that it is formatted correctly as module names must be alphanumeric and must begin with a letter.



18
19
20
21
22
23
24
# File 'lib/drupal/create_module.rb', line 18

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.



172
173
174
175
176
# File 'lib/drupal/create_module.rb', line 172

def create_dir(dir)
  dir = "#{@dir}/#{dir}"
  puts "... Creating directory '#{dir}'"
  Dir.mkdir(dir)
end

#create_file(filepath, contents = '') ⇒ Object

Create a new file.



179
180
181
182
183
184
185
# File 'lib/drupal/create_module.rb', line 179

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_weightsObject

Register hook weights



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/drupal/create_module.rb', line 78

def create_hook_weights
  @hook_weights = [
      'perm',
      'cron',
      'boot',
      'init',
      'menu',
      'theme',
      'form_alter',
      'block',
    ]
end

#create_moduleObject

Create module from wizard results.



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/drupal/create_module.rb', line 92

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_database_files
  self.create_module_info_file
  puts 'Module created :)'
end

#create_module_database_filesObject

Create .install file, database includes and schema..



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/drupal/create_module.rb', line 131

def create_module_database_files
  if not @table.empty?
    create_file("#{@module}.install", "<?php\n")
    append_template("#{@module}.install", 'comments/file', @tokens)
    ['install', 'schema'].each do |hook|
      append_template("#{@module}.install", "incs/#{hook}", @tokens)
    end
    
    create_file("#{@module}_db.inc", "<?php\n")
    append_template("#{@module}_db.inc", 'comments/file_db', @tokens)
    append_template("#{@module}_db.inc", "incs/module_db", @tokens)
    
    append_template("#{@module}.module", "hooks/init", @tokens) unless @hooks.include('init')
  end
end

#create_module_dirsObject

Create directories.



105
106
107
# File 'lib/drupal/create_module.rb', line 105

def create_module_dirs
  @dirs.each{ |dir| create_dir("#{@module}/#{dir}") }
end

#create_module_fileObject

Create .module file.



119
120
121
122
123
124
125
126
127
128
# File 'lib/drupal/create_module.rb', line 119

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)
    end
  end
end

#create_module_filesObject

Create file templates.



110
111
112
113
114
115
116
# File 'lib/drupal/create_module.rb', line 110

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_fileObject

Create info file.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/drupal/create_module.rb', line 148

def create_module_info_file
  tokens = {
    'module_name' => @module_name,
    'module_description' => @module_description,
    'version' => @version,
  }

  if !@module_dependencies.nil?
    #In 6.x each dep has a new line. In 5.x they are on a single line.
    if (@version == '6.x')
      @module_dependencies.each do |dependency|
       tokens['dependencies_chunk'] = "\ndependencies[] = #{dependency}"
      end       
    elsif (@version == '5.x')
      tokens['dependencies_chunk'] = "dependencies = " + @module_dependencies.join(',')
    end
  end
  
  filepath = "#{@module}.info"
  create_file(filepath)
  append_template(filepath, "base/info", tokens)
end

#create_tokensObject

Create global tokens.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/drupal/create_module.rb', line 57

def create_tokens
  if not @table.nil?
    @record = @table.singularize
  else
    @record = ''
  end
  
  @tokens = {
      :module => @module,
      :link => @link,
      :email => @email,
      :author => @author,
      :module_name => @module_name,
      :module_description => @module_description,
      :module_dependencies => @module_dependencies,
      :table => @table,
      :record => @record,
    }
end

#get_templates(type) ⇒ Object

Get array of templates of a certain type.



236
237
238
# File 'lib/drupal/create_module.rb', line 236

def get_templates(type)
  Dir[get_template_location << type << '/*']
end

#list_templates(title, type) ⇒ Object

List templates available of a certain type.



231
232
233
# File 'lib/drupal/create_module.rb', line 231

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.



8
9
10
11
12
13
# File 'lib/drupal/create_module.rb', line 8

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_wizardObject

Run module generation wizard.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/drupal/create_module.rb', line 27

def run_wizard
  # TODO create self.log() with padding to even output
  defaults = get_defaults

  # TODO make each ask have a commandline-option equivalent.
  @version = self.ask('What drupal version to scaffold for?:', defaults[:version])
  @author = self.ask('What is your name?:', defaults[:author])
  @link = self.ask('What is the URI to your companies website?:', defaults[:link])
  @email = self.ask('What is your email?:', defaults[:email])
  @module_name = self.ask('Enter a human readable name for your module:', defaults[:module_name])
  @table = self.ask('Database table name for your module. Without the modulename. A table name "pirates" on the module "cool_people" we would give us a table "cool_people_pirates" Leave empty for no database-scaffolding.')
  @module_description = self.ask('Enter a short description of your module:', defaults[:module_description])
  @module_dependencies = self.ask('Enter a list of dependencies for your module:', defaults[:module_dependencies], 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