android-xml 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +45 -3
- data/lib/android-xml.rb +2 -0
- data/lib/android-xml/defaults.rb +2 -1
- data/lib/android-xml/exts.rb +15 -0
- data/lib/android-xml/file.rb +16 -5
- data/lib/android-xml/main.rb +37 -81
- data/lib/android-xml/setup.rb +90 -0
- data/lib/android-xml/string.rb +13 -0
- data/lib/android-xml/tag.rb +30 -25
- data/lib/android-xml/version.rb +1 -1
- data/spec/android_manifest_spec.rb +1 -3
- data/spec/android_menu_spec.rb +2 -4
- data/spec/android_resources_spec.rb +3 -6
- data/spec/clone_tag_spec.rb +75 -0
- data/spec/create_tag_spec.rb +29 -0
- data/spec/{generate_factory_spec.rb → factory_spec.rb} +2 -2
- data/spec/generate_spec.rb +11 -15
- data/spec/inc_tag_spec.rb +49 -0
- data/spec/write_to_file_spec.rb +73 -0
- metadata +15 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aa0f42ed58287d9bdc46a69123836185ae4f3970
|
4
|
+
data.tar.gz: 92c2daff63d6f3880fcd0ebd5b071d1854a676bd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b869769e94809366a4d5e677e91b256731cd2cf1b6f7a7aa7cc65a9b854670a897f963d0a5c5e773d3eabf18ca0c1ca3c13cfe3f417c6ca7bd5435ba4a60139b
|
7
|
+
data.tar.gz: 5635c3ee0a5229a582adc3c4f78dcd79cab3e4d1a9b3371b220dd706c4e5182d7a4b9009d94d1e4212defe401488d31c6d5269a757ccabe990f5753f75f6d4b6
|
data/README.md
CHANGED
@@ -16,14 +16,14 @@ AndroidXml.file('res/values/strings.xml') do
|
|
16
16
|
end
|
17
17
|
|
18
18
|
AndroidXml.write_all
|
19
|
+
AndroidXml.clean_up 'res/'
|
19
20
|
```
|
20
21
|
|
21
22
|
> ruby generate_xml.rb
|
22
23
|
✓ res/values/strings.xml
|
23
24
|
|
24
25
|
> cat res/values/strings.xml
|
25
|
-
|
26
|
-
<!-- Do not edit this file. It was generated by AndroidXml -->
|
26
|
+
<!-- Do not edit this file. It was generated by AndroidXml. -->
|
27
27
|
<resources>
|
28
28
|
<string name="app_name">AppAppApp</string>
|
29
29
|
</resources>
|
@@ -76,7 +76,11 @@ AndroidXml.resource('res/values/strings.xml') do
|
|
76
76
|
string(name: 'app_name') { 'AppAppApp' }
|
77
77
|
end
|
78
78
|
|
79
|
+
# `clean_up` finds files that were generated by AndroidXml and removes them.
|
80
|
+
# Files created by `write_all` are not removed. Accepts a string or array of
|
81
|
+
# strings.
|
79
82
|
AndroidXml.write_all
|
83
|
+
AndroidXml.clean_up 'res/'
|
80
84
|
```
|
81
85
|
|
82
86
|
# Helpers
|
@@ -118,7 +122,44 @@ AndroidXml.setup do
|
|
118
122
|
end
|
119
123
|
```
|
120
124
|
|
121
|
-
#
|
125
|
+
# Re-using tags
|
126
|
+
|
127
|
+
When building a layout with variations for landscape/portrait/size, you will
|
128
|
+
often have big chunks that are reusable. Let's say you have a button that is
|
129
|
+
in a FrameLayout, and centered at the bottom:
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
close_button = AndroidXml.Button(
|
133
|
+
layout_width: 'wrap_content',
|
134
|
+
layout_height: 'wrap_content',
|
135
|
+
id: '@+id/cheat_button',
|
136
|
+
layout_gravity: 'bottom|center',
|
137
|
+
text: '@string/cheat_button'
|
138
|
+
)
|
139
|
+
|
140
|
+
AndroidXml.file('res/layout/some_activity.xml') do
|
141
|
+
RelativeLayout(...) do
|
142
|
+
# ...
|
143
|
+
include close_button
|
144
|
+
end
|
145
|
+
end
|
146
|
+
```
|
147
|
+
|
148
|
+
Note: so far, this could also be accomplished using a [Helper](#helpers):
|
149
|
+
|
150
|
+
You can `clone` a tag and make changes to it:
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
AndroidXml.file('res/layout-land/some_activity.xml') do
|
154
|
+
RelativeLayout(...) do
|
155
|
+
# ...
|
156
|
+
|
157
|
+
include close_button(padding: '')
|
158
|
+
end
|
159
|
+
end
|
160
|
+
```
|
161
|
+
|
162
|
+
# Rakefile to build the XML files
|
122
163
|
|
123
164
|
```ruby
|
124
165
|
require 'rake'
|
@@ -136,6 +177,7 @@ task :generate do
|
|
136
177
|
end
|
137
178
|
|
138
179
|
AndroidXml.write_all
|
180
|
+
AndroidXml.clean_up 'res/'
|
139
181
|
end
|
140
182
|
```
|
141
183
|
|
data/lib/android-xml.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require_relative './android-xml/main'
|
2
|
+
require_relative './android-xml/setup'
|
2
3
|
require_relative './android-xml/tag'
|
3
4
|
require_relative './android-xml/file'
|
4
5
|
require_relative './android-xml/defaults'
|
6
|
+
require_relative './android-xml/exts'
|
5
7
|
|
6
8
|
AndroidXml.setup_defaults
|
data/lib/android-xml/defaults.rb
CHANGED
@@ -14,6 +14,7 @@ module AndroidXml
|
|
14
14
|
all do
|
15
15
|
# suppress 'android:' prefix to all style attributes
|
16
16
|
rename :style
|
17
|
+
rename context: 'tools:context'
|
17
18
|
end
|
18
19
|
|
19
20
|
# disable the xmlns attribute on the resource node
|
@@ -22,7 +23,7 @@ module AndroidXml
|
|
22
23
|
end
|
23
24
|
|
24
25
|
# remove the 'android:' prefix
|
25
|
-
tag :string, :style, :item, :color do
|
26
|
+
tag :string, :style, :item, :color, :array do
|
26
27
|
rename :name
|
27
28
|
end
|
28
29
|
tag :style do
|
data/lib/android-xml/file.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
|
1
4
|
module AndroidXml
|
2
5
|
|
3
6
|
class XmlFile < Tag
|
@@ -10,8 +13,10 @@ module AndroidXml
|
|
10
13
|
AndroidXml.files << self
|
11
14
|
end
|
12
15
|
|
13
|
-
def method_missing(method_name,
|
14
|
-
|
16
|
+
def method_missing(method_name, attrs={}, &block)
|
17
|
+
if @root
|
18
|
+
raise "There can be only one (new: #{method_name}, old: #{@root})"
|
19
|
+
end
|
15
20
|
|
16
21
|
@root = super
|
17
22
|
@root.is_root = true
|
@@ -19,8 +24,7 @@ module AndroidXml
|
|
19
24
|
end
|
20
25
|
|
21
26
|
def generate
|
22
|
-
output = "
|
23
|
-
output << "<!-- Do not edit this file. It was generated by AndroidXml -->\n"
|
27
|
+
output = "<!-- Do not edit this file. It was generated by AndroidXml. -->\n"
|
24
28
|
output << generate_block
|
25
29
|
end
|
26
30
|
|
@@ -31,10 +35,17 @@ module AndroidXml
|
|
31
35
|
end
|
32
36
|
|
33
37
|
def write
|
38
|
+
dirname = File.dirname(self.filename)
|
39
|
+
|
40
|
+
unless File.exists?(dirname)
|
41
|
+
warn "\033[33m⚑\033[0m mkdir -p #{dirname}"
|
42
|
+
FileUtils.mkdir_p(dirname)
|
43
|
+
end
|
44
|
+
|
34
45
|
File.open(self.filename, 'w') do |f|
|
35
46
|
f.write(self.to_s)
|
36
47
|
end
|
37
|
-
|
48
|
+
warn "\033[32m✓\033[0m created #{self.filename}"
|
38
49
|
end
|
39
50
|
|
40
51
|
end
|
data/lib/android-xml/main.rb
CHANGED
@@ -1,13 +1,6 @@
|
|
1
1
|
module AndroidXml
|
2
|
-
ROOT = '__root_node_with_a_long_special_name__'
|
3
|
-
ALL = '__all_node_with_a_long_special_name__'
|
4
|
-
|
5
2
|
module_function
|
6
3
|
|
7
|
-
def tab=(value)
|
8
|
-
@tab = value
|
9
|
-
end
|
10
|
-
|
11
4
|
def file(filename, &block)
|
12
5
|
XmlFile.new(filename, &block)
|
13
6
|
end
|
@@ -16,8 +9,15 @@ module AndroidXml
|
|
16
9
|
@files ||= []
|
17
10
|
end
|
18
11
|
|
19
|
-
def
|
20
|
-
|
12
|
+
def generated
|
13
|
+
@generated ||= []
|
14
|
+
end
|
15
|
+
|
16
|
+
def write_all(opts={})
|
17
|
+
clean_up = opts.fetch(:clean_up, false)
|
18
|
+
|
19
|
+
AndroidXml.files.each do |xml_file|
|
20
|
+
generated << File.absolute_path(xml_file.filename)
|
21
21
|
xml_file.write
|
22
22
|
end
|
23
23
|
|
@@ -25,98 +25,54 @@ module AndroidXml
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def output_all
|
28
|
-
files.each do |xml_file|
|
28
|
+
AndroidXml.files.each do |xml_file|
|
29
29
|
xml_file.out
|
30
30
|
puts
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
def
|
35
|
-
|
36
|
-
|
37
|
-
|
34
|
+
def clean_up(locations)
|
35
|
+
locations = [locations] unless locations.is_a?(Array)
|
36
|
+
|
37
|
+
locations.each do |location|
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
39
|
+
Dir.glob(File.join(location, '**/*.xml')).each do |filename|
|
40
|
+
filename = File.absolute_path(filename)
|
41
|
+
unless generated.include?(filename)
|
42
|
+
if File.new(filename).read =~ /generated by AndroidXml/
|
43
|
+
warn "\033[31m␡\033[0m #{filename}"
|
44
|
+
File.delete(filename)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
46
48
|
end
|
47
49
|
end
|
48
50
|
|
49
|
-
def
|
50
|
-
|
51
|
+
def setup(&block)
|
52
|
+
Setup.setup(&block)
|
51
53
|
end
|
52
54
|
|
53
55
|
def reset
|
54
|
-
|
55
|
-
@tags = nil
|
56
|
+
Setup.reset
|
56
57
|
@files = nil
|
58
|
+
@generated = nil
|
57
59
|
end
|
58
60
|
|
59
|
-
def
|
60
|
-
|
61
|
+
def tag(tag_name, attrs=nil, &block)
|
62
|
+
attrs ||= {}
|
63
|
+
Tag.new(tag_name, attrs, &block)
|
61
64
|
end
|
62
65
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
tag(ROOT, &block)
|
69
|
-
end
|
70
|
-
|
71
|
-
def all_tag
|
72
|
-
tags[ALL]
|
73
|
-
end
|
74
|
-
|
75
|
-
def all(&block)
|
76
|
-
tag(ALL, &block)
|
77
|
-
end
|
78
|
-
|
79
|
-
def tag(*names, &block)
|
80
|
-
context_was = @context
|
81
|
-
|
82
|
-
names.each do |name|
|
83
|
-
if name.is_a?(Hash)
|
84
|
-
@context = nil
|
85
|
-
name.each do |shortcut, tag_name|
|
86
|
-
raise "There can be only one key-value pair" if @context
|
87
|
-
|
88
|
-
@context = shortcut.to_s
|
89
|
-
tags[@context][:rename] = tag_name.to_s
|
90
|
-
end
|
91
|
-
else
|
92
|
-
@context = name.to_s
|
93
|
-
end
|
94
|
-
|
95
|
-
instance_exec(&block)
|
96
|
-
end
|
97
|
-
|
98
|
-
@context = context_was
|
99
|
-
end
|
100
|
-
|
101
|
-
def rename(attrs)
|
102
|
-
if attrs.is_a?(Hash)
|
103
|
-
attrs.each do |attr_name, attr_rename|
|
104
|
-
tags[@context][:attrs][attr_name.to_s] = attr_rename.to_s
|
66
|
+
# create a file or a new tag.
|
67
|
+
def method_missing(method_name, filename_or_attrs=nil, &block)
|
68
|
+
if filename_or_attrs.is_a?(String) || filename_or_attrs.is_a?(Symbol)
|
69
|
+
xml_file = file(filename) do
|
70
|
+
send(method_name, &block)
|
105
71
|
end
|
72
|
+
xml_file
|
106
73
|
else
|
107
|
-
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
def defaults(attrs)
|
112
|
-
tags[@context][:defaults].merge!(attrs)
|
113
|
-
end
|
114
|
-
|
115
|
-
def method_missing(method_name, filename, &block)
|
116
|
-
xml_file = file(filename) do
|
117
|
-
send(method_name, &block)
|
74
|
+
tag(method_name, filename_or_attrs, &block)
|
118
75
|
end
|
119
|
-
xml_file
|
120
76
|
end
|
121
77
|
|
122
78
|
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module AndroidXml
|
2
|
+
|
3
|
+
class Setup
|
4
|
+
class << self
|
5
|
+
ROOT = '__root_node_with_a_long_special_name__'
|
6
|
+
ALL = '__all_node_with_a_long_special_name__'
|
7
|
+
|
8
|
+
def setup(&block)
|
9
|
+
instance_exec(&block)
|
10
|
+
end
|
11
|
+
|
12
|
+
def tabs(value)
|
13
|
+
@tab = value
|
14
|
+
end
|
15
|
+
|
16
|
+
def tags
|
17
|
+
@tags ||= Hash.new do |hash, key|
|
18
|
+
hash[key] = {
|
19
|
+
attrs: {},
|
20
|
+
defaults: {},
|
21
|
+
rename: nil,
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def tab
|
27
|
+
@tab ||= ' '
|
28
|
+
end
|
29
|
+
|
30
|
+
def reset
|
31
|
+
@tab = nil
|
32
|
+
@tags = nil
|
33
|
+
end
|
34
|
+
|
35
|
+
def root_tag
|
36
|
+
tags[ROOT]
|
37
|
+
end
|
38
|
+
|
39
|
+
def root(&block)
|
40
|
+
tag(ROOT, &block)
|
41
|
+
end
|
42
|
+
|
43
|
+
def all_tag
|
44
|
+
tags[ALL]
|
45
|
+
end
|
46
|
+
|
47
|
+
def all(&block)
|
48
|
+
tag(ALL, &block)
|
49
|
+
end
|
50
|
+
|
51
|
+
def tag(*names, &block)
|
52
|
+
context_was = @context
|
53
|
+
|
54
|
+
names.each do |name|
|
55
|
+
if name.is_a?(Hash)
|
56
|
+
@context = nil
|
57
|
+
name.each do |shortcut, tag_name|
|
58
|
+
raise "There can be only one key-value pair" if @context
|
59
|
+
|
60
|
+
@context = shortcut.to_s
|
61
|
+
tags[@context][:rename] = tag_name.to_s
|
62
|
+
end
|
63
|
+
else
|
64
|
+
@context = name.to_s
|
65
|
+
end
|
66
|
+
|
67
|
+
instance_exec(&block)
|
68
|
+
end
|
69
|
+
|
70
|
+
@context = context_was
|
71
|
+
end
|
72
|
+
|
73
|
+
def rename(attrs)
|
74
|
+
if attrs.is_a?(Hash)
|
75
|
+
attrs.each do |attr_name, attr_rename|
|
76
|
+
tags[@context][:attrs][attr_name.to_s] = attr_rename.to_s
|
77
|
+
end
|
78
|
+
else
|
79
|
+
tags[@context][:attrs][attrs.to_s] = attrs.to_s
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def defaults(attrs)
|
84
|
+
tags[@context][:defaults].merge!(attrs)
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
data/lib/android-xml/tag.rb
CHANGED
@@ -3,46 +3,51 @@ module AndroidXml
|
|
3
3
|
class Tag
|
4
4
|
attr_accessor :is_root
|
5
5
|
|
6
|
-
def initialize(tag,
|
6
|
+
def initialize(tag, attrs={}, &block)
|
7
7
|
@buffer = []
|
8
|
-
@attrs = {}
|
8
|
+
@attrs = {}.merge(attrs)
|
9
9
|
@raw_tag = tag.to_s
|
10
|
-
@text = nil
|
11
10
|
|
12
|
-
if rename =
|
11
|
+
if rename = Setup.tags[tag.to_s][:rename]
|
13
12
|
@tag = rename
|
14
13
|
else
|
15
14
|
@tag = tag.to_s.gsub('_', '-')
|
16
15
|
end
|
17
16
|
|
18
|
-
args.each do |arg|
|
19
|
-
if arg.is_a?(Hash)
|
20
|
-
@attrs.merge!(arg)
|
21
|
-
elsif arg.is_a?(String)
|
22
|
-
@text = arg
|
23
|
-
else
|
24
|
-
raise ArgumentError.new("Unknown argument #{arg.inspect} in #{self.class}#new")
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
17
|
@generate = block
|
29
18
|
end
|
30
19
|
|
31
|
-
def method_missing(method_name,
|
32
|
-
tag = Tag.new(method_name,
|
33
|
-
|
20
|
+
def method_missing(method_name, attrs={}, &block)
|
21
|
+
tag = Tag.new(method_name, attrs, &block)
|
22
|
+
include tag
|
34
23
|
tag
|
35
24
|
end
|
36
25
|
|
26
|
+
def clone(attrs={}, &block)
|
27
|
+
block ||= @generate
|
28
|
+
Tag.new(@tag, @attrs.merge(attrs), &block)
|
29
|
+
end
|
30
|
+
|
31
|
+
def include(tag, &block)
|
32
|
+
if tag.is_a?(Tag)
|
33
|
+
if block_given?
|
34
|
+
tag = tag.clone(&block)
|
35
|
+
end
|
36
|
+
@buffer << tag
|
37
|
+
else
|
38
|
+
super
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
37
42
|
def attrs(whitespace)
|
38
43
|
attrs = {}
|
39
44
|
|
40
|
-
attrs.merge!(
|
45
|
+
attrs.merge!(Setup.all_tag[:defaults])
|
41
46
|
if is_root
|
42
|
-
attrs.merge!(
|
47
|
+
attrs.merge!(Setup.root_tag[:defaults])
|
43
48
|
end
|
44
49
|
|
45
|
-
attrs.merge!(
|
50
|
+
attrs.merge!(Setup.tags[@raw_tag][:defaults])
|
46
51
|
attrs.merge!(@attrs)
|
47
52
|
|
48
53
|
output = ''
|
@@ -52,10 +57,10 @@ module AndroidXml
|
|
52
57
|
|
53
58
|
key = key.to_s
|
54
59
|
|
55
|
-
if
|
56
|
-
xml_key =
|
57
|
-
elsif
|
58
|
-
xml_key =
|
60
|
+
if Setup.tags[@tag][:attrs].key?(key)
|
61
|
+
xml_key = Setup.tags[@tag][:attrs][key]
|
62
|
+
elsif Setup.all_tag[:attrs].key?(key)
|
63
|
+
xml_key = Setup.all_tag[:attrs][key]
|
59
64
|
elsif key.to_s.include?(':')
|
60
65
|
xml_key = key.to_s
|
61
66
|
else
|
@@ -76,7 +81,7 @@ module AndroidXml
|
|
76
81
|
whitespace = "#{tab} #{' ' * @tag.length}"
|
77
82
|
output = "#{tab}<#{@tag}#{attrs(whitespace)}"
|
78
83
|
if @generate
|
79
|
-
inside = generate_block(tab +
|
84
|
+
inside = generate_block(tab + Setup.tab)
|
80
85
|
if !inside || inside.strip.empty?
|
81
86
|
output << " />\n"
|
82
87
|
else
|
data/lib/android-xml/version.rb
CHANGED
@@ -27,8 +27,7 @@ describe 'AndroidManifest.xml' do
|
|
27
27
|
end
|
28
28
|
|
29
29
|
expect(xml.to_s).to eql <<-XML
|
30
|
-
|
31
|
-
<!-- Do not edit this file. It was generated by AndroidXml -->
|
30
|
+
<!-- Do not edit this file. It was generated by AndroidXml. -->
|
32
31
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
33
32
|
package="com.your_name_here.AnyAndroidApp"
|
34
33
|
android:versionCode="1"
|
@@ -49,7 +48,6 @@ describe 'AndroidManifest.xml' do
|
|
49
48
|
</application>
|
50
49
|
</manifest>
|
51
50
|
XML
|
52
|
-
|
53
51
|
end
|
54
52
|
|
55
53
|
end
|
data/spec/android_menu_spec.rb
CHANGED
@@ -23,8 +23,7 @@ describe 'Android menu files' do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
expect(xml.to_s).to eql <<-XML
|
26
|
-
|
27
|
-
<!-- Do not edit this file. It was generated by AndroidXml -->
|
26
|
+
<!-- Do not edit this file. It was generated by AndroidXml. -->
|
28
27
|
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
29
28
|
<item android:id="@+id/action_search"
|
30
29
|
android:icon="@drawable/ic_action_search"
|
@@ -52,8 +51,7 @@ XML
|
|
52
51
|
end
|
53
52
|
|
54
53
|
expect(xml.to_s).to eql <<-XML
|
55
|
-
|
56
|
-
<!-- Do not edit this file. It was generated by AndroidXml -->
|
54
|
+
<!-- Do not edit this file. It was generated by AndroidXml. -->
|
57
55
|
<resources>
|
58
56
|
<string name="app_name">Harroo</string>
|
59
57
|
<string name="hello">Well, Harroo!</string>
|
@@ -21,8 +21,7 @@ describe 'Android resource files' do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
expect(xml.to_s).to eql <<-XML
|
24
|
-
|
25
|
-
<!-- Do not edit this file. It was generated by AndroidXml -->
|
24
|
+
<!-- Do not edit this file. It was generated by AndroidXml. -->
|
26
25
|
<resources>
|
27
26
|
<style name="CodeFont"
|
28
27
|
parent="@android:style/TextAppearance.Medium">
|
@@ -50,8 +49,7 @@ XML
|
|
50
49
|
end
|
51
50
|
|
52
51
|
expect(xml.to_s).to eql <<-XML
|
53
|
-
|
54
|
-
<!-- Do not edit this file. It was generated by AndroidXml -->
|
52
|
+
<!-- Do not edit this file. It was generated by AndroidXml. -->
|
55
53
|
<resources>
|
56
54
|
<string name="app_name">Harroo</string>
|
57
55
|
<string name="hello">Well, Harroo!</string>
|
@@ -73,8 +71,7 @@ XML
|
|
73
71
|
end
|
74
72
|
|
75
73
|
expect(xml.to_s).to eql <<-XML
|
76
|
-
|
77
|
-
<!-- Do not edit this file. It was generated by AndroidXml -->
|
74
|
+
<!-- Do not edit this file. It was generated by AndroidXml. -->
|
78
75
|
<resources>
|
79
76
|
<color name="foo">#f00</color>
|
80
77
|
</resources>
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'android-xml'
|
2
|
+
|
3
|
+
|
4
|
+
describe 'Cloning a tag' do
|
5
|
+
|
6
|
+
before do
|
7
|
+
AndroidXml.reset
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should be able to clone a tag' do
|
11
|
+
tag = AndroidXml.any_tag(id: '@+id/any_id', text: 'tag-text') do
|
12
|
+
nested tag: 'also works'
|
13
|
+
nested tags: 'also work' do
|
14
|
+
yup they: 'work'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
clone = tag.clone
|
19
|
+
|
20
|
+
expect(clone.to_s).to eql(<<-XML
|
21
|
+
<any-tag android:id="@+id/any_id"
|
22
|
+
android:text="tag-text">
|
23
|
+
<nested android:tag="also works" />
|
24
|
+
<nested android:tags="also work">
|
25
|
+
<yup android:they="work" />
|
26
|
+
</nested>
|
27
|
+
</any-tag>
|
28
|
+
XML
|
29
|
+
)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should be able to clone a tag and replace attrs' do
|
33
|
+
tag = AndroidXml.any_tag(id: '@+id/any_id', text: 'tag-text') do
|
34
|
+
nested tag: 'also works'
|
35
|
+
nested tags: 'also work' do
|
36
|
+
yup they: 'work'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
clone = tag.clone(text: 'clone-text')
|
41
|
+
|
42
|
+
expect(clone.to_s).to eql(<<-XML
|
43
|
+
<any-tag android:id="@+id/any_id"
|
44
|
+
android:text="clone-text">
|
45
|
+
<nested android:tag="also works" />
|
46
|
+
<nested android:tags="also work">
|
47
|
+
<yup android:they="work" />
|
48
|
+
</nested>
|
49
|
+
</any-tag>
|
50
|
+
XML
|
51
|
+
)
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should be able to clone a tag and replace content' do
|
55
|
+
tag = AndroidXml.any_tag(id: '@+id/any_id', text: 'tag-text') do
|
56
|
+
nested tag: 'also works'
|
57
|
+
nested tags: 'also work' do
|
58
|
+
yup they: 'work'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
clone = tag.clone(text: 'clone-text') do
|
63
|
+
cloned tag: 'also works'
|
64
|
+
end
|
65
|
+
|
66
|
+
expect(clone.to_s).to eql(<<-XML
|
67
|
+
<any-tag android:id="@+id/any_id"
|
68
|
+
android:text="clone-text">
|
69
|
+
<cloned android:tag="also works" />
|
70
|
+
</any-tag>
|
71
|
+
XML
|
72
|
+
)
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'android-xml'
|
2
|
+
|
3
|
+
|
4
|
+
describe 'Creating a tag with AndroidXml.any_tag' do
|
5
|
+
|
6
|
+
before do
|
7
|
+
AndroidXml.reset
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should create a tag if no filename is given' do
|
11
|
+
tag = AndroidXml.any_tag(id: '@+id/any_id') do
|
12
|
+
nested tag: 'also works'
|
13
|
+
nested tags: 'also work' do
|
14
|
+
yup they: 'work'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
expect(tag.to_s).to eql(<<-XML
|
19
|
+
<any-tag android:id="@+id/any_id">
|
20
|
+
<nested android:tag="also works" />
|
21
|
+
<nested android:tags="also work">
|
22
|
+
<yup android:they="work" />
|
23
|
+
</nested>
|
24
|
+
</any-tag>
|
25
|
+
XML
|
26
|
+
)
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'android-xml'
|
2
2
|
|
3
|
+
|
3
4
|
describe AndroidXml do
|
4
5
|
|
5
6
|
before do
|
@@ -16,8 +17,7 @@ describe AndroidXml do
|
|
16
17
|
end
|
17
18
|
|
18
19
|
expect(xml.to_s).to eql <<-XML
|
19
|
-
|
20
|
-
<!-- Do not edit this file. It was generated by AndroidXml -->
|
20
|
+
<!-- Do not edit this file. It was generated by AndroidXml. -->
|
21
21
|
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
22
22
|
<item android:id="@+id/action_search"
|
23
23
|
android:icon="@drawable/ic_action_search"
|
data/spec/generate_spec.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'android-xml'
|
2
2
|
|
3
|
+
|
3
4
|
describe AndroidXml do
|
4
5
|
|
5
6
|
before do
|
@@ -12,8 +13,7 @@ describe AndroidXml do
|
|
12
13
|
end
|
13
14
|
|
14
15
|
expect(xml.to_s).to eql <<-XML
|
15
|
-
|
16
|
-
<!-- Do not edit this file. It was generated by AndroidXml -->
|
16
|
+
<!-- Do not edit this file. It was generated by AndroidXml. -->
|
17
17
|
<any-tag />
|
18
18
|
XML
|
19
19
|
end
|
@@ -24,8 +24,7 @@ XML
|
|
24
24
|
end
|
25
25
|
|
26
26
|
expect(xml.to_s).to eql <<-XML
|
27
|
-
|
28
|
-
<!-- Do not edit this file. It was generated by AndroidXml -->
|
27
|
+
<!-- Do not edit this file. It was generated by AndroidXml. -->
|
29
28
|
<any-tag android:with="properties" />
|
30
29
|
XML
|
31
30
|
end
|
@@ -36,8 +35,7 @@ XML
|
|
36
35
|
end
|
37
36
|
|
38
37
|
expect(xml.to_s).to eql <<-XML
|
39
|
-
|
40
|
-
<!-- Do not edit this file. It was generated by AndroidXml -->
|
38
|
+
<!-- Do not edit this file. It was generated by AndroidXml. -->
|
41
39
|
<any-tag>with text</any-tag>
|
42
40
|
XML
|
43
41
|
end
|
@@ -52,8 +50,7 @@ XML
|
|
52
50
|
end
|
53
51
|
|
54
52
|
expect(xml.to_s).to eql <<-XML
|
55
|
-
|
56
|
-
<!-- Do not edit this file. It was generated by AndroidXml -->
|
53
|
+
<!-- Do not edit this file. It was generated by AndroidXml. -->
|
57
54
|
<nested>
|
58
55
|
<tags />
|
59
56
|
<tags />
|
@@ -70,11 +67,12 @@ XML
|
|
70
67
|
tags
|
71
68
|
end
|
72
69
|
end
|
73
|
-
AndroidXml.
|
70
|
+
AndroidXml.setup do
|
71
|
+
tabs ' '
|
72
|
+
end
|
74
73
|
|
75
74
|
expect(xml.to_s).to eql <<-XML
|
76
|
-
|
77
|
-
<!-- Do not edit this file. It was generated by AndroidXml -->
|
75
|
+
<!-- Do not edit this file. It was generated by AndroidXml. -->
|
78
76
|
<nested>
|
79
77
|
<tags />
|
80
78
|
<tags />
|
@@ -93,8 +91,7 @@ XML
|
|
93
91
|
end
|
94
92
|
|
95
93
|
expect(xml.to_s).to eql <<-XML
|
96
|
-
|
97
|
-
<!-- Do not edit this file. It was generated by AndroidXml -->
|
94
|
+
<!-- Do not edit this file. It was generated by AndroidXml. -->
|
98
95
|
<nested>
|
99
96
|
<tags />
|
100
97
|
<tags />
|
@@ -111,8 +108,7 @@ XML
|
|
111
108
|
end
|
112
109
|
|
113
110
|
expect(xml.to_s).to eql <<-XML
|
114
|
-
|
115
|
-
<!-- Do not edit this file. It was generated by AndroidXml -->
|
111
|
+
<!-- Do not edit this file. It was generated by AndroidXml. -->
|
116
112
|
<nested android:with=""
|
117
113
|
android:many="properties">
|
118
114
|
<tags android:with=""
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'android-xml'
|
2
|
+
|
3
|
+
|
4
|
+
describe 'Including a tag' do
|
5
|
+
|
6
|
+
before do
|
7
|
+
AndroidXml.reset
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should be able to include a tag' do
|
11
|
+
tag = AndroidXml.any_tag(id: '@+id/any_id')
|
12
|
+
file = AndroidXml.file('tmp/test.xml') do
|
13
|
+
resources do
|
14
|
+
include tag
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
expect(file.generate).to eql(<<-XML
|
19
|
+
<!-- Do not edit this file. It was generated by AndroidXml. -->
|
20
|
+
<resources>
|
21
|
+
<any-tag android:id="@+id/any_id" />
|
22
|
+
</resources>
|
23
|
+
XML
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should be able to include a cloned tag' do
|
28
|
+
tag = AndroidXml.any_tag(id: '@+id/any_id')
|
29
|
+
file = AndroidXml.file('tmp/test.xml') do
|
30
|
+
resources do
|
31
|
+
include tag.clone(text: 'cloned text') do
|
32
|
+
another_tag works: 'fine'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
expect(file.generate).to eql(<<-XML
|
38
|
+
<!-- Do not edit this file. It was generated by AndroidXml. -->
|
39
|
+
<resources>
|
40
|
+
<any-tag android:id="@+id/any_id"
|
41
|
+
android:text="cloned text">
|
42
|
+
<another-tag android:works="fine" />
|
43
|
+
</any-tag>
|
44
|
+
</resources>
|
45
|
+
XML
|
46
|
+
)
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'android-xml'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
|
5
|
+
describe 'Writing to a file' do
|
6
|
+
|
7
|
+
before do
|
8
|
+
AndroidXml.reset
|
9
|
+
$stderr.reopen('/dev/null', 'w')
|
10
|
+
end
|
11
|
+
|
12
|
+
after do
|
13
|
+
warn "\033[31m␡\033[0m tmp/"
|
14
|
+
FileUtils.remove_dir('tmp/')
|
15
|
+
$stderr = STDERR
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should write an xml file' do
|
19
|
+
filename = 'tmp/file1.xml'
|
20
|
+
xml = AndroidXml.file(filename) do
|
21
|
+
any_tag
|
22
|
+
end
|
23
|
+
xml.write
|
24
|
+
|
25
|
+
expect(File.exists?(filename)).to be true
|
26
|
+
|
27
|
+
expect(File.new(filename).read).to eql <<-XML
|
28
|
+
<!-- Do not edit this file. It was generated by AndroidXml. -->
|
29
|
+
<any-tag />
|
30
|
+
XML
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should create a folder if necessary' do
|
34
|
+
filename = 'tmp/a/b/c/file2.xml'
|
35
|
+
xml = AndroidXml.file(filename) do
|
36
|
+
another_tag
|
37
|
+
end
|
38
|
+
xml.write
|
39
|
+
|
40
|
+
expect(File.exists?(filename)).to be true
|
41
|
+
|
42
|
+
expect(File.new(filename).read).to eql <<-XML
|
43
|
+
<!-- Do not edit this file. It was generated by AndroidXml. -->
|
44
|
+
<another-tag />
|
45
|
+
XML
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should remove stale files' do
|
49
|
+
stale_file = 'tmp/files/stale.xml'
|
50
|
+
FileUtils.mkdir_p(File.dirname(stale_file))
|
51
|
+
File.open(stale_file, 'w') do |f|
|
52
|
+
f.write("<!-- Do not edit this file. It was generated by AndroidXml. -->\n")
|
53
|
+
end
|
54
|
+
|
55
|
+
not_my_file = 'tmp/files/keep.xml'
|
56
|
+
FileUtils.mkdir_p(File.dirname(not_my_file))
|
57
|
+
File.open(not_my_file, 'w') do |f|
|
58
|
+
f.write("This file is fine!")
|
59
|
+
end
|
60
|
+
|
61
|
+
filename = 'tmp/file3.xml'
|
62
|
+
xml = AndroidXml.file(filename) do
|
63
|
+
another_tag
|
64
|
+
end
|
65
|
+
AndroidXml.write_all
|
66
|
+
AndroidXml.clean_up 'tmp/'
|
67
|
+
|
68
|
+
expect(File.exists?(filename)).to be true
|
69
|
+
expect(File.exists?(stale_file)).to be false
|
70
|
+
expect(File.exists?(not_my_file)).to be true
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: android-xml
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Colin T.A. Gray
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -35,8 +35,11 @@ extensions: []
|
|
35
35
|
extra_rdoc_files: []
|
36
36
|
files:
|
37
37
|
- lib/android-xml/defaults.rb
|
38
|
+
- lib/android-xml/exts.rb
|
38
39
|
- lib/android-xml/file.rb
|
39
40
|
- lib/android-xml/main.rb
|
41
|
+
- lib/android-xml/setup.rb
|
42
|
+
- lib/android-xml/string.rb
|
40
43
|
- lib/android-xml/tag.rb
|
41
44
|
- lib/android-xml/version.rb
|
42
45
|
- lib/android-xml.rb
|
@@ -44,8 +47,12 @@ files:
|
|
44
47
|
- spec/android_manifest_spec.rb
|
45
48
|
- spec/android_menu_spec.rb
|
46
49
|
- spec/android_resources_spec.rb
|
47
|
-
- spec/
|
50
|
+
- spec/clone_tag_spec.rb
|
51
|
+
- spec/create_tag_spec.rb
|
52
|
+
- spec/factory_spec.rb
|
48
53
|
- spec/generate_spec.rb
|
54
|
+
- spec/inc_tag_spec.rb
|
55
|
+
- spec/write_to_file_spec.rb
|
49
56
|
homepage: https://github.com/colinta/android-xml
|
50
57
|
licenses:
|
51
58
|
- BSD
|
@@ -74,5 +81,9 @@ test_files:
|
|
74
81
|
- spec/android_manifest_spec.rb
|
75
82
|
- spec/android_menu_spec.rb
|
76
83
|
- spec/android_resources_spec.rb
|
77
|
-
- spec/
|
84
|
+
- spec/clone_tag_spec.rb
|
85
|
+
- spec/create_tag_spec.rb
|
86
|
+
- spec/factory_spec.rb
|
78
87
|
- spec/generate_spec.rb
|
88
|
+
- spec/inc_tag_spec.rb
|
89
|
+
- spec/write_to_file_spec.rb
|