cmd_executable 0.1.9 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2afa4096ae4f4725dfc86a931af507f5e372535e8b48ccd62e8f7ffbd6576b8d
4
- data.tar.gz: 48757e7931d24467727cddbfb05c6af9f4a9fa32d33de70f0cb439e05975814a
3
+ metadata.gz: 2be28fc8b7b25d870c1ecd8043f8e170e7778671ab748db457a5e11d416711b7
4
+ data.tar.gz: 8e5fd645751d0beec7b4b29873de947252c87ae20ceef72704af7892f20b8dc6
5
5
  SHA512:
6
- metadata.gz: 01ae2b36a2381bc78638d67336bbb953bd7acc924d267718865bf1f4bdcc7ae12f1aa2a6e4819e514deea2bb4f39fd472293949116a2a22b9c07a42b2bbf09c5
7
- data.tar.gz: 3ccc53e35f9080d02360724de21f0f86131361ab2e135cc0d833c2818003788bb7595b72132aaabf3aae452530c358372e9ff6e2849702751a3f6d0f84755f55
6
+ metadata.gz: 0745f442bea47b45b4b3024599be749d1cdc21e7ec52d8ab3de6cba1d679ea2d1e5ded411cc194896165fcad528d0d92d6305f318b2edb5f5ab501c0d83a814e
7
+ data.tar.gz: 2827fbf6270af7564eb8fec73ab55985c7a366f2521bc4b16f600e43f463dfce38f8fe439432369b96230812f1e96189a67d4c7c1f4ef9794d1a84756cbb77ca
data/README.md CHANGED
@@ -14,24 +14,48 @@ gem 'cmd_executable'
14
14
 
15
15
  And then execute:
16
16
 
17
- $ bundle install
17
+ `$ bundle install`
18
18
 
19
19
  Or install it yourself as:
20
20
 
21
- $ gem install cmd_executable
21
+ `$ gem install cmd_executable`
22
22
 
23
23
  ## Usage
24
+ ### as Module (include)
24
25
  ```
25
26
  require 'cmd_executable'
26
27
 
27
28
  class Klass
28
- include 'CmdExecutable'
29
-
30
- def instance_method
31
- executable?('ls')
32
- end
29
+ include CmdExecutable
30
+
31
+ def instance_method
32
+ executable?('/bin/ls')
33
+ executable?('ls')
34
+ executable?(:ls)
35
+ executable?('./bin/setup')
36
+ end
33
37
  end
34
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
+
47
+ ### as CLI
48
+ Check executable? :
49
+
50
+ `$ cmd_executable -c [/path/to/command]`
51
+ or
52
+ `$ cmd_executable -c [../path/to/command]`
53
+ or
54
+ `$ cmd_executable -c [command]`
55
+
56
+ Show help :
57
+
58
+ `$ cmd_executable -h`
35
59
 
36
60
  ## Development
37
61
 
@@ -14,9 +14,9 @@ Gem::Specification.new do |spec|
14
14
  it's return true if given command usable on Linux.
15
15
  DESC
16
16
 
17
- spec.homepage = 'https://github.com/toshiki670/cmd_executable/wiki'
17
+ spec.homepage = 'https://github.com/toshiki670/cmd_executable'
18
18
  spec.license = 'MIT'
19
- spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
19
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
20
20
 
21
21
  spec.metadata['homepage_uri'] = spec.homepage
22
22
  spec.metadata['source_code_uri'] = 'https://github.com/toshiki670/cmd_executable'
@@ -33,7 +33,11 @@ Gem::Specification.new do |spec|
33
33
 
34
34
  spec.add_development_dependency 'bundler', '~> 2.1'
35
35
  spec.add_development_dependency 'pry', '~> 0.13.1'
36
+ spec.add_development_dependency 'pry-doc', '~> 1.1'
36
37
  spec.add_development_dependency 'rake', '~> 13.0'
37
38
  spec.add_development_dependency 'rspec', '~> 3.9'
38
39
  spec.add_development_dependency 'rubocop', '~> 0.85.0'
40
+ spec.add_development_dependency 'stream_capture', '~> 1.0'
41
+
42
+ spec.add_dependency 'thor', '~> 1.0'
39
43
  end
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'cmd_executable'
4
+ require 'cmd_executable/runner'
5
+
6
+ CmdExecutable::Runner.start(ARGV)
@@ -23,6 +23,8 @@
23
23
  # THE SOFTWARE.
24
24
 
25
25
  require 'English'
26
+ require 'cmd_executable/parser'
27
+ require 'cmd_executable/runner'
26
28
  require 'cmd_executable/version'
27
29
 
28
30
  # Command Executable
@@ -31,7 +33,7 @@ require 'cmd_executable/version'
31
33
  # require 'cmd_executable'
32
34
  #
33
35
  # class Klass
34
- # include 'CmdExecutable'
36
+ # include CmdExecutable
35
37
  #
36
38
  # def instance_method
37
39
  # executable?('ls')
@@ -41,13 +43,14 @@ module CmdExecutable
41
43
  class CmdExecutableError < StandardError; end
42
44
 
43
45
  def executable?(command)
44
- path = File.split(command).yield_self do |dirname, basename|
45
- dirname = File.absolute_path?(dirname) ? dirname : ''
46
- dirname += File::SEPARATOR if dirname.rindex(File::SEPARATOR)
47
- dirname + basename.split.first
48
- end
46
+ CmdExecutable.executable?(command)
47
+ end
48
+
49
+ def self.executable?(command)
50
+ parsed = CmdExecutable::Parser.new(command)
51
+ raise ArgumentError unless parsed.validate?
49
52
 
50
- `type '#{path}' > /dev/null 2>&1`.yield_self do
53
+ `type "#{parsed.command}" > /dev/null 2>&1`.yield_self do
51
54
  $CHILD_STATUS.success?
52
55
  end
53
56
  end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The MIT License (MIT)
4
+ #
5
+ # Copyright (c) 2020 Toshiki
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in
15
+ # all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ # THE SOFTWARE.
24
+
25
+ require 'English'
26
+
27
+ module CmdExecutable
28
+ class ParserError < StandardError; end
29
+
30
+ # Parser for CmdExecutable
31
+ class Parser
32
+ def initialize(raw)
33
+ @raw = raw
34
+ @raw.freeze
35
+ end
36
+
37
+ def validate?
38
+ !@raw.nil? &&
39
+ command_class_validate? &&
40
+ !@raw.empty? &&
41
+ !include_invalid_char?
42
+ end
43
+
44
+ def command
45
+ parse if @command.nil?
46
+ @command
47
+ end
48
+
49
+ private
50
+
51
+ def command_class_validate?
52
+ @raw.is_a?(String) ||
53
+ @raw.is_a?(Symbol)
54
+ end
55
+
56
+ def include_invalid_char?
57
+ @raw.match?(/\r\n|\r|\n/) ||
58
+ @raw.match?(/\$\(.*\)/)
59
+ end
60
+
61
+ def parse
62
+ raise CmdExecutable::ParserError unless validate?
63
+
64
+ path = escape_char(@raw.to_s.chomp)
65
+ @dirname = parse_dirname(path)
66
+ @basename = parse_basename(path)
67
+ @command = @dirname + @basename
68
+ self
69
+ end
70
+
71
+ def no_separator_at_the_right_end_regex
72
+ /(?<!#{File::SEPARATOR})\Z/
73
+ end
74
+
75
+ def a_dot_only_regex
76
+ /\A\.\Z/
77
+ end
78
+
79
+ def current_path_at_the_left_regex
80
+ /\A\.#{File::SEPARATOR}/
81
+ end
82
+
83
+ def basename_exist?(path)
84
+ path.match?(no_separator_at_the_right_end_regex)
85
+ end
86
+
87
+ def no_right_separator_exists?(dir)
88
+ dir.match?(no_separator_at_the_right_end_regex)
89
+ end
90
+
91
+ def current_path?(path)
92
+ path.match?(current_path_at_the_left_regex)
93
+ end
94
+
95
+ def parse_dirname(path)
96
+ return path unless basename_exist?(path)
97
+
98
+ dir = File.dirname(path)
99
+ return '' if dir.match?(a_dot_only_regex) && !current_path?(path)
100
+
101
+ no_right_separator_exists?(dir) ? (dir + File::SEPARATOR) : dir
102
+ end
103
+
104
+ def parse_basename(path)
105
+ return '' unless basename_exist?(path)
106
+
107
+ File.basename(path).split.first
108
+ end
109
+
110
+ def escape_char(path)
111
+ path.gsub(/"/, '\"')
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The MIT License (MIT)
4
+ #
5
+ # Copyright (c) 2020 Toshiki
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in
15
+ # all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ # THE SOFTWARE.
24
+
25
+ require 'cmd_executable'
26
+ require 'thor'
27
+
28
+ module CmdExecutable
29
+ # CLI Runner
30
+ #
31
+ # Usage on CLI:
32
+ # $ cmd_executable ls
33
+ # > OK
34
+ class Runner < Thor
35
+ include CmdExecutable
36
+
37
+ map '-c' => :check
38
+
39
+ desc '-c [/path/to/command]', "It's return true if given command usable on Linux."
40
+ def check(command = '')
41
+ if executable?(command)
42
+ puts 'OK'
43
+ exit 0
44
+ else
45
+ puts 'NOT FOUND'
46
+ exit 1
47
+ end
48
+ end
49
+
50
+ map %w[-v --version] => :version
51
+ desc '-v --version', 'Show version.'
52
+ def version
53
+ puts CmdExecutable::VERSION
54
+ end
55
+ end
56
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CmdExecutable
4
- VERSION = '0.1.9'
4
+ VERSION = '1.2.0'
5
5
  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: 0.1.9
4
+ version: 1.2.0
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-04 00:00:00.000000000 Z
11
+ date: 2020-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.13.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-doc
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.1'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rake
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +94,34 @@ dependencies:
80
94
  - - "~>"
81
95
  - !ruby/object:Gem::Version
82
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'
111
+ - !ruby/object:Gem::Dependency
112
+ name: thor
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.0'
83
125
  description: |2
84
126
  This module adds a method "executable?(command)".
85
127
  it's return true if given command usable on Linux.
@@ -94,7 +136,6 @@ files:
94
136
  - ".github/workflows/ruby_check.yml"
95
137
  - ".gitignore"
96
138
  - ".rubocop.yml"
97
- - ".travis.yml"
98
139
  - CODE_OF_CONDUCT.md
99
140
  - Gemfile
100
141
  - LICENSE
@@ -105,12 +146,14 @@ files:
105
146
  - cmd_executable.gemspec
106
147
  - exe/cmd_executable
107
148
  - lib/cmd_executable.rb
149
+ - lib/cmd_executable/parser.rb
150
+ - lib/cmd_executable/runner.rb
108
151
  - lib/cmd_executable/version.rb
109
- homepage: https://github.com/toshiki670/cmd_executable/wiki
152
+ homepage: https://github.com/toshiki670/cmd_executable
110
153
  licenses:
111
154
  - MIT
112
155
  metadata:
113
- homepage_uri: https://github.com/toshiki670/cmd_executable/wiki
156
+ homepage_uri: https://github.com/toshiki670/cmd_executable
114
157
  source_code_uri: https://github.com/toshiki670/cmd_executable
115
158
  changelog_uri: https://github.com/toshiki670/cmd_executable/releases
116
159
  post_install_message:
@@ -121,7 +164,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
121
164
  requirements:
122
165
  - - ">="
123
166
  - !ruby/object:Gem::Version
124
- version: 2.3.0
167
+ version: 2.7.0
125
168
  required_rubygems_version: !ruby/object:Gem::Requirement
126
169
  requirements:
127
170
  - - ">="
@@ -1,6 +0,0 @@
1
- ---
2
- language: ruby
3
- cache: bundler
4
- rvm:
5
- - 2.7.1
6
- before_install: gem install bundler -v 2.1.4