lib/seek/sample_templates/generator.rb in sample-template-generator-gem-0.2.1 vs lib/seek/sample_templates/generator.rb in sample-template-generator-gem-0.3.0

- old
+ new

@@ -1,46 +1,29 @@ -require 'open4' +require 'cocaine' module Seek module SampleTemplates - def self.generate(sheet_name, sheet_index, columns, path) - Seek::SampleTemplates::Generator.new(path, create_json(columns, sheet_index, sheet_name)).generate - end - - def self.create_json(columns, sheet_index, sheet_name) - { sheet_name: sheet_name, sheet_index: sheet_index, columns: columns.collect(&:as_json) }.to_json - end - - class Column - attr_accessor :name, :contents - def initialize(name, contents = []) - @name = name - @contents = contents - end - - def as_json - { @name => @contents } - end - end - + # Generator class for creating templates. + # Generally this shouldn't be used directly, but instead should be used + # through Seek::SampleTemplates.generate(..) class Generator - JAR_VERSION = '0.2'.freeze - JAR_PATH = File.dirname(__FILE__) + "/../../../jars/sample-template-generator-#{JAR_VERSION}.jar" + JAR_VERSION = '0.3'.freeze + JAR_PATH = File.dirname(__FILE__) + + "/../../../jars/sample-template-generator-#{JAR_VERSION}.jar" DEFAULT_MEMORY_ALLOCATION = '512M'.freeze BUFFER_SIZE = 250_000 # 1/4 a megabyte - attr_accessor :json, :path, :memory_allocation + attr_reader :json, :path, :memory_allocation def initialize(path, json, memory_allocation = DEFAULT_MEMORY_ALLOCATION) @path = path @json = json @memory_allocation = memory_allocation - raise Exception, 'Windows is not currently supported' if windows? end def generate - run_with_open4 + run_with_cocaine end private def command @@ -48,32 +31,14 @@ command += " -f '#{path}'" command += " -j '#{json}'" command end - def run_with_open4 - output = '' - err_message = '' - command = command() - status = Open4.popen4(command) do |_pid, _stdin, stdout, stderr| - until (line = stdout.gets(BUFFER_SIZE)).nil? - output << line - end - stdout.close - - until (line = stderr.gets(BUFFER_SIZE)).nil? - err_message << line - end - stderr.close - end - - raise err_message if status.to_i.nonzero? - + def run_with_cocaine + output = Cocaine::CommandLine.new(command).run output.strip - end - - def windows? - !(RUBY_PLATFORM =~ /mswin32/ || RUBY_PLATFORM =~ /mingw32/).nil? + rescue Cocaine::ExitStatusError => exception + raise exception.message end end end end