Class: Fastlane::Actions::UpdateAndroidStringsAction

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

Class Method Summary collapse

Class Method Details

.authorsObject



58
59
60
# File 'lib/fastlane/plugin/update_android_strings/actions/update_android_strings_action.rb', line 58

def self.authors
  ["bang"]
end

.available_optionsObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fastlane/plugin/update_android_strings/actions/update_android_strings_action.rb', line 71

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :xml_path,
  env_name: "UPDATE_ANDROID_STRINGS_XML_PATH",
  description: "Path to strings.xml",
  optional: true),
    FastlaneCore::ConfigItem.new(key: :block,
                         description: "A block to process strings.xml with custom logic",
                            optional: false,
                            is_string: false)
  ]
end

.descriptionObject



54
55
56
# File 'lib/fastlane/plugin/update_android_strings/actions/update_android_strings_action.rb', line 54

def self.description
  "Update Android res strings.xml"
end

.detailsObject



66
67
68
69
# File 'lib/fastlane/plugin/update_android_strings/actions/update_android_strings_action.rb', line 66

def self.details
  # Optional:
  "This plugin help you update strings.xml"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
# File 'lib/fastlane/plugin/update_android_strings/actions/update_android_strings_action.rb', line 84

def self.is_supported?(platform)
  # Adjust this if your plugin only works for a particular platform (iOS vs. Android, for example)
  # See: https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Platforms.md
  #
  # [:ios, :mac, :android].include?(platform)
  [:android].include?(platform)
  true
end

.return_valueObject



62
63
64
# File 'lib/fastlane/plugin/update_android_strings/actions/update_android_strings_action.rb', line 62

def self.return_value
  # If your method provides a return value, you can describe here what it does
end

.run(params) ⇒ Object



4
5
6
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
48
49
50
51
52
# File 'lib/fastlane/plugin/update_android_strings/actions/update_android_strings_action.rb', line 4

def self.run(params)
  require 'ox'

  if params[:block]
    if params[:xml_path]
      xmlPath = params[:xml_path]
    else
      xmlPath = "android/app/src/main/res/values/strings.xml"
    end
    unless File.exist?(xmlPath)
      UI.user_error!("The xml file at path `#{xmlPath}` doesn't exist.")
    end
    f = File.open(xmlPath, "r")
    xml = f.read
    f.close

    strings = Ox.parse(xml)

    unless strings
      UI.error("xml is Empty!")
      return
    end
    # find key and update text with new value
    strings.nodes.each do |element|
      if element.respond_to? :name and element["name"] == "reactNativeCodePush_androidDeploymentKey"
        element.replace_text("hello")
      end
    end
					if params[:block]
hashes = {}
      params[:block].call(hashes)

      Helper::UpdateAndroidStringsHelper.updateXml(strings.nodes, hashes)

      # dump new xml file
      newXml = Ox.dump(strings)
      if xml != newXml
        File.write(xmlPath, newXml)
        UI.success("Update xml file success.")
      else
        UI.message("Nothing has changed.")
      end
    end

  else
    UI.important("You haven't specified block parameter to update your xml file")
    false
  end
end