Class: Fastlane::Actions::PluralConverterAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/plural_converter/actions/plural_converter_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



68
69
70
# File 'lib/fastlane/plugin/plural_converter/actions/plural_converter_action.rb', line 68

def self.authors
  ["Benoit Deldicque"]
end

.available_optionsObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fastlane/plugin/plural_converter/actions/plural_converter_action.rb', line 72

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :plist_path,
                                 env_name: "PLURAL_CONVERTER_PLIST_PATH",
                                 description: "File path for the new Plist file",
                                 optional: false,
                                 type: String),
    FastlaneCore::ConfigItem.new(key: :xml_path,
                                 env_name: "PLURAL_CONVERTER_XML_PATH",
                                 description: "File path for the Android XML source file",
                                 optional: false,
                                 type: String),
    FastlaneCore::ConfigItem.new(key: :exclude_key_prefix,
                                 env_name: "PLURAL_CONVERTER_EXCLUDE_PREFIX",
                                 description: "Prefix used to avoid converting keys",
                                 optional: true,
                                 type: String)
  ]
end

.descriptionObject



64
65
66
# File 'lib/fastlane/plugin/plural_converter/actions/plural_converter_action.rb', line 64

def self.description
  "Convert Android plural XML resource file to iOS stringsdict file."
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/fastlane/plugin/plural_converter/actions/plural_converter_action.rb', line 92

def self.is_supported?(platform)
  true
end

.run(params) ⇒ Object



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fastlane/plugin/plural_converter/actions/plural_converter_action.rb', line 8

def self.run(params)
  require 'xcodeproj'
  require 'fastlane_core/configuration/config_item'

  if params[:plist_path] && params[:xml_path]
    plist_path = params[:plist_path]
    xml_path = params[:xml_path]
    key_prefix = params[:exclude_key_prefix]

    if File.exist?(xml_path)
      # Create and open empty file.
      File.write(plist_path, '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict></dict></plist>')
      plist = Xcodeproj::Plist.read_from_path(plist_path)

      # Read XML and populate PLIST hash.
      xml = File.open(xml_path)
      doc = REXML::Document.new(xml)
      doc.elements.each('resources/plurals') do |plural|
        key = plural.attributes['name']

        # Don't process key with prefix
        if key_prefix.to_s.empty? == false && key.start_with?(key_prefix.to_s)
          UI.verbose("Skipping key: #{key}")
          next
        end

        format_key = "#{key}_value"
        dict = { 'NSStringLocalizedFormatKey' => "%#\@#{format_key}@" }
        items_dict = { 'NSStringFormatSpecTypeKey' => 'NSStringPluralRuleType', 'NSStringFormatValueTypeKey' => 'd' }

        plural.elements.each('item') do |item|
          quantity_key = item.attributes['quantity']
          items_dict[quantity_key] = item.text
        end

        dict[format_key] = items_dict
        plist[key] = dict
      end

      # Write changes to PLIST file
      Xcodeproj::Plist.write_to_path(plist, plist_path)

      UI.success("Updated #{params[:plist_path]} 💾.")
      File.read(plist_path)

    else
      UI.user_error!("Couldn't find xml file at path '#{xml_path}'")
    end

  elsif params[:xml_path].nil?
    UI.user_error!("You must specify an xml path")
  elsif params[:plist_path].nil?
    UI.user_error!("You must specify a plist path")
  end
end