lib/arli/commands/install.rb in arli-0.8.3 vs lib/arli/commands/install.rb in arli-0.9.0

- old
+ new

@@ -4,42 +4,69 @@ require 'arli' require 'arduino/library' require_relative 'base' require_relative 'bundle' +require 'arli/library' module Arli module Commands - class Install < Bundle + class Install < Base require 'arduino/library/include' - attr_accessor :library + attr_accessor :library, + :arlifile, + :install_argument, + :install_method + include ::Arli::Library + def setup - validate_argument + super - self.library = identify_library(runtime.argv.first) - validate_library + self.install_argument = runtime.argv.first + raise InvalidInstallSyntaxError, + "Missing installation argument: a name, a file or a URL." unless install_argument - self.arlifile = Arli::ArliFile.new(config: config, libraries: [library]) if library + self.library = identify_library(install_argument) + raise Arli::Errors::LibraryNotFound, + "Library #{cfg.to_hash} was not found" unless library + + self.arlifile = Arli::ArliFile.new(config: config, libraries: [ library ]) + if config.trace + info("found library using #{install_method}:\n#{library.inspect}") + end end + def run + arlifile.install + end + # arg can be 'Adafruit GFX Library' def identify_library(arg) - if File.exist?(arg) - begin - Arduino::Library::Model.from(arg) - rescue - nil - end - elsif arg =~ %r[https?://] - Arduino::Library::Model.from_hash(url: arg, name: File.basename(arg)) - else - results = search(name: /^#{arg}$/i) - validate_search(arg, results) - results.sort.last if results && !results.empty? - end + results = if arg =~ %r[https?://]i + self.install_method = :url + r = search(url: /^#{arg}$/i) + if r.empty? + self.install_method = :website + r = search(website: /^#{arg}$/i) + end + if r.empty? + self.install_method = :custom + r = [ Arduino::Library::Model.from_hash(url: arg, name: File.basename(arg)) ] + end + r + elsif File.exist?(arg) || arg =~ /\.zip$/ + self.install_method = :archiveFileName + search(archiveFileName: "#{File.basename(arg)}") + else + self.install_method = :name + search(name: /^#{arg}$/) + end + + validate_search(arg, results) + results.sort.last if results && !results.empty? end def params " • #{library.to_s}" end @@ -50,22 +77,13 @@ private def validate_search(arg, results) raise Arli::Errors::LibraryNotFound, - "Can't find library by argument #{arg.bold.yellow}" if results.nil? || results.empty? + "Can't find library by argument #{arg.bold.yellow}, searching by #{install_method}" if results.nil? || results.empty? + dupes = results.map(&:name).uniq.size raise Arli::Errors::TooManyMatchesError, - "More than one match found for #{arg.bold.yellow}" if results.map(&:name).uniq.size > 1 - end - - def validate_library - raise Arli::Errors::LibraryNotFound, - "Library #{cfg.to_hash} was not found" unless library - end - - def validate_argument - raise InvalidInstallSyntaxError, - "Missing installation argument: a name, a file or a URL." unless runtime.argv.first + "More than one match found for #{arg.bold.yellow} — #{dupes} libraries matched" if dupes > 1 end def cfg config.install end