Class: Fastlane::Actions::IncrementVersionCodeAction

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

Class Method Summary collapse

Class Method Details

.authorsObject



85
86
87
# File 'lib/fastlane/plugin/increment_version_code/actions/increment_version_code_action.rb', line 85

def self.authors
  ["Jérémy TOUDIC"]
end

.available_optionsObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fastlane/plugin/increment_version_code/actions/increment_version_code_action.rb', line 89

def self.available_options
    [
        FastlaneCore::ConfigItem.new(key: :app_folder_name,
                                env_name: "INCREMENTVERSIONCODE_APP_FOLDER_NAME",
                             description: "The name of the application source folder in the Android project (default: app)",
                                optional: true,
                                    type: String,
                           default_value:"app"),
       FastlaneCore::ConfigItem.new(key: :gradle_file_path,
                               env_name: "INCREMENTVERSIONCODE_GRADLE_FILE_PATH",
                            description: "The relative path to the gradle file containing the version code parameter (default:app/build.gradle)",
                               optional: true,
                                   type: String,
                          default_value: nil),
        FastlaneCore::ConfigItem.new(key: :version_code,
                                env_name: "INCREMENTVERSIONCODE_VERSION_CODE",
                             description: "Change to a specific version (optional)",
                                optional: true,
                                    type: Integer,
                           default_value: 0),
        FastlaneCore::ConfigItem.new(key: :ext_constant_name,
                                env_name: "INCREMENTVERSIONCODE_EXT_CONSTANT_NAME",
                             description: "If the version code is set in an ext constant, specify the constant name (optional)",
                                optional: true,
                                    type: String,
                           default_value: "versionCode")
    ]
end

.descriptionObject



81
82
83
# File 'lib/fastlane/plugin/increment_version_code/actions/increment_version_code_action.rb', line 81

def self.description
  "Increment the version code of your android project."
end

.incrementVersion(path, new_version_code, constant_name) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fastlane/plugin/increment_version_code/actions/increment_version_code_action.rb', line 42

def self.incrementVersion(path, new_version_code, constant_name)
    if !File.file?(path)
        UI.message(" -> No file exist at path: (#{path})!")
        return -1
    end
    begin
        foundVersionCode = "false"
        temp_file = Tempfile.new('fastlaneIncrementVersionCode')
        File.open(path, 'r') do |file|
            file.each_line do |line|
                if line.include? constant_name and foundVersionCode=="false"
                    UI.message(" -> line: (#{line})!")
                  versionComponents = line.strip.split(' ')
                  version_code = versionComponents[versionComponents.length-1].tr("\"","")
                  if new_version_code <= 0
                      new_version_code = version_code.to_i + 1
                  end
                  if !!(version_code =~ /\A[-+]?[0-9]+\z/)
                      line.replace line.sub(version_code, new_version_code.to_s)
                      foundVersionCode = "true"
                  end
                  temp_file.puts line
                else
                temp_file.puts line
             end
        end
        file.close
      end
      temp_file.rewind
      temp_file.close
      FileUtils.mv(temp_file.path, path)
      temp_file.unlink
    end
    if foundVersionCode == "true"
        return new_version_code
    end
    return -1
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/fastlane/plugin/increment_version_code/actions/increment_version_code_action.rb', line 124

def self.is_supported?(platform)
  [:android].include?(platform)
end

.outputObject



118
119
120
121
122
# File 'lib/fastlane/plugin/increment_version_code/actions/increment_version_code_action.rb', line 118

def self.output
  [
    ['VERSION_CODE', 'The new version code of the project']
  ]
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
# File 'lib/fastlane/plugin/increment_version_code/actions/increment_version_code_action.rb', line 7

def self.run(params)

  version_code = "0"
  new_version_code ||= params[:version_code]

  constant_name ||= params[:ext_constant_name]

  gradle_file_path ||= params[:gradle_file_path]
  if gradle_file_path != nil
      UI.message("The increment_version_code plugin will use gradle file at (#{gradle_file_path})!")
      new_version_code = incrementVersion(gradle_file_path, new_version_code, constant_name)
  else
      app_folder_name ||= params[:app_folder_name]
      UI.message("The get_version_code plugin is looking inside your project folder (#{app_folder_name})!")

      #temp_file = Tempfile.new('fastlaneIncrementVersionCode')
      #foundVersionCode = "false"
      Dir.glob("**/#{app_folder_name}/build.gradle") do |path|
          UI.message(" -> Found a build.gradle file at path: (#{path})!")
          new_version_code = incrementVersion(path, new_version_code, constant_name)
      end

  end

  if new_version_code == -1
      UI.user_error!("Impossible to find the version code with the specified properties 😭")
  else
      # Store the version name in the shared hash
      Actions.lane_context["VERSION_CODE"]=new_version_code
      UI.success("☝️ Version code has been changed to #{new_version_code}")
  end

  return new_version_code
end