|
| 1 | +require "fastlane/action" |
| 2 | +require "active_support/core_ext/hash" |
| 3 | +require "pattern_patch" |
| 4 | +require_relative "../helper/update_helper" |
| 5 | + |
| 6 | +module Fastlane |
| 7 | + module Actions |
| 8 | + class VersionBumpAction < Action |
| 9 | + class << self |
| 10 | + def run(params) |
| 11 | + version = new_version params |
| 12 | + |
| 13 | + UI.message "Bumping to version #{version}." |
| 14 | + |
| 15 | + update_package_json version |
| 16 | + patch_index_js version |
| 17 | + update_pods_in_tests_and_examples |
| 18 | + sh "git", "commit", "-a", "-m", "[Fastlane] Version bump to #{version}" |
| 19 | + end |
| 20 | + |
| 21 | + def available_options |
| 22 | + [ |
| 23 | + FastlaneCore::ConfigItem.new( |
| 24 | + key: :version, |
| 25 | + type: String, |
| 26 | + description: "New version", |
| 27 | + optional: true, |
| 28 | + default_value: nil |
| 29 | + ), |
| 30 | + FastlaneCore::ConfigItem.new( |
| 31 | + key: :tag, |
| 32 | + is_string: false, |
| 33 | + description: "Whether to tag after committing", |
| 34 | + optional: true, |
| 35 | + default_value: false |
| 36 | + ) |
| 37 | + ] |
| 38 | + end |
| 39 | + |
| 40 | + def update_package_json(version) |
| 41 | + package_json[:version] = version |
| 42 | + json_text = JSON.generate( |
| 43 | + package_json, |
| 44 | + indent: " ", |
| 45 | + object_nl: "\n", |
| 46 | + array_nl: "\n", |
| 47 | + space: " " |
| 48 | + ) |
| 49 | + |
| 50 | + File.write "package.json", "#{json_text}\n" |
| 51 | + end |
| 52 | + |
| 53 | + def patch_index_js(version) |
| 54 | + PatternPatch::Patch.new( |
| 55 | + regexp: /(VERSION = ")\d+\.\d+\.\d+/, |
| 56 | + text: "\\1#{version}", |
| 57 | + mode: :replace |
| 58 | + ).apply "src/index.js" |
| 59 | + end |
| 60 | + |
| 61 | + def new_version(params) |
| 62 | + version = params[:version] |
| 63 | + |
| 64 | + if version.nil? |
| 65 | + # Increment version from package.json |
| 66 | + # Gem::Version doesn't seem to have a reasonable method for this. |
| 67 | + components = current_version.split(".") |
| 68 | + components[-1] = (components[-1].to_i + 1).to_s |
| 69 | + version = components.join(".") |
| 70 | + end |
| 71 | + |
| 72 | + version |
| 73 | + end |
| 74 | + |
| 75 | + def current_version |
| 76 | + package_json[:version] |
| 77 | + end |
| 78 | + |
| 79 | + def package_json |
| 80 | + return @package_json if @package_json |
| 81 | + @package_json = JSON.parse(File.read("package.json")).symbolize_keys |
| 82 | + @package_json |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | +end |
0 commit comments