getversion 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,5 @@
1
+ require_relative 'executable_path_finder'
1
2
  require_relative 'execution_context'
2
- require_relative 'path_finder'
3
- require_relative 'path'
4
3
 
5
4
  module GetVersion
6
5
  class Executable
@@ -13,20 +12,20 @@ module GetVersion
13
12
  @block = block
14
13
  end
15
14
 
16
- def version
17
- c = ExecutionContext.new path
18
- c.execute &@block if @block
19
- end
20
-
21
- def path
22
- dir = PathFinder.find_path @name
23
- dir = nil if dir == '.'
24
- dir ? Path.new(dir, name).evaluated_path : @name
15
+ def version(dir=nil)
16
+ ExecutionContext.execute path(dir), &@block
25
17
  end
26
18
 
27
19
  def self.version(name, &block)
28
20
  Executable.new(name, nil, &block).version
29
21
  end
30
22
 
23
+ def path(dir=nil)
24
+ dir ||= ExecutablePathFinder.find_dir @name
25
+ dir = nil if dir == '.'
26
+ return @name unless dir
27
+ File.join dir, @name
28
+ end
29
+
31
30
  end
32
31
  end
@@ -0,0 +1,43 @@
1
+ module GetVersion
2
+ class ExecutablePathFinder
3
+
4
+ def initialize(name)
5
+ @name = name
6
+ end
7
+
8
+ def find_dir
9
+ path = find_path
10
+ File.dirname path if path
11
+ end
12
+
13
+ def find_path
14
+ is_windows? ? find_path_on_windows : find_path_on_unix
15
+ end
16
+
17
+ def self.find_dir(name)
18
+ ExecutablePathFinder.new(name).find_dir
19
+ end
20
+
21
+ private
22
+
23
+ def is_windows?
24
+ !ENV['WINDIR'].nil?
25
+ end
26
+
27
+ def find_path_on_unix
28
+ path = `which #{@name}`.strip
29
+ return if path.empty?
30
+ real_path = `readlink #{path}`.strip
31
+ real_path = nil if real_path.empty?
32
+ real_path || path
33
+ end
34
+
35
+ def find_path_on_windows
36
+ paths = ENV['PATH'].split ';'
37
+ extensions = ENV['PATHEXT'].split ';'
38
+ possibilities = paths.map { |path| extensions.map { |ext| File.join(path, @name + ext.downcase).gsub('/', '\\') } }.flatten
39
+ possibilities.find { |f| File.exist? f }
40
+ end
41
+
42
+ end
43
+ end
@@ -7,36 +7,16 @@ module GetVersion
7
7
  @executables = []
8
8
  end
9
9
 
10
- def get_version(name)
11
- executable = find_executable(name) || null_executable(name)
12
- executable.version
13
- end
14
-
15
- def guess_version(name)
16
- executable = find_executable(name) || executable_for_guessing(name)
17
- executable.version
18
- end
19
-
20
10
  def add_executable(name, namespace, &block)
21
11
  @executables << Executable.new(name, namespace, &block)
22
12
  end
23
13
 
24
- private
25
-
26
- def find_executable(name)
14
+ def get_executable(name)
27
15
  @executables.find { |e| e.name == name }
28
16
  end
29
17
 
30
- def null_executable(name)
31
- Executable.new name, nil
32
- end
18
+ def get_version(path)
33
19
 
34
- def executable_for_guessing(name)
35
- Executable.new name, nil do
36
- args = %w(--version -version /version version --help -help /help help) << nil
37
- versions = args.map { |arg| find_version_in_output arg }.compact.uniq
38
- versions[0] if versions.length == 1
39
- end
40
20
  end
41
21
 
42
22
  end
@@ -1,10 +1,10 @@
1
- require 'yaml'
2
- require_relative 'executable'
3
- require_relative 'version'
1
+ require_relative 'platform_versions'
4
2
 
5
3
  module GetVersion
6
4
  class ExecutionContext
7
5
 
6
+ include PlatformVersions
7
+
8
8
  def initialize(path)
9
9
  @path = path
10
10
  end
@@ -13,24 +13,12 @@ module GetVersion
13
13
  instance_eval &block
14
14
  end
15
15
 
16
- def find_version_in_output(arg=nil)
17
- output = `"#{@path}" #{arg} 2>&1`
18
- v = Version.match output
19
- v ? v.version : nil
20
- end
21
-
22
- def google_appengine_version
23
- path = File.dirname @path
24
- version_file = File.join path, 'VERSION'
25
- YAML::load_file(version_file)['release']
16
+ def self.execute(path, &block)
17
+ ExecutionContext.new(path).execute(&block)
26
18
  end
27
19
 
28
- def osx_version
29
- Executable.version('sw_vers') { find_version_in_output '-productVersion' }
30
- end
31
-
32
- def windows_version
33
- Executable.version('ver') { find_version_in_output }
20
+ def find_version_in_output(arg=nil)
21
+ `"#{@path}" #{arg} 2>&1`[/(\d+\.)+\d+/]
34
22
  end
35
23
 
36
24
  end
@@ -0,0 +1,22 @@
1
+ require 'yaml'
2
+ require_relative 'executable'
3
+
4
+ module GetVersion
5
+ module PlatformVersions
6
+
7
+ def google_appengine_version
8
+ dir = File.dirname @path
9
+ version_file = File.join dir, 'VERSION'
10
+ YAML::load_file(version_file)['release']
11
+ end
12
+
13
+ def osx_version
14
+ Executable.version('sw_vers') { find_version_in_output '-productVersion' }
15
+ end
16
+
17
+ def windows_version
18
+ Executable.version('ver') { find_version_in_output }
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,53 @@
1
+ require_relative 'executable'
2
+
3
+ module GetVersion
4
+ class VersionFinder
5
+
6
+ def initialize(executable_path, executable_store)
7
+ @path = executable_path
8
+ @executable_store = executable_store
9
+ end
10
+
11
+ def get_version
12
+ get null_executable
13
+ end
14
+
15
+ def guess_version
16
+ get executable_for_guessing
17
+ end
18
+
19
+ private
20
+
21
+ def get(executable_if_nil)
22
+ executable = @executable_store.get_executable(name) || executable_if_nil
23
+ executable.version dir
24
+ end
25
+
26
+ def path
27
+ @path
28
+ end
29
+
30
+ def name
31
+ File.basename path
32
+ end
33
+
34
+ def dir
35
+ dir = File.dirname path
36
+ dir = nil if dir == '.'
37
+ dir
38
+ end
39
+
40
+ def null_executable
41
+ Executable.new name, nil
42
+ end
43
+
44
+ def executable_for_guessing
45
+ Executable.new name, nil do
46
+ args = %w(--version -version /version version --help -help /help help) << nil
47
+ versions = args.map { |arg| find_version_in_output arg }.compact.uniq
48
+ versions[0] if versions.length == 1
49
+ end
50
+ end
51
+
52
+ end
53
+ end
data/lib/getversion.rb CHANGED
@@ -1,19 +1,24 @@
1
1
  require_relative 'getversion/executable_store'
2
+ require_relative 'getversion/version_finder'
2
3
 
3
4
  module GetVersion
4
5
 
5
6
  @executable_store = ExecutableStore.new
6
7
 
7
- def self.[](name)
8
- @executable_store.get_version name
8
+ def self.[](path)
9
+ version_finder(path).get_version
9
10
  end
10
11
 
11
- def self.guess(name)
12
- @executable_store.guess_version name
12
+ def self.guess(path)
13
+ version_finder(path).guess_version
13
14
  end
14
15
 
15
16
  private
16
17
 
18
+ def self.version_finder(path)
19
+ VersionFinder.new(path, @executable_store)
20
+ end
21
+
17
22
  def self.namespace(namespace)
18
23
  @namespace = namespace
19
24
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: getversion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-21 00:00:00.000000000 Z
12
+ date: 2013-07-22 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Provides a standard interface for obtaining CLI application version numbers.
15
15
  email: matthew@matthewriley.name
@@ -26,13 +26,11 @@ files:
26
26
  - lib/getversion/detectors/uncategorised.rb
27
27
  - lib/getversion/detectors.rb
28
28
  - lib/getversion/executable.rb
29
+ - lib/getversion/executable_path_finder.rb
29
30
  - lib/getversion/executable_store.rb
30
31
  - lib/getversion/execution_context.rb
31
- - lib/getversion/path.rb
32
- - lib/getversion/path_finder.rb
33
- - lib/getversion/path_finders/unix_path_finder.rb
34
- - lib/getversion/path_finders/windows_path_finder.rb
35
- - lib/getversion/version.rb
32
+ - lib/getversion/platform_versions.rb
33
+ - lib/getversion/version_finder.rb
36
34
  - lib/getversion.rb
37
35
  - !binary |-
38
36
  YmluL2dldHZlcnNpb24ucmI=
@@ -1,28 +0,0 @@
1
- class Path
2
-
3
- def initialize(*path)
4
- path.select! { |p| p unless p.empty? }
5
- @path = File.join path
6
- end
7
-
8
- def evaluated_path
9
- return unless @path
10
- `echo #{normalised_path}`.strip
11
- end
12
-
13
- def normalised_path
14
- return unless @path
15
- is_windows ? @path.gsub('/', '\\') : @path.gsub('\\', '/')
16
- end
17
-
18
- def to_s
19
- evaluated_path
20
- end
21
-
22
- private
23
-
24
- def is_windows
25
- !ENV['WINDIR'].nil?
26
- end
27
-
28
- end
@@ -1,17 +0,0 @@
1
- require_relative 'path_finders/unix_path_finder'
2
- require_relative 'path_finders/windows_path_finder'
3
-
4
- class PathFinder
5
-
6
- def self.find_path(executable_name)
7
- path_finder = is_windows? ? WindowsPathFinder.new : UnixPathFinder.new
8
- path_finder.find_path executable_name
9
- end
10
-
11
- private
12
-
13
- def self.is_windows?
14
- !ENV['WINDIR'].nil?
15
- end
16
-
17
- end
@@ -1,10 +0,0 @@
1
- class UnixPathFinder
2
-
3
- def find_path(executable_name)
4
- path = `which #{executable_name}`
5
- real_path = `readlink #{path}`.strip
6
- result = real_path.empty? ? path : real_path
7
- result.empty? ? nil : File.dirname(result)
8
- end
9
-
10
- end
@@ -1,11 +0,0 @@
1
- class WindowsPathFinder
2
-
3
- def find_path(executable_name)
4
- paths = ENV['PATH'].split ';'
5
- extensions = ENV['PATHEXT'].split ';'
6
- possibilities = paths.map { |path| extensions.map { |ext| File.join(path, executable_name + ext.downcase).gsub('/', '\\') } }.flatten
7
- executable = possibilities.select { |exe| File.exist? exe }.first
8
- executable ? File.dirname(executable) : nil
9
- end
10
-
11
- end
@@ -1,43 +0,0 @@
1
- class Version
2
-
3
- VERSION_REGEX = '(?:\d+\.)+(?:\d+)'
4
- DELIMITER = '.'
5
-
6
- attr_accessor :version
7
-
8
- def initialize(version)
9
- raise "#{version} is not a valid version." unless Version.is_valid? version
10
- @version = version
11
- end
12
-
13
- def compact
14
- first 2, ''
15
- end
16
-
17
- def first(count, delimiter=DELIMITER)
18
- to_a.first(count).join(delimiter)
19
- end
20
-
21
- def to_s
22
- @version
23
- end
24
-
25
- def to_a
26
- @version.split DELIMITER
27
- end
28
-
29
- def self.match(value)
30
- exp = Regexp.new VERSION_REGEX
31
- version = value.scan(exp)[0]
32
- return unless version
33
- Version.new(version)
34
- end
35
-
36
- private
37
-
38
- def self.is_valid?(version)
39
- exp = Regexp.new "^#{VERSION_REGEX}$"
40
- version.scan(exp)[0] != nil
41
- end
42
-
43
- end