#!/usr/bin/env ruby # coding: utf-8 # This file has been generated by easy_app_helper Gem on the Sun Dec 11 12:11:18 2016 # See 'https://rubygems.org/gems/easy_app_helper' require 'rubygems' require 'bundler/setup' require 'easy_app_helper' require 'easy_app_helper/scripts' require 'signature_generator' require 'html_minifier' module SignatureGenerator class SgMasterScript < EasyAppHelper::Scripts::Master # If you want to manage sub-commands a-la-git, include following # include EasyAppHelper::Scripts::SubCommandManager NAME = 'Sg' DESCRIPTION = 'Description of sg' attr_reader :template_io, :output_io, :context def initialize super(NAME, SignatureGenerator::VERSION, DESCRIPTION, 'sg') end def add_script_options ## Create here your extra command-line options ## Here under are examples using potentially gem config layer to display default option value... ## Check Slop documentation for further info. config.add_command_line_section do |slop| slop.on :f, :'template-file', 'Specify the signature template file. Default is STDIN.', argument: true, as: String slop.on :o, :'output-file', 'Specify the output file for the signature. Default is STDOUT.', argument: true, as: String slop.on :force, 'Force overwriting output file.', argument: false slop.on :'max-retry', "Number of times retrying is allowed when prompted for values. Default is #{SignatureGenerator::Processor::MAX_RETRY}.", argument: true, as: Integer slop.on :var=, 'Define variables in the form name=value', argument: true, as: Array slop.on :n, :'no-minify', 'Will not minify the resulting html signature. By default will do', argument: false slop.on :'inline-images', 'Encodes remote images as Base64', argument: false end end def do_process # Your code here. content = template_io.read logger.debug content processor = SignatureGenerator::Processor.new context: context, max_retry: config[:'max-retry'] signature = processor.transform content logger.debug signature unless config[:'no-minify'] minifier = HtmlMinifier::Minifier.new collapseWhitespace: true, collapseInlineTagWhitespace: true signature = minifier.minify signature logger.debug 'Minifying resulting signature' logger.debug signature end output_io.puts signature ensure template_io.close output_io.close end def check_config # Check the config and raise an exception if incorrect. config[:var] ||=[] # Check max retry config[:'max-retry'] ||= SignatureGenerator::Processor::MAX_RETRY # Context setup @context = {} config[:var].each do |definition| valid = false definition.match(/^\s*(?[a-z][a-z0-9_]*[!\?]?)\s*=\s*(?.+)\s*$/) do |md| logger.warning "Overwriting existing variable '#{md['var_name']}' !" if context[md['var_name']] context[md['var_name'].to_sym] = md['var_value'] valid = true end raise SignatureGenerator::Error, 'Invalid property specified !' unless valid end # Input definition @template_io = case config[:'template-file'] when NilClass logger.debug 'Template from STDIN' STDIN when String file = File.expand_path(config[:'template-file']) raise SignatureGenerator::Error, 'Invalid template file specified !' unless File.readable? file io = File.open file, 'r' logger.debug "Template from file '#{file}'" io end # Output definition @output_io = case config[:'output-file'] when NilClass logger.debug 'Signature to STDOUT' STDOUT when String file = File.expand_path(config[:'output-file']) if File.exists? file raise SignatureGenerator::Error, 'Output file already exists ! Use --force' unless config[:force] end io = File.open file, 'w' logger.debug "Signature generated into file '#{file}'" io end end end end SignatureGenerator::SgMasterScript.new.run