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.1'

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 (private)

create a file or a new tag.



87
88
89
90
91
92
93
94
95
96
# File 'lib/android-xml/main.rb', line 87

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/android-xml/main.rb', line 52

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



22
23
24
# File 'lib/android-xml/main.rb', line 22

def file(filename, &block)
  XmlFile.new(filename, &block)
end

.filesObject



26
27
28
# File 'lib/android-xml/main.rb', line 26

def files
  @files ||= []
end

.generatedObject



30
31
32
# File 'lib/android-xml/main.rb', line 30

def generated
  @generated ||= []
end

.method_missing(method_name, filename_or_attrs = nil, &block) ⇒ Object

create a file or a new tag.



87
88
89
90
91
92
93
94
95
96
# File 'lib/android-xml/main.rb', line 87

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

Returns:

  • (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_allObject



45
46
47
48
49
50
# File 'lib/android-xml/main.rb', line 45

def output_all
  AndroidXml.files.each do |xml_file|
    xml_file.out
    puts
  end
end

.resetObject



73
74
75
76
77
78
79
# File 'lib/android-xml/main.rb', line 73

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
# 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')
    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



69
70
71
# File 'lib/android-xml/main.rb', line 69

def setup(&block)
  Setup.setup(&block)
end

.setup_defaultsObject



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
    # assign the xmlns to all root nodes
    root do
      defaults 'xmlns:android' => 'http://schemas.android.com/apk/res/android'
    end

    all do
      # suppress 'android:' prefix to all style attributes
      rename :style
      rename context: 'tools:context'
    end

    # disable the xmlns attribute on the resource node
    tag :resources do
      defaults 'xmlns:android' => nil
    end

    # remove the 'android:' prefix
    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

    # creates a couple "tag shortcuts"
    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



81
82
83
84
# File 'lib/android-xml/main.rb', line 81

def tag(tag_name, attrs=nil, &block)
  attrs ||= {}
  Tag.new(tag_name, attrs, &block)
end

.write_all(opts = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/android-xml/main.rb', line 34

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