Module: Retrospec::Plugins::V1::ModuleHelpers

Included in:
Config, Plugin
Defined in:
lib/retrospec/plugins/v1/module_helpers.rb

Instance Method Summary collapse

Instance Method Details

#create_content(type, dest, src = nil, overwrite = false) ⇒ Object

creates the content, then notifies the user


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 32

def create_content(type, dest, src = nil, overwrite = false)
  case type
    when :file
      File.open(dest, 'w') do |f|
        f.puts(src)
      end
    when :dir
      FileUtils.mkdir_p(dest)
    when :link
      FileUtils.copy_entry(src,dest,false,false,true) # always remove destination
    when :mv
      FileUtils.mv(src,dest)
    when :touch
      FileUtils.touch(dest)
    when :cp
      FileUtils.cp(src,dest)
  end
  notify(dest, overwrite)
end

#default_retrospec_dirObject

the directory where the config, repos, and other info are saved


169
170
171
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 169

def default_retrospec_dir
  File.expand_path(File.join(ENV['HOME'], '.retrospec' ))
end

#notify(dest, overwrite = false) ⇒ Object

notifies the user of what action will be performed + creates a file

  • + overwrites a file


23
24
25
26
27
28
29
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 23

def notify(dest, overwrite=false)
  if overwrite
    puts " - + #{dest}".info
  else
    puts " + #{dest}".info
  end
end

#overwrite?(dest) ⇒ Boolean

sets @overwrite_all if the user chooses 'a' and saves for next time

Parameters:

  • dest (String)
    • the destination of the file

Returns:

  • (Boolean)

55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 55

def overwrite?(dest)
  return true if overwrite_all
  return false unless overwrite_enabled?
  answer = prompt_for_overwrite(dest)
  if answer == 'a'
    print "Are you sure you wish to overwrite all generated files?(y/n): ".fatal
    sure = gets.chomp.downcase
    return @@overwrite_all = true if sure == 'y'
    overwrite?(dest)
  else
    answer == 'y'
  end
end

#overwrite_allBoolean

stores the answer if the user wants to overwrite all files

Returns:

  • (Boolean)

    true if overwrite_all mode is enabled


11
12
13
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 11

def overwrite_all
  @@overwrite_all ||= !ENV['RETROSPEC_OVERWRITE_ALL'].nil?
end

#overwrite_enabled?Boolean

Returns true if overwrite mode is enabled.

Returns:

  • (Boolean)

    true if overwrite mode is enabled


16
17
18
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 16

def overwrite_enabled?
  !ENV['RETROSPEC_OVERWRITE_ENABLE'].nil?
end

#prompt_for_overwrite(dest) ⇒ Object


69
70
71
72
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 69

def prompt_for_overwrite(dest)
  print "Overwrite #{dest}?(y/n/a): ".cyan
  gets.chomp.downcase
end

#retrospec_file?(file) ⇒ Bool

determines if the contains the extension retrospec have the word sync

Parameters:

  • - (String)

    destination of the file

Returns:

  • (Bool)
    • true if the file should be synced


100
101
102
103
104
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 100

def retrospec_file?(file)
  filename = File.basename(file)
  parts = filename.split('.')
  parts.include?('retrospec')
end

#retrospec_repos_dirObject


173
174
175
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 173

def retrospec_repos_dir
  File.join(default_retrospec_dir, 'repos')
end

#safe_copy_file(src, dest, sync_file = false) ⇒ Object

safely copy an existing file to another dest


134
135
136
137
138
139
140
141
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 134

def safe_copy_file(src, dest, sync_file = false)
  unless should_create?(dest, sync_file)
    return $stderr.puts "!! #{dest} already exists".warning
  end
  return safe_touch(src) unless File.exists?(src)
  safe_mkdir(File.dirname(dest))
  create_content(:cp, dest, src)
end

#safe_create_directory_files(template_dir, module_path, spec_object, filter = nil) ⇒ Object

creates any file that is contained in the templates_dir directory structure loops through the directory looking for erb files or other files. strips the erb extension and renders the template to the current module path filenames must named how they would appear in the normal module path. The directory structure where the file is contained

Parameters:

  • template (String)

    directory of where to find templates

  • module_path (String)
    • path to the module

  • spec_object (String)
    • the context that is used for template rendering

  • filter (String) (defaults to: nil)
    • a regex string used to filter out files


201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 201

def safe_create_directory_files(template_dir, module_path, spec_object, filter = nil)
  templates = Find.find(template_dir).sort
  templates.each do |template|
    next if template =~ filter
    dest = template.gsub(template_dir, module_path)
    if File.symlink?(template)
      safe_create_symlink(template, dest)
    elsif File.directory?(template)
      safe_mkdir(dest)
    else
      # because some plugins contain erb files themselves any erb file will be copied only
      # so we need to designate which files should be rendered with .retrospec.erb
      # render any file ending in .retrospec_erb as a template
      sync_file = sync_file?(template)
      retrospec_file = retrospec_file?(template)
      dest = dest.gsub(/\.sync/, '') if sync_file
      if retrospec_file
        dest = dest.gsub(/\.retrospec\.erb/, '')
        safe_create_template_file(dest, template, spec_object, sync_file)
      else
        safe_copy_file(template, dest, sync_file)
      end
    end
  end
end

#safe_create_file(dest, content, sync_file = false) ⇒ Object

safely creates a file and does not override the existing file


155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 155

def safe_create_file(dest, content, sync_file = false)
  unless should_create?(dest, sync_file)
    old_content = File.read(dest)
    # if we did a better comparison of content we could be smarter about when we create files
    if old_content != content
      $stderr.puts "!! #{dest} already exists and differs from template".warning
    end
  else
    safe_mkdir(File.dirname(dest)) unless File.exists? File.dirname(dest)
    create_content(:file, dest, content)
  end
end

#safe_create_module_files(template_dir, module_path, spec_object, filter = nil) ⇒ Object

creates any file that is contained in the templates/modules_files directory structure loops through the directory looking for erb files or other files. strips the erb extension and renders the template to the current module path filenames must named how they would appear in the normal module path. The directory structure where the file is contained

Parameters:

  • template (String)

    directory of where to find templates

  • module_path (String)
    • path to the module

  • spec_object (String)
    • the context that is used for template rendering

  • filter (String) (defaults to: nil)
    • a regex string used to filter out files


238
239
240
241
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 238

def safe_create_module_files(template_dir, module_path, spec_object, filter = nil)
  dir = File.join(template_dir,'module_files')
  safe_create_directory_files(dir, module_path, spec_object, filter)
end

copy the symlink and preserve the link


126
127
128
129
130
131
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 126

def safe_create_symlink(src,dest)
  unless should_create?(dest)
    return $stderr.puts "!! #{dest} already exists and differs from template".warning
  end
  create_content(:link, dest, src)
end

#safe_create_template_file(path, template, spec_object, sync_file = false) ⇒ Object

path is the full path of the file to create template is the full path to the template file spec_object is any bindable object which the templates uses for context


180
181
182
183
184
185
186
187
188
189
190
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 180

def safe_create_template_file(path, template, spec_object, sync_file = false)
  # check to ensure parent directory exists
  file_dir_path = File.expand_path(File.dirname(path))
  safe_mkdir(file_dir_path) unless File.exists?(file_dir_path)
  File.open(template) do |file|
    renderer = ERB.new(file.read, 0, '-')
    content = renderer.result spec_object.get_binding
    dest_path = File.expand_path(path)
    safe_create_file(dest_path, content, sync_file)
  end
end

#safe_mkdir(dir) ⇒ Object

only creates a directory if the directory doesn't already exist


75
76
77
78
79
80
81
82
83
84
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 75

def safe_mkdir(dir)
  dir = File.expand_path(dir)
  if File.exists? dir
    unless File.directory? dir
      $stderr.puts "!! #{dir} already exists and is not a directory".fatal
    end
  else
    create_content(:dir, dir)
  end
end

#safe_move_file(src, dest) ⇒ Object

move the file, safely


118
119
120
121
122
123
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 118

def safe_move_file(src,dest)
  unless should_create?(dest)
    return $stderr.puts "!! #{dest} already exists and differs from template".warning
  end
  create_content(:mv, dest, src)
end

#safe_touch(file) ⇒ Object

touch a file, this is useful for setting up trigger files


144
145
146
147
148
149
150
151
152
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 144

def safe_touch(file)
  if File.exists? file
    unless File.file? file
      $stderr.puts "!! #{file} already exists and is not a regular file".fatal
    end
  else
    create_content(:touch, file)
  end
end

#should_create?(dest, sync_file = false) ⇒ Bool

Returns - true if the file should be created or overwritten.

Parameters:

  • - (String)

    destination of the file

  • - (Bool)

    true if the file should be synced

Returns:

  • (Bool)
    • true if the file should be created or overwritten


109
110
111
112
113
114
115
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 109

def should_create?(dest, sync_file = false)
  return true unless File.exists?(dest)
  return true if sync_file
  return true if overwrite?(dest)
  return true if File.zero?(dest)
  false
end

#sync_file?(file) ⇒ Bool

determines if the file should be synced by checking if the any of the file extensions have the word sync

Parameters:

  • - (String)

    destination of the file

Returns:

  • (Bool)
    • true if the file should be synced


90
91
92
93
94
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 90

def sync_file?(file)
  filename = File.basename(file)
  parts = filename.split('.')
  parts.include?('sync')
end