1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.applyPlugin = applyPlugin;
7exports.getGoogleServicesFilePath = getGoogleServicesFilePath;
8exports.setClassPath = setClassPath;
9exports.setGoogleServicesFile = setGoogleServicesFile;
10exports.withGoogleServicesFile = exports.withClassPath = exports.withApplyPlugin = void 0;
11
12function _path() {
13  const data = _interopRequireDefault(require("path"));
14
15  _path = function () {
16    return data;
17  };
18
19  return data;
20}
21
22function _androidPlugins() {
23  const data = require("../plugins/android-plugins");
24
25  _androidPlugins = function () {
26    return data;
27  };
28
29  return data;
30}
31
32function _withDangerousMod() {
33  const data = require("../plugins/withDangerousMod");
34
35  _withDangerousMod = function () {
36    return data;
37  };
38
39  return data;
40}
41
42function _fs() {
43  const data = require("../utils/fs");
44
45  _fs = function () {
46    return data;
47  };
48
49  return data;
50}
51
52function _warnings() {
53  const data = require("../utils/warnings");
54
55  _warnings = function () {
56    return data;
57  };
58
59  return data;
60}
61
62function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
63
64const DEFAULT_TARGET_PATH = './android/app/google-services.json';
65const googleServicesClassPath = 'com.google.gms:google-services';
66const googleServicesPlugin = 'com.google.gms.google-services'; // NOTE(brentvatne): This may be annoying to keep up to date...
67
68const googleServicesVersion = '4.3.3';
69
70const withClassPath = config => {
71  return (0, _androidPlugins().withProjectBuildGradle)(config, config => {
72    if (config.modResults.language === 'groovy') {
73      config.modResults.contents = setClassPath(config, config.modResults.contents);
74    } else {
75      (0, _warnings().addWarningAndroid)('android.googleServicesFile', `Cannot automatically configure project build.gradle if it's not groovy`);
76    }
77
78    return config;
79  });
80};
81
82exports.withClassPath = withClassPath;
83
84const withApplyPlugin = config => {
85  return (0, _androidPlugins().withAppBuildGradle)(config, config => {
86    if (config.modResults.language === 'groovy') {
87      config.modResults.contents = applyPlugin(config, config.modResults.contents);
88    } else {
89      (0, _warnings().addWarningAndroid)('android.googleServicesFile', `Cannot automatically configure app build.gradle if it's not groovy`);
90    }
91
92    return config;
93  });
94};
95/**
96 * Add `google-services.json` to project
97 */
98
99
100exports.withApplyPlugin = withApplyPlugin;
101
102const withGoogleServicesFile = config => {
103  return (0, _withDangerousMod().withDangerousMod)(config, ['android', async config => {
104    await setGoogleServicesFile(config, config.modRequest.projectRoot);
105    return config;
106  }]);
107};
108
109exports.withGoogleServicesFile = withGoogleServicesFile;
110
111function getGoogleServicesFilePath(config) {
112  var _config$android$googl, _config$android;
113
114  return (_config$android$googl = (_config$android = config.android) === null || _config$android === void 0 ? void 0 : _config$android.googleServicesFile) !== null && _config$android$googl !== void 0 ? _config$android$googl : null;
115}
116
117async function setGoogleServicesFile(config, projectRoot, targetPath = DEFAULT_TARGET_PATH) {
118  const partialSourcePath = getGoogleServicesFilePath(config);
119
120  if (!partialSourcePath) {
121    return false;
122  }
123
124  const completeSourcePath = _path().default.resolve(projectRoot, partialSourcePath);
125
126  const destinationPath = _path().default.resolve(projectRoot, targetPath);
127
128  try {
129    await (0, _fs().copyFilePathToPathAsync)(completeSourcePath, destinationPath);
130  } catch (e) {
131    console.log(e);
132    throw new Error(`Cannot copy google-services.json from ${completeSourcePath} to ${destinationPath}. Please make sure the source and destination paths exist.`);
133  }
134
135  return true;
136}
137/**
138 * Adding the Google Services plugin
139 * NOTE(brentvatne): string replacement is a fragile approach! we need a
140 * better solution than this.
141 */
142
143
144function setClassPath(config, buildGradle) {
145  const googleServicesFile = getGoogleServicesFilePath(config);
146
147  if (!googleServicesFile) {
148    return buildGradle;
149  }
150
151  if (buildGradle.includes(googleServicesClassPath)) {
152    return buildGradle;
153  } //
154
155
156  return buildGradle.replace(/dependencies\s?{/, `dependencies {
157        classpath '${googleServicesClassPath}:${googleServicesVersion}'`);
158}
159
160function applyPlugin(config, appBuildGradle) {
161  const googleServicesFile = getGoogleServicesFilePath(config);
162
163  if (!googleServicesFile) {
164    return appBuildGradle;
165  } // Make sure the project does not have the plugin already
166
167
168  const pattern = new RegExp(`apply\\s+plugin:\\s+['"]${googleServicesPlugin}['"]`);
169
170  if (appBuildGradle.match(pattern)) {
171    return appBuildGradle;
172  } // Add it to the end of the file
173
174
175  return appBuildGradle + `\napply plugin: '${googleServicesPlugin}'`;
176}
177//# sourceMappingURL=GoogleServices.js.map