Class: Composify::Template

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

Constant Summary collapse

FILE_LIST =

TODO: Make these work on windows?

{
  :entrypoint => './templates/bin/entrypoint.sh.erb',
  :docker_compose => './templates/docker-compose.yml.erb',
  :dockerfile_base => './templates/Dockerfile.base.erb',
  :dockerfile_production => './templates/Dockerfile.production.erb',
  :dockerfile_development => './templates/Dockerfile.development.erb',
  :makefile => './templates/Makefile.erb',
  # :makefile_internal => './templates/Makefile.internal.erb',
}

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Template

Returns a new instance of Template.



20
21
22
# File 'lib/composify.rb', line 20

def initialize( file )
  @file = file
end

Instance Method Details

#destinationObject



24
25
26
# File 'lib/composify.rb', line 24

def destination
  return @_destination ||= @file.gsub( /^\.\/templates\//, './' ).gsub(/\.erb/, '')
end

#result(bind) ⇒ Object

bind - the binding to use for ERB#result



29
30
31
32
33
34
35
# File 'lib/composify.rb', line 29

def result( bind )
  ERB.new(
    File.read( File.expand_path( File.join("..", @file), __FILE__ ) ),
    nil,
    '-'
  ).result( bind )
end

#write(bind, force: false) ⇒ Object

bind - the binding to use for ERB#result



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/composify.rb', line 38

def write( bind, force: false )
  if File.exists?( destination ) && !force
    STDERR.puts( "File already exists at #{destination}.  Refusing to overwrite" )
    return
  end

  if (dirname = File.dirname(destination)) != "."
    FileUtils.mkdir_p( dirname )
  end

  puts "Attempting to write #{destination}"
  File.open( destination, "w" ) do |f|
    contents = result( bind )
    f.write( contents )
  end
end