Class: Picky::Generators::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/picky-generators/generators/base.rb

Overview

Simple Base generator.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier, name, prototype_path, *args) ⇒ Base

Returns a new instance of Base



21
22
23
24
25
# File 'lib/picky-generators/generators/base.rb', line 21

def initialize identifier, name, prototype_path, *args
  @identifier        = identifier
  @name              = name
  @prototype_basedir = expand_prototype_path prototype_path
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier



19
20
21
# File 'lib/picky-generators/generators/base.rb', line 19

def identifier
  @identifier
end

#nameObject (readonly)

Returns the value of attribute name



19
20
21
# File 'lib/picky-generators/generators/base.rb', line 19

def name
  @name
end

#prototype_basedirObject (readonly)

Returns the value of attribute prototype_basedir



19
20
21
# File 'lib/picky-generators/generators/base.rb', line 19

def prototype_basedir
  @prototype_basedir
end

Instance Method Details

#all_prototype_files(from = nil) ⇒ Object



97
98
99
100
# File 'lib/picky-generators/generators/base.rb', line 97

def all_prototype_files from = nil
  from ||= prototype_basedir
  Dir[File.join(from, '**', '*')]
end

#copy_all_files(from = nil) ⇒ Object



44
45
46
47
48
49
# File 'lib/picky-generators/generators/base.rb', line 44

def copy_all_files from = nil
  all_prototype_files(from).each do |filename|
    next if filename.match(/\.textile$/)
    copy_single_file filename, from
  end
end

#copy_single_file(filename, from = nil) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/picky-generators/generators/base.rb', line 58

def copy_single_file filename, from = nil
  target = target_filename_for filename, from
  if File.exists? target
    exists target
  else
    smart_copy filename, target
  end
end

#create_target_directoryObject



33
34
35
36
37
38
39
40
# File 'lib/picky-generators/generators/base.rb', line 33

def create_target_directory
  if File.exists?(target_directory)
    exists target_directory
  else
    FileUtils.mkdir target_directory
    created target_directory
  end
end

#created(entry) ⇒ Object



108
109
110
# File 'lib/picky-generators/generators/base.rb', line 108

def created entry
  exclaim "#{entry} \x1b[32mcreated\x1b[m."
end

#exclaim(something) ⇒ Object



116
117
118
# File 'lib/picky-generators/generators/base.rb', line 116

def exclaim something
  puts something
end

#exists(entry) ⇒ Object



112
113
114
# File 'lib/picky-generators/generators/base.rb', line 112

def exists entry
  exclaim "#{entry} \x1b[31mexists\x1b[m, skipping."
end

#expand_prototype_path(prototype_path) ⇒ Object



27
28
29
# File 'lib/picky-generators/generators/base.rb', line 27

def expand_prototype_path prototype_path
  File.expand_path "../../../../prototypes/#{prototype_path}", __FILE__
end

#generate_for(type, prototype_paths, steps) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/picky-generators/generators/base.rb', line 120

def generate_for type, prototype_paths, steps
  exclaim "Setting up Picky #{type} \"#{name}\"."
  
  create_target_directory
  copy_all_files
  
  prototype_paths.each do |path|
    copy_all_files expand_prototype_path(path)
  end
  
  exclaim "\"#{name}\" is a great project name! Have fun :)\n"
  exclaim ""
  exclaim "Next steps:"
  
  steps.each.with_index do |step, i|
    exclaim "#{i+1}. #{step}"
  end
end

#smart_copy(filename, target) ⇒ Object

Well, “smart” ;)



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/picky-generators/generators/base.rb', line 69

def smart_copy filename, target
  # p "Trying to copy #{filename} -> #{target}"
  FileUtils.copy_file filename, target
  created target
rescue Errno::EISDIR
  # p "EISDIR #{filename} -> #{target}"
  FileUtils.rm target
  FileUtils.mkdir_p target unless Dir.exists?(target)
  created target
rescue Errno::EEXIST
  # p "EEXIST #{filename} -> #{target}"
  exists target
rescue Errno::ENOTDIR
  # p "ENOTDIR #{filename} -> #{target}"
  FileUtils.mkdir_p File.dirname(target) rescue nil
  retry
rescue Errno::ENOENT => e
  # p "ENOENT #{filename} -> #{target}"
  if File.exists? filename
    FileUtils.mkdir_p File.dirname(target)
    retry
  else
    raise e
  end
end

#target_directoryObject



104
105
106
# File 'lib/picky-generators/generators/base.rb', line 104

def target_directory
  File.expand_path name, Dir.pwd
end

#target_filename_for(filename, from = nil) ⇒ Object



53
54
55
# File 'lib/picky-generators/generators/base.rb', line 53

def target_filename_for filename, from = nil
  filename.gsub(%r{#{from || prototype_basedir}}, target_directory)
end