OptparsePlus Internal - treat as private

Stuff to implement optparse_plus's CLI app. These stuff isn't generally for your use and it's not included when you require 'optparse_plus'

Methods
A
C
G
R
T
Instance Public methods
add_to_file(file,lines,options = {})

Add content to a file

file

path to the file

lines

Array of String representing the lines to add

options

Hash of options:

:before

A regexp that will appear right after the new content. i.e. this is where to insert said content.

# File lib/optparse_plus/cli.rb, line 37
def add_to_file(file,lines,options = {})
  new_lines = []
  found_line = false
  File.open(file).readlines.each do |line|
    line.chomp!
    if options[:before] && options[:before] === line
      found_line = true
      new_lines += lines
    end
    new_lines << line
  end

  raise "No line matched #{options[:before]}" if options[:before] && !found_line

  new_lines += lines unless options[:before]
  File.open(file,'w') do |fp|
    new_lines.each { |line| fp.puts line }
  end
end
check_and_prepare_basedir!(basedir,force)

Checks that the basedir can be used, either by not existing, or by existing and force is true. In that case, we clean it out entirely

basedir

base directory where the user wants to create a new project

force

if true, and basedir exists, delete it before proceeding

This will exit the app if the dir exists and force is false

# File lib/optparse_plus/cli.rb, line 19
def check_and_prepare_basedir!(basedir,force)
  if File.exists? basedir
    if force
      rm_rf basedir, :verbose => true, :secure => true
    else
      exit_now! 1,"error: #{basedir} exists, use --force to override"
    end
  end
  mkdir_p basedir
end
copy_file(relative_path,options = {})

Copies a file, running it through ERB

relative_path

path to the file, relative to the project root, minus the .erb extension You should use forward slashes to separate paths; this method will handle making the ultimate path OS independent.

options

Options to affect how the copy is done:

:from

The name of the profile from which to find the file, “full” by default

:as

The name the file should get if not the one in relative_path

:executable

true if this file should be set executable

:binding

the binding to use for the template

# File lib/optparse_plus/cli.rb, line 67
def copy_file(relative_path,options = {})
  options[:from] ||= :full

  relative_path = File.join(relative_path.split(/\//))

  template_path = File.join(template_dir(options[:from]),relative_path + ".erb")
  template = ERB.new(File.open(template_path).readlines.join(''))

  relative_path_parts = File.split(relative_path)
  relative_path_parts[-1] = options[:as] if options[:as]

  erb_binding = options[:binding] or binding

  File.open(File.join(relative_path_parts),'w') do |file|
    file.puts template.result(erb_binding)
    file.chmod(0755) if options[:executable]
  end
end
gemspec()
# File lib/optparse_plus/cli.rb, line 105
def gemspec
  @gemspec || @gemspec=_get_gemspec
end
render_license_partial(partial)
# File lib/optparse_plus/cli.rb, line 101
def render_license_partial(partial)
  ERB.new(File.read(template_dir('full/'+partial))).result(binding).strip 
end
template_dir(from)

Get the location of the templates for profile “from”

# File lib/optparse_plus/cli.rb, line 87
def template_dir(from)
  File.join(File.dirname(__FILE__),'..','..','templates',from.to_s)
end
template_dirs_in(profile)
# File lib/optparse_plus/cli.rb, line 91
def template_dirs_in(profile)
  template_dir = template_dir(profile)

  Dir["#{template_dir}/**/*"].select { |x| 
    File.directory? x 
  }.map { |dir| 
    dir.gsub(/^#{template_dir}\//,'')
  }
end