Sha256: 5f9bca23265e4dc3a928095f8aef58e0485111975a4a040888225a6d13574da3

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

#!/usr/bin/env ruby
# encoding: UTF-8
# (c) ANB Andrew Bizyaev

require 'phtools/utils/ruby_version.rb'
require 'phtools/version'
require 'phtools/utils'
require 'phtools/error.rb'
require 'phtools/ph_file.rb'
require 'docopt'

# Foto Tools
module PhTools
  # Main class processing input stream
  class Runner
    def initialize(usage, file_type = [])
      case Utils.os
      when :windows
        # workaround for win32
        ARGV.map! { |a| a.encode('utf-8', 'filesystem') }
        @os = Utils::OSWin.new
      else
        @os = Utils::OSUnix.new
      end
      @tool_name = File.basename($PROGRAM_NAME)
      @options_cli = Docopt.docopt(usage, version: "v#{PhTools::VERSION}")
      @file_type = file_type
      PhTools.debug = true if @options_cli['--debug']

      validate_options

      if PhTools.debug
        STDERR.puts "Instance Variables: "
        STDERR.puts context
      end

    rescue Docopt::Exit => e
      STDERR.puts e.message
      exit 1
    rescue => e
      PhTools.puts_error "FATAL: #{e.message}", e
      exit 1
    end

    def run!
      return if STDIN.tty?
      ARGV.clear
      process_before

      ARGF.each_line do |line|
        filename = line.chomp
        begin
          PhFile.validate_file!(filename, @file_type)
          ftfile = PhFile.new(filename)
          @os.output process_file(ftfile)
        rescue PhTools::Error => e
          PhTools.puts_error "ERROR: '#{filename}' - #{e.message}", e
        end
      end

      process_after

    rescue SignalException
      PhTools.puts_error 'EXIT on user interrupt Ctrl-C'
      exit 1
    rescue => e
      PhTools.puts_error "FATAL: #{e.message}", e
      exit 1
    end

    private

    def validate_options
    end

    def process_before
    end

    def process_file(file)
      file
    end

    def process_after
    end

    def context
      self.instance_variables.map do |item|
        { item => self.instance_variable_get(item) }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
phtools-0.6.0 lib/phtools/runner.rb