2016-10-08 13:25:38 +02:00
|
|
|
module Hbc
|
|
|
|
class CaskLoader
|
|
|
|
def self.load_from_file(path)
|
|
|
|
raise CaskError, "File '#{path}' does not exist" unless path.exist?
|
|
|
|
raise CaskError, "File '#{path}' is not readable" unless path.readable?
|
|
|
|
raise CaskError, "File '#{path}' is not a plain file" unless path.file?
|
|
|
|
|
|
|
|
token = path.basename(".rb").to_s
|
2016-10-08 19:20:54 +02:00
|
|
|
content = IO.read(path).force_encoding("UTF-8")
|
2016-10-08 13:25:38 +02:00
|
|
|
|
|
|
|
new(token, content, path).load
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.load_from_string(token, content)
|
|
|
|
new(token, content).load
|
|
|
|
end
|
|
|
|
|
|
|
|
def load
|
|
|
|
instance_eval(@content, __FILE__, __LINE__)
|
|
|
|
rescue CaskError, StandardError, ScriptError => e
|
|
|
|
e.message.concat(" while loading '#{@token}'")
|
|
|
|
e.message.concat(" from '#{@path}'") unless @path.nil?
|
|
|
|
raise e, e.message
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def initialize(token, content, path = nil)
|
|
|
|
@token = token
|
|
|
|
@content = content
|
|
|
|
@path = path unless path.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def cask(header_token, &block)
|
|
|
|
raise CaskTokenDoesNotMatchError.new(@token, header_token) unless @token == header_token
|
|
|
|
|
|
|
|
if @path.nil?
|
2017-02-03 22:00:28 +01:00
|
|
|
Cask.new(@token, &block)
|
2016-10-08 13:25:38 +02:00
|
|
|
else
|
2017-02-03 22:00:28 +01:00
|
|
|
Cask.new(@token, sourcefile_path: @path, &block)
|
2016-10-08 13:25:38 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|