atli 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +8 -0
  3. data/CHANGELOG.md +193 -0
  4. data/CONTRIBUTING.md +20 -0
  5. data/LICENSE.md +24 -0
  6. data/README.md +44 -0
  7. data/atli.gemspec +30 -0
  8. data/bin/thor +6 -0
  9. data/lib/thor.rb +868 -0
  10. data/lib/thor/actions.rb +322 -0
  11. data/lib/thor/actions/create_file.rb +104 -0
  12. data/lib/thor/actions/create_link.rb +60 -0
  13. data/lib/thor/actions/directory.rb +118 -0
  14. data/lib/thor/actions/empty_directory.rb +143 -0
  15. data/lib/thor/actions/file_manipulation.rb +364 -0
  16. data/lib/thor/actions/inject_into_file.rb +109 -0
  17. data/lib/thor/base.rb +773 -0
  18. data/lib/thor/command.rb +192 -0
  19. data/lib/thor/core_ext/hash_with_indifferent_access.rb +97 -0
  20. data/lib/thor/core_ext/io_binary_read.rb +12 -0
  21. data/lib/thor/core_ext/ordered_hash.rb +129 -0
  22. data/lib/thor/error.rb +32 -0
  23. data/lib/thor/group.rb +281 -0
  24. data/lib/thor/invocation.rb +182 -0
  25. data/lib/thor/line_editor.rb +17 -0
  26. data/lib/thor/line_editor/basic.rb +37 -0
  27. data/lib/thor/line_editor/readline.rb +88 -0
  28. data/lib/thor/parser.rb +5 -0
  29. data/lib/thor/parser/argument.rb +70 -0
  30. data/lib/thor/parser/arguments.rb +175 -0
  31. data/lib/thor/parser/option.rb +146 -0
  32. data/lib/thor/parser/options.rb +221 -0
  33. data/lib/thor/parser/shared_option.rb +23 -0
  34. data/lib/thor/rake_compat.rb +71 -0
  35. data/lib/thor/runner.rb +324 -0
  36. data/lib/thor/shell.rb +81 -0
  37. data/lib/thor/shell/basic.rb +439 -0
  38. data/lib/thor/shell/color.rb +149 -0
  39. data/lib/thor/shell/html.rb +126 -0
  40. data/lib/thor/util.rb +268 -0
  41. data/lib/thor/version.rb +22 -0
  42. metadata +114 -0
@@ -0,0 +1,17 @@
1
+ require "thor/line_editor/basic"
2
+ require "thor/line_editor/readline"
3
+
4
+ class Thor
5
+ module LineEditor
6
+ def self.readline(prompt, options = {})
7
+ best_available.new(prompt, options).readline
8
+ end
9
+
10
+ def self.best_available
11
+ [
12
+ Thor::LineEditor::Readline,
13
+ Thor::LineEditor::Basic
14
+ ].detect(&:available?)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,37 @@
1
+ class Thor
2
+ module LineEditor
3
+ class Basic
4
+ attr_reader :prompt, :options
5
+
6
+ def self.available?
7
+ true
8
+ end
9
+
10
+ def initialize(prompt, options)
11
+ @prompt = prompt
12
+ @options = options
13
+ end
14
+
15
+ def readline
16
+ $stdout.print(prompt)
17
+ get_input
18
+ end
19
+
20
+ private
21
+
22
+ def get_input
23
+ if echo?
24
+ $stdin.gets
25
+ else
26
+ # Lazy-load io/console since it is gem-ified as of 2.3
27
+ require "io/console" if RUBY_VERSION > "1.9.2"
28
+ $stdin.noecho(&:gets)
29
+ end
30
+ end
31
+
32
+ def echo?
33
+ options.fetch(:echo, true)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,88 @@
1
+ begin
2
+ require "readline"
3
+ rescue LoadError
4
+ end
5
+
6
+ class Thor
7
+ module LineEditor
8
+ class Readline < Basic
9
+ def self.available?
10
+ Object.const_defined?(:Readline)
11
+ end
12
+
13
+ def readline
14
+ if echo?
15
+ ::Readline.completion_append_character = nil
16
+ # Ruby 1.8.7 does not allow Readline.completion_proc= to receive nil.
17
+ if complete = completion_proc
18
+ ::Readline.completion_proc = complete
19
+ end
20
+ ::Readline.readline(prompt, add_to_history?)
21
+ else
22
+ super
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def add_to_history?
29
+ options.fetch(:add_to_history, true)
30
+ end
31
+
32
+ def completion_proc
33
+ if use_path_completion?
34
+ proc { |text| PathCompletion.new(text).matches }
35
+ elsif completion_options.any?
36
+ proc do |text|
37
+ completion_options.select { |option| option.start_with?(text) }
38
+ end
39
+ end
40
+ end
41
+
42
+ def completion_options
43
+ options.fetch(:limited_to, [])
44
+ end
45
+
46
+ def use_path_completion?
47
+ options.fetch(:path, false)
48
+ end
49
+
50
+ class PathCompletion
51
+ attr_reader :text
52
+ private :text
53
+
54
+ def initialize(text)
55
+ @text = text
56
+ end
57
+
58
+ def matches
59
+ relative_matches
60
+ end
61
+
62
+ private
63
+
64
+ def relative_matches
65
+ absolute_matches.map { |path| path.sub(base_path, "") }
66
+ end
67
+
68
+ def absolute_matches
69
+ Dir[glob_pattern].map do |path|
70
+ if File.directory?(path)
71
+ "#{path}/"
72
+ else
73
+ path
74
+ end
75
+ end
76
+ end
77
+
78
+ def glob_pattern
79
+ "#{base_path}#{text}*"
80
+ end
81
+
82
+ def base_path
83
+ "#{Dir.pwd}/"
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,5 @@
1
+ require "thor/parser/argument"
2
+ require "thor/parser/arguments"
3
+ require "thor/parser/option"
4
+ require "thor/parser/shared_option"
5
+ require "thor/parser/options"
@@ -0,0 +1,70 @@
1
+ class Thor
2
+ class Argument #:nodoc:
3
+ VALID_TYPES = [:numeric, :hash, :array, :string]
4
+
5
+ attr_reader :name, :description, :enum, :required, :type, :default, :banner
6
+ alias_method :human_name, :name
7
+
8
+ def initialize(name, options = {})
9
+ class_name = self.class.name.split("::").last
10
+
11
+ type = options[:type]
12
+
13
+ raise ArgumentError, "#{class_name} name can't be nil." if name.nil?
14
+ raise ArgumentError, "Type :#{type} is not valid for #{class_name.downcase}s." if type && !valid_type?(type)
15
+
16
+ @name = name.to_s
17
+ @description = options[:desc]
18
+ @required = options.key?(:required) ? options[:required] : true
19
+ @type = (type || :string).to_sym
20
+ @default = options[:default]
21
+ @banner = options[:banner] || default_banner
22
+ @enum = options[:enum]
23
+
24
+ validate! # Trigger specific validations
25
+ end
26
+
27
+ def usage
28
+ required? ? banner : "[#{banner}]"
29
+ end
30
+
31
+ def required?
32
+ required
33
+ end
34
+
35
+ def show_default?
36
+ case default
37
+ when Array, String, Hash
38
+ !default.empty?
39
+ else
40
+ default
41
+ end
42
+ end
43
+
44
+ protected
45
+
46
+ def validate!
47
+ raise ArgumentError, "An argument cannot be required and have default value." if required? && !default.nil?
48
+ raise ArgumentError, "An argument cannot have an enum other than an array." if @enum && !@enum.is_a?(Array)
49
+ end
50
+
51
+ def valid_type?(type)
52
+ self.class::VALID_TYPES.include?(type.to_sym)
53
+ end
54
+
55
+ def default_banner
56
+ case type
57
+ when :boolean
58
+ nil
59
+ when :string, :default
60
+ human_name.upcase
61
+ when :numeric
62
+ "N"
63
+ when :hash
64
+ "key:value"
65
+ when :array
66
+ "one two three"
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,175 @@
1
+ class Thor
2
+ class Arguments #:nodoc: # rubocop:disable ClassLength
3
+ NUMERIC = /[-+]?(\d*\.\d+|\d+)/
4
+
5
+ # Receives an array of args and returns two arrays, one with arguments
6
+ # and one with switches.
7
+ #
8
+ def self.split(args)
9
+ arguments = []
10
+
11
+ args.each do |item|
12
+ break if item =~ /^-/
13
+ arguments << item
14
+ end
15
+
16
+ [arguments, args[Range.new(arguments.size, -1)]]
17
+ end
18
+
19
+ def self.parse(*args)
20
+ to_parse = args.pop
21
+ new(*args).parse(to_parse)
22
+ end
23
+
24
+ # Takes an array of Thor::Argument objects.
25
+ #
26
+ def initialize(arguments = [])
27
+ @assigns = {}
28
+ @non_assigned_required = []
29
+ @switches = arguments
30
+
31
+ arguments.each do |argument|
32
+ if !argument.default.nil?
33
+ @assigns[argument.human_name] = argument.default
34
+ elsif argument.required?
35
+ @non_assigned_required << argument
36
+ end
37
+ end
38
+ end
39
+
40
+ def parse(args)
41
+ @pile = args.dup
42
+
43
+ @switches.each do |argument|
44
+ break unless peek
45
+ @non_assigned_required.delete(argument)
46
+ @assigns[argument.human_name] = send(:"parse_#{argument.type}", argument.human_name)
47
+ end
48
+
49
+ check_requirement!
50
+ @assigns
51
+ end
52
+
53
+ def remaining
54
+ @pile
55
+ end
56
+
57
+ private
58
+
59
+ def no_or_skip?(arg)
60
+ arg =~ /^--(no|skip)-([-\w]+)$/
61
+ $2
62
+ end
63
+
64
+ def last?
65
+ @pile.empty?
66
+ end
67
+
68
+ def peek
69
+ @pile.first
70
+ end
71
+
72
+ def shift
73
+ @pile.shift
74
+ end
75
+
76
+ def unshift(arg)
77
+ if arg.is_a?(Array)
78
+ @pile = arg + @pile
79
+ else
80
+ @pile.unshift(arg)
81
+ end
82
+ end
83
+
84
+ def current_is_value?
85
+ peek && peek.to_s !~ /^-/
86
+ end
87
+
88
+ # Runs through the argument array getting strings that contains ":" and
89
+ # mark it as a hash:
90
+ #
91
+ # [ "name:string", "age:integer" ]
92
+ #
93
+ # Becomes:
94
+ #
95
+ # { "name" => "string", "age" => "integer" }
96
+ #
97
+ def parse_hash(name)
98
+ return shift if peek.is_a?(Hash)
99
+ hash = {}
100
+
101
+ while current_is_value? && peek.include?(":")
102
+ key, value = shift.split(":", 2)
103
+ raise MalformattedArgumentError, "You can't specify '#{key}' more than once in option '#{name}'; got #{key}:#{hash[key]} and #{key}:#{value}" if hash.include? key
104
+ hash[key] = value
105
+ end
106
+ hash
107
+ end
108
+
109
+ # Runs through the argument array getting all strings until no string is
110
+ # found or a switch is found.
111
+ #
112
+ # ["a", "b", "c"]
113
+ #
114
+ # And returns it as an array:
115
+ #
116
+ # ["a", "b", "c"]
117
+ #
118
+ def parse_array(name)
119
+ return shift if peek.is_a?(Array)
120
+ array = []
121
+ array << shift while current_is_value?
122
+ array
123
+ end
124
+
125
+ # Check if the peek is numeric format and return a Float or Integer.
126
+ # Check if the peek is included in enum if enum is provided.
127
+ # Otherwise raises an error.
128
+ #
129
+ def parse_numeric(name)
130
+ return shift if peek.is_a?(Numeric)
131
+
132
+ unless peek =~ NUMERIC && $& == peek
133
+ raise MalformattedArgumentError, "Expected numeric value for '#{name}'; got #{peek.inspect}"
134
+ end
135
+
136
+ value = $&.index(".") ? shift.to_f : shift.to_i
137
+ if @switches.is_a?(Hash) && switch = @switches[name]
138
+ if switch.enum && !switch.enum.include?(value)
139
+ raise MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum.join(', ')}; got #{value}"
140
+ end
141
+ end
142
+ value
143
+ end
144
+
145
+ # Parse string:
146
+ # for --string-arg, just return the current value in the pile
147
+ # for --no-string-arg, nil
148
+ # Check if the peek is included in enum if enum is provided. Otherwise raises an error.
149
+ #
150
+ def parse_string(name)
151
+ if no_or_skip?(name)
152
+ nil
153
+ else
154
+ value = shift
155
+ if @switches.is_a?(Hash) && switch = @switches[name]
156
+ if switch.enum && !switch.enum.include?(value)
157
+ raise MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum.join(', ')}; got #{value}"
158
+ end
159
+ end
160
+ value
161
+ end
162
+ end
163
+
164
+ # Raises an error if @non_assigned_required array is not empty.
165
+ #
166
+ def check_requirement!
167
+ return if @non_assigned_required.empty?
168
+ names = @non_assigned_required.map do |o|
169
+ o.respond_to?(:switch_name) ? o.switch_name : o.human_name
170
+ end.join("', '")
171
+ class_name = self.class.name.split("::").last.downcase
172
+ raise RequiredArgumentMissingError, "No value provided for required #{class_name} '#{names}'"
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,146 @@
1
+ class Thor
2
+ class Option < Argument #:nodoc:
3
+ attr_reader :aliases, :group, :lazy_default, :hide
4
+
5
+ VALID_TYPES = [:boolean, :numeric, :hash, :array, :string]
6
+
7
+ def initialize(name, options = {})
8
+ @check_default_type = options[:check_default_type]
9
+ options[:required] = false unless options.key?(:required)
10
+ super
11
+ @lazy_default = options[:lazy_default]
12
+ @group = options[:group].to_s.capitalize if options[:group]
13
+ @aliases = Array(options[:aliases])
14
+ @hide = options[:hide]
15
+ end
16
+
17
+ # This parse quick options given as method_options. It makes several
18
+ # assumptions, but you can be more specific using the option method.
19
+ #
20
+ # parse :foo => "bar"
21
+ # #=> Option foo with default value bar
22
+ #
23
+ # parse [:foo, :baz] => "bar"
24
+ # #=> Option foo with default value bar and alias :baz
25
+ #
26
+ # parse :foo => :required
27
+ # #=> Required option foo without default value
28
+ #
29
+ # parse :foo => 2
30
+ # #=> Option foo with default value 2 and type numeric
31
+ #
32
+ # parse :foo => :numeric
33
+ # #=> Option foo without default value and type numeric
34
+ #
35
+ # parse :foo => true
36
+ # #=> Option foo with default value true and type boolean
37
+ #
38
+ # The valid types are :boolean, :numeric, :hash, :array and :string. If none
39
+ # is given a default type is assumed. This default type accepts arguments as
40
+ # string (--foo=value) or booleans (just --foo).
41
+ #
42
+ # By default all options are optional, unless :required is given.
43
+ #
44
+ def self.parse(key, value)
45
+ if key.is_a?(Array)
46
+ name, *aliases = key
47
+ else
48
+ name = key
49
+ aliases = []
50
+ end
51
+
52
+ name = name.to_s
53
+ default = value
54
+
55
+ type = case value
56
+ when Symbol
57
+ default = nil
58
+ if VALID_TYPES.include?(value)
59
+ value
60
+ elsif required = (value == :required) # rubocop:disable AssignmentInCondition
61
+ :string
62
+ end
63
+ when TrueClass, FalseClass
64
+ :boolean
65
+ when Numeric
66
+ :numeric
67
+ when Hash, Array, String
68
+ value.class.name.downcase.to_sym
69
+ end
70
+
71
+ new(name.to_s, :required => required, :type => type, :default => default, :aliases => aliases)
72
+ end
73
+
74
+ def switch_name
75
+ @switch_name ||= dasherized? ? name : dasherize(name)
76
+ end
77
+
78
+ def human_name
79
+ @human_name ||= dasherized? ? undasherize(name) : name
80
+ end
81
+
82
+ def usage(padding = 0)
83
+ sample = if banner && !banner.to_s.empty?
84
+ "#{switch_name}=#{banner}".dup
85
+ else
86
+ switch_name
87
+ end
88
+
89
+ sample = "[#{sample}]".dup unless required?
90
+
91
+ if boolean?
92
+ sample << ", [#{dasherize('no-' + human_name)}]" unless (name == "force") || name.start_with?("no-")
93
+ end
94
+
95
+ if aliases.empty?
96
+ (" " * padding) << sample
97
+ else
98
+ "#{aliases.join(', ')}, #{sample}"
99
+ end
100
+ end
101
+
102
+ VALID_TYPES.each do |type|
103
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
104
+ def #{type}?
105
+ self.type == #{type.inspect}
106
+ end
107
+ RUBY
108
+ end
109
+
110
+ protected
111
+
112
+ def validate!
113
+ raise ArgumentError, "An option cannot be boolean and required." if boolean? && required?
114
+ validate_default_type! if @check_default_type
115
+ end
116
+
117
+ def validate_default_type!
118
+ default_type = case @default
119
+ when nil
120
+ return
121
+ when TrueClass, FalseClass
122
+ required? ? :string : :boolean
123
+ when Numeric
124
+ :numeric
125
+ when Symbol
126
+ :string
127
+ when Hash, Array, String
128
+ @default.class.name.downcase.to_sym
129
+ end
130
+
131
+ raise ArgumentError, "Expected #{@type} default value for '#{switch_name}'; got #{@default.inspect} (#{default_type})" unless default_type == @type
132
+ end
133
+
134
+ def dasherized?
135
+ name.index("-") == 0
136
+ end
137
+
138
+ def undasherize(str)
139
+ str.sub(/^-{1,2}/, "")
140
+ end
141
+
142
+ def dasherize(str)
143
+ (str.length > 1 ? "--" : "-") + str.tr("_", "-")
144
+ end
145
+ end
146
+ end