Class: Fastlane::Actions::IncrementVersionNameAction

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

Class Method Summary collapse

Class Method Details

.authorObject



88
89
90
# File 'lib/fastlane/plugin/increment_version_name/actions/increment_version_name_action.rb', line 88

def self.author
  "fierysolid"
end

.available_optionsObject



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
117
118
# File 'lib/fastlane/plugin/increment_version_name/actions/increment_version_name_action.rb', line 92

def self.available_options
    [
        FastlaneCore::ConfigItem.new(key: :bump_type,
                                     env_name: "INCREMENTVERSIONNAME_BUMP_TYPE",
                                     description: "The type of this version bump. Available: patch, minor, major",
                                     default_value: "patch",
                                     verify_block: proc do |value|
                                       UI.user_error!("Available values are 'patch', 'minor' and 'major'") unless ['patch', 'minor', 'major'].include?(value)
                                     end),
       FastlaneCore::ConfigItem.new(key: :gradle_file_path,
                               env_name: "INCREMENTVERSIONNAME_GRADLE_FILE_PATH",
                            description: "The relative path to the gradle file containing the version name parameter (default:app/build.gradle)",
                               optional: true,
                                   type: String,
                          default_value: nil),
        FastlaneCore::ConfigItem.new(key: :version_name,
                                     env_name: "INCREMENTVERSIONNAME_VERSION_NAME",
                                     description: "Change to a specific version. This will replace the bump type value",
                                     optional: true),
        FastlaneCore::ConfigItem.new(key: :ext_constant_name,
                                env_name: "INCREMENTVERSIONNAME_EXT_CONSTANT_NAME",
                             description: "If the version name is set in an ext constant, specify the constant name (optional)",
                                optional: true,
                                    type: String,
                           default_value: "versionName")
    ]
end

.categoryObject



134
135
136
# File 'lib/fastlane/plugin/increment_version_name/actions/increment_version_name_action.rb', line 134

def self.category
  :project
end

.descriptionObject



77
78
79
# File 'lib/fastlane/plugin/increment_version_name/actions/increment_version_name_action.rb', line 77

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

.detailsObject



81
82
83
84
85
86
# File 'lib/fastlane/plugin/increment_version_name/actions/increment_version_name_action.rb', line 81

def self.details
  [
    "This action will increment the version name.",
    "You first have to set up your Android project, if you haven't done it already."
  ].join("\n")
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/fastlane/plugin/increment_version_name/actions/increment_version_name_action.rb', line 9

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

.outputObject



120
121
122
123
124
# File 'lib/fastlane/plugin/increment_version_name/actions/increment_version_name_action.rb', line 120

def self.output
  [
    ['VERSION_NAME', 'The new version name of the project']
  ]
end

.return_typeObject



126
127
128
# File 'lib/fastlane/plugin/increment_version_name/actions/increment_version_name_action.rb', line 126

def self.return_type
  :string
end

.return_valueObject



130
131
132
# File 'lib/fastlane/plugin/increment_version_name/actions/increment_version_name_action.rb', line 130

def self.return_value
  "The new version number"
end

.run(params) ⇒ Object



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
66
67
68
69
70
71
72
73
74
75
# File 'lib/fastlane/plugin/increment_version_name/actions/increment_version_name_action.rb', line 13

def self.run(params)
  
  new_version_name ||= params[:version_name]
  constant_name ||= params[:ext_constant_name]
  gradle_file_path ||= params[:gradle_file_path]
  
  if !File.file?(gradle_file_path)
      UI.message(" -> No file exist at gradle file path: (#{gradle_file_path})!")
      return -1
  end
  
  begin
    foundVersionName = "false"
    temp_file = Tempfile.new('fastlaneIncrementVersionName')
    File.open(gradle_file_path, 'r') do |file|
      file.each_line do |line|
        if line.include? constant_name and foundVersionName=="false"
          versionComponents = line.strip.split(' ')
          current_version = versionComponents[versionComponents.length-1].tr("\"","")
          if params[:version_name]
            UI.verbose("Your current version (#{current_version}) does not respect the format A.B.C") unless current_version =~ /\d.\d.\d/
          else
            UI.user_error!("Your current version (#{current_version}) does not respect the format A.B.C") unless current_version =~ /\d+.\d+.\d+/
            version_array = current_version.split(".").map(&:to_i)
            case params[:bump_type]
            when "patch"
              version_array[2] = version_array[2] + 1
              new_version_name = version_array.join(".")
            when "minor"
              version_array[1] = version_array[1] + 1
              version_array[2] = version_array[2] = 0
              new_version_name = version_array.join(".")
            when "major"
              version_array[0] = version_array[0] + 1
              version_array[1] = version_array[1] = 0
              version_array[1] = version_array[2] = 0
              new_version_name = version_array.join(".")
            end
          end
          line.replace line.sub(current_version, new_version_name.to_s)
          foundVersionName = "true"
          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, gradle_file_path)
    temp_file.unlink
  end
  if foundVersionName == "true"
    Actions.lane_context["VERSION_NAME"]=new_version_name
    UI.success("☝️ Version name has been changed to #{new_version_name}")
  else
    UI.user_error!("Unable to find the versionName 😭")
  end
rescue => ex
  UI.error('Before being able to increment and read the versionName from your build.gradle, you first need to setup your project properly.')
  raise ex
end