require "io/console" module Sapos module Print include Thor::Actions class CLI < Thor desc "setup", "Setup Wizard" def setup info config = {} url = ask("Configuration URL") response = RestClient.get(url) data = JSON.parse(response) config_file = "#{Sapos::Print.app_directory}/config.yml" user_id = nil if File.exist?(config_file) template = ERB.new(File.read(config_file)) result = YAML.load(template.result(binding)) user_id = result[:user_id] end user_id = SecureRandom.uuid if user_id.nil? config[:q] = data["channel"] config[:key] = data["key"] config[:printer] = ask("Printer Name: ") config[:adapter] = ask("Printer Adapter: ", limited_to: ["console", "cups", "windows"]) config[:interface] = ask("Interface", limited_to: ["text", "escp"]) config[:user_id] = user_id if yes?("Are you sure that you want to write the configuration file?") Sapos::Print::Configuration.write(config) ap config say "Configuration Saved! Cheers" end end desc "start", "run service" def start info config = Sapos::Print.config puts ["Q=#{config.q.to_s}", "Printer=#{config.printer.to_s}", "Adapter=#{config.adapter.to_s}", "Interface=#{config.interface}"].join("\n") print_dir = File.join(Sapos::Print.app_directory, 'print') if File.exists?(print_dir) FileUtils.rm_rf Dir.glob("#{print_dir}/*") end Sapos::Print::QReader.printer_config = config Sapos::Print::QReader.verbose = options[:verbose] Sapos::Print::QReader.run! end desc "print", "demo print" def print info config = Sapos::Print.config printer = Printer.new(config) printer.print(document: "This is a test\nprint page\nwithno special commands", document_number: "test", print_control: true) end def windows_test_print info filename = "test.print" dir = File.join(Dir.home,".sapos-print") FileUtils.mkdir_p dir unless File.exists?(dir) dir print_dir = File.join(dir, 'print') FileUtils.mkdir_p print_dir unless File.exists?(print_dir) filename = File.join(print_dir, filename) puts "Test file Path: #{filename}" File.open(filename, 'w'){|f| f.write("Test\nTest\nTest")} puts "Execute COPY Command" command = "COPY "+"'"+filename.gsub("/","\\")+"'"+'"\\\\%COMPUTERNAME%\\'+"TM" if !system(command) puts "ERROR" else puts "OK" end end no_tasks do def info if @printed.nil? @printed ||= begin print_wrapped("=+" * 40) say("SaposPrint v#{Sapos::Print::VERSION}") say "... by Agile Software Development [Copyright © 2016-#{Time.now.year}]" print_wrapped("=+" * 40) puts "\n" end end end end end end end