Module: AndroidXml
- Defined in:
- lib/android-xml/tag.rb,
lib/android-xml/file.rb,
lib/android-xml/main.rb,
lib/android-xml/setup.rb,
lib/android-xml/string.rb,
lib/android-xml/version.rb,
lib/android-xml/defaults.rb
Defined Under Namespace
Classes: Setup, Tag, XmlFile
Constant Summary
collapse
- Version =
'1.4.0'
Class Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, filename_or_attrs = nil, &block) ⇒ Object
create a file or a new tag.
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/android-xml/main.rb', line 88
def method_missing(method_name, filename_or_attrs=nil, &block)
if filename_or_attrs.is_a?(String) || filename_or_attrs.is_a?(Symbol)
xml_file = file(filename_or_attrs) do
send(method_name, &block)
end
xml_file
else
tag(method_name, filename_or_attrs, &block)
end
end
|
Class Method Details
.clean_up(locations) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/android-xml/main.rb', line 53
def clean_up(locations)
locations = [locations] unless locations.is_a?(Array)
locations.each do |location|
Dir.glob(File.join(location, '**/*.xml')).each do |filename|
abs_path = File.absolute_path(filename)
unless generated.include?(abs_path)
if File.new(abs_path).read =~ /generated by AndroidXml/
warn "\033[31m␡\033[0m #{filename}"
File.delete(abs_path)
end
end
end
end
end
|
.file(filename, &block) ⇒ Object
23
24
25
|
# File 'lib/android-xml/main.rb', line 23
def file(filename, &block)
XmlFile.new(filename, &block)
end
|
.files ⇒ Object
27
28
29
|
# File 'lib/android-xml/main.rb', line 27
def files
@files ||= []
end
|
.generated ⇒ Object
31
32
33
|
# File 'lib/android-xml/main.rb', line 31
def generated
@generated ||= []
end
|
.method_missing(method_name, filename_or_attrs = nil, &block) ⇒ Object
create a file or a new tag.
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/android-xml/main.rb', line 88
def method_missing(method_name, filename_or_attrs=nil, &block)
if filename_or_attrs.is_a?(String) || filename_or_attrs.is_a?(Symbol)
xml_file = file(filename_or_attrs) do
send(method_name, &block)
end
xml_file
else
tag(method_name, filename_or_attrs, &block)
end
end
|
.missing_strings? ⇒ Boolean
57
58
59
60
61
62
63
64
|
# File 'lib/android-xml/string.rb', line 57
def missing_strings?
diff = Tag.found_strings - Tag.registered_strings
unless diff.empty?
diff.each do |name|
puts " string(name='#{Tag.format_string(name)}') { '#{Tag.format_string(name)}' }"
end
end
end
|
.output_all ⇒ Object
46
47
48
49
50
51
|
# File 'lib/android-xml/main.rb', line 46
def output_all
AndroidXml.files.each do |xml_file|
xml_file.out
puts
end
end
|
.reset ⇒ Object
74
75
76
77
78
79
80
|
# File 'lib/android-xml/main.rb', line 74
def reset
Setup.reset
@files = nil
@generated = nil
Tag.found_strings.clear
Tag.registered_strings.clear
end
|
.run(opts = {}) ⇒ Object
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/android-xml/main.rb', line 4
def run(opts={})
folder = opts[:in]
clean_up = opts.fetch(:clean_up, true)
if folder
xml_location = File.join(Dir.getwd, folder, '**/*.rb')
p xml_location
Dir.glob(xml_location) do |file_name|
require_relative file_name
end
end
AndroidXml.write_all
if clean_up
AndroidXml.clean_up('res/')
end
AndroidXml.missing_strings?
end
|
.setup(&block) ⇒ Object
70
71
72
|
# File 'lib/android-xml/main.rb', line 70
def setup(&block)
Setup.setup(&block)
end
|
.setup_defaults ⇒ Object
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/android-xml/defaults.rb', line 7
def setup_defaults
setup do
root do
defaults 'xmlns:android' => 'http://schemas.android.com/apk/res/android'
end
all do
rename :style
rename context: 'tools:context'
end
tag :resources do
defaults 'xmlns:android' => nil
end
tag :string, :style, :item, :color, :array do
rename :name
end
tag :item do
rename :type
end
tag :style do
rename :parent
end
tag :manifest do
rename :package
end
tag :main_action => 'action' do
defaults name: 'android.intent.action.MAIN'
end
tag :launcher_category => 'category' do
defaults name: 'android.intent.category.LAUNCHER'
end
end
end
|
.tag(tag_name, attrs = nil, &block) ⇒ Object
82
83
84
85
|
# File 'lib/android-xml/main.rb', line 82
def tag(tag_name, attrs=nil, &block)
attrs ||= {}
Tag.new(tag_name, attrs, &block)
end
|
.write_all(opts = {}) ⇒ Object
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/android-xml/main.rb', line 35
def write_all(opts={})
clean_up = opts.fetch(:clean_up, false)
AndroidXml.files.each do |xml_file|
generated << File.absolute_path(xml_file.filename)
xml_file.write
end
@files = nil
end
|