# Customise this file, documentation can be found here: # github.com/fastlane/fastlane/tree/master/fastlane/docs # All available actions: github.com/fastlane/fastlane/blob/master/fastlane/docs/Actions.md # can also be listed using the `fastlane actions` command

# Change the syntax highlighting to Ruby # All lines starting with a # are ignored when running `fastlane`

# If you want to automatically update fastlane if a new version is available: # update_fastlane

# This is the minimum version number required. # Update this, if you use features of a newer version fastlane_version “1.107.0”

default_platform :ios

for random hotel wi-fi behind firewalls. ENV = “-t DAV”

platform :ios do

before_all do
  # ENV["SLACK_URL"] = "https://hooks.slack.com/services/..."
end

desc "Runs all the tests"
lane :test do
  scan
end

desc "Sync certificates to local machine"
lane :certificates do
  match(app_identifier: "<%= bundle_identifier %>", type: "appstore")
  match(app_identifier: "<%= bundle_identifier %>", type: "development")
end

lane :set_build_number do
  config = File.read '../package.json'
  config_data = JSON.parse config
  build_num = config_data["build"]

  increment_build_number(xcodeproj: './ios/<%= project_name %>.xcodeproj', build_number: build_num)
end

lane :set_version_number do
  config = File.read '../package.json'
  config_data = JSON.parse config
  version_num = config_data["version"]

  increment_version_number(xcodeproj: './ios/<%= project_name %>.xcodeproj', version_number: version_num)
end

desc "Submit a new Beta Build to Apple TestFlight"
desc "This will also make sure the profile is up to date"
lane :beta do
  puts ENV["PROVISIONING_PROFILE_SPECIFIER"]
  match(type: "appstore") # more information: https://codesigning.guide
  set_build_number
  set_version_number
  gym(project: './ios/<%= project_name %>.xcodeproj')
  pilot

  # sh "your_script.sh"
  # You can also use other beta testing services here (run `fastlane actions`)
end

desc "Deploy a new version to the App Store"
lane :release do
  # snapshot
  match(type: "appstore")
  gym # Build your app - more options available
  deliver(force: true)
  # frameit
end

# You can define as many lanes as you want

after_all do |lane|
  # This block is called, only if the executed lane was successful

  # slack(
  #   message: "Successfully deployed new App Update."
  # )
end

error do |lane, exception|
  # slack(
  #   message: exception.message,
  #   success: false
  # )
end

end

platform :android do

lane :set_version_number do
  config = File.read '../package.json'
  config_data = JSON.parse config

  version = config_data["version"]

  path = '../android/app/build.gradle'
  re = /versionName\s+"(\d+\.\d+\.\d+)"/

  s = File.read(path)
  s[re, 1] = version

  f = File.new(path, 'w')
  f.write(s)
  f.close
end

lane :set_build_number do
  config = File.read '../package.json'
  config_data = JSON.parse config

  build = config_data["build"]

  path = '../android/app/build.gradle'
  re = /versionCode\s+(\d+)/

  s = File.read(path)
  s[re, 1] = build

  f = File.new(path, 'w')
  f.write(s)
  f.close
end

desc "Deploy a new version to the Google Play Store"
lane :alpha do
  deploy(track: 'alpha')
end

lane :beta do
  deploy(track: 'beta', env: 'production')
end

lane :release do
  deploy(track: 'production', env: 'production')
end

lane :deploy do |options|
  set_build_number
  set_version_number

  gradle \
    task: "clean",
    project_dir: "./android"

  gradle(
    task: "assemble",
    build_type: "Release",
    project_dir: "./android"
  )

  signed = sign_apk(
    apk_path: "./android/app/build/outputs/apk/app-release-unsigned.apk",
    keystore_path: "./fastlane/<%= project_name %>.jks",
    alias: "<%= project_name %>"
  )

  zipalign(apk_path: "#{lane_context[SharedValues::SIGNED_APK_PATH]}")

  supply(
    track: options[:track],
    json_key: "fastlane/key.json",
    package_name: "<%= bundle_identifier %>",
    apk: "#{lane_context[SharedValues::SIGNED_APK_PATH]}"
  )
end

end

# More information about multiple platforms in fastlane: github.com/fastlane/fastlane/blob/master/fastlane/docs/Platforms.md # All available actions: github.com/fastlane/fastlane/blob/master/fastlane/docs/Actions.md

# fastlane reports which actions are used # No personal data is recorded. Learn more at github.com/fastlane/enhancer