Class: Ufo::Init

Inherits:
Object
  • Object
show all
Defined in:
lib/ufo/init.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Init

Returns a new instance of Init.



8
9
10
11
# File 'lib/ufo/init.rb', line 8

def initialize(options = {})
  @options = options
  @project_root = options[:project_root] || '.'
end

Instance Method Details

#add_gitignoreObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ufo/init.rb', line 49

def add_gitignore
  gitignore_path = "#{@project_root}/.gitignore"
  if File.exist?(gitignore_path)
    ignores = IO.read(gitignore_path)
    has_ignore = ignores.include?("ufo/output")
    ignores << ufo_ignores unless has_ignore
  else
    ignores = ufo_ignores
  end
  IO.write(gitignore_path, ignores)
end

#setupObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ufo/init.rb', line 13

def setup
  puts "Setting up ufo project...".blue unless @options[:quiet]
  source_root = File.expand_path("../../starter_project", __FILE__)
  # https://ruby-doc.org/core-2.2.0/Dir.html
  # use the File::FNM_DOTMATCH flag or something like "{*,.*}".
  paths = Dir.glob("#{source_root}/**/{*,.*}").
            select {|p| File.file?(p) }
  paths.each do |src|
    dest = src.gsub(%r{.*starter_project/},'')
    dest = "#{@project_root}/#{dest}"

    if File.exist?(dest) and !@options[:force]
      puts "exists: #{dest}".yellow unless @options[:quiet]
    else
      dirname = File.dirname(dest)
      FileUtils.mkdir_p(dirname) unless File.exist?(dirname)
      if dest =~ /\.erb$/
        FileUtils.cp(src, dest)
      else
        write_erb_result(src, dest)
      end
      puts "created: #{dest}".green unless @options[:quiet]
    end
  end
  puts "Starter ufo files created.".blue
  File.chmod(0755, "#{@project_root}/bin/deploy")
  add_gitignore
end

#ufo_ignoresObject



61
62
63
64
65
66
67
# File 'lib/ufo/init.rb', line 61

def ufo_ignores
  ignores =<<-EOL
/ufo/output
/ufo/docker_image_name*.txt
/ufo/version
EOL
end

#write_erb_result(src, dest) ⇒ Object



42
43
44
45
46
47
# File 'lib/ufo/init.rb', line 42

def write_erb_result(src, dest)
  source = IO.read(src)
  b = ERBContext.new(@options).get_binding
  output = ERB.new(source).result(b)
  IO.write(dest, output)
end