Class: Dappgen::Machines::ExecuteMachine

Inherits:
Object
  • Object
show all
Defined in:
lib/dappgen/machines/execute_machine.rb

Instance Method Summary collapse

Constructor Details

#initializeExecuteMachine

Returns a new instance of ExecuteMachine.



10
11
12
# File 'lib/dappgen/machines/execute_machine.rb', line 10

def initialize
  @origin_dir = `pwd`.chomp
end

Instance Method Details

#cleanup(_) ⇒ Object



78
79
# File 'lib/dappgen/machines/execute_machine.rb', line 78

def cleanup(_)
end

#copy_base(source) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dappgen/machines/execute_machine.rb', line 31

def copy_base(source)
  if source.nil?
    puts "Copying static files to '#{@cur_dir_name}'"
    Dir["#{@static_files_dir}/*"].each do |object|
      FileUtils.cp_r object, @cur_dir_name
    end
  else
    `rm -rf .tmp`
    `git clone #{source} .tmp`
    Dir['.tmp/*'].each do |object|
      FileUtils.cp_r object, @cur_dir_name
    end
    `rm -rf .tmp`
  end

  puts "Applying replacers to '#{@cur_dir_name}'"
  Dir["#{@replacer_files_dir}/**/*.erb"].each do |template|

    basename = File.join(@cur_dir_name, template.sub(%r{^#{@replacer_files_dir}/}, '')).sub(/.erb$/, '')
    generator = Erubis::Eruby.new(File.read(template), filename: template)

    FileUtils.mkdir_p File.dirname(basename)

    result = generator.result(@opts)
    File.write(basename, result)
  end
end

#generate(file, opts) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dappgen/machines/execute_machine.rb', line 64

def generate(file, opts)
  puts "Generating '#{@cur_dir_name}/#{file}'"

  template = File.join(@template_files_dir, opts[:with])

  basename = File.join(@cur_dir_name, file)
  generator = Erubis::Eruby.new(File.read(template), filename: template)

  FileUtils.mkdir_p File.dirname(basename)

  result = generator.result(@opts)
  File.write(basename, result)
end

#remove(file) ⇒ Object



59
60
61
62
# File 'lib/dappgen/machines/execute_machine.rb', line 59

def remove(file)
  puts "Removing '#{@cur_dir_name}/#{file}'"
  FileUtils.rm_r(File.join(@cur_dir_name, file))
end

#set_thing(thing, opts) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/dappgen/machines/execute_machine.rb', line 14

def set_thing(thing, opts)
  puts "Generating a #{thing}"

  @thing = thing
  @opts = Hashie::Mash.new(opts[:opts])
  @base_data_dir = File.join(Gem.loaded_specs['dappgen'].full_gem_path, 'data', 'dappgen', 'generators', thing)
  @static_files_dir = File.join(@base_data_dir, 'base')
  @replacer_files_dir = File.join(@base_data_dir, 'replacers')
  @template_files_dir = File.join(@base_data_dir, 'templates')
end

#setdir(name) ⇒ Object



25
26
27
28
29
# File 'lib/dappgen/machines/execute_machine.rb', line 25

def setdir(name)
  @cur_dir_name = name
  puts "Preparing directory '#{name}'"
  FileUtils.mkdir_p name
end