@sentry/wizard 3.10.0 → 3.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/CHANGELOG.md +54 -7
  2. package/dist/lib/Constants.d.ts +1 -0
  3. package/dist/lib/Constants.js +5 -0
  4. package/dist/lib/Constants.js.map +1 -1
  5. package/dist/lib/Steps/ChooseIntegration.js +8 -4
  6. package/dist/lib/Steps/ChooseIntegration.js.map +1 -1
  7. package/dist/lib/Steps/Integrations/Android.d.ts +9 -0
  8. package/dist/lib/Steps/Integrations/Android.js +86 -0
  9. package/dist/lib/Steps/Integrations/Android.js.map +1 -0
  10. package/dist/lib/Steps/Integrations/ReactNative.js +3 -3
  11. package/dist/lib/Steps/Integrations/ReactNative.js.map +1 -1
  12. package/dist/lib/Steps/PromptForParameters.js +36 -3
  13. package/dist/lib/Steps/PromptForParameters.js.map +1 -1
  14. package/dist/lib/Steps/SentryProjectSelector.js +1 -1
  15. package/dist/lib/Steps/SentryProjectSelector.js.map +1 -1
  16. package/dist/package.json +4 -3
  17. package/dist/src/android/android-wizard.d.ts +2 -0
  18. package/dist/src/android/android-wizard.js +225 -0
  19. package/dist/src/android/android-wizard.js.map +1 -0
  20. package/dist/src/android/code-tools.d.ts +47 -0
  21. package/dist/src/android/code-tools.js +173 -0
  22. package/dist/src/android/code-tools.js.map +1 -0
  23. package/dist/src/android/gradle.d.ts +62 -0
  24. package/dist/src/android/gradle.js +286 -0
  25. package/dist/src/android/gradle.js.map +1 -0
  26. package/dist/src/android/manifest.d.ts +57 -0
  27. package/dist/src/android/manifest.js +183 -0
  28. package/dist/src/android/manifest.js.map +1 -0
  29. package/dist/src/android/templates.d.ts +11 -0
  30. package/dist/src/android/templates.js +34 -0
  31. package/dist/src/android/templates.js.map +1 -0
  32. package/dist/src/apple/apple-wizard.js +123 -64
  33. package/dist/src/apple/apple-wizard.js.map +1 -1
  34. package/dist/src/apple/cocoapod.js +4 -3
  35. package/dist/src/apple/cocoapod.js.map +1 -1
  36. package/dist/src/apple/code-tools.d.ts +1 -1
  37. package/dist/src/apple/code-tools.js +43 -19
  38. package/dist/src/apple/code-tools.js.map +1 -1
  39. package/dist/src/apple/fastlane.d.ts +1 -1
  40. package/dist/src/apple/fastlane.js +12 -6
  41. package/dist/src/apple/fastlane.js.map +1 -1
  42. package/dist/src/apple/templates.d.ts +2 -2
  43. package/dist/src/apple/templates.js +4 -4
  44. package/dist/src/apple/templates.js.map +1 -1
  45. package/dist/src/apple/xcode-manager.d.ts +19 -3
  46. package/dist/src/apple/xcode-manager.js +126 -24
  47. package/dist/src/apple/xcode-manager.js.map +1 -1
  48. package/dist/src/nextjs/nextjs-wizard.js +49 -11
  49. package/dist/src/nextjs/nextjs-wizard.js.map +1 -1
  50. package/dist/src/nextjs/templates.d.ts +2 -0
  51. package/dist/src/nextjs/templates.js +6 -2
  52. package/dist/src/nextjs/templates.js.map +1 -1
  53. package/dist/src/remix/remix-wizard.js +10 -20
  54. package/dist/src/remix/remix-wizard.js.map +1 -1
  55. package/dist/src/sourcemaps/sourcemaps-wizard.js +26 -13
  56. package/dist/src/sourcemaps/sourcemaps-wizard.js.map +1 -1
  57. package/dist/src/sourcemaps/tools/nextjs.js +1 -1
  58. package/dist/src/sourcemaps/tools/nextjs.js.map +1 -1
  59. package/dist/src/sourcemaps/tools/sentry-cli.js +19 -16
  60. package/dist/src/sourcemaps/tools/sentry-cli.js.map +1 -1
  61. package/dist/src/sourcemaps/tools/vite.d.ts +2 -1
  62. package/dist/src/sourcemaps/tools/vite.js +123 -111
  63. package/dist/src/sourcemaps/tools/vite.js.map +1 -1
  64. package/dist/src/sourcemaps/tools/webpack.d.ts +6 -1
  65. package/dist/src/sourcemaps/tools/webpack.js +290 -25
  66. package/dist/src/sourcemaps/tools/webpack.js.map +1 -1
  67. package/dist/src/sourcemaps/utils/detect-tool.d.ts +1 -1
  68. package/dist/src/sourcemaps/utils/detect-tool.js.map +1 -1
  69. package/dist/src/sveltekit/sdk-setup.js +5 -5
  70. package/dist/src/sveltekit/sdk-setup.js.map +1 -1
  71. package/dist/src/sveltekit/sveltekit-wizard.js +34 -44
  72. package/dist/src/sveltekit/sveltekit-wizard.js.map +1 -1
  73. package/dist/src/telemetry.js +1 -0
  74. package/dist/src/telemetry.js.map +1 -1
  75. package/dist/src/utils/ast-utils.d.ts +9 -5
  76. package/dist/src/utils/ast-utils.js +26 -11
  77. package/dist/src/utils/ast-utils.js.map +1 -1
  78. package/dist/src/utils/clack-utils.d.ts +74 -28
  79. package/dist/src/utils/clack-utils.js +427 -264
  80. package/dist/src/utils/clack-utils.js.map +1 -1
  81. package/dist/src/utils/package-manager.d.ts +10 -0
  82. package/dist/{lib/Helper/PackageManager.js → src/utils/package-manager.js} +42 -74
  83. package/dist/src/utils/package-manager.js.map +1 -0
  84. package/dist/src/utils/release-registry.d.ts +1 -0
  85. package/dist/src/utils/release-registry.js +68 -0
  86. package/dist/src/utils/release-registry.js.map +1 -0
  87. package/dist/src/utils/sentrycli-utils.d.ts +4 -0
  88. package/dist/src/utils/sentrycli-utils.js +41 -0
  89. package/dist/src/utils/sentrycli-utils.js.map +1 -0
  90. package/dist/test/android/code-tools.test.d.ts +1 -0
  91. package/dist/test/android/code-tools.test.js +34 -0
  92. package/dist/test/android/code-tools.test.js.map +1 -0
  93. package/dist/test/sourcemaps/tools/vite.test.d.ts +1 -0
  94. package/dist/test/sourcemaps/tools/vite.test.js +132 -0
  95. package/dist/test/sourcemaps/tools/vite.test.js.map +1 -0
  96. package/dist/test/sourcemaps/tools/webpack.test.d.ts +1 -0
  97. package/dist/test/sourcemaps/tools/webpack.test.js +179 -0
  98. package/dist/test/sourcemaps/tools/webpack.test.js.map +1 -0
  99. package/dist/test/utils/ast-utils.test.js +42 -7
  100. package/dist/test/utils/ast-utils.test.js.map +1 -1
  101. package/dist/test/utils/clack-utils.test.d.ts +1 -0
  102. package/dist/test/utils/clack-utils.test.js +200 -0
  103. package/dist/test/utils/clack-utils.test.js.map +1 -0
  104. package/lib/Constants.ts +5 -0
  105. package/lib/Steps/ChooseIntegration.ts +7 -3
  106. package/lib/Steps/Integrations/Android.ts +23 -0
  107. package/lib/Steps/Integrations/ReactNative.ts +9 -3
  108. package/lib/Steps/PromptForParameters.ts +48 -3
  109. package/lib/Steps/SentryProjectSelector.ts +3 -1
  110. package/package.json +4 -3
  111. package/src/android/android-wizard.ts +204 -0
  112. package/src/android/code-tools.ts +170 -0
  113. package/src/android/gradle.ts +250 -0
  114. package/src/android/manifest.ts +180 -0
  115. package/src/android/templates.ts +88 -0
  116. package/src/apple/apple-wizard.ts +113 -35
  117. package/src/apple/cocoapod.ts +6 -3
  118. package/src/apple/code-tools.ts +46 -18
  119. package/src/apple/fastlane.ts +6 -12
  120. package/src/apple/templates.ts +2 -8
  121. package/src/apple/xcode-manager.ts +167 -25
  122. package/src/nextjs/nextjs-wizard.ts +72 -8
  123. package/src/nextjs/templates.ts +16 -2
  124. package/src/remix/remix-wizard.ts +10 -15
  125. package/src/sourcemaps/sourcemaps-wizard.ts +19 -5
  126. package/src/sourcemaps/tools/nextjs.ts +2 -2
  127. package/src/sourcemaps/tools/sentry-cli.ts +8 -7
  128. package/src/sourcemaps/tools/vite.ts +143 -79
  129. package/src/sourcemaps/tools/webpack.ts +369 -30
  130. package/src/sourcemaps/utils/detect-tool.ts +2 -1
  131. package/src/sveltekit/sdk-setup.ts +10 -6
  132. package/src/sveltekit/sveltekit-wizard.ts +5 -14
  133. package/src/telemetry.ts +2 -0
  134. package/src/utils/ast-utils.ts +29 -11
  135. package/src/utils/clack-utils.ts +485 -283
  136. package/src/utils/package-manager.ts +61 -0
  137. package/src/utils/release-registry.ts +19 -0
  138. package/src/utils/sentrycli-utils.ts +22 -0
  139. package/test/android/code-tools.test.ts +49 -0
  140. package/test/sourcemaps/tools/vite.test.ts +149 -0
  141. package/test/sourcemaps/tools/webpack.test.ts +303 -0
  142. package/test/utils/ast-utils.test.ts +28 -9
  143. package/test/utils/clack-utils.test.ts +142 -0
  144. package/dist/lib/Helper/PackageManager.d.ts +0 -22
  145. package/dist/lib/Helper/PackageManager.js.map +0 -1
  146. package/dist/src/utils/vendor/clack-custom-select.d.ts +0 -21
  147. package/dist/src/utils/vendor/clack-custom-select.js +0 -137
  148. package/dist/src/utils/vendor/clack-custom-select.js.map +0 -1
  149. package/lib/Helper/PackageManager.ts +0 -59
  150. package/src/utils/vendor/clack-custom-select.ts +0 -160
@@ -0,0 +1,62 @@
1
+ /**
2
+ * A Gradle project may contain multiple modules, some of them may be applications, some of them may be libraries.
3
+ * We are only interested in applications. For example:
4
+ *
5
+ * myproject/
6
+ * app/
7
+ * lib1/
8
+ * lib2/
9
+ * wearApp/
10
+ *
11
+ * In this case^ we are interested in app/ and wearApp/
12
+ *
13
+ * @param buildGradleFiles a list of build.gradle(.kts) paths that contain the com.android.application plugin
14
+ * @returns the selected project for setting up
15
+ */
16
+ export declare function selectAppFile(buildGradleFiles: string[]): Promise<string>;
17
+ /**
18
+ * Patches a build.gradle(.kts) file that contains `com.android.application` plugin.
19
+ * There are multiple cases we have to handle here:
20
+ * - An existing `plugins {}` block:
21
+ * - We just have to add our plugin inside the block
22
+ * - No existing `plugins {}` block
23
+ * - We have to add the entire block in the beginning of the file, BUT *after imports*
24
+ *
25
+ * For example (2nd case):
26
+ *
27
+ * ```
28
+ * import net.ltgt.gradle.errorprone.errorprone
29
+ *
30
+ * // our plugins block goes here <--
31
+ * plugins {
32
+ * id("io.sentry.android.gradle") version "3.12.0"
33
+ * }
34
+ *
35
+ * apply(plugin = "com.android.application")
36
+ *
37
+ * android {
38
+ * ...
39
+ * }
40
+ * ```
41
+ *
42
+ * In the end we run `./gradlew` to verify the config is build-able and not broken.
43
+ *
44
+ * @param appFile the selected Gradle application project
45
+ * @returns true if successfully added Sentry Gradle config, false otherwise
46
+ */
47
+ export declare function addGradlePlugin(appFile: string, orgSlug: string, projectSlug: string): Promise<boolean>;
48
+ /**
49
+ * Looks for the applications packageName (namespace) in the specified build.gradle(.kts) file.
50
+ *
51
+ * ```
52
+ * android {
53
+ * namespace 'my.package.name' <-- this is what we extract
54
+ *
55
+ * compileSdkVersion = 31
56
+ * ...
57
+ * }
58
+ * ```
59
+ * @param appFile
60
+ * @returns the packageName(namespace) of the app if available
61
+ */
62
+ export declare function getNamespace(appFile: string): string | undefined;
@@ -0,0 +1,286 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __generator = (this && this.__generator) || function (thisArg, body) {
35
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
36
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
37
+ function verb(n) { return function (v) { return step([n, v]); }; }
38
+ function step(op) {
39
+ if (f) throw new TypeError("Generator is already executing.");
40
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
41
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
42
+ if (y = 0, t) op = [op[0] & 2, t.value];
43
+ switch (op[0]) {
44
+ case 0: case 1: t = op; break;
45
+ case 4: _.label++; return { value: op[1], done: false };
46
+ case 5: _.label++; y = op[1]; op = [0]; continue;
47
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
48
+ default:
49
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
50
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
51
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
52
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
53
+ if (t[2]) _.ops.pop();
54
+ _.trys.pop(); continue;
55
+ }
56
+ op = body.call(thisArg, _);
57
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
58
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
59
+ }
60
+ };
61
+ var __importDefault = (this && this.__importDefault) || function (mod) {
62
+ return (mod && mod.__esModule) ? mod : { "default": mod };
63
+ };
64
+ Object.defineProperty(exports, "__esModule", { value: true });
65
+ exports.getNamespace = exports.addGradlePlugin = exports.selectAppFile = void 0;
66
+ /* eslint-disable @typescript-eslint/no-unsafe-assignment */
67
+ var fs = __importStar(require("fs"));
68
+ var clack_utils_1 = require("../utils/clack-utils");
69
+ var templates_1 = require("./templates");
70
+ var bash = __importStar(require("../utils/bash"));
71
+ var Sentry = __importStar(require("@sentry/node"));
72
+ // @ts-ignore - clack is ESM and TS complains about that. It works though
73
+ var clack = __importStar(require("@clack/prompts"));
74
+ var chalk_1 = __importDefault(require("chalk"));
75
+ var release_registry_1 = require("../utils/release-registry");
76
+ /**
77
+ * A Gradle project may contain multiple modules, some of them may be applications, some of them may be libraries.
78
+ * We are only interested in applications. For example:
79
+ *
80
+ * myproject/
81
+ * app/
82
+ * lib1/
83
+ * lib2/
84
+ * wearApp/
85
+ *
86
+ * In this case^ we are interested in app/ and wearApp/
87
+ *
88
+ * @param buildGradleFiles a list of build.gradle(.kts) paths that contain the com.android.application plugin
89
+ * @returns the selected project for setting up
90
+ */
91
+ function selectAppFile(buildGradleFiles) {
92
+ return __awaiter(this, void 0, void 0, function () {
93
+ var appFiles, index, file, text, appFile_1, appFile;
94
+ return __generator(this, function (_a) {
95
+ switch (_a.label) {
96
+ case 0:
97
+ appFiles = [];
98
+ for (index = 0; index < buildGradleFiles.length; index++) {
99
+ file = buildGradleFiles[index];
100
+ text = fs.readFileSync(file, 'utf8');
101
+ if (/\(?["']com\.android\.application["']\)?(?!.*\S)/.test(text)) {
102
+ appFiles.push(file);
103
+ }
104
+ }
105
+ if (!(appFiles.length === 0)) return [3 /*break*/, 2];
106
+ Sentry.setTag('custom-build-logic', true);
107
+ return [4 /*yield*/, (0, clack_utils_1.abortIfCancelled)(clack.text({
108
+ message: "Unable to find your app's directory. \n Please enter the relative path to your app's build.gradle file from the root project",
109
+ placeholder: 'app/build.gradle.kts',
110
+ validate: function (value) {
111
+ if (!value.includes('.gradle') || !fs.existsSync(value))
112
+ return "Not a valid gradle file.";
113
+ },
114
+ }))];
115
+ case 1:
116
+ appFile_1 = _a.sent();
117
+ return [2 /*return*/, appFile_1];
118
+ case 2:
119
+ if (!(appFiles.length === 1)) return [3 /*break*/, 3];
120
+ Sentry.setTag('multiple-projects', false);
121
+ appFile = appFiles[0];
122
+ return [3 /*break*/, 5];
123
+ case 3:
124
+ Sentry.setTag('multiple-projects', true);
125
+ return [4 /*yield*/, (0, clack_utils_1.askForItemSelection)(appFiles, 'Which project do you want to add Sentry to?')];
126
+ case 4:
127
+ appFile = (_a.sent()).value;
128
+ _a.label = 5;
129
+ case 5:
130
+ Sentry.setTag('custom-build-logic', false);
131
+ return [2 /*return*/, appFile];
132
+ }
133
+ });
134
+ });
135
+ }
136
+ exports.selectAppFile = selectAppFile;
137
+ /**
138
+ * Patches a build.gradle(.kts) file that contains `com.android.application` plugin.
139
+ * There are multiple cases we have to handle here:
140
+ * - An existing `plugins {}` block:
141
+ * - We just have to add our plugin inside the block
142
+ * - No existing `plugins {}` block
143
+ * - We have to add the entire block in the beginning of the file, BUT *after imports*
144
+ *
145
+ * For example (2nd case):
146
+ *
147
+ * ```
148
+ * import net.ltgt.gradle.errorprone.errorprone
149
+ *
150
+ * // our plugins block goes here <--
151
+ * plugins {
152
+ * id("io.sentry.android.gradle") version "3.12.0"
153
+ * }
154
+ *
155
+ * apply(plugin = "com.android.application")
156
+ *
157
+ * android {
158
+ * ...
159
+ * }
160
+ * ```
161
+ *
162
+ * In the end we run `./gradlew` to verify the config is build-able and not broken.
163
+ *
164
+ * @param appFile the selected Gradle application project
165
+ * @returns true if successfully added Sentry Gradle config, false otherwise
166
+ */
167
+ function addGradlePlugin(appFile, orgSlug, projectSlug) {
168
+ return __awaiter(this, void 0, void 0, function () {
169
+ var gradleScript, pluginVersion, pluginsBlockMatch, newGradleScript, regex, importsMatch, insertIndex, insertIndex, buildSpinner, e_1;
170
+ return __generator(this, function (_a) {
171
+ switch (_a.label) {
172
+ case 0:
173
+ gradleScript = fs.readFileSync(appFile, 'utf8');
174
+ if (/\(?["']io\.sentry\.android\.gradle["']\)?/.test(gradleScript)) {
175
+ // sentry gradle plugin is already installed
176
+ clack.log.success(chalk_1.default.greenBright("".concat(chalk_1.default.bold('Sentry Gradle plugin'), " is already added to the project.")));
177
+ maybeAddSourceContextConfig(appFile, gradleScript, orgSlug, projectSlug);
178
+ return [2 /*return*/, true];
179
+ }
180
+ return [4 /*yield*/, (0, release_registry_1.fetchSdkVersion)('sentry.java.android.gradle-plugin')];
181
+ case 1:
182
+ pluginVersion = _a.sent();
183
+ pluginsBlockMatch = /plugins\s*{[^{}]*}/.exec(gradleScript);
184
+ if (!pluginsBlockMatch) {
185
+ regex = /import\s+[\w.]+/gm;
186
+ importsMatch = regex.exec(gradleScript);
187
+ insertIndex = 0;
188
+ while (importsMatch) {
189
+ insertIndex = importsMatch.index + importsMatch[0].length + 1;
190
+ importsMatch = regex.exec(gradleScript);
191
+ }
192
+ if (appFile.endsWith('.kts')) {
193
+ newGradleScript =
194
+ gradleScript.slice(0, insertIndex) +
195
+ (0, templates_1.pluginsBlockKts)(pluginVersion) +
196
+ gradleScript.slice(insertIndex);
197
+ }
198
+ else {
199
+ newGradleScript =
200
+ gradleScript.slice(0, insertIndex) +
201
+ (0, templates_1.pluginsBlock)(pluginVersion) +
202
+ gradleScript.slice(insertIndex);
203
+ }
204
+ }
205
+ else {
206
+ insertIndex = pluginsBlockMatch.index + pluginsBlockMatch[0].length - 1;
207
+ if (appFile.endsWith('.kts')) {
208
+ newGradleScript =
209
+ gradleScript.slice(0, insertIndex) +
210
+ (0, templates_1.pluginKts)(pluginVersion) +
211
+ gradleScript.slice(insertIndex);
212
+ }
213
+ else {
214
+ newGradleScript =
215
+ gradleScript.slice(0, insertIndex) +
216
+ (0, templates_1.plugin)(pluginVersion) +
217
+ gradleScript.slice(insertIndex);
218
+ }
219
+ }
220
+ fs.writeFileSync(appFile, newGradleScript, 'utf8');
221
+ maybeAddSourceContextConfig(appFile, newGradleScript, orgSlug, projectSlug);
222
+ buildSpinner = clack.spinner();
223
+ buildSpinner.start('Running ./gradlew to verify changes (this may take a few minutes)...');
224
+ _a.label = 2;
225
+ case 2:
226
+ _a.trys.push([2, 4, , 5]);
227
+ return [4 /*yield*/, bash.execute('./gradlew')];
228
+ case 3:
229
+ _a.sent();
230
+ buildSpinner.stop(chalk_1.default.greenBright("".concat(chalk_1.default.bold('Sentry Gradle plugin'), " added to the project.")));
231
+ return [3 /*break*/, 5];
232
+ case 4:
233
+ e_1 = _a.sent();
234
+ buildSpinner.stop();
235
+ Sentry.captureException('Gradle Sync failed');
236
+ return [2 /*return*/, false];
237
+ case 5: return [2 /*return*/, true];
238
+ }
239
+ });
240
+ });
241
+ }
242
+ exports.addGradlePlugin = addGradlePlugin;
243
+ /**
244
+ * Looks for the applications packageName (namespace) in the specified build.gradle(.kts) file.
245
+ *
246
+ * ```
247
+ * android {
248
+ * namespace 'my.package.name' <-- this is what we extract
249
+ *
250
+ * compileSdkVersion = 31
251
+ * ...
252
+ * }
253
+ * ```
254
+ * @param appFile
255
+ * @returns the packageName(namespace) of the app if available
256
+ */
257
+ function getNamespace(appFile) {
258
+ var gradleScript = fs.readFileSync(appFile, 'utf8');
259
+ var namespaceMatch = /namespace\s*=?\s*['"]([^'"]+)['"]/i.exec(gradleScript);
260
+ if (!namespaceMatch || namespaceMatch.length <= 1) {
261
+ clack.log.warn('Unable to determine application package name.');
262
+ Sentry.captureException('No package name');
263
+ return undefined;
264
+ }
265
+ var namespace = namespaceMatch[1];
266
+ return namespace;
267
+ }
268
+ exports.getNamespace = getNamespace;
269
+ /**
270
+ * Adds source context configuration to the gradleScript if `sentry {}` block is not yet configured,
271
+ *
272
+ * @param appFile
273
+ * @param gradleScript
274
+ */
275
+ function maybeAddSourceContextConfig(appFile, gradleScript, orgSlug, projectSlug) {
276
+ if (!/sentry\s*\{[^}]*\}/i.test(gradleScript)) {
277
+ // if no sentry {} block is configured, we add our own with source context enabled
278
+ if (appFile.endsWith('.kts')) {
279
+ fs.appendFileSync(appFile, (0, templates_1.sourceContextKts)(orgSlug, projectSlug), 'utf8');
280
+ }
281
+ else {
282
+ fs.appendFileSync(appFile, (0, templates_1.sourceContext)(orgSlug, projectSlug), 'utf8');
283
+ }
284
+ }
285
+ }
286
+ //# sourceMappingURL=gradle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gradle.js","sourceRoot":"","sources":["../../../src/android/gradle.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4DAA4D;AAC5D,qCAAyB;AACzB,oDAA6E;AAC7E,yCAOqB;AACrB,kDAAsC;AACtC,mDAAuC;AACvC,yEAAyE;AACzE,oDAAwC;AACxC,gDAA0B;AAC1B,8DAA4D;AAE5D;;;;;;;;;;;;;;GAcG;AACH,SAAsB,aAAa,CACjC,gBAA0B;;;;;;oBAEpB,QAAQ,GAAG,EAAE,CAAC;oBACpB,KAAS,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,gBAAgB,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE;wBACtD,IAAI,GAAG,gBAAgB,CAAC,KAAK,CAAC,CAAC;wBAC/B,IAAI,GAAG,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;wBAC3C,IAAI,iDAAiD,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;4BAChE,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;yBACrB;qBACF;yBAEG,CAAA,QAAQ,CAAC,MAAM,KAAK,CAAC,CAAA,EAArB,wBAAqB;oBACvB,MAAM,CAAC,MAAM,CAAC,oBAAoB,EAAE,IAAI,CAAC,CAAC;oBAC1B,qBAAM,IAAA,8BAAgB,EACpC,KAAK,CAAC,IAAI,CAAC;4BACT,OAAO,EAAE,qIAC4E;4BACrF,WAAW,EAAE,sBAAsB;4BACnC,QAAQ,YAAC,KAAK;gCACZ,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC;oC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AAoB,EACpB,OAAe,EACf,WAAmB;IAEnB,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,YAAY,CAAC,EAAE;QAC7C,kFAAkF;QAClF,IAAI,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE;YAC5B,EAAE,CAAC,cAAc,CACf,OAAO,EACP,IAAA,4BAAgB,EAAC,OAAO,EAAE,WAAW,CAAC,EACtC,MAAM,CACP,CAAC;SACH;aAAM;YACL,EAAE,CAAC,cAAc,CAAC,OAAO,EAAE,IAAA,yBAAa,EAAC,OAAO,EAAE,WAAW,CAAC,EAAE,MAAM,CAAC,CAAC;SACzE;KACF;AACH,CAAC","sourcesContent":["/* eslint-disable @typescript-eslint/no-unsafe-assignment */\nimport * as fs from 'fs';\nimport { abortIfCancelled, askForItemSelection } from '../utils/clack-utils';\nimport {\n plugin,\n pluginKts,\n pluginsBlock,\n pluginsBlockKts,\n sourceContext,\n sourceContextKts,\n} from './templates';\nimport * as bash from '../utils/bash';\nimport * as Sentry from '@sentry/node';\n// @ts-ignore - clack is ESM and TS complains about that. It works though\nimport * as clack from '@clack/prompts';\nimport chalk from 'chalk';\nimport { fetchSdkVersion } from '../utils/release-registry';\n\n/**\n * A Gradle project may contain multiple modules, some of them may be applications, some of them may be libraries.\n * We are only interested in applications. For example:\n *\n * myproject/\n * app/\n * lib1/\n * lib2/\n * wearApp/\n *\n * In this case^ we are interested in app/ and wearApp/\n *\n * @param buildGradleFiles a list of build.gradle(.kts) paths that contain the com.android.application plugin\n * @returns the selected project for setting up\n */\nexport async function selectAppFile(\n buildGradleFiles: string[],\n): Promise<string> {\n const appFiles = [];\n for (let index = 0; index < buildGradleFiles.length; index++) {\n const file = buildGradleFiles[index];\n const text = fs.readFileSync(file, 'utf8');\n if (/\\(?[\"']com\\.android\\.application[\"']\\)?(?!.*\\S)/.test(text)) {\n appFiles.push(file);\n }\n }\n\n if (appFiles.length === 0) {\n Sentry.setTag('custom-build-logic', true);\n const appFile = await abortIfCancelled(\n clack.text({\n message: `Unable to find your app's directory. \n Please enter the relative path to your app's build.gradle file from the root project`,\n placeholder: 'app/build.gradle.kts',\n validate(value) {\n if (!value.includes('.gradle') || !fs.existsSync(value))\n return `Not a valid gradle file.`;\n },\n }),\n );\n return appFile;\n }\n\n let appFile;\n if (appFiles.length === 1) {\n Sentry.setTag('multiple-projects', false);\n appFile = appFiles[0];\n } else {\n Sentry.setTag('multiple-projects', true);\n appFile = (\n await askForItemSelection(\n appFiles,\n 'Which project do you want to add Sentry to?',\n )\n ).value;\n }\n Sentry.setTag('custom-build-logic', false);\n return appFile;\n}\n\n/**\n * Patches a build.gradle(.kts) file that contains `com.android.application` plugin.\n * There are multiple cases we have to handle here:\n * - An existing `plugins {}` block:\n * - We just have to add our plugin inside the block\n * - No existing `plugins {}` block\n * - We have to add the entire block in the beginning of the file, BUT *after imports*\n *\n * For example (2nd case):\n *\n * ```\n * import net.ltgt.gradle.errorprone.errorprone\n *\n * // our plugins block goes here <--\n * plugins {\n * id(\"io.sentry.android.gradle\") version \"3.12.0\"\n * }\n *\n * apply(plugin = \"com.android.application\")\n *\n * android {\n * ...\n * }\n * ```\n *\n * In the end we run `./gradlew` to verify the config is build-able and not broken.\n *\n * @param appFile the selected Gradle application project\n * @returns true if successfully added Sentry Gradle config, false otherwise\n */\nexport async function addGradlePlugin(\n appFile: string,\n orgSlug: string,\n projectSlug: string,\n): Promise<boolean> {\n const gradleScript = fs.readFileSync(appFile, 'utf8');\n\n if (/\\(?[\"']io\\.sentry\\.android\\.gradle[\"']\\)?/.test(gradleScript)) {\n // sentry gradle plugin is already installed\n clack.log.success(\n chalk.greenBright(\n `${chalk.bold(\n 'Sentry Gradle plugin',\n )} is already added to the project.`,\n ),\n );\n maybeAddSourceContextConfig(appFile, gradleScript, orgSlug, projectSlug);\n return true;\n }\n\n const pluginVersion = await fetchSdkVersion(\n 'sentry.java.android.gradle-plugin',\n );\n const pluginsBlockMatch = /plugins\\s*{[^{}]*}/.exec(gradleScript);\n let newGradleScript;\n if (!pluginsBlockMatch) {\n // no \"plugins {}\" block, we can just add our own after imports\n const regex = /import\\s+[\\w.]+/gm;\n let importsMatch = regex.exec(gradleScript);\n let insertIndex = 0;\n while (importsMatch) {\n insertIndex = importsMatch.index + importsMatch[0].length + 1;\n importsMatch = regex.exec(gradleScript);\n }\n\n if (appFile.endsWith('.kts')) {\n newGradleScript =\n gradleScript.slice(0, insertIndex) +\n pluginsBlockKts(pluginVersion) +\n gradleScript.slice(insertIndex);\n } else {\n newGradleScript =\n gradleScript.slice(0, insertIndex) +\n pluginsBlock(pluginVersion) +\n gradleScript.slice(insertIndex);\n }\n } else {\n const insertIndex =\n pluginsBlockMatch.index + pluginsBlockMatch[0].length - 1;\n if (appFile.endsWith('.kts')) {\n newGradleScript =\n gradleScript.slice(0, insertIndex) +\n pluginKts(pluginVersion) +\n gradleScript.slice(insertIndex);\n } else {\n newGradleScript =\n gradleScript.slice(0, insertIndex) +\n plugin(pluginVersion) +\n gradleScript.slice(insertIndex);\n }\n }\n fs.writeFileSync(appFile, newGradleScript, 'utf8');\n\n maybeAddSourceContextConfig(appFile, newGradleScript, orgSlug, projectSlug);\n\n const buildSpinner = clack.spinner();\n\n buildSpinner.start(\n 'Running ./gradlew to verify changes (this may take a few minutes)...',\n );\n\n try {\n await bash.execute('./gradlew');\n buildSpinner.stop(\n chalk.greenBright(\n `${chalk.bold('Sentry Gradle plugin')} added to the project.`,\n ),\n );\n } catch (e) {\n buildSpinner.stop();\n Sentry.captureException('Gradle Sync failed');\n return false;\n }\n\n return true;\n}\n\n/**\n * Looks for the applications packageName (namespace) in the specified build.gradle(.kts) file.\n *\n * ```\n * android {\n * namespace 'my.package.name' <-- this is what we extract\n *\n * compileSdkVersion = 31\n * ...\n * }\n * ```\n * @param appFile\n * @returns the packageName(namespace) of the app if available\n */\nexport function getNamespace(appFile: string): string | undefined {\n const gradleScript = fs.readFileSync(appFile, 'utf8');\n\n const namespaceMatch = /namespace\\s*=?\\s*['\"]([^'\"]+)['\"]/i.exec(\n gradleScript,\n );\n if (!namespaceMatch || namespaceMatch.length <= 1) {\n clack.log.warn('Unable to determine application package name.');\n Sentry.captureException('No package name');\n return undefined;\n }\n\n const namespace = namespaceMatch[1];\n return namespace;\n}\n\n/**\n * Adds source context configuration to the gradleScript if `sentry {}` block is not yet configured,\n *\n * @param appFile\n * @param gradleScript\n */\nfunction maybeAddSourceContextConfig(\n appFile: string,\n gradleScript: string,\n orgSlug: string,\n projectSlug: string,\n) {\n if (!/sentry\\s*\\{[^}]*\\}/i.test(gradleScript)) {\n // if no sentry {} block is configured, we add our own with source context enabled\n if (appFile.endsWith('.kts')) {\n fs.appendFileSync(\n appFile,\n sourceContextKts(orgSlug, projectSlug),\n 'utf8',\n );\n } else {\n fs.appendFileSync(appFile, sourceContext(orgSlug, projectSlug), 'utf8');\n }\n }\n}\n"]}
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Looks for the closing </application> tag in the manifest and adds the Sentry config after it.
3
+ *
4
+ * For example:
5
+ * ```xml
6
+ * <manifest xmlns:android="http://schemas.android.com/apk/res/android"
7
+ * xmlns:tools="http://schemas.android.com/tools">
8
+ *
9
+ * <application>
10
+ * ...
11
+ * // this is what we add and more
12
+ * <meta-data android:name="io.sentry.dsn" android:value="__dsn__" />
13
+ * </application> <!-- we are looking for this one
14
+ * </manifest>
15
+ * ```
16
+ *
17
+ * @param manifestFile the path to the main AndroidManifest.xml file
18
+ * @param dsn
19
+ * @returns true if successfully patched the manifest, false otherwise
20
+ */
21
+ export declare function addManifestSnippet(manifestFile: string, dsn: string): boolean;
22
+ /**
23
+ * There might be multiple <activity> in the manifest, as well as multiple <activity-alias> with category LAUNCHER,
24
+ * but only one main activity with action MAIN. We are looking for this one by parsing xml and walking it.
25
+ *
26
+ * In addition, older Android versions required to specify the packag name in the manifest,
27
+ * while the new ones - in the Gradle config. So we are just sanity checking if the package name
28
+ * is in the manifest and returning it as well.
29
+ *
30
+ * For example:
31
+ *
32
+ * ```xml
33
+ * <manifest xmlns:android="http://schemas.android.com/apk/res/android"
34
+ * xmlns:tools="http://schemas.android.com/tools"
35
+ * package="com.example.sample">
36
+ *
37
+ * <application>
38
+ * <activity
39
+ * android:name="ui.MainActivity"
40
+ * ...other props>
41
+ * <intent-filter>
42
+ * <action android:name="android.intent.action.MAIN" /> <!-- we are looking for this one
43
+ *
44
+ * <category android:name="android.intent.category.LAUNCHER" />
45
+ * </intent-filter>
46
+ * </activity>
47
+ * </application>
48
+ * </manifest>
49
+ * ```
50
+ *
51
+ * @param manifestFile path to the AndroidManifest.xml file
52
+ * @returns package name (if available in the manifest) + the main activity name
53
+ */
54
+ export declare function getMainActivity(manifestFile: string): {
55
+ packageName?: string;
56
+ activityName?: string;
57
+ };
@@ -0,0 +1,183 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.getMainActivity = exports.addManifestSnippet = void 0;
30
+ /* eslint-disable @typescript-eslint/no-unsafe-member-access */
31
+ /* eslint-disable @typescript-eslint/no-unsafe-assignment */
32
+ var fs = __importStar(require("fs"));
33
+ // @ts-ignore - clack is ESM and TS complains about that. It works though
34
+ var clack = __importStar(require("@clack/prompts"));
35
+ var Sentry = __importStar(require("@sentry/node"));
36
+ var templates_1 = require("./templates");
37
+ var xml_js_1 = __importDefault(require("xml-js"));
38
+ var chalk_1 = __importDefault(require("chalk"));
39
+ /**
40
+ * Looks for the closing </application> tag in the manifest and adds the Sentry config after it.
41
+ *
42
+ * For example:
43
+ * ```xml
44
+ * <manifest xmlns:android="http://schemas.android.com/apk/res/android"
45
+ * xmlns:tools="http://schemas.android.com/tools">
46
+ *
47
+ * <application>
48
+ * ...
49
+ * // this is what we add and more
50
+ * <meta-data android:name="io.sentry.dsn" android:value="__dsn__" />
51
+ * </application> <!-- we are looking for this one
52
+ * </manifest>
53
+ * ```
54
+ *
55
+ * @param manifestFile the path to the main AndroidManifest.xml file
56
+ * @param dsn
57
+ * @returns true if successfully patched the manifest, false otherwise
58
+ */
59
+ function addManifestSnippet(manifestFile, dsn) {
60
+ if (!fs.existsSync(manifestFile)) {
61
+ clack.log.warn('AndroidManifest.xml not found.');
62
+ Sentry.captureException('No AndroidManifest file');
63
+ return false;
64
+ }
65
+ var manifestContent = fs.readFileSync(manifestFile, 'utf8');
66
+ if (/android:name="io\.sentry[^"]*"/i.test(manifestContent)) {
67
+ // sentry is already configured
68
+ clack.log.success(chalk_1.default.greenBright('Sentry SDK is already configured.'));
69
+ return true;
70
+ }
71
+ var applicationMatch = /<\/application>/i.exec(manifestContent);
72
+ if (!applicationMatch) {
73
+ clack.log.warn('<application> tag not found within the manifest.');
74
+ Sentry.captureException('No <application> tag');
75
+ return false;
76
+ }
77
+ var insertionIndex = applicationMatch.index;
78
+ var newContent = manifestContent.slice(0, insertionIndex) +
79
+ (0, templates_1.manifest)(dsn) +
80
+ manifestContent.slice(insertionIndex);
81
+ fs.writeFileSync(manifestFile, newContent, 'utf8');
82
+ clack.log.success(chalk_1.default.greenBright("Updated ".concat(chalk_1.default.bold('AndroidManifest.xml'), " with the Sentry SDK configuration.")));
83
+ return true;
84
+ }
85
+ exports.addManifestSnippet = addManifestSnippet;
86
+ /**
87
+ * There might be multiple <activity> in the manifest, as well as multiple <activity-alias> with category LAUNCHER,
88
+ * but only one main activity with action MAIN. We are looking for this one by parsing xml and walking it.
89
+ *
90
+ * In addition, older Android versions required to specify the packag name in the manifest,
91
+ * while the new ones - in the Gradle config. So we are just sanity checking if the package name
92
+ * is in the manifest and returning it as well.
93
+ *
94
+ * For example:
95
+ *
96
+ * ```xml
97
+ * <manifest xmlns:android="http://schemas.android.com/apk/res/android"
98
+ * xmlns:tools="http://schemas.android.com/tools"
99
+ * package="com.example.sample">
100
+ *
101
+ * <application>
102
+ * <activity
103
+ * android:name="ui.MainActivity"
104
+ * ...other props>
105
+ * <intent-filter>
106
+ * <action android:name="android.intent.action.MAIN" /> <!-- we are looking for this one
107
+ *
108
+ * <category android:name="android.intent.category.LAUNCHER" />
109
+ * </intent-filter>
110
+ * </activity>
111
+ * </application>
112
+ * </manifest>
113
+ * ```
114
+ *
115
+ * @param manifestFile path to the AndroidManifest.xml file
116
+ * @returns package name (if available in the manifest) + the main activity name
117
+ */
118
+ function getMainActivity(manifestFile) {
119
+ var _a, _b, _c, _d;
120
+ if (!fs.existsSync(manifestFile)) {
121
+ clack.log.warn('AndroidManifest.xml not found.');
122
+ Sentry.captureException('No AndroidManifest file');
123
+ return {};
124
+ }
125
+ var manifestContent = fs.readFileSync(manifestFile, 'utf8');
126
+ var converted = xml_js_1.default.xml2js(manifestContent, {
127
+ compact: true,
128
+ });
129
+ var activities = (_b = (_a = converted.manifest) === null || _a === void 0 ? void 0 : _a.application) === null || _b === void 0 ? void 0 : _b.activity;
130
+ var packageName = (_d = (_c = converted.manifest) === null || _c === void 0 ? void 0 : _c._attributes) === null || _d === void 0 ? void 0 : _d['package'];
131
+ if (!activities) {
132
+ clack.log.warn('No activity found in AndroidManifest.');
133
+ Sentry.captureException('No Activity');
134
+ return {};
135
+ }
136
+ var mainActivity;
137
+ if (Array.isArray(activities)) {
138
+ var withIntentFilter = activities.filter(function (a) { return !!a['intent-filter']; });
139
+ mainActivity = withIntentFilter.find(function (a) { return isMainActivity(a); });
140
+ }
141
+ else if (isMainActivity(activities)) {
142
+ mainActivity = activities;
143
+ }
144
+ if (!mainActivity) {
145
+ clack.log.warn('No main activity found in AndroidManifest.');
146
+ Sentry.captureException('No Main Activity');
147
+ return {};
148
+ }
149
+ var attrs = mainActivity._attributes;
150
+ var activityName = attrs === null || attrs === void 0 ? void 0 : attrs['android:name'];
151
+ return { packageName: packageName, activityName: activityName };
152
+ }
153
+ exports.getMainActivity = getMainActivity;
154
+ function isMainActivity(activity) {
155
+ var intentFilters = activity['intent-filter'];
156
+ if (Array.isArray(intentFilters)) {
157
+ return intentFilters.some(function (i) {
158
+ var action = i.action;
159
+ return hasMainAction(action);
160
+ });
161
+ }
162
+ else {
163
+ var action = intentFilters.action;
164
+ return hasMainAction(action);
165
+ }
166
+ }
167
+ function hasMainAction(action) {
168
+ if (!action) {
169
+ return false;
170
+ }
171
+ function isMain(attrs) {
172
+ return (attrs === null || attrs === void 0 ? void 0 : attrs['android:name']) === 'android.intent.action.MAIN';
173
+ }
174
+ if (Array.isArray(action)) {
175
+ return action.some(function (c) {
176
+ return isMain(c._attributes);
177
+ });
178
+ }
179
+ else {
180
+ return isMain(action._attributes);
181
+ }
182
+ }
183
+ //# sourceMappingURL=manifest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"manifest.js","sourceRoot":"","sources":["../../../src/android/manifest.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+DAA+D;AAC/D,4DAA4D;AAC5D,qCAAyB;AACzB,yEAAyE;AACzE,oDAAwC;AACxC,mDAAuC;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C,CAAC;YACnB,OAAO,MAAM,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;KACJ;SAAM;QACL,OAAO,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;KACnC;AACH,CAAC","sourcesContent":["/* eslint-disable @typescript-eslint/no-unsafe-member-access */\n/* eslint-disable @typescript-eslint/no-unsafe-assignment */\nimport * as fs from 'fs';\n// @ts-ignore - clack is ESM and TS complains about that. It works though\nimport * as clack from '@clack/prompts';\nimport * as Sentry from '@sentry/node';\nimport { manifest } from './templates';\nimport xml, { Attributes, ElementCompact } from 'xml-js';\nimport chalk from 'chalk';\n\n/**\n * Looks for the closing </application> tag in the manifest and adds the Sentry config after it.\n *\n * For example:\n * ```xml\n * <manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n * xmlns:tools=\"http://schemas.android.com/tools\">\n *\n * <application>\n * ...\n * // this is what we add and more\n * <meta-data android:name=\"io.sentry.dsn\" android:value=\"__dsn__\" />\n * </application> <!-- we are looking for this one\n * </manifest>\n * ```\n *\n * @param manifestFile the path to the main AndroidManifest.xml file\n * @param dsn\n * @returns true if successfully patched the manifest, false otherwise\n */\nexport function addManifestSnippet(manifestFile: string, dsn: string): boolean {\n if (!fs.existsSync(manifestFile)) {\n clack.log.warn('AndroidManifest.xml not found.');\n Sentry.captureException('No AndroidManifest file');\n return false;\n }\n\n const manifestContent = fs.readFileSync(manifestFile, 'utf8');\n\n if (/android:name=\"io\\.sentry[^\"]*\"/i.test(manifestContent)) {\n // sentry is already configured\n clack.log.success(chalk.greenBright('Sentry SDK is already configured.'));\n return true;\n }\n\n const applicationMatch = /<\\/application>/i.exec(manifestContent);\n if (!applicationMatch) {\n clack.log.warn('<application> tag not found within the manifest.');\n Sentry.captureException('No <application> tag');\n return false;\n }\n\n const insertionIndex = applicationMatch.index;\n const newContent =\n manifestContent.slice(0, insertionIndex) +\n manifest(dsn) +\n manifestContent.slice(insertionIndex);\n fs.writeFileSync(manifestFile, newContent, 'utf8');\n\n clack.log.success(\n chalk.greenBright(\n `Updated ${chalk.bold(\n 'AndroidManifest.xml',\n )} with the Sentry SDK configuration.`,\n ),\n );\n\n return true;\n}\n\n/**\n * There might be multiple <activity> in the manifest, as well as multiple <activity-alias> with category LAUNCHER,\n * but only one main activity with action MAIN. We are looking for this one by parsing xml and walking it.\n *\n * In addition, older Android versions required to specify the packag name in the manifest,\n * while the new ones - in the Gradle config. So we are just sanity checking if the package name\n * is in the manifest and returning it as well.\n *\n * For example:\n *\n * ```xml\n * <manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n * xmlns:tools=\"http://schemas.android.com/tools\"\n * package=\"com.example.sample\">\n *\n * <application>\n * <activity\n * android:name=\"ui.MainActivity\"\n * ...other props>\n * <intent-filter>\n * <action android:name=\"android.intent.action.MAIN\" /> <!-- we are looking for this one\n *\n * <category android:name=\"android.intent.category.LAUNCHER\" />\n * </intent-filter>\n * </activity>\n * </application>\n * </manifest>\n * ```\n *\n * @param manifestFile path to the AndroidManifest.xml file\n * @returns package name (if available in the manifest) + the main activity name\n */\nexport function getMainActivity(manifestFile: string): {\n packageName?: string;\n activityName?: string;\n} {\n if (!fs.existsSync(manifestFile)) {\n clack.log.warn('AndroidManifest.xml not found.');\n Sentry.captureException('No AndroidManifest file');\n return {};\n }\n\n const manifestContent = fs.readFileSync(manifestFile, 'utf8');\n const converted: ElementCompact = xml.xml2js(manifestContent, {\n compact: true,\n });\n const activities: ElementCompact[] | ElementCompact | undefined =\n converted.manifest?.application?.activity;\n const packageName: string | undefined =\n converted.manifest?._attributes?.['package'];\n\n if (!activities) {\n clack.log.warn('No activity found in AndroidManifest.');\n Sentry.captureException('No Activity');\n return {};\n }\n\n let mainActivity;\n if (Array.isArray(activities)) {\n const withIntentFilter = activities.filter((a) => !!a['intent-filter']);\n mainActivity = withIntentFilter.find((a) => isMainActivity(a));\n } else if (isMainActivity(activities)) {\n mainActivity = activities;\n }\n\n if (!mainActivity) {\n clack.log.warn('No main activity found in AndroidManifest.');\n Sentry.captureException('No Main Activity');\n return {};\n }\n\n const attrs = mainActivity._attributes;\n const activityName = attrs?.['android:name'] as string | undefined;\n return { packageName: packageName, activityName: activityName };\n}\n\nfunction isMainActivity(activity: ElementCompact): boolean {\n const intentFilters: ElementCompact[] | ElementCompact =\n activity['intent-filter'];\n if (Array.isArray(intentFilters)) {\n return intentFilters.some((i) => {\n const action: ElementCompact[] | ElementCompact | undefined = i.action;\n return hasMainAction(action);\n });\n } else {\n const action: ElementCompact[] | ElementCompact | undefined =\n intentFilters.action;\n return hasMainAction(action);\n }\n}\n\nfunction hasMainAction(\n action: ElementCompact[] | ElementCompact | undefined,\n): boolean {\n if (!action) {\n return false;\n }\n\n function isMain(attrs?: Attributes): boolean {\n return attrs?.['android:name'] === 'android.intent.action.MAIN';\n }\n\n if (Array.isArray(action)) {\n return action.some((c) => {\n return isMain(c._attributes);\n });\n } else {\n return isMain(action._attributes);\n }\n}\n"]}
@@ -0,0 +1,11 @@
1
+ export declare const pluginsBlock: (version?: string) => string;
2
+ export declare const pluginsBlockKts: (version?: string) => string;
3
+ export declare const plugin: (version?: string) => string;
4
+ export declare const pluginKts: (version?: string) => string;
5
+ export declare const manifest: (dsn: string) => string;
6
+ export declare const sentryImport = "import io.sentry.Sentry;\n";
7
+ export declare const sentryImportKt = "import io.sentry.Sentry\n";
8
+ export declare const testErrorSnippet = "\n // waiting for view to draw to better represent a captured error with a screenshot\n findViewById(android.R.id.content).getViewTreeObserver().addOnGlobalLayoutListener(() -> {\n try {\n throw new Exception(\"This app uses Sentry! :)\");\n } catch (Exception e) {\n Sentry.captureException(e);\n }\n });\n";
9
+ export declare const testErrorSnippetKt = "\n // waiting for view to draw to better represent a captured error with a screenshot\n findViewById<android.view.View>(android.R.id.content).viewTreeObserver.addOnGlobalLayoutListener {\n try {\n throw Exception(\"This app uses Sentry! :)\")\n } catch (e: Exception) {\n Sentry.captureException(e)\n }\n }\n";
10
+ export declare const sourceContext: (orgSlug: string, projectSlug: string) => string;
11
+ export declare const sourceContextKts: (orgSlug: string, projectSlug: string) => string;