Class: Fastlane::Actions::ChangeDomainAction

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

Class Method Summary collapse

Class Method Details

.authorsObject



71
72
73
# File 'lib/fastlane/plugin/change_domain/actions/change_domain_action.rb', line 71

def self.authors
  ["yeppao"]
end

.available_optionsObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fastlane/plugin/change_domain/actions/change_domain_action.rb', line 84

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :package_name,
                            env_name: "",
                         description: "New package name",
                            optional: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :manifest,
                            env_name: "",
                         description: "Location of your project AndroidManifest.xml",
                            optional: false,
                                type: String,
                       default_value: "app/src/main/AndroidManifest.xml"),
    FastlaneCore::ConfigItem.new(key: :path,
                            env_name: "",
                            description: "Path of root Android project folder",
                            is_string: true,
                            optional: false,
                            default_value: ".")
  ]
end

.descriptionObject



67
68
69
# File 'lib/fastlane/plugin/change_domain/actions/change_domain_action.rb', line 67

def self.description
  "Change the package identifier in the AndroidManifest.xml file"
end

.detailsObject



79
80
81
82
# File 'lib/fastlane/plugin/change_domain/actions/change_domain_action.rb', line 79

def self.details
  # Optional:
  "It will permits you to change the package name of your project before the build"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/fastlane/plugin/change_domain/actions/change_domain_action.rb', line 106

def self.is_supported?(platform)
  platform == :android
end

.return_valueObject



75
76
77
# File 'lib/fastlane/plugin/change_domain/actions/change_domain_action.rb', line 75

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

.run(params) ⇒ 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fastlane/plugin/change_domain/actions/change_domain_action.rb', line 7

def self.run(params)
  require 'nokogiri'

  path = params[:path]
  packageName = params[:package_name]
  manifest = params[:manifest]

  doc = File.open(manifest) { |f|
    @doc = Nokogiri::XML(f)

    originalPackageName = nil

    @doc.css("manifest").each do |response_node|
      originalPackageName = response_node["package"]
      response_node["package"] = packageName

      UI.message("Updating package name to: #{packageName}")
    end

    if originalPackageName != packageName
      File.write(manifest, @doc.to_xml)

      folder = originalPackageName.gsub('.', '/')
      new_folder = packageName.gsub('.', '/')
      
      if File.exist?("#{path}/app/src/main/java/#{folder}")
        sourcesPath = "/app/src"
        gradlePath = "/app"
        new_folder_path = "#{path}#{sourcesPath}/main/java/#{new_folder}"
        java_sources = Dir.glob("#{path}#{sourcesPath}/main/java/#{folder}/**.java")
        kotlin_sources = Dir.glob("#{path}#{sourcesPath}/main/java/#{folder}/**.kt")
      elsif File.exist?("#{path}/src/#{folder}")
        sourcesPath = "/src"
        gradlePath = "/."
        new_folder_path = "#{path}#{sourcesPath}/#{new_folder}"
        java_sources = Dir.glob("#{path}#{sourcesPath}/#{folder}/**.java")
        kotlin_sources = Dir.glob("#{path}#{sourcesPath}/#{folder}/**.kt")
      end
    
      FileUtils::mkdir_p new_folder_path
      
      java_sources.each do |file|
        FileUtils.mv file, new_folder_path
      end
      
      kotlin_sources.each do |file|
        FileUtils.mv file, new_folder_path
      end

       Bundler.with_clean_env do
        sh "find #{path}#{sourcesPath} -name '*.java' -type f -exec sed -i -e 's/#{originalPackageName}/#{packageName}/' {} +;"
        sh "find #{path}#{sourcesPath} -name '*.kt' -type f -exec sed -i -e 's/#{originalPackageName}/#{packageName}/' {} +;"
        sh "find #{path}#{gradlePath} -name 'build.gradle' -type f -exec sed -i -e 's/#{originalPackageName}/#{packageName}/' {} +"
      end
    end

    UI.message("#{originalPackageName} successfully updated to #{packageName}")
  }
end