lib/arli/arli_file.rb in arli-0.3.2 vs lib/arli/arli_file.rb in arli-0.5.1

- old
+ new

@@ -1,24 +1,67 @@ require 'arli' -require 'arduino/library' require 'yaml' +require 'forwardable' +require 'arduino/library' module Arli class ArliFile require 'arduino/library/include' + include Enumerable + extend Forwardable def_delegators :@dependencies, *(Array.new.methods - Object.methods) - DEFAULT_FILE_NAME = 'ArliFile.yml'.freeze + attr_accessor :dependencies, :file_hash, :file, :lib_path, :resolved - attr_accessor :dependencies, :arli_hash, :file + def initialize(lib_path: Arli.library_path, + arlifile_path: nil) - def initialize(file = DEFAULT_FILE_NAME) - self.file = file - self.arli_hash = ::YAML.load(File.read(file)) - self.dependencies = arli_hash['dependencies'].map do |lib_hash| - library_from(lib_hash) + self.lib_path = lib_path + self.file = arlifile_path ? "#{arlifile_path}/#{Arli::Config::DEFAULT_FILENAME}" : + Arli::Config::DEFAULT_FILENAME + raise(Arli::Errors::ArliFileNotFound, 'Arlifile could not be found') unless file || !File.exist?(file) + self.dependencies = [] + + parse! + end + + + def libraries + self.dependencies + end + + def within_lib_path + FileUtils.mkpath(lib_path) unless Dir.exist?(lib_path) + Dir.chdir(lib_path) do + yield if block_given? end end + + def each_dependency(&_block) + within_lib_path do + dependencies.each do |dependency| + yield(dependency) + end + end + end + + def error(*args) + STDERR.puts *args.join("\n") + end + + def info(*args) + STDOUT.puts *args.join("\n") if Arli.debug? + end + + private + + def parse! + self.file_hash = ::YAML.load(::File.read(self.file)) + self.dependencies = file_hash['dependencies'].map do |lib| + ::Arduino::Library::Model.from_hash(lib) + end + end + end end