knife-chop 0.2.6

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.
Files changed (55) hide show
  1. checksums.yaml +15 -0
  2. data/Gemfile +24 -0
  3. data/Gemfile.lock +154 -0
  4. data/LICENSE.txt +20 -0
  5. data/README.rdoc +23 -0
  6. data/Rakefile +54 -0
  7. data/TODO.rdoc +46 -0
  8. data/VERSION +1 -0
  9. data/bin/chop +48 -0
  10. data/knife-chop.gemspec +118 -0
  11. data/lib/chef/knife/chop/chef_data_bag_item.rb +43 -0
  12. data/lib/chef/knife/chop/chef_environment.rb +47 -0
  13. data/lib/chef/knife/chop/chef_knife.rb +85 -0
  14. data/lib/chef/knife/chop/chef_part.rb +191 -0
  15. data/lib/chef/knife/chop/chef_role.rb +48 -0
  16. data/lib/chef/knife/chop/cookbook_upload.rb +143 -0
  17. data/lib/chef/knife/chop/data_bag_from_file.rb +87 -0
  18. data/lib/chef/knife/chop/environment_from_file.rb +79 -0
  19. data/lib/chef/knife/chop/errors.rb +5 -0
  20. data/lib/chef/knife/chop/logging.rb +245 -0
  21. data/lib/chef/knife/chop/role_from_file.rb +45 -0
  22. data/lib/chef/knife/chop/translate.rb +23 -0
  23. data/lib/chef/knife/chop/translate/eden.rb +23 -0
  24. data/lib/chef/knife/chop/translate/rbeautify.rb +24 -0
  25. data/lib/chef/knife/chop/ui.rb +110 -0
  26. data/lib/chef/knife/chop/version.rb +9 -0
  27. data/lib/chef/knife/chop_base.rb +821 -0
  28. data/lib/chef/knife/chop_translate.rb +161 -0
  29. data/lib/chef/knife/chop_upload.rb +199 -0
  30. data/lib/ruby-beautify/Gemfile +4 -0
  31. data/lib/ruby-beautify/LICENSE +22 -0
  32. data/lib/ruby-beautify/README.md +39 -0
  33. data/lib/ruby-beautify/RELEASE.md +13 -0
  34. data/lib/ruby-beautify/Rakefile +2 -0
  35. data/lib/ruby-beautify/bin/rbeautify +28 -0
  36. data/lib/ruby-beautify/lib/beautifier.rb +168 -0
  37. data/lib/ruby-beautify/lib/ruby-beautify.rb +26 -0
  38. data/lib/ruby-beautify/lib/ruby-beautify/block_end.rb +23 -0
  39. data/lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb +153 -0
  40. data/lib/ruby-beautify/lib/ruby-beautify/block_start.rb +119 -0
  41. data/lib/ruby-beautify/lib/ruby-beautify/config/ruby.rb +131 -0
  42. data/lib/ruby-beautify/lib/ruby-beautify/language.rb +37 -0
  43. data/lib/ruby-beautify/lib/ruby-beautify/line.rb +53 -0
  44. data/lib/ruby-beautify/lib/ruby-beautify/version.rb +3 -0
  45. data/lib/ruby-beautify/ruby-beautify.gemspec +17 -0
  46. data/lib/ruby-beautify/spec/fixtures/ruby.yml +408 -0
  47. data/lib/ruby-beautify/spec/rbeautify/block_matcher_spec.rb +89 -0
  48. data/lib/ruby-beautify/spec/rbeautify/block_start_spec.rb +51 -0
  49. data/lib/ruby-beautify/spec/rbeautify/config/ruby_spec.rb +183 -0
  50. data/lib/ruby-beautify/spec/rbeautify/line_spec.rb +73 -0
  51. data/lib/ruby-beautify/spec/rbeautify_spec.rb +1 -0
  52. data/lib/ruby-beautify/spec/spec_helper.rb +124 -0
  53. data/spec/knife-chop_spec.rb +7 -0
  54. data/spec/spec_helper.rb +12 -0
  55. metadata +233 -0
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/ruby
2
+ require 'ruby-beautify'
3
+ require 'optparse'
4
+
5
+ include RBeautify
6
+
7
+ Options = OptionParser.new do |opts|
8
+ opts.on("-V", "--version", "Print version") { |version| puts RBeautify::VERSION;exit 0}
9
+ opts.banner = "Usage: print ruby into a pretty format, or break trying."
10
+ end
11
+ Options.parse!
12
+
13
+ if ARGV.empty?
14
+ begin
15
+ puts RBeautify.beautify_string :ruby, STDIN
16
+ rescue Exception => e
17
+ puts e.message
18
+ exit
19
+ end
20
+ else
21
+ ARGV.each do |f|
22
+ if File.exist? f
23
+ puts RBeautify.beautify_string :ruby, open(f).read
24
+ else
25
+ puts "No such file: #{f}"
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,168 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Ruby beautifier, version 2.1, 09/11/2006
4
+ # Copyright (c) 2006, P. Lutus
5
+ # Released under the GPL
6
+
7
+ # Changes
8
+ # Craig Williams
9
+ # 12/26/2011
10
+ # Modified (very minor) to work with a Sublime Text 2 plugin
11
+
12
+ # indent regexp tests
13
+
14
+ @indentExp = [
15
+ /^module\b/,
16
+ /^if\b/,
17
+ /^\@{0,2}[\w\.]*[\s\t]*\=[\s\t]*if\b/,
18
+ /(=\s*|^)until\b/,
19
+ /(=\s*|^)for\b/,
20
+ /^unless\b/,
21
+ /^\@{0,2}[\w\.]*[\s\t]*\=[\s\t]*unless\b/,
22
+ /(=\s*|^)while\b/,
23
+ /(=\s*|^)begin\b/,
24
+ /(^| )case\b/,
25
+ /\bthen\b/,
26
+ /^class\b/,
27
+ /^rescue\b/,
28
+ /^def\b/,
29
+ /\bdo\b/,
30
+ /^else\b/,
31
+ /^elsif\b/,
32
+ /^ensure\b/,
33
+ /\bwhen\b/,
34
+ /\{[^\}]*$/,
35
+ /\[[^\]]*$/,
36
+ /\([^\)]*$/
37
+ ]
38
+
39
+ # outdent regexp tests
40
+
41
+ @outdentExp = [
42
+ /^rescue\b/,
43
+ /^ensure\b/,
44
+ /^elsif\b/,
45
+ /^end\b/,
46
+ /^else\b/,
47
+ /\bwhen\b/,
48
+ /^[^\{]*\}/,
49
+ /^[^\[]*\]/,
50
+ /^[^\(]*\)/
51
+ ]
52
+
53
+ def makeTab(tab)
54
+ (tab < 0) ? "" : @tabStr * @tabSize * tab
55
+ end
56
+
57
+ def addLine(line,tab)
58
+ line.strip!
59
+ line = makeTab(tab)+line if line.length > 0
60
+ line + "\n"
61
+ end
62
+
63
+ def beautifyRuby(contents)
64
+ commentBlock = false
65
+ programEnd = false
66
+ multiLineArray = Array.new
67
+ multiLineStr = ""
68
+ tab = 0
69
+ source = contents
70
+ dest = ""
71
+ source.split("\n").each do |line|
72
+ if(!programEnd)
73
+ # detect program end mark
74
+ if(line =~ /^__END__$/)
75
+ programEnd = true
76
+ else
77
+ # combine continuing lines
78
+ if(!(line =~ /^\s*#/) && line =~ /[^\\]\\\s*$/)
79
+ multiLineArray.push line
80
+ multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1")
81
+ next
82
+ end
83
+
84
+ # add final line
85
+ if(multiLineStr.length > 0)
86
+ multiLineArray.push line
87
+ multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1")
88
+ end
89
+
90
+ tline = ((multiLineStr.length > 0)?multiLineStr:line).strip
91
+ if(tline =~ /^=begin/)
92
+ commentBlock = true
93
+ end
94
+ end
95
+ end
96
+ if(commentBlock || programEnd)
97
+ # add the line unchanged
98
+ dest += line + "\n"
99
+ else
100
+ commentLine = (tline =~ /^#/)
101
+ if(!commentLine)
102
+ # throw out sequences that will
103
+ # only sow confusion
104
+ while tline.gsub!(/'.*?'/,"")
105
+ end
106
+ while tline.gsub!(/".*?"/,"")
107
+ end
108
+ while tline.gsub!(/\`.*?\`/,"")
109
+ end
110
+ while tline.gsub!(/\{[^\{]*?\}/,"")
111
+ end
112
+ while tline.gsub!(/\([^\(]*?\)/,"")
113
+ end
114
+ while tline.gsub!(/\/.*?\//,"")
115
+ end
116
+ while tline.gsub!(/%r(.).*?\1/,"")
117
+ end
118
+ tline.gsub!(/\\\"/,"'")
119
+ @outdentExp.each do |re|
120
+ if(tline =~ re)
121
+ tab -= 1
122
+ break
123
+ end
124
+ end
125
+ end
126
+ if (multiLineArray.length > 0)
127
+ multiLineArray.each do |ml|
128
+ dest += addLine(ml,tab)
129
+ end
130
+ multiLineArray.clear
131
+ multiLineStr = ""
132
+ else
133
+ dest += addLine(line,tab)
134
+ end
135
+ if(!commentLine)
136
+ @indentExp.each do |re|
137
+ if(tline =~ re && !(tline =~ /\s+end\s*$/))
138
+ tab += 1
139
+ break
140
+ end
141
+ end
142
+ end
143
+ end
144
+ if(tline =~ /^=end/)
145
+ commentBlock = false
146
+ end
147
+ end
148
+ STDOUT.write(dest)
149
+ # uncomment this to complain about mismatched blocks
150
+ # if(tab != 0)
151
+ # STDERR.puts "#{path}: Indentation error: #{tab}"
152
+ # end
153
+ end
154
+
155
+ tab_or_space = ARGV.first
156
+ path = ARGV.last
157
+ path.gsub!("\\", "/") if RUBY_PLATFORM =~ /mswin/
158
+ contents = IO.read(path)
159
+
160
+ if tab_or_space == 'space'
161
+ @tabSize = 2
162
+ @tabStr = " "
163
+ else
164
+ @tabSize = 1
165
+ @tabStr = "\t"
166
+ end
167
+
168
+ beautifyRuby(contents)
@@ -0,0 +1,26 @@
1
+ require "ruby-beautify/version"
2
+ require 'ruby-beautify/block_start'
3
+ require 'ruby-beautify/block_end'
4
+ require 'ruby-beautify/block_matcher'
5
+ require 'ruby-beautify/language'
6
+ require 'ruby-beautify/line'
7
+ require 'ruby-beautify/config/ruby'
8
+
9
+ module RBeautify
10
+ def self.beautify_string(language, source, use_tabs=false)
11
+ dest = ""
12
+ block = nil
13
+
14
+ unless language.is_a? RBeautify::Language
15
+ language = RBeautify::Language.language(language)
16
+ end
17
+
18
+ source.lines.each_with_index do |line_content, line_number|
19
+ line = RBeautify::Line.new(language, line_content, line_number, block, use_tabs)
20
+ dest += line.format + "\n"
21
+ block = line.block
22
+ end
23
+
24
+ return dest
25
+ end
26
+ end # module RBeautify
@@ -0,0 +1,23 @@
1
+ module RBeautify
2
+
3
+ class BlockEnd
4
+
5
+ attr_accessor :block_start, :offset, :match, :after_match
6
+
7
+ def initialize(block_start, offset, match, after_match)
8
+ self.block_start = block_start
9
+ self.offset = offset
10
+ self.match = match
11
+ self.after_match = after_match
12
+ end
13
+
14
+ def end_offset
15
+ offset + match.length
16
+ end
17
+
18
+ def end_can_also_be_start?
19
+ block_start.block_matcher.end_can_also_be_start?
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,153 @@
1
+ module RBeautify
2
+
3
+ class BlockMatcher
4
+
5
+ attr_reader :language, :name, :starts, :ends, :options
6
+
7
+ def initialize(language, name, starts, ends, options = {})
8
+ @language = language
9
+ @name = name
10
+ @starts = starts
11
+ @ends = ends.nil? ? starts : ends
12
+ @options = options
13
+ end
14
+
15
+ class << self
16
+ def parse(language, original_block, line_number, string, current_offset)
17
+ block_end = original_block && original_block.parse_block_end(string, current_offset)
18
+
19
+ if (block_start = first_block_start(language,
20
+ original_block,
21
+ line_number,
22
+ string,
23
+ current_offset,
24
+ block_end.nil? ? nil : block_end.offset))
25
+
26
+ block_end = nil
27
+ end
28
+
29
+ if block_end
30
+ # Check whether the section of the line which is a block end is also a block start
31
+ if block_end.end_can_also_be_start? &&
32
+ (block_start_candidate = first_block_start(language, block_end.block_start.parent, line_number, string, current_offset)) &&
33
+ block_start_candidate.offset == block_end.offset
34
+ block_start = block_start_candidate
35
+ end
36
+
37
+ end
38
+
39
+ if block_start
40
+ if debug
41
+ puts "MATCH: '#{string.slice(0, string.length - block_start.match.length - block_start.after_match.length)}<START type=#{block_start.name}>#{block_start.match}</START>#{block_start.after_match}'"
42
+ end
43
+ parse(language, block_start, line_number, block_start.after_match, block_start.end_offset)
44
+ elsif block_end
45
+ if debug
46
+ puts "MATCH: '#{string.slice(0, string.length - block_end.match.length - block_end.after_match.length)}<END>#{block_end.match}</END>#{block_end.after_match}'"
47
+ end
48
+ parse(language, block_end.block_start.parent, line_number, block_end.after_match, block_end.end_offset)
49
+ else
50
+ original_block
51
+ end
52
+ end
53
+
54
+ def debug=(value)
55
+ @debug = value
56
+ end
57
+
58
+ def debug
59
+ @debug
60
+ end
61
+
62
+ private
63
+ def first_block_start(language, parent_block, line_number, string, offset, maximum_offset = nil)
64
+ first_block_start = nil
65
+ language.matchers.each do |matcher|
66
+ if matcher.can_nest?(parent_block)
67
+ if (block_start_candidate = matcher.parse_block_start(string, parent_block, offset, line_number)) &&
68
+ (maximum_offset.nil? || maximum_offset > block_start_candidate.offset)
69
+ first_block_start = block_start_candidate
70
+ maximum_offset = first_block_start.offset
71
+ end
72
+ end
73
+ end
74
+ first_block_start
75
+ end
76
+ end
77
+
78
+ def parse_block_start(string, parent_block, offset, line_number)
79
+ if !string.empty? && (match = starts.match(string))
80
+ RBeautify::BlockStart.new(self, parent_block, line_number, offset + match.begin(0), match[0], match.post_match)
81
+ end
82
+ end
83
+
84
+ def indent_end_line?(block)
85
+ evaluate_option_for_block(:indent_end_line, block)
86
+ end
87
+
88
+ def indent_size(block)
89
+ evaluate_option_for_block(:indent_size, block) || language.indent_size
90
+ end
91
+
92
+ # Look for blocks within the content of this one
93
+ def parse_content?
94
+ options[:parse_content] != false
95
+ end
96
+
97
+ # Indent the content of this block
98
+ def format_content?
99
+ options[:format_content] != false
100
+ end
101
+
102
+ def can_nest?(parent_block)
103
+ return false unless parent_block.nil? || parent_block.parse_content?
104
+
105
+ if options[:nest_only]
106
+ parent_block && options[:nest_only].include?(parent_block.name)
107
+ else
108
+ parent_block.nil? ||
109
+ options[:nest_except].nil? || !options[:nest_except].include?(parent_block.name)
110
+ end
111
+ end
112
+
113
+ def ends?
114
+ ends != false
115
+ end
116
+
117
+ def end_is_implicit?
118
+ options[:end] == :implicit
119
+ end
120
+
121
+ def end_can_also_be_start?
122
+ if ends == starts
123
+ options[:end_can_also_be_start] == true
124
+ else
125
+ options[:end_can_also_be_start] != false
126
+ end
127
+ end
128
+
129
+ def negate_ends_match?
130
+ options[:negate_ends_match]
131
+ end
132
+
133
+ # True if blocks can contain the escape character \ which needs to be
134
+ # checked for on end match
135
+ def escape_character?
136
+ options[:escape_character] == true
137
+ end
138
+
139
+ def inspect
140
+ name
141
+ end
142
+
143
+ private
144
+ def evaluate_option_for_block(key, block)
145
+ if options[key] && options[key].respond_to?(:call)
146
+ options[key].call(block)
147
+ else
148
+ options[key]
149
+ end
150
+ end
151
+
152
+ end
153
+ end
@@ -0,0 +1,119 @@
1
+ module RBeautify
2
+
3
+ class BlockStart
4
+
5
+ attr_reader :block_matcher, :parent, :offset, :match, :after_match, :line_number
6
+
7
+ class << self
8
+ def first_common_ancestor(first, second)
9
+ if first.nil? || second.nil?
10
+ nil
11
+ else
12
+ (first.ancestors & second.ancestors).last
13
+ end
14
+ end
15
+ end
16
+
17
+ def initialize(block_matcher, parent, line_number, offset, match, after_match)
18
+ @block_matcher = block_matcher
19
+ @parent = parent
20
+ @offset = offset
21
+ @match = match
22
+ @after_match = after_match
23
+ @line_number = line_number
24
+ end
25
+
26
+ def end_offset
27
+ offset + match.length
28
+ end
29
+
30
+ def parse_block_end(string, offset)
31
+ block_end = parse_explicit_block_end(string, offset)
32
+
33
+ # Handle case where end is implicit
34
+ if block_end.nil? && end_is_implicit? && parent
35
+ block_end = parent.parse_block_end(string, offset)
36
+ end
37
+
38
+ block_end
39
+ end
40
+
41
+ def format_content?
42
+ block_matcher.format_content?
43
+ end
44
+
45
+ def parse_content?
46
+ block_matcher.parse_content?
47
+ end
48
+
49
+ def indent_end_line?
50
+ block_matcher.indent_end_line?(self)
51
+ end
52
+
53
+ def total_indent_size
54
+ parent.nil? ? indent_size : parent.total_indent_size + indent_size
55
+ end
56
+
57
+ def indent_size
58
+ block_matcher.indent_size(self)
59
+ end
60
+
61
+ def end_is_implicit?
62
+ block_matcher.end_is_implicit?
63
+ end
64
+
65
+ def name
66
+ block_matcher.name
67
+ end
68
+
69
+ # Returns true if strict ancestor of
70
+ def strict_ancestor_of?(block_start)
71
+ block_start && block_start.parent && (self == block_start.parent || strict_ancestor_of?(block_start.parent))
72
+ end
73
+
74
+ def ancestors
75
+ if parent
76
+ parent.ancestors + [self]
77
+ else
78
+ [self]
79
+ end
80
+ end
81
+
82
+ private
83
+ def ends?
84
+ block_matcher.ends?
85
+ end
86
+
87
+ def negate_ends_match?
88
+ block_matcher.negate_ends_match?
89
+ end
90
+
91
+ def escape_character?
92
+ block_matcher.escape_character?
93
+ end
94
+
95
+ def parse_explicit_block_end(string, offset)
96
+ block_end = nil
97
+
98
+ if ends?
99
+
100
+ if match = block_matcher.ends.match(string)
101
+ unless negate_ends_match?
102
+ if escape_character? &&
103
+ ((escape_chars = match.pre_match.match(/\\*$/)) && (escape_chars[0].size % 2 == 1))
104
+ # If there are an odd number of escape characters just before
105
+ # the match then this match should be skipped
106
+ return parse_explicit_block_end(match.post_match, offset + escape_chars[0].size + match[0].length)
107
+ else
108
+ return RBeautify::BlockEnd.new(self, offset + match.begin(0), match[0], match.post_match)
109
+ end
110
+ end
111
+ elsif negate_ends_match?
112
+ return RBeautify::BlockEnd.new(self, offset, '', string)
113
+ end
114
+
115
+ end
116
+ end
117
+
118
+ end
119
+ end