global
def load_appium_txt opts
-
(Array
- the require files. nil if require doesn't exist)
Parameters:
-
opts
(Hash
) -- file: '/path/to/appium.txt', verbose: true
def load_appium_txt opts raise 'opts must be a hash' unless opts.kind_of? Hash opts.each_pair { |k,v| opts[k.to_s.downcase.strip.intern] = v } opts = {} if opts.nil? file = opts.fetch :file, nil raise 'Must pass file' unless file verbose = opts.fetch :verbose, false # Check for env vars in .txt parent_dir = File.dirname file toml = File.expand_path File.join parent_dir, 'appium.txt' puts "appium.txt path: #{toml}" if verbose # @private def update data, *args args.each do |name| var = data[name] ENV[name] = var if var end end toml_exists = File.exists? toml puts "Exists? #{toml_exists}" if verbose data = nil if toml_exists require 'toml' puts "Loading #{toml}" if verbose # bash requires A="OK" # toml requires A = "OK" # # A="OK" => A = "OK" data = File.read toml data = data.split("\n").map do |line| line.sub /([^\s])\=/, "\\1 = " end.join "\n" data = TOML::Parser.new(data).parsed ap data unless data.empty? if verbose update data, 'APP_PATH', 'APP_APK', 'APP_PACKAGE', 'APP_ACTIVITY', 'APP_WAIT_ACTIVITY', 'DEVICE' # Ensure app path is absolute ENV['APP_PATH'] = File.expand_path ENV['APP_PATH'] if ENV['APP_PATH'] && !ENV['APP_PATH'].empty? # device is not case sensitive ENV['DEVICE'] = ENV['DEVICE'].strip.downcase if !ENV['DEVICE'].nil? if ! %w(ios android selendroid).include? ENV['DEVICE'] raise %(DEVICE="#{ENV['DEVICE']}" must be ios, android, or selendroid.) end end # return list of require files as an array # nil if require doesn't exist if data && data['require'] r = data['require'] r = r.kind_of?(Array) ? r : [ r ] # ensure files are absolute r.map! do |file| file = file.include?(File::Separator) ? file : File.join(parent_dir, file) file = File.expand_path file File.exists?(file) ? file : nil end r.compact! # remove nils files = [] # now expand dirs r.each do |item| unless File.directory? item # save file files << item next # only look inside folders end Dir.glob(File.join(item, '**/*.rb')) do |file| # do not add folders to the file list files << File.expand_path(file) unless File.directory? file end end files end end