Class: Terraspace::Compiler::Writer

Inherits:
Object
  • Object
show all
Includes:
Basename, Util
Defined in:
lib/terraspace/compiler/writer.rb

Instance Method Summary collapse

Methods included from Util::Pretty

#pretty_path, #pretty_time

Methods included from Util::Sure

#sure?

Methods included from Util::Logging

#logger

Methods included from Basename

#basename

Constructor Details

#initialize(mod, options = {}) ⇒ Writer

Returns a new instance of Writer.



6
7
8
9
10
# File 'lib/terraspace/compiler/writer.rb', line 6

def initialize(mod, options={})
  @mod, @options = mod, options
  @src_path = options[:src_path]
  @dest_name = options[:dest_name] # override generated name
end

Instance Method Details

#dest_dirObject



24
25
26
27
28
29
30
# File 'lib/terraspace/compiler/writer.rb', line 24

def dest_dir
  if @mod.is_a?(Terraspace::Mod::Remote)
    File.dirname(@src_path) # for Mod::Remote src is dest
  else
    @mod.cache_dir
  end
end

#dest_pathObject



12
13
14
15
16
# File 'lib/terraspace/compiler/writer.rb', line 12

def dest_path
  name = get_name
  name = basename(name)
  "#{dest_dir}/#{name}"
end

#get_nameObject



18
19
20
21
22
# File 'lib/terraspace/compiler/writer.rb', line 18

def get_name
  return @dest_name if @dest_name
  return @src_path if Terraspace.pass_file?(@src_path)
  @src_path.sub('.rb','.tf.json')
end

#write(content) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/terraspace/compiler/writer.rb', line 32

def write(content)
  FileUtils.mkdir_p(File.dirname(dest_path))
  if content.respond_to?(:path) # IO filehandle
    FileUtils.cp(content.path, dest_path) # preserves permission
  else # just content
    IO.write(dest_path, content, mode: "wb") unless content.nil?
  end
  logger.debug "Created #{Terraspace::Util.pretty_path(dest_path)}"
end