lib/pione/component/package.rb in pione-0.2.1 vs lib/pione/component/package.rb in pione-0.2.2
- old
+ new
@@ -14,25 +14,34 @@
end
end
# Package is a container of rules, scripts, scenarios, and etc.
class Package < StructX
+ member :location
member :info, default: {}
member :bin
- member :scenarios, default: []
+ member :scenario_paths, default: []
member :documents, default: []
forward_as_key Proc.new{info}, "PackageName", :name
+ forward_as_key Proc.new{info}, "Edition", :edition
+ forward_as_key Proc.new{info}, "Tag", :tag
+ forward_as_key Proc.new{info}, "HashID", :hash_id
forward :@unified_document, :find, :find_rule
forward! :@unified_document, :rules, :create_root_rule, :params
def initialize(*args)
super(*args)
+ info["Edition"] = "origin" unless info["Edition"]
build_unified_document
validate
end
+ def scenarios
+ scenario_paths.map {|path| PackageScenarioReader.read(location, path)}
+ end
+
# Upload the package files to the location.
#
# @return [void]
def upload(dest)
if bin and bin.exist?
@@ -48,14 +57,17 @@
# @param name [String]
# scenario name
# @return [PackageScenario]
# the scenario
def find_scenario(name)
- if name == :anything
- scenarios.first
- else
- scenarios.find {|scenario| scenario.name == name}
+ scenario_paths.each do |path|
+ if name == :anything
+ return PackageScenarioReader.read(location, path)
+ else
+ scenario = PackageScenarioReader.read(location, path)
+ return scenario if scenario.name == name
+ end
end
end
private
@@ -79,123 +91,10 @@
end
end
end
end
- # PackageReader is a reader for packages.
- class PackageReader
- class << self
- # Read a pacakge from the location.
- #
- # @param location [Location::BasicLocation]
- # location of package
- # @return [Package]
- # the package
- def read(location)
- new(location).read
- end
- end
-
- attr_reader :location
- attr_reader :type
-
- # @param location [Location]
- # package location
- def initialize(location)
- @location = location
- @type = check_package_type
- end
-
- # Read the package.
- #
- # @return [Package]
- # the package
- def read
- case @type
- when :directory
- return read_package_directory
- when :pione_document_file
- return read_pione_document_file
- end
- end
-
- private
-
- # Check package type.
- #
- # @return [Symbol]
- # package type
- def check_package_type
- return :directory if @location.directory?
- if File.extname(@location.basename) == ".pione"
- return :pione_document_file
- end
- raise ArgumentError.new(@location)
- end
-
- # Read package directory.
- #
- # @return [Package]
- # the package
- def read_package_directory
- info = read_package_info
- Package.new(
- info: info,
- bin: @location + "bin",
- scenarios: find_scenarios,
- documents: find_documents(info["PackageName"])
- )
- end
-
- # Read PIONE document.
- #
- # @return [Package]
- # the package
- def read_pione_document_file
- document = Component::Document.load(@location, "Main")
- Package.new(info: {"PackageName" => "Main"}, documents: [document])
- end
-
- # Read the informations from the package location.
- #
- # @return [Hash]
- # package information table
- def read_package_info
- YAML.load((@location + "package.yml").read)
- rescue Location::NotFound
- raise InvalidPackageError.new(self, "package.yml not found in %s" % @location.uri)
- end
-
- # Find scenarios from the package location.
- #
- # @return [Array<PackageScenario>]
- # scenarios
- def find_scenarios
- if (@location + "scenario" + "scenario.yml").exist?
- [PackageScenarioReader.read(@location + "scenario")]
- else
- if (@location + "scenario").exist? and (@location + "scenario").directory?
- (@location + "scenario").entries.map do |scenario|
- PackageScenarioReader.read(scenario)
- end.compact
- else
- []
- end
- end
- end
-
- # Find documents from the packcage location.
- #
- # @return [Array<Document>]
- # documents
- def find_documents(package_name)
- @location.entries.select do |entry|
- entry.file? and entry.path.extname == ".pione"
- end.map {|entry| Document.load(entry, package_name) }
- end
- end
-
# RehearsalResult represents error result of rehearsal test.
class RehearsalResult < StructX
member :key
member :name
@@ -213,41 +112,71 @@
# PackageScenario is a class for expected scenario of rule's behavior.
class PackageScenario
include SimpleIdentity
attr_reader :location
+ attr_reader :package_path
attr_reader :info
forward_as_key :@info, "ScenarioName", :name
+ forward_as_key :@info, "Edition", :edition
+ forward_as_key :@info, "Version", :version
+ forward_as_key :@info, "Date", :date
# @param location [BasicLocation]
# scenario location
# @param info [Hash]
# scenario information table
- def initialize(location, info)
+ def initialize(location, package_path, info)
@location = location
+ @package_path = package_path
@info = info
+ @package_path = package_path
end
# Return the input location. If the scenario doesn't have input location,
# return nil.
#
# @return [BasicLocation]
# the input location
def input
- input_location = @location + "input"
+ input_location = @location + @package_path + "input"
input_location if input_location.exist?
end
+ # Return input file locations.
+ #
+ # @return [BasicLocation]
+ # input file locations
+ def inputs
+ if info.has_key?("Inputs")
+ info["Inputs"].map {|name| @location + @package_path + "input" + name}
+ else
+ []
+ end
+ end
+
# Return the output location.
#
# @return [BasicLocation]
# the output location
def output
- @location + "output"
+ @location + @package_path + "output"
end
+ # Return output file locations.
+ #
+ # @return [BasicLocation]
+ # output file locations
+ def outputs
+ if info.has_key?("Outputs")
+ info["Outputs"].map {|name| @location + @package_path + "output" + name}
+ else
+ []
+ end
+ end
+
# Validate reheasal results.
def validate(result_location)
return [] unless output.exist?
errors = []
@@ -261,50 +190,9 @@
else
errors << RehearsalResult.new(:not_exist, name)
end
end
return errors
- end
- end
-
- # PackageScenarioReader is a reader for loading scenarios.
- class PackageScenarioReader
- def self.read(location)
- new(location).read
- end
-
- # @param location [Location]
- # the scenario location
- def initialize(location)
- @location = location
- end
-
- # Read scenario files.
- #
- # @return [PackageScenario]
- # the scenario
- def read
- begin
- info = read_scenario_informations
- PackageScenario.new(@location, info)
- rescue
- nil
- end
- end
-
- private
-
- # Read scenario information table.
- #
- # @return [Hash]
- # scenario information table
- def read_scenario_informations
- path = @location + "scenario.yml"
- if path.exist?
- YAML.load(path.read)
- else
- {"ScenarioName" => @location.basename}
- end
end
end
end
end