module AndroidXml

Constants

Version

Public Instance Methods

clean_up(locations) click to toggle source
# 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) click to toggle source
# File lib/android-xml/main.rb, line 22
def file(filename, &block)
  XmlFile.new(filename, &block)
end
files() click to toggle source
# File lib/android-xml/main.rb, line 26
def files
  @files ||= []
end
generated() click to toggle source
# File lib/android-xml/main.rb, line 30
def generated
  @generated ||= []
end
method_missing(method_name, filename_or_attrs=nil, &block) click to toggle source

create a file or a new tag.

# 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?() click to toggle source
# 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() click to toggle source
# File lib/android-xml/main.rb, line 45
def output_all
  AndroidXml.files.each do |xml_file|
    xml_file.out
    puts
  end
end
reset() click to toggle source
# 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={}) click to toggle source
# 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) click to toggle source
# File lib/android-xml/main.rb, line 69
def setup(&block)
  Setup.setup(&block)
end
setup_defaults() click to toggle source
# 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) click to toggle source
# 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={}) click to toggle source
# 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