Method: Jsus::SourceFile.from_file

Defined in:
lib/jsus/source_file.rb

.from_file(filename, options = {}) ⇒ Jsus::SourceFile

Initializes a SourceFile given the filename and options

Parameters:

  • filename (String)
  • options (Hash) (defaults to: {})

Options Hash (options):

Returns:

Raises:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/jsus/source_file.rb', line 49

def self.from_file(filename, options = {})
  if File.exists?(filename)
    source = File.open(filename, 'r:utf-8') {|f| f.read }
    bom = RUBY_VERSION =~ /1.9/ ? "\uFEFF" : "\xEF\xBB\xBF"
    source.gsub!(bom, "")
    yaml_data = source.match(%r(^/\*\s*(---.*?)\*/)m)
    if (yaml_data && yaml_data[1] && header = YAML.load(yaml_data[1]))
      options[:header]            = header
      options[:relative_filename] = filename
      options[:filename]          = File.expand_path(filename)
      options[:content]           = source
      new(options)
    else
      raise BadSourceFileException, "#{filename} is missing a header or header is invalid"
    end
  else
    raise BadSourceFileException, "Referenced #{filename} does not exist. #{options[:package] ? "Referenced from package #{options[:package].name}" : ""}"
  end
rescue Exception => e
  if !e.kind_of?(BadSourceFileException) # if we didn't raise the error; like in YAML, for example
    raise "Exception #{e.inspect} happened on #{filename}. Please take appropriate measures"
  else # if we did it, just reraise
    raise e
  end
end