-
Notifications
You must be signed in to change notification settings - Fork 6
/
GVUtils.rb
executable file
·92 lines (86 loc) · 2.79 KB
/
GVUtils.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# GVUtils.rb
# Leonhard
#
# Created by greg on 28/07/10.
# Copyright 2010, 2011 Gregoire Lejeune. All rights reserved.
module GVUtils
# Since this code is an adaptation of Launchy::Application#find_executable
# (http://copiousfreetime.rubyforge.org/launchy/Launchy/Application.html)
# it follow is licence :
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED AS IS AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
# OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
def find_executable(bin, paths) #:nodoc:
paths = ENV['PATH'].split(File::PATH_SEPARATOR) if paths.nil? or paths.empty?
paths.each do |path|
file = (path.nil?)?bin:File.join(path,bin)
if File.executable?(file) and not File.directory?(file) then
return file
elsif RUBY_PLATFORM =~ /mswin|mingw/
found_ext = (ENV['PATHEXT'] || '.exe;.bat;.com').split(";").find {|ext| File.executable?(file + ext) }
return file + found_ext if found_ext
end
end
return nil
end
def escape_path_containing_blanks(path) #:nodoc:
path.gsub!(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
path_elements = path.split(File::SEPARATOR)
path_elements.map! do |element|
if element.include?(' ')
"\"#{element}\""
else
element
end
end
path_elements.join(File::SEPARATOR)
end
def output_and_errors_from_command(cmd) #:nodoc:
unless defined? Open3
begin
require 'open3'
require 'win32/open3'
rescue LoadError
end
end
begin
begin
Open3.popen3( cmd ) do |stdin, stdout, stderr|
stdin.close
stdout.binmode
[stdout.read, stderr.read]
end
rescue Errno::ENOENT => e
["", "GraphViz path invalid! Please change it in preferences."]
rescue => e
["", e.message]
end
rescue NotImplementedError, NoMethodError
begin
IO.popen( cmd ) do |stdout|
stdout.binmode
[stdout.read, nil]
end
rescue => e
["", e.message]
end
end
end
def output_from_command(cmd) #:nodoc:
output, errors = output_and_errors_from_command(cmd)
if errors.nil? || errors.strip.empty?
output
else
raise "Error from #{cmd}:\n#{errors}"
end
end
end