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