Module: Methadone::CLI

Defined in:
lib/methadone/cli.rb

Overview

Methadone Internal - treat as private

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

Instance Method Summary collapse

Instance Method Details

#add_to_file(file, lines, options = {}) ⇒ Object

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.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/methadone/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) ⇒ Object

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



19
20
21
22
23
24
25
26
27
28
# File 'lib/methadone/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 = {}) ⇒ Object

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/methadone/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

#gemspecObject



105
106
107
# File 'lib/methadone/cli.rb', line 105

def gemspec
  @gemspec || @gemspec=_get_gemspec
end

#render_license_partial(partial) ⇒ Object



101
102
103
# File 'lib/methadone/cli.rb', line 101

def render_license_partial(partial)
  ERB.new(File.read(template_dir('full/'+partial))).result(binding).strip 
end

#template_dir(from) ⇒ Object

Get the location of the templates for profile “from”



87
88
89
# File 'lib/methadone/cli.rb', line 87

def template_dir(from)
  File.join(File.dirname(__FILE__),'..','..','templates',from.to_s)
end

#template_dirs_in(profile) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/methadone/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