cmd_executable 1.0.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +17 -6
- data/cmd_executable.gemspec +1 -0
- data/lib/cmd_executable.rb +7 -3
- data/lib/cmd_executable/parser.rb +24 -4
- data/lib/cmd_executable/runner.rb +8 -1
- data/lib/cmd_executable/version.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 66d9c1f30fd634e52392e967c92564f48b5f3d5dad32b48525328772615df8e8
|
4
|
+
data.tar.gz: 162f824d3f8c8007416451e56bc21a4baf49b6d7f4ec6c8ac7bd122e2aa9b3f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f3fd927f9e7f20bd2bade3e088dd237a94c15cea0ca54cdb63d95a42fc32098b9d73bcf60970b3206b5949f967ea73ccccbd3de310997968e3699f2789935d1
|
7
|
+
data.tar.gz: c123587be2664391d5e6598ba470f6669abac78fc87cceef24b8d124258b7314528adba13465ed53b76528d4f30160a494af44b2d3dcdabe9b17bd1353f20999
|
data/README.md
CHANGED
@@ -21,18 +21,29 @@ Or install it yourself as:
|
|
21
21
|
`$ gem install cmd_executable`
|
22
22
|
|
23
23
|
## Usage
|
24
|
-
### as Module
|
24
|
+
### as Module (include)
|
25
25
|
```
|
26
26
|
require 'cmd_executable'
|
27
27
|
|
28
28
|
class Klass
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
29
|
+
include CmdExecutable
|
30
|
+
|
31
|
+
def instance_method
|
32
|
+
executable?('/bin/ls')
|
33
|
+
executable?('ls')
|
34
|
+
executable?(:ls)
|
35
|
+
executable?('./bin/setup')
|
36
|
+
end
|
34
37
|
end
|
35
38
|
```
|
39
|
+
### as Module method
|
40
|
+
```
|
41
|
+
CmdExecutable.executable?('/bin/ls')
|
42
|
+
CmdExecutable.executable?('ls')
|
43
|
+
CmdExecutable.executable?(:ls)
|
44
|
+
CmdExecutable.executable?('./bin/setup')
|
45
|
+
```
|
46
|
+
|
36
47
|
### as CLI
|
37
48
|
Check executable? :
|
38
49
|
|
data/cmd_executable.gemspec
CHANGED
@@ -37,6 +37,7 @@ Gem::Specification.new do |spec|
|
|
37
37
|
spec.add_development_dependency 'rake', '~> 13.0'
|
38
38
|
spec.add_development_dependency 'rspec', '~> 3.9'
|
39
39
|
spec.add_development_dependency 'rubocop', '~> 0.85.0'
|
40
|
+
spec.add_development_dependency 'stream_capture', '~> 1.0'
|
40
41
|
|
41
42
|
spec.add_dependency 'thor', '~> 1.0'
|
42
43
|
end
|
data/lib/cmd_executable.rb
CHANGED
@@ -33,7 +33,7 @@ require 'cmd_executable/version'
|
|
33
33
|
# require 'cmd_executable'
|
34
34
|
#
|
35
35
|
# class Klass
|
36
|
-
# include
|
36
|
+
# include CmdExecutable
|
37
37
|
#
|
38
38
|
# def instance_method
|
39
39
|
# executable?('ls')
|
@@ -43,10 +43,14 @@ module CmdExecutable
|
|
43
43
|
class CmdExecutableError < StandardError; end
|
44
44
|
|
45
45
|
def executable?(command)
|
46
|
+
CmdExecutable.executable?(command)
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.executable?(command)
|
46
50
|
parsed = CmdExecutable::Parser.new(command)
|
47
|
-
raise
|
51
|
+
raise CmdExecutable::ParserError, parsed.raw unless parsed.validate?
|
48
52
|
|
49
|
-
`type
|
53
|
+
`type "#{parsed.command}" > /dev/null 2>&1`.yield_self do
|
50
54
|
$CHILD_STATUS.success?
|
51
55
|
end
|
52
56
|
end
|
@@ -29,6 +29,8 @@ module CmdExecutable
|
|
29
29
|
|
30
30
|
# Parser for CmdExecutable
|
31
31
|
class Parser
|
32
|
+
attr_reader :raw
|
33
|
+
|
32
34
|
def initialize(raw)
|
33
35
|
@raw = raw
|
34
36
|
@raw.freeze
|
@@ -37,7 +39,8 @@ module CmdExecutable
|
|
37
39
|
def validate?
|
38
40
|
!@raw.nil? &&
|
39
41
|
command_class_validate? &&
|
40
|
-
!@raw.empty?
|
42
|
+
!@raw.empty? &&
|
43
|
+
!include_invalid_char?
|
41
44
|
end
|
42
45
|
|
43
46
|
def command
|
@@ -52,10 +55,15 @@ module CmdExecutable
|
|
52
55
|
@raw.is_a?(Symbol)
|
53
56
|
end
|
54
57
|
|
58
|
+
def include_invalid_char?
|
59
|
+
@raw.match?(/\r\n|\r|\n/) ||
|
60
|
+
@raw.match?(/\$\(.*\)/)
|
61
|
+
end
|
62
|
+
|
55
63
|
def parse
|
56
|
-
raise CmdExecutable::ParserError unless validate?
|
64
|
+
raise CmdExecutable::ParserError, @raw unless validate?
|
57
65
|
|
58
|
-
path = @raw.to_s.chomp
|
66
|
+
path = escape_char(@raw.to_s.chomp)
|
59
67
|
@dirname = parse_dirname(path)
|
60
68
|
@basename = parse_basename(path)
|
61
69
|
@command = @dirname + @basename
|
@@ -70,6 +78,10 @@ module CmdExecutable
|
|
70
78
|
/\A\.\Z/
|
71
79
|
end
|
72
80
|
|
81
|
+
def current_path_at_the_left_regex
|
82
|
+
/\A\.#{File::SEPARATOR}/
|
83
|
+
end
|
84
|
+
|
73
85
|
def basename_exist?(path)
|
74
86
|
path.match?(no_separator_at_the_right_end_regex)
|
75
87
|
end
|
@@ -78,11 +90,15 @@ module CmdExecutable
|
|
78
90
|
dir.match?(no_separator_at_the_right_end_regex)
|
79
91
|
end
|
80
92
|
|
93
|
+
def current_path?(path)
|
94
|
+
path.match?(current_path_at_the_left_regex)
|
95
|
+
end
|
96
|
+
|
81
97
|
def parse_dirname(path)
|
82
98
|
return path unless basename_exist?(path)
|
83
99
|
|
84
100
|
dir = File.dirname(path)
|
85
|
-
return '' if dir.match?(a_dot_only_regex)
|
101
|
+
return '' if dir.match?(a_dot_only_regex) && !current_path?(path)
|
86
102
|
|
87
103
|
no_right_separator_exists?(dir) ? (dir + File::SEPARATOR) : dir
|
88
104
|
end
|
@@ -92,5 +108,9 @@ module CmdExecutable
|
|
92
108
|
|
93
109
|
File.basename(path).split.first
|
94
110
|
end
|
111
|
+
|
112
|
+
def escape_char(path)
|
113
|
+
path.gsub(/"/, '\"')
|
114
|
+
end
|
95
115
|
end
|
96
116
|
end
|
@@ -34,6 +34,10 @@ module CmdExecutable
|
|
34
34
|
class Runner < Thor
|
35
35
|
include CmdExecutable
|
36
36
|
|
37
|
+
def self.exit_on_failure?
|
38
|
+
true
|
39
|
+
end
|
40
|
+
|
37
41
|
map '-c' => :check
|
38
42
|
|
39
43
|
desc '-c [/path/to/command]', "It's return true if given command usable on Linux."
|
@@ -45,12 +49,15 @@ module CmdExecutable
|
|
45
49
|
puts 'NOT FOUND'
|
46
50
|
exit 1
|
47
51
|
end
|
52
|
+
rescue CmdExecutable::ParserError => e
|
53
|
+
warn "Invalid command: `#{e.message}'"
|
54
|
+
exit 16
|
48
55
|
end
|
49
56
|
|
50
57
|
map %w[-v --version] => :version
|
51
58
|
desc '-v --version', 'Show version.'
|
52
59
|
def version
|
53
|
-
|
60
|
+
puts CmdExecutable::VERSION
|
54
61
|
end
|
55
62
|
end
|
56
63
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cmd_executable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Toshiki
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-06-
|
11
|
+
date: 2020-06-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -94,6 +94,20 @@ dependencies:
|
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: 0.85.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: stream_capture
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.0'
|
97
111
|
- !ruby/object:Gem::Dependency
|
98
112
|
name: thor
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|