Class: Another::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/another/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Runner

Returns a new instance of Runner.



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/another/runner.rb', line 3

def initialize(args)
  @args = args
  return if test_mode?
  return puts("Probably for the best.") unless confirmed?
  say "Copying files..."
  FileUtils.mkdir target_directory
  Dir[File.join(template_directory, "**", "**")].each do |file|
    copy!(file.split('/templates/').last)
  end
  say "done!"
end

Instance Method Details

#ask(msg) {|$stdin.gets.chomp| ... } ⇒ Object

Yields:

  • ($stdin.gets.chomp)


67
68
69
70
# File 'lib/another/runner.rb', line 67

def ask(msg)
  print msg unless test_mode?
  yield $stdin.gets.chomp
end

#confirmed?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
# File 'lib/another/runner.rb', line 15

def confirmed?
  say "Warning: Creating new projects makes you vulnerable to mockery.".bold
  ask "Are you sure you want to create a new project? ".bold do |answer|
    answer =~ /^y/i
  end
end

#copy!(path) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/another/runner.rb', line 49

def copy!(path)
  begin
    read(path) \
      .erb_eval(binding) \
      .write(target_directory(path))
  rescue Errno::EISDIR
    FileUtils.mkdir target_directory(path)
  end
end

#module_nameObject



30
31
32
# File 'lib/another/runner.rb', line 30

def module_name
  project_name.gsub(/\W+/, '_').split('_').map(&:capitalize).join
end

#project_nameObject



26
27
28
# File 'lib/another/runner.rb', line 26

def project_name
  @args.first
end

#read(path) ⇒ Object



59
60
61
# File 'lib/another/runner.rb', line 59

def read(path)
  File.read(template_directory(path))
end

#say(msg) ⇒ Object



63
64
65
# File 'lib/another/runner.rb', line 63

def say(msg)
  puts(msg) unless test_mode?
end

#target_directory(path = '') ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/another/runner.rb', line 34

def target_directory(path='')
  File.expand_path \
    [
      Dir.pwd,
      project_name,
      path \
        .gsub('PROJECT', project_name.gsub(/\W+/, '_')) \
        .sans('.erb')
    ].compact.join("/")
end

#template_directory(path = '') ⇒ Object



45
46
47
# File 'lib/another/runner.rb', line 45

def template_directory(path='')
  File.join(File.dirname(__FILE__), *%W[.. .. templates #{path}])
end

#test_mode?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/another/runner.rb', line 22

def test_mode?
  defined?(Spec)
end