Class: BoxenModuleMaker::Bin

Inherits:
Object
  • Object
show all
Defined in:
lib/boxen-module-maker/bin.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Bin

Returns a new instance of Bin.



8
9
10
11
12
# File 'lib/boxen-module-maker/bin.rb', line 8

def initialize(args)
  @args = args
  @target_dir = "puppet-#{@args[0]}"
  @application_name = args[0]
end

Instance Method Details

#commit_working_version(target_dir) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/boxen-module-maker/bin.rb', line 75

def commit_working_version(target_dir)
  Dir.chdir(target_dir) do
    @repo = Git.init()

    begin
      @repo.add('.')
    rescue Git::GitExecuteError => e
      #raise GitAddFailed, "There was some problem adding this directory to the git changeset"
      raise
    end

    begin
      @repo.commit "#{@target_dir} generated by boxen-module-maker"
    rescue Git::GitExecuteError => e
      raise
    end

  end
end

#get_boxen_template(target_dir) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/boxen-module-maker/bin.rb', line 48

def get_boxen_template(target_dir)
  Dir.chdir(target_dir) do
    begin
      @repo = Git.init()
    rescue Git::GitExecuteError => e
      raise GitInitFailed, "Encountered an error during gitification. Maybe the repo already exists, or has already been pushed to?"
    end

    @repo.add_remote('template', 'https://github.com/boxen/puppet-template.git')
    @repo.remote('template').fetch
    @repo.checkout('master')

    # Remote remove is broken on ruby-git right now...
    # https://github.com/schacon/ruby-git/pull/42
    # Time for a hacky fix! :D
    cmd = 'git remote remove template'
    value = `#{cmd}`

    begin
      @repo.add('.')
    rescue Git::GitExecuteError => e
      #raise GitAddFailed, "There was some problem adding this directory to the git changeset"
      raise
    end
  end
end

#output_template_in_target(source, destination = source) ⇒ Object



101
102
103
104
105
# File 'lib/boxen-module-maker/bin.rb', line 101

def output_template_in_target(source, destination = source)
  final_destination = File.join(@target_dir, destination)
  template_result   = render_template(source)
  File.open(final_destination, 'w') {|file| file.write(template_result)}
end

#rename_template_files(module_name) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/boxen-module-maker/bin.rb', line 38

def rename_template_files(module_name)
  Dir.chdir(module_name) do
    FileUtils.mv "spec/fixtures/modules/template/", "spec/fixtures/modules/#{@application_name}/"
    FileUtils.rm "spec/classes/template_spec.rb"
  end
  output_template_in_target 'manifests/init.pp', File.join('manifests', 'init.pp')
  output_template_in_target 'module_spec.rb', File.join('spec/classes/', "#{@application_name}_spec.rb")

end

#render_template(source) ⇒ Object



95
96
97
98
99
# File 'lib/boxen-module-maker/bin.rb', line 95

def render_template(source)
  template_contents = File.read(File.join(template_dir, source))
  template          = ERB.new(template_contents, nil, '<>')
  template.result(binding).gsub(/\n\n\n+/, "\n\n")
end

#runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/boxen-module-maker/bin.rb', line 14

def run
  if @args[0].nil?
    abort "boxen-module-maker: Please specify the name of the module you wish to make"
  elsif @args.length > 1
    abort "boxen-module-maker: Too many arguments, one at a time please."
  end

  module_name = "puppet-#{@args[0]}"
  if File.exists?(module_name)
    warn "boxen-module-maker: [SKIPPING] '#{module_name}' already exists"
    return 1
  elsif File.exists?(module_name.downcase)
    warn "boxen-module-maker: [SKIPPING] '#{module_name.downcase}' exists, which could conflict with `#{module_name}'"
    return 1
  else
    puts "boxen-module-maker: [CREATE FOLDER] Making '#{module_name}'"
    FileUtils.mkdir_p(module_name)
    get_boxen_template(module_name)
    rename_template_files(module_name)
    commit_working_version(module_name)
    return 0
  end
end

#template_dirObject



107
108
109
# File 'lib/boxen-module-maker/bin.rb', line 107

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