Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pepijnve committed Sep 26, 2016
1 parent 50cfa94 commit 30ab925
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 69 deletions.
3 changes: 2 additions & 1 deletion lib/asciidoctor-diagram/blockdiag/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def self.define_processors(name)
::Asciidoctor::Diagram.const_set("#{name}BlockMacroProcessor", block_macro)
end

include CliGenerator
include Which

def blockdiag(tool, parent, source, format)
Expand All @@ -105,7 +106,7 @@ def blockdiag(tool, parent, source, format)
# a '3' suffix.
alt_cmd_name = "#{tool.downcase}3"

CliGenerator.generate_stdin(which(parent, cmd_name, :alt_cmds => [alt_cmd_name]), format.to_s, source.to_s) do |tool_path, output_path|
generate_stdin(which(parent, cmd_name, :alt_cmds => [alt_cmd_name]), format.to_s, source.to_s) do |tool_path, output_path|
[tool_path, '-a', '-o', Platform.native_path(output_path), "-T#{format.to_s}", '-']
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/asciidoctor-diagram/erd/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Asciidoctor
module Diagram
# @private
module Erd
include CliGenerator
include Which

def self.included(mod)
Expand All @@ -21,11 +22,11 @@ def erd(parent, source, format)
erd_path = which(parent, 'erd')
dot_path = which(parent, 'dot', :alt_attrs => ['graphvizdot'])

dot_code = CliGenerator.generate_stdin(erd_path, format.to_s, source.to_s) do |tool_path, output_path|
dot_code = generate_stdin(erd_path, format.to_s, source.to_s) do |tool_path, output_path|
[tool_path, '-o', Platform.native_path(output_path), '-f', 'dot']
end

CliGenerator.generate_stdin(dot_path, format.to_s, dot_code) do |tool_path, output_path|
generate_stdin(dot_path, format.to_s, dot_code) do |tool_path, output_path|
[tool_path, "-o#{Platform.native_path(output_path)}", "-T#{format.to_s}"]
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/asciidoctor-diagram/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ def process(parent, reader_or_target, attributes)
raise e
else
text = "Failed to generate image: #{e.message}"
warn %(asciidoctor-diagram: ERROR: #{text})
warn_msg = text.dup
if $VERBOSE
warn_msg << "\n" << e.backtrace.join("\n")
end
warn %(asciidoctor-diagram: ERROR: #{warn_msg})
text << "\n"
text << source.code
Asciidoctor::Block.new parent, :listing, :source => text, :attributes => attributes
Expand Down
3 changes: 2 additions & 1 deletion lib/asciidoctor-diagram/graphviz/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Asciidoctor
module Diagram
# @private
module Graphviz
include CliGenerator
include Which

def self.included(mod)
Expand All @@ -18,7 +19,7 @@ def self.included(mod)
end

def graphviz(parent, source, format)
CliGenerator.generate_stdin(which(parent, 'dot', :alt_attrs => ['graphvizdot']), format.to_s, source.to_s) do |tool_path, output_path|
generate_stdin(which(parent, 'dot', :alt_attrs => ['graphvizdot']), format.to_s, source.to_s) do |tool_path, output_path|
args = [tool_path, "-o#{Platform.native_path(output_path)}", "-T#{format.to_s}"]

layout = source.attr('layout')
Expand Down
8 changes: 4 additions & 4 deletions lib/asciidoctor-diagram/meme/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ def meme(p, c, format)
options = c.attr('options', '').split(',')
noupcase = options.include?('noupcase')

dimensions = CliGenerator.run_cli(identify, '-format', '%w %h', bg_img).match(/(?<w>\d+) (?<h>\d+)/)
dimensions = Cli.run(identify, '-format', '%w %h', bg_img).match(/(?<w>\d+) (?<h>\d+)/)
bg_width = dimensions['w'].to_i
bg_height = dimensions['h'].to_i
label_width = bg_width
label_height = bg_height / 5

if top_label
top_img = Tempfile.new(['meme', '.png'])
CliGenerator.run_cli(
Cli.run(
convert,
'-background', 'none',
'-fill', fill_color,
Expand All @@ -62,7 +62,7 @@ def meme(p, c, format)

if bottom_label
bottom_img = Tempfile.new(['meme', '.png'])
CliGenerator.run_cli(
Cli.run(
convert,
'-background', 'none',
'-fill', fill_color,
Expand Down Expand Up @@ -91,7 +91,7 @@ def meme(p, c, format)

args << final_img.path

CliGenerator.run_cli(*args)
Cli.run(*args)

File.binread(final_img)
end
Expand Down
6 changes: 4 additions & 2 deletions lib/asciidoctor-diagram/mermaid/extension.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative '../extensions'
require_relative '../util/cli'
require_relative '../util/cli_generator'
require_relative '../util/platform'
require_relative '../util/which'
Expand All @@ -7,6 +8,7 @@ module Asciidoctor
module Diagram
# @private
module Mermaid
include CliGenerator
include Which

def self.included(mod)
Expand All @@ -19,7 +21,7 @@ def self.included(mod)

def mermaid(parent, source, format)
mermaid = which(parent, 'mermaid')
@is_mermaid_v6 ||= `#{mermaid} --version`.split('.')[0].to_i >= 6
@is_mermaid_v6 ||= ::Asciidoctor::Diagram::Cli.run(mermaid, '--version').split('.')[0].to_i >= 6
# Mermaid >= 6.0.0 requires PhantomJS 2.1; older version required 1.9
phantomjs = which(parent, 'phantomjs', :alt_attrs => [@is_mermaid_v6 ? 'phantomjs_2' : 'phantomjs_19'])

Expand All @@ -30,7 +32,7 @@ def mermaid(parent, source, format)

width = source.attr('width')

CliGenerator.generate_file(mermaid, 'mmd', format.to_s, source.to_s) do |tool_path, input_path, output_path|
generate_file(mermaid, 'mmd', format.to_s, source.to_s) do |tool_path, input_path, output_path|
output_dir = File.dirname(output_path)
output_file = File.expand_path(File.basename(input_path) + ".#{format.to_s}", output_dir)

Expand Down
3 changes: 2 additions & 1 deletion lib/asciidoctor-diagram/shaape/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Asciidoctor
module Diagram
# @private
module Shaape
include CliGenerator
include Which

def self.included(mod)
Expand All @@ -18,7 +19,7 @@ def self.included(mod)
end

def shaape(parent, source, format)
CliGenerator.generate_stdin(which(parent, 'shaape'), format.to_s, source.to_s) do |tool_path, output_path|
generate_stdin(which(parent, 'shaape'), format.to_s, source.to_s) do |tool_path, output_path|
[tool_path, '-o', Platform.native_path(output_path), '-t', format.to_s, '-']
end
end
Expand Down
19 changes: 19 additions & 0 deletions lib/asciidoctor-diagram/util/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'tempfile'
require 'open3'

module Asciidoctor
module Diagram
# @private
module Cli
def self.run(*args)
stdout, stderr, status = Open3.capture3(*args)

if status != 0
raise "#{File.basename(args[0])} failed: #{stdout.empty? ? stderr : stdout}"
end

stdout.empty? ? stderr : stdout
end
end
end
end
20 changes: 5 additions & 15 deletions lib/asciidoctor-diagram/util/cli_generator.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require 'tempfile'
require 'open3'
require_relative 'cli'

module Asciidoctor
module Diagram
# @private
module CliGenerator
def self.generate_stdin(tool, format, code)
def generate_stdin(tool, format, code)
tool_name = File.basename(tool)

target_file = Tempfile.new([tool_name, ".#{format}"])
Expand All @@ -20,7 +20,7 @@ def self.generate_stdin(tool, format, code)
end
end

def self.generate_file(tool, input_ext, output_ext, code)
def generate_file(tool, input_ext, output_ext, code)
tool_name = File.basename(tool)

source_file = Tempfile.new([tool_name, ".#{input_ext}"])
Expand All @@ -42,7 +42,7 @@ def self.generate_file(tool, input_ext, output_ext, code)
end
end

def self.generate(opts, target_file, open3_opts = {})
def generate(opts, target_file, open3_opts = {})
case opts
when Array
args = opts
Expand All @@ -54,7 +54,7 @@ def self.generate(opts, target_file, open3_opts = {})
raise "Block passed to generate_file should return an Array or a Hash"
end

output = run_cli(*args, open3_opts)
output = ::Asciidoctor::Diagram::Cli.run(*args, open3_opts)

raise "#{args[0]} failed: #{output}" unless File.exist?(out_file || target_file.path)

Expand All @@ -64,16 +64,6 @@ def self.generate(opts, target_file, open3_opts = {})

File.binread(target_file.path)
end

def self.run_cli(*args)
stdout, stderr, status = Open3.capture3(*args)

if status != 0
raise "#{File.basename(args[0])} failed: #{stdout.empty? ? stderr : stdout}"
end

stdout.empty? ? stderr : stdout
end
end
end
end
2 changes: 1 addition & 1 deletion lib/asciidoctor-diagram/util/java.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def self.create_error(prefix_msg, response)
if content_type.start_with? 'application/json'
json = JSON.parse(response[:body].force_encoding(Encoding::UTF_8))
ruby_bt = Kernel.caller(2)
java_bt = json['stk'].map { |java_line| "#{java_line[0]}:#{java_line[3]}: in `#{java_line[2]}'" }
java_bt = json['stk'].map { |java_line| "#{java_line[0]}:#{java_line[3]}: in '#{java_line[2]}'" }
error = RuntimeError.new("#{prefix_msg}: #{json['msg']}")
error.set_backtrace java_bt + ruby_bt
raise error
Expand Down
120 changes: 94 additions & 26 deletions lib/asciidoctor-diagram/util/java_socket.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'socket'
require 'rbconfig'

require_relative 'cli'
require_relative 'platform'
require_relative 'which'

module Asciidoctor
Expand All @@ -15,10 +16,10 @@ def initialize(java, classpath)
args << '-Djava.awt.headless=true'
args << '-cp'
# special case for cygwin, it requires path translation for java to work
if RbConfig::CONFIG['host_os'] =~ /cygwin/i
if ::Asciidoctor::Diagram::Platform.os_variant == :cygwin
cygpath = ::Asciidoctor::Diagram::Which.which('cygpath')
if(cygpath != nil)
args << classpath.flatten.map { |jar| `cygpath -w "#{jar}"`.strip }.join(";")
if cygpath != nil
args << classpath.flatten.map { |jar| ::Asciidoctor::Diagram::Cli.run(cygpath, '-w', jar).strip }.join(";")
else
puts 'cygwin warning: cygpath not found'
args << classpath.flatten.join(File::PATH_SEPARATOR)
Expand Down Expand Up @@ -77,15 +78,13 @@ def self.send_request(req)

private
def self.find_java
if /cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM
# Windows
path_to(ENV['JAVA_HOME'], 'bin/java.exe') || registry_lookup || ::Asciidoctor::Diagram::Which.which('java')
elsif /darwin/ =~ RUBY_PLATFORM
# Mac
path_to(ENV['JAVA_HOME'], 'bin/java') || path_to(`/usr/libexec/java_home`.strip, 'bin/java') || ::Asciidoctor::Diagram::Which.which('java')
else
# Other unix-like system
path_to(ENV['JAVA_HOME'], 'bin/java') || ::Asciidoctor::Diagram::Which.which('java')
case ::Asciidoctor::Diagram::Platform.os
when :windows
path_to(ENV['JAVA_HOME'], 'bin/java.exe') || registry_lookup || ::Asciidoctor::Diagram::Which.which('java')
when :macosx
path_to(ENV['JAVA_HOME'], 'bin/java') || path_to(::Asciidoctor::Diagram::Cli.run('/usr/libexec/java_home').strip, 'bin/java') || ::Asciidoctor::Diagram::Which.which('java')
else
path_to(ENV['JAVA_HOME'], 'bin/java') || ::Asciidoctor::Diagram::Which.which('java')
end
end

Expand All @@ -98,21 +97,90 @@ def self.path_to(java_home, java_binary)
end
end

JDK_KEY = 'HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit'
JRE_KEY = 'HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment'

def self.registry_lookup
key_re = /^HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\.*\\([0-9\.]+)/
value_re = /\s*JavaHome\s*REG_SZ\s*(.*)/
result = `reg query "HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft" /s /v JavaHome`.lines.map { |l| l.strip }
vms = result.each_slice(3).map do |_, key, value|
key_match = key_re.match(key)
value_match = value_re.match(value)
if key_match && value_match
[key_match[1].split('.').map { |v| v.to_i }, value_match[1]]
else
nil
registry_current(JRE_KEY) || registry_current(JDK_KEY) || registry_any()
end

def self.registry_current(key)
current_version = registry_query(key, 'CurrentVersion')
if current_version
java_home = registry_query("#{key}\\#{current_version}", 'JavaHome')
java_exe(java_home)
else
nil
end
end

def self.registry_any()
java_homes = registry_query('HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft', 'JavaHome', :recursive => true).values
java_homes.map { |path| java_exe(path) }.find { |exe| !exe.nil? }
end

def self.java_exe(java_home)
java = File.expand_path('bin/java.exe', java_home)

if File.executable?(java)
java
else
nil
end
end

def self.registry_query(key, value = nil, opts = {})
args = ['reg', 'query']
args << key
args << '/v' << value unless value.nil?
args << '/s' if opts[:recursive]

begin
lines = ::Asciidoctor::Diagram::Cli.run(*args).lines.reject { |l| l.strip.empty? }.each
rescue
lines = [].each
end

result = {}

while true
begin
begin
k = lines.next
rescue StopIteration
break
end

unless k.start_with? key
next
end

v = nil
begin
v = lines.next.strip if lines.peek.start_with?(' ')
rescue StopIteration
break
end

if !k.valid_encoding? || (v && !v.valid_encoding?)
next
end

if v && (md = /([^\s]+)\s+(REG_[^\s]+)\s+(.+)/.match(v))
v_name = md[1]
v_value = md[3]
result["#{k}\\#{v_name}"] = v_value
else
result[k] = v
end
end
end.reject { |v| v.nil? }.sort_by { |v| v[0] }
java_exes = vms.map { |version, path| File.expand_path('bin/java.exe', path) }.select { |exe| File.executable?(exe) }
java_exes && java_exes[0]
end

if value && !opts[:recursive]
result.values[0]
else
result
end
end
end
end
Expand Down
Loading

0 comments on commit 30ab925

Please sign in to comment.