1082815dcSEvan Bacon"use strict";
2082815dcSEvan Bacon
3082815dcSEvan BaconObject.defineProperty(exports, "__esModule", {
4082815dcSEvan Bacon  value: true
5082815dcSEvan Bacon});
6082815dcSEvan Baconexports.applyPlugin = applyPlugin;
7082815dcSEvan Baconexports.getGoogleServicesFilePath = getGoogleServicesFilePath;
8082815dcSEvan Baconexports.setClassPath = setClassPath;
9082815dcSEvan Baconexports.setGoogleServicesFile = setGoogleServicesFile;
10082815dcSEvan Baconexports.withGoogleServicesFile = exports.withClassPath = exports.withApplyPlugin = void 0;
11082815dcSEvan Baconfunction _path() {
12082815dcSEvan Bacon  const data = _interopRequireDefault(require("path"));
13082815dcSEvan Bacon  _path = function () {
14082815dcSEvan Bacon    return data;
15082815dcSEvan Bacon  };
16082815dcSEvan Bacon  return data;
17082815dcSEvan Bacon}
18082815dcSEvan Baconfunction _androidPlugins() {
19082815dcSEvan Bacon  const data = require("../plugins/android-plugins");
20082815dcSEvan Bacon  _androidPlugins = function () {
21082815dcSEvan Bacon    return data;
22082815dcSEvan Bacon  };
23082815dcSEvan Bacon  return data;
24082815dcSEvan Bacon}
25082815dcSEvan Baconfunction _withDangerousMod() {
26082815dcSEvan Bacon  const data = require("../plugins/withDangerousMod");
27082815dcSEvan Bacon  _withDangerousMod = function () {
28082815dcSEvan Bacon    return data;
29082815dcSEvan Bacon  };
30082815dcSEvan Bacon  return data;
31082815dcSEvan Bacon}
32082815dcSEvan Baconfunction _fs() {
33082815dcSEvan Bacon  const data = require("../utils/fs");
34082815dcSEvan Bacon  _fs = function () {
35082815dcSEvan Bacon    return data;
36082815dcSEvan Bacon  };
37082815dcSEvan Bacon  return data;
38082815dcSEvan Bacon}
39082815dcSEvan Baconfunction _warnings() {
40082815dcSEvan Bacon  const data = require("../utils/warnings");
41082815dcSEvan Bacon  _warnings = function () {
42082815dcSEvan Bacon    return data;
43082815dcSEvan Bacon  };
44082815dcSEvan Bacon  return data;
45082815dcSEvan Bacon}
46082815dcSEvan Baconfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
47082815dcSEvan Baconconst DEFAULT_TARGET_PATH = './android/app/google-services.json';
48082815dcSEvan Baconconst googleServicesClassPath = 'com.google.gms:google-services';
49*ee216927STomasz Sapetaconst googleServicesPlugin = 'com.google.gms.google-services';
50082815dcSEvan Bacon
51*ee216927STomasz Sapeta// NOTE(brentvatne): This may be annoying to keep up to date...
52082815dcSEvan Baconconst googleServicesVersion = '4.3.3';
53082815dcSEvan Baconconst withClassPath = config => {
54082815dcSEvan Bacon  return (0, _androidPlugins().withProjectBuildGradle)(config, config => {
55082815dcSEvan Bacon    if (config.modResults.language === 'groovy') {
56082815dcSEvan Bacon      config.modResults.contents = setClassPath(config, config.modResults.contents);
57082815dcSEvan Bacon    } else {
58082815dcSEvan Bacon      (0, _warnings().addWarningAndroid)('android.googleServicesFile', `Cannot automatically configure project build.gradle if it's not groovy`);
59082815dcSEvan Bacon    }
60082815dcSEvan Bacon    return config;
61082815dcSEvan Bacon  });
62082815dcSEvan Bacon};
63082815dcSEvan Baconexports.withClassPath = withClassPath;
64082815dcSEvan Baconconst withApplyPlugin = config => {
65082815dcSEvan Bacon  return (0, _androidPlugins().withAppBuildGradle)(config, config => {
66082815dcSEvan Bacon    if (config.modResults.language === 'groovy') {
67082815dcSEvan Bacon      config.modResults.contents = applyPlugin(config, config.modResults.contents);
68082815dcSEvan Bacon    } else {
69082815dcSEvan Bacon      (0, _warnings().addWarningAndroid)('android.googleServicesFile', `Cannot automatically configure app build.gradle if it's not groovy`);
70082815dcSEvan Bacon    }
71082815dcSEvan Bacon    return config;
72082815dcSEvan Bacon  });
73082815dcSEvan Bacon};
74*ee216927STomasz Sapeta
75082815dcSEvan Bacon/**
76082815dcSEvan Bacon * Add `google-services.json` to project
77082815dcSEvan Bacon */
78082815dcSEvan Baconexports.withApplyPlugin = withApplyPlugin;
79082815dcSEvan Baconconst withGoogleServicesFile = config => {
80082815dcSEvan Bacon  return (0, _withDangerousMod().withDangerousMod)(config, ['android', async config => {
81082815dcSEvan Bacon    await setGoogleServicesFile(config, config.modRequest.projectRoot);
82082815dcSEvan Bacon    return config;
83082815dcSEvan Bacon  }]);
84082815dcSEvan Bacon};
85082815dcSEvan Baconexports.withGoogleServicesFile = withGoogleServicesFile;
86082815dcSEvan Baconfunction getGoogleServicesFilePath(config) {
87082815dcSEvan Bacon  var _config$android$googl, _config$android;
88082815dcSEvan Bacon  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;
89082815dcSEvan Bacon}
90082815dcSEvan Baconasync function setGoogleServicesFile(config, projectRoot, targetPath = DEFAULT_TARGET_PATH) {
91082815dcSEvan Bacon  const partialSourcePath = getGoogleServicesFilePath(config);
92082815dcSEvan Bacon  if (!partialSourcePath) {
93082815dcSEvan Bacon    return false;
94082815dcSEvan Bacon  }
95082815dcSEvan Bacon  const completeSourcePath = _path().default.resolve(projectRoot, partialSourcePath);
96082815dcSEvan Bacon  const destinationPath = _path().default.resolve(projectRoot, targetPath);
97082815dcSEvan Bacon  try {
98082815dcSEvan Bacon    await (0, _fs().copyFilePathToPathAsync)(completeSourcePath, destinationPath);
99082815dcSEvan Bacon  } catch (e) {
100082815dcSEvan Bacon    console.log(e);
101082815dcSEvan Bacon    throw new Error(`Cannot copy google-services.json from ${completeSourcePath} to ${destinationPath}. Please make sure the source and destination paths exist.`);
102082815dcSEvan Bacon  }
103082815dcSEvan Bacon  return true;
104082815dcSEvan Bacon}
105*ee216927STomasz Sapeta
106082815dcSEvan Bacon/**
107082815dcSEvan Bacon * Adding the Google Services plugin
108082815dcSEvan Bacon * NOTE(brentvatne): string replacement is a fragile approach! we need a
109082815dcSEvan Bacon * better solution than this.
110082815dcSEvan Bacon */
111082815dcSEvan Baconfunction setClassPath(config, buildGradle) {
112082815dcSEvan Bacon  const googleServicesFile = getGoogleServicesFilePath(config);
113082815dcSEvan Bacon  if (!googleServicesFile) {
114082815dcSEvan Bacon    return buildGradle;
115082815dcSEvan Bacon  }
116082815dcSEvan Bacon  if (buildGradle.includes(googleServicesClassPath)) {
117082815dcSEvan Bacon    return buildGradle;
118*ee216927STomasz Sapeta  }
119082815dcSEvan Bacon
120*ee216927STomasz Sapeta  //
121082815dcSEvan Bacon  return buildGradle.replace(/dependencies\s?{/, `dependencies {
122082815dcSEvan Bacon        classpath '${googleServicesClassPath}:${googleServicesVersion}'`);
123082815dcSEvan Bacon}
124082815dcSEvan Baconfunction applyPlugin(config, appBuildGradle) {
125082815dcSEvan Bacon  const googleServicesFile = getGoogleServicesFilePath(config);
126082815dcSEvan Bacon  if (!googleServicesFile) {
127082815dcSEvan Bacon    return appBuildGradle;
128*ee216927STomasz Sapeta  }
129082815dcSEvan Bacon
130*ee216927STomasz Sapeta  // Make sure the project does not have the plugin already
131082815dcSEvan Bacon  const pattern = new RegExp(`apply\\s+plugin:\\s+['"]${googleServicesPlugin}['"]`);
132082815dcSEvan Bacon  if (appBuildGradle.match(pattern)) {
133082815dcSEvan Bacon    return appBuildGradle;
134*ee216927STomasz Sapeta  }
135082815dcSEvan Bacon
136*ee216927STomasz Sapeta  // Add it to the end of the file
137082815dcSEvan Bacon  return appBuildGradle + `\napply plugin: '${googleServicesPlugin}'`;
138082815dcSEvan Bacon}
139082815dcSEvan Bacon//# sourceMappingURL=GoogleServices.js.map