Class: GenerateCommand

Inherits:
Command show all
Includes:
Rcli::Actions
Defined in:
lib/commands/generate.rb

Instance Attribute Summary

Attributes included from Rcli::Actions

#behavior

Attributes inherited from Command

#description

Instance Method Summary collapse

Methods included from Rcli::Actions

#action, #append_file, #apply, #chmod, #copy_file, #create_file, #destination_root, #destination_root=, #directory, #empty_directory, #find_in_source_paths, #get, #gsub_file, #in_root, included, #inject_into_class, #inject_into_file, #inside, #prepend_file, #relative_to_original_destination_root, #remove_file, #run, #run_ruby_script, #source_paths, #template, #thor

Methods inherited from Command

#after_init, #before_init, default_cmd, describe, description, get_allowed_commands, #help, #initialize, load, load_all, #run, show_use, usage

Constructor Details

This class inherits a constructor from Command

Instance Method Details

#mainObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/commands/generate.rb', line 7

def main()
      
  if @params[:args].length != 1
    puts "ERROR: You must provide a unique app name as an argument."
    exit
  end

  if File.directory? Dir.pwd + DS + @params[:args][0]
    puts "ERROR: folder already exists. Please choose another name"
    exit
  end
      
  app_name = @params[:args][0]
  
  tpl_dir = Rcli::GEM_LIB + DS + 'templates' + DS + 'new_app'
      
  # iterate through the new_app template
  Dir[ tpl_dir + DS + '**' + DS + '*.*'].each do |f|
    # TODO : Use Pathname#relative_path_from for this.
    new_file_name = f[tpl_dir.length + 1 ,f.length - tpl_dir.length]
    
    new_file_name.gsub!(/RCLIAPPNAME/,app_name)

    puts "creating " + app_name + DS + new_file_name
    create_file Dir.pwd() + DS + app_name + DS + new_file_name do
      contents = File.read(f)
      contents.gsub(/RCLIAPPNAME/,app_name)
    end
    
    if f == tpl_dir + DS + 'RCLIAPPNAME.rb'
      puts "Changing executable mode of " + new_file_name
      File.chmod(0755,Dir.pwd() + DS + app_name + DS + new_file_name)
    end
  end
end