require 'fileutils' module BuildMaster class CottaFile attr_reader :system, :path def initialize(system, path) @system = system @path = path end def cotta return Cotta.new(system) end def name return @path.basename.to_s end def parent return CottaDir.new(@system, @path.parent) end def extname return @path.extname end def basename return @path.basename(extname).to_s end def exists? return @system.file_exists?(@path) end def copy_to(target_file) target_file.parent.mkdirs @system.copy(path, target_file.path) end def move_to(target_file) target_file.parent.mkdirs @system.move(path, target_file.path) end def save(content = '') write {|file| file.printf content.to_s} end def write(&block) parent.mkdirs open('w', &block) end def load content = nil read {|file| content = file.read} return content end def read(&block) open('r', &block) end def foreach() open('r') do |file| file.each {|line| yield line} end end def delete @system.delete_file(@path) end def ==(other) return false unless other.kind_of? CottaFile return @system == other.system && @path == other.path end def to_s return "#{super}-#@system-#@path" end private def open(*args) result = f = @system.io(@path, *args) if block_given? begin result = yield f ensure f.close end end return result end end end