1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.getVersionCode = getVersionCode;
7exports.getVersionName = getVersionName;
8exports.setMinBuildScriptExtVersion = setMinBuildScriptExtVersion;
9exports.setVersionCode = setVersionCode;
10exports.setVersionName = setVersionName;
11exports.withVersion = exports.withBuildScriptExtMinimumVersion = void 0;
12
13function _androidPlugins() {
14  const data = require("../plugins/android-plugins");
15
16  _androidPlugins = function () {
17    return data;
18  };
19
20  return data;
21}
22
23function _warnings() {
24  const data = require("../utils/warnings");
25
26  _warnings = function () {
27    return data;
28  };
29
30  return data;
31}
32
33const withVersion = config => {
34  return (0, _androidPlugins().withAppBuildGradle)(config, config => {
35    if (config.modResults.language === 'groovy') {
36      config.modResults.contents = setVersionCode(config, config.modResults.contents);
37      config.modResults.contents = setVersionName(config, config.modResults.contents);
38    } else {
39      (0, _warnings().addWarningAndroid)('android.versionCode', `Cannot automatically configure app build.gradle if it's not groovy`);
40    }
41
42    return config;
43  });
44};
45/** Sets a numeric version for a value in the project.gradle buildscript.ext object to be at least the provided props.minVersion, if the existing value is greater then no change will be made. */
46
47
48exports.withVersion = withVersion;
49
50const withBuildScriptExtMinimumVersion = (config, props) => {
51  return (0, _androidPlugins().withProjectBuildGradle)(config, config => {
52    if (config.modResults.language === 'groovy') {
53      config.modResults.contents = setMinBuildScriptExtVersion(config.modResults.contents, props);
54    } else {
55      (0, _warnings().addWarningAndroid)('withBuildScriptExtVersion', `Cannot automatically configure project build.gradle if it's not groovy`);
56    }
57
58    return config;
59  });
60};
61
62exports.withBuildScriptExtMinimumVersion = withBuildScriptExtMinimumVersion;
63
64function setMinBuildScriptExtVersion(buildGradle, {
65  name,
66  minVersion
67}) {
68  var _buildGradle$match;
69
70  const regex = new RegExp(`(${name}\\s?=\\s?)(\\d+(?:\\.\\d+)?)`);
71  const currentVersion = (_buildGradle$match = buildGradle.match(regex)) === null || _buildGradle$match === void 0 ? void 0 : _buildGradle$match[2];
72
73  if (!currentVersion) {
74    (0, _warnings().addWarningAndroid)('withBuildScriptExtVersion', `Cannot set minimum buildscript.ext.${name} version because the property "${name}" cannot be found or does not have a numeric value.`); // TODO: Maybe just add the property...
75
76    return buildGradle;
77  }
78
79  const currentVersionNum = Number(currentVersion);
80  return buildGradle.replace(regex, `$1${Math.max(minVersion, currentVersionNum)}`);
81}
82
83function getVersionName(config) {
84  var _config$version;
85
86  return (_config$version = config.version) !== null && _config$version !== void 0 ? _config$version : null;
87}
88
89function setVersionName(config, buildGradle) {
90  const versionName = getVersionName(config);
91
92  if (versionName === null) {
93    return buildGradle;
94  }
95
96  const pattern = new RegExp(`versionName ".*"`);
97  return buildGradle.replace(pattern, `versionName "${versionName}"`);
98}
99
100function getVersionCode(config) {
101  var _config$android$versi, _config$android;
102
103  return (_config$android$versi = (_config$android = config.android) === null || _config$android === void 0 ? void 0 : _config$android.versionCode) !== null && _config$android$versi !== void 0 ? _config$android$versi : 1;
104}
105
106function setVersionCode(config, buildGradle) {
107  const versionCode = getVersionCode(config);
108
109  if (versionCode === null) {
110    return buildGradle;
111  }
112
113  const pattern = new RegExp(`versionCode.*`);
114  return buildGradle.replace(pattern, `versionCode ${versionCode}`);
115}
116//# sourceMappingURL=Version.js.map