rix 0.0.2 → 0.1.0
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.
- data/bin/rix +13 -2
- data/lib/rix.rb +6 -0
- data/lib/rix/command.rb +62 -0
- data/lib/rix/commands/add_element.rb +33 -0
- data/lib/rix/commands/count.rb +13 -0
- data/lib/rix/commands/help.rb +1 -1
- data/lib/rix/commands/remove.rb +22 -0
- data/lib/rix/commands/rename.rb +30 -0
- data/lib/rix/commands/show.rb +19 -0
- data/lib/rix/registry.rb +1 -1
- data/lib/rix/version.rb +1 -1
- metadata +6 -2
- data/README +0 -0
data/bin/rix
CHANGED
@@ -5,15 +5,24 @@ $:.unshift File.join(File.dirname(__FILE__), %w[.. lib])
|
|
5
5
|
require 'rix'
|
6
6
|
require 'optparse'
|
7
7
|
|
8
|
+
Rix::Registry.instance.register 'add-element', Rix::Commands::AddElement
|
9
|
+
Rix::Registry.instance.register 'remove', Rix::Commands::Remove
|
10
|
+
Rix::Registry.instance.register 'rename', Rix::Commands::Rename
|
11
|
+
Rix::Registry.instance.register 'count', Rix::Commands::Count
|
12
|
+
Rix::Registry.instance.register 'show', Rix::Commands::Show
|
8
13
|
Rix::Registry.instance.register 'help', Rix::Commands::Help
|
9
14
|
|
10
15
|
if ARGV.size == 0
|
11
16
|
puts "Type 'rix --help' for usage."
|
12
|
-
exit 0
|
13
17
|
elsif not ARGV[0].start_with? '-'
|
14
18
|
name = ARGV.shift
|
15
19
|
command = Rix::Registry.instance[name]
|
16
|
-
|
20
|
+
begin
|
21
|
+
command.execute
|
22
|
+
rescue => e
|
23
|
+
puts "#{e.to_s.capitalize}. Type 'rix help #{name}' for usage."
|
24
|
+
exit 1
|
25
|
+
end
|
17
26
|
else
|
18
27
|
opts = OptionParser.new do |opts|
|
19
28
|
opts.on("-v", "--version") { puts Rix::VERSION; exit 0 }
|
@@ -26,3 +35,5 @@ else
|
|
26
35
|
exit 1
|
27
36
|
end
|
28
37
|
end
|
38
|
+
|
39
|
+
exit 0
|
data/lib/rix.rb
CHANGED
@@ -1,6 +1,12 @@
|
|
1
|
+
require 'rexml/document'
|
1
2
|
require 'rix/registry'
|
2
3
|
require 'rix/command'
|
3
4
|
require 'rix/commands/help'
|
5
|
+
require 'rix/commands/add_element'
|
6
|
+
require 'rix/commands/remove'
|
7
|
+
require 'rix/commands/rename'
|
8
|
+
require 'rix/commands/count'
|
9
|
+
require 'rix/commands/show'
|
4
10
|
require 'rix/commands/unknown'
|
5
11
|
require 'rix/help'
|
6
12
|
require 'rix/version'
|
data/lib/rix/command.rb
CHANGED
@@ -2,5 +2,67 @@ require 'rix/commands/unknown'
|
|
2
2
|
|
3
3
|
module Rix
|
4
4
|
class Command
|
5
|
+
attr_accessor :files
|
6
|
+
attr_accessor :xpath
|
7
|
+
|
8
|
+
def opts
|
9
|
+
OptionParser.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def help
|
13
|
+
opts.help
|
14
|
+
end
|
15
|
+
|
16
|
+
def execute
|
17
|
+
@files = []
|
18
|
+
@value = ""
|
19
|
+
|
20
|
+
opts.parse!
|
21
|
+
|
22
|
+
@xpath = ARGV.shift.dup if ARGV.size > 0
|
23
|
+
# @files will hold all remaining command-line arguments on Windows,
|
24
|
+
# and expanded filenames on Mac, Linux etc.
|
25
|
+
@files = ARGV.uniq if ARGV.size > 0
|
26
|
+
|
27
|
+
raise "Missing XPath expression" if @xpath.nil? or @xpath.empty?
|
28
|
+
|
29
|
+
before_all
|
30
|
+
|
31
|
+
@files.each do |pattern|
|
32
|
+
# Using Dir is only needed for Windows, where arguments are not expanded,
|
33
|
+
# on Mac, Linux etc. this will have no effect.
|
34
|
+
Dir[pattern].each do |path|
|
35
|
+
File.open(path, 'r') do |file|
|
36
|
+
document = REXML::Document.new(file)
|
37
|
+
nodes = REXML::XPath.match(document, @xpath)
|
38
|
+
before(path, nodes)
|
39
|
+
nodes.each do |node|
|
40
|
+
on_node(node)
|
41
|
+
end
|
42
|
+
after(path, document)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
after_all
|
48
|
+
end
|
49
|
+
|
50
|
+
def before_all
|
51
|
+
end
|
52
|
+
|
53
|
+
def before(path, nodes)
|
54
|
+
end
|
55
|
+
|
56
|
+
def on_node(node)
|
57
|
+
end
|
58
|
+
|
59
|
+
def after(path, document)
|
60
|
+
#File.open(path, 'w') do |file|
|
61
|
+
# document.write file
|
62
|
+
#end
|
63
|
+
end
|
64
|
+
|
65
|
+
def after_all
|
66
|
+
end
|
5
67
|
end
|
6
68
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Rix
|
2
|
+
module Commands
|
3
|
+
class AddElement < Command
|
4
|
+
attr_accessor :name
|
5
|
+
attr_accessor :value
|
6
|
+
|
7
|
+
def opts
|
8
|
+
OptionParser.new do |opts|
|
9
|
+
opts.banner = "Usage: rix add-element [options] <xpath> <files>"
|
10
|
+
opts.separator ""
|
11
|
+
opts.separator "Options:"
|
12
|
+
opts.on("-n", "--name NAME", "The name of the new element") { |name| @name = name }
|
13
|
+
opts.on("-v", "--value VALUE", "The value of the new element") { |value| @value = value }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def before_all
|
18
|
+
raise "Missing option: -n, --name" if @name.nil? or @name.empty?
|
19
|
+
end
|
20
|
+
|
21
|
+
def on_node(node)
|
22
|
+
element = node.add_element @name
|
23
|
+
element.text = @value
|
24
|
+
end
|
25
|
+
|
26
|
+
def after(path, document)
|
27
|
+
File.open(path, 'w') do |file|
|
28
|
+
document.write file
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/rix/commands/help.rb
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
module Rix
|
2
|
+
module Commands
|
3
|
+
class Remove < Command
|
4
|
+
|
5
|
+
def help
|
6
|
+
"Usage: rix remove <xpath> <files>"
|
7
|
+
end
|
8
|
+
|
9
|
+
def on_node(node)
|
10
|
+
node.parent.delete_element node if node.is_a? REXML::Element
|
11
|
+
# BUG: 'node.element.delete_attribute node' doesn't work
|
12
|
+
node.element.delete_attribute node.name if node.is_a? REXML::Attribute
|
13
|
+
end
|
14
|
+
|
15
|
+
def after(path, document)
|
16
|
+
File.open(path, 'w') do |file|
|
17
|
+
document.write file
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Rix
|
2
|
+
module Commands
|
3
|
+
class Rename < Command
|
4
|
+
attr_accessor :name
|
5
|
+
|
6
|
+
def opts
|
7
|
+
OptionParser.new do |opts|
|
8
|
+
opts.banner = "Usage: rix rename [options] <xpath> <files>"
|
9
|
+
opts.separator ""
|
10
|
+
opts.separator "Options:"
|
11
|
+
opts.on("-n", "--name NAME", "The new name") { |name| @name = name }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def before_all
|
16
|
+
raise "Missing option: -n, --name" if @name.nil? or @name.empty?
|
17
|
+
end
|
18
|
+
|
19
|
+
def on_node(node)
|
20
|
+
node.name = @name if node.respond_to? :name
|
21
|
+
end
|
22
|
+
|
23
|
+
def after(path, document)
|
24
|
+
File.open(path, 'w') do |file|
|
25
|
+
document.write file
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Rix
|
2
|
+
module Commands
|
3
|
+
class Show < Command
|
4
|
+
def initialize
|
5
|
+
@formatter = REXML::Formatters::Pretty.new
|
6
|
+
@formatter.compact = true
|
7
|
+
end
|
8
|
+
|
9
|
+
def help
|
10
|
+
"Usage: rix show <xpath> <files>"
|
11
|
+
end
|
12
|
+
|
13
|
+
def on_node(node)
|
14
|
+
@formatter.write(node, $stdout)
|
15
|
+
puts
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/rix/registry.rb
CHANGED
data/lib/rix/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -18,10 +18,14 @@ executables:
|
|
18
18
|
extensions: []
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
|
-
- README
|
22
21
|
- bin/rix
|
23
22
|
- lib/rix/command.rb
|
23
|
+
- lib/rix/commands/add_element.rb
|
24
|
+
- lib/rix/commands/count.rb
|
24
25
|
- lib/rix/commands/help.rb
|
26
|
+
- lib/rix/commands/remove.rb
|
27
|
+
- lib/rix/commands/rename.rb
|
28
|
+
- lib/rix/commands/show.rb
|
25
29
|
- lib/rix/commands/unknown.rb
|
26
30
|
- lib/rix/help.rb
|
27
31
|
- lib/rix/registry.rb
|
data/README
DELETED
File without changes
|