@sentry/wizard
Version:
Sentry wizard helping you to configure your project
286 lines • 13.8 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
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;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNamespace = exports.addGradlePlugin = exports.selectAppFile = void 0;
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
var fs = __importStar(require("fs"));
var clack_utils_1 = require("../utils/clack-utils");
var templates_1 = require("./templates");
var bash = __importStar(require("../utils/bash"));
var Sentry = __importStar(require("@sentry/node"));
// @ts-ignore - clack is ESM and TS complains about that. It works though
var clack = __importStar(require("@clack/prompts"));
var chalk_1 = __importDefault(require("chalk"));
var release_registry_1 = require("../utils/release-registry");
/**
* A Gradle project may contain multiple modules, some of them may be applications, some of them may be libraries.
* We are only interested in applications. For example:
*
* myproject/
* app/
* lib1/
* lib2/
* wearApp/
*
* In this case^ we are interested in app/ and wearApp/
*
* @param buildGradleFiles a list of build.gradle(.kts) paths that contain the com.android.application plugin
* @returns the selected project for setting up
*/
function selectAppFile(buildGradleFiles) {
return __awaiter(this, void 0, void 0, function () {
var appFiles, index, file, text, appFile_1, appFile;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
appFiles = [];
for (index = 0; index < buildGradleFiles.length; index++) {
file = buildGradleFiles[index];
text = fs.readFileSync(file, 'utf8');
if (/\(?["']com\.android\.application["']\)?(?!.*\S)/.test(text)) {
appFiles.push(file);
}
}
if (!(appFiles.length === 0)) return [3 /*break*/, 2];
Sentry.setTag('custom-build-logic', true);
return [4 /*yield*/, (0, clack_utils_1.abortIfCancelled)(clack.text({
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",
placeholder: 'app/build.gradle.kts',
validate: function (value) {
if (!value.includes('.gradle') || !fs.existsSync(value))
return "Not a valid gradle file.";
},
}))];
case 1:
appFile_1 = _a.sent();
return [2 /*return*/, appFile_1];
case 2:
if (!(appFiles.length === 1)) return [3 /*break*/, 3];
Sentry.setTag('multiple-projects', false);
appFile = appFiles[0];
return [3 /*break*/, 5];
case 3:
Sentry.setTag('multiple-projects', true);
return [4 /*yield*/, (0, clack_utils_1.askForItemSelection)(appFiles, 'Which project do you want to add Sentry to?')];
case 4:
appFile = (_a.sent()).value;
_a.label = 5;
case 5:
Sentry.setTag('custom-build-logic', false);
return [2 /*return*/, appFile];
}
});
});
}
exports.selectAppFile = selectAppFile;
/**
* Patches a build.gradle(.kts) file that contains `com.android.application` plugin.
* There are multiple cases we have to handle here:
* - An existing `plugins {}` block:
* - We just have to add our plugin inside the block
* - No existing `plugins {}` block
* - We have to add the entire block in the beginning of the file, BUT *after imports*
*
* For example (2nd case):
*
* ```
* import net.ltgt.gradle.errorprone.errorprone
*
* // our plugins block goes here <--
* plugins {
* id("io.sentry.android.gradle") version "3.12.0"
* }
*
* apply(plugin = "com.android.application")
*
* android {
* ...
* }
* ```
*
* In the end we run `./gradlew` to verify the config is build-able and not broken.
*
* @param appFile the selected Gradle application project
* @returns true if successfully added Sentry Gradle config, false otherwise
*/
function addGradlePlugin(appFile, orgSlug, projectSlug) {
return __awaiter(this, void 0, void 0, function () {
var gradleScript, pluginVersion, pluginsBlockMatch, newGradleScript, regex, importsMatch, insertIndex, insertIndex, buildSpinner, e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
gradleScript = fs.readFileSync(appFile, 'utf8');
if (/\(?["']io\.sentry\.android\.gradle["']\)?/.test(gradleScript)) {
// sentry gradle plugin is already installed
clack.log.success(chalk_1.default.greenBright("".concat(chalk_1.default.bold('Sentry Gradle plugin'), " is already added to the project.")));
maybeAddSourceContextConfig(appFile, gradleScript, orgSlug, projectSlug);
return [2 /*return*/, true];
}
return [4 /*yield*/, (0, release_registry_1.fetchSdkVersion)('sentry.java.android.gradle-plugin')];
case 1:
pluginVersion = _a.sent();
pluginsBlockMatch = /plugins\s*{[^{}]*}/.exec(gradleScript);
if (!pluginsBlockMatch) {
regex = /import\s+[\w.]+/gm;
importsMatch = regex.exec(gradleScript);
insertIndex = 0;
while (importsMatch) {
insertIndex = importsMatch.index + importsMatch[0].length + 1;
importsMatch = regex.exec(gradleScript);
}
if (appFile.endsWith('.kts')) {
newGradleScript =
gradleScript.slice(0, insertIndex) +
(0, templates_1.pluginsBlockKts)(pluginVersion) +
gradleScript.slice(insertIndex);
}
else {
newGradleScript =
gradleScript.slice(0, insertIndex) +
(0, templates_1.pluginsBlock)(pluginVersion) +
gradleScript.slice(insertIndex);
}
}
else {
insertIndex = pluginsBlockMatch.index + pluginsBlockMatch[0].length - 1;
if (appFile.endsWith('.kts')) {
newGradleScript =
gradleScript.slice(0, insertIndex) +
(0, templates_1.pluginKts)(pluginVersion) +
gradleScript.slice(insertIndex);
}
else {
newGradleScript =
gradleScript.slice(0, insertIndex) +
(0, templates_1.plugin)(pluginVersion) +
gradleScript.slice(insertIndex);
}
}
fs.writeFileSync(appFile, newGradleScript, 'utf8');
maybeAddSourceContextConfig(appFile, newGradleScript, orgSlug, projectSlug);
buildSpinner = clack.spinner();
buildSpinner.start('Running ./gradlew to verify changes (this may take a few minutes)...');
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, bash.execute('./gradlew')];
case 3:
_a.sent();
buildSpinner.stop(chalk_1.default.greenBright("".concat(chalk_1.default.bold('Sentry Gradle plugin'), " added to the project.")));
return [3 /*break*/, 5];
case 4:
e_1 = _a.sent();
buildSpinner.stop();
Sentry.captureException('Gradle Sync failed');
return [2 /*return*/, false];
case 5: return [2 /*return*/, true];
}
});
});
}
exports.addGradlePlugin = addGradlePlugin;
/**
* Looks for the applications packageName (namespace) in the specified build.gradle(.kts) file.
*
* ```
* android {
* namespace 'my.package.name' <-- this is what we extract
*
* compileSdkVersion = 31
* ...
* }
* ```
* @param appFile
* @returns the packageName(namespace) of the app if available
*/
function getNamespace(appFile) {
var gradleScript = fs.readFileSync(appFile, 'utf8');
var namespaceMatch = /namespace\s*=?\s*['"]([^'"]+)['"]/i.exec(gradleScript);
if (!namespaceMatch || namespaceMatch.length <= 1) {
clack.log.warn('Unable to determine application package name.');
Sentry.captureException('No package name');
return undefined;
}
var namespace = namespaceMatch[1];
return namespace;
}
exports.getNamespace = getNamespace;
/**
* Adds source context configuration to the gradleScript if `sentry {}` block is not yet configured,
*
* @param appFile
* @param gradleScript
*/
function maybeAddSourceContextConfig(appFile, gradleScript, orgSlug, projectSlug) {
if (!/sentry\s*\{[^}]*\}/i.test(gradleScript)) {
// if no sentry {} block is configured, we add our own with source context enabled
if (appFile.endsWith('.kts')) {
fs.appendFileSync(appFile, (0, templates_1.sourceContextKts)(orgSlug, projectSlug), 'utf8');
}
else {
fs.appendFileSync(appFile, (0, templates_1.sourceContext)(orgSlug, projectSlug), 'utf8');
}
}
}
//# sourceMappingURL=gradle.js.map