Skip to content

Commit 0561306

Browse files
Zycon42HazAT
authored andcommitted
fix: Parse current build variant from task name
Fixes #255
1 parent 9001c5c commit 0561306

File tree

1 file changed

+93
-77
lines changed

1 file changed

+93
-77
lines changed

sentry.gradle

Lines changed: 93 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import org.apache.tools.ant.taskdefs.condition.Os
22

3+
import java.util.regex.Matcher
4+
import java.util.regex.Pattern
5+
36
def config = project.hasProperty("sentry") ? project.sentry : [];
47

58
gradle.projectsEvaluated {
69
def releases = [:];
710
android.applicationVariants.each { variant ->
811
def releaseName = "${variant.getApplicationId()}-${variant.getVersionName()}";
912
variant.outputs.each { output ->
10-
def processTask = output.getProcessResources();
1113
def versionCode = output.getVersionCode();
12-
releases[releaseName] = [output.getName(), releaseName, versionCode];
14+
releases[variant.getName()] = [output.getName(), releaseName, versionCode];
1315
}
1416
}
15-
1617
// separately we then hook into the bundle task of react native to inject
1718
// sourcemap generation parameters. In case for whatever reason no release
1819
// was found for the asset folder we just bail.
@@ -59,85 +60,100 @@ gradle.projectsEvaluated {
5960
// .findAll{!['class', 'active'].contains(it.key)}
6061
// .join('\n')
6162

63+
def currentVariant = "";
64+
def pattern = Pattern.compile("bundle([A-Z][A-Za-z]+)JsAndAssets")
65+
Matcher matcher = pattern.matcher(bundleTask.name)
66+
if (matcher.find()) {
67+
def match = matcher.group(1);
68+
currentVariant = match.substring(0, 1).toLowerCase() + match.substring(1);
69+
}
70+
71+
def currentRelease = null;
6272
releases.each { key, release ->
63-
def variant = release[0]
64-
def releaseName = release[1]
65-
def versionCodes = release[2]
66-
67-
def cliTask = tasks.create(
68-
name: bundleTask.getName() + variant + "SentryUpload",
69-
type: Exec) {
70-
description = "upload debug symbols to sentry"
71-
72-
def propertiesFile = "$reactRoot/android/sentry.properties";
73-
if (config.flavorAware) {
74-
propertiesFile = "$reactRoot/android/sentry-$variant"+".properties"
75-
println "For $variant using: $propertiesFile"
76-
} else {
77-
environment("SENTRY_PROPERTIES", propertiesFile)
78-
}
79-
Properties sentryProps = new Properties();
80-
try {
81-
sentryProps.load(new FileInputStream(propertiesFile));
82-
} catch (FileNotFoundException e) {
83-
println "File not found: $propertiesFile"
84-
}
85-
def cliExecutable = sentryProps.get("cli.executable", "$reactRoot/node_modules/sentry-cli-binary/bin/sentry-cli");
86-
87-
workingDir reactRoot
88-
89-
def args = [
90-
cliExecutable
91-
];
92-
if (config.logLevel) {
93-
args.push("--log-level");
94-
args.push(config.logLevel);
95-
}
96-
97-
if (config.flavorAware) {
98-
args.push("--url");
99-
args.push(sentryProps.get("defaults.url"));
100-
args.push("--auth-token");
101-
args.push(sentryProps.get("auth.token"));
102-
}
103-
104-
args.push("react-native");
105-
args.push("gradle");
106-
args.push("--bundle");
107-
args.push(bundleOutput);
108-
args.push("--sourcemap");
109-
args.push(sourcemapOutput);
110-
args.push("--release");
111-
args.push(releaseName);
112-
113-
if (config.flavorAware) {
114-
args.push("--org");
115-
args.push(sentryProps.get("defaults.org"));
116-
args.push("--project");
117-
args.push(sentryProps.get("defaults.project"));
118-
}
119-
120-
versionCodes.each { versionCode ->
121-
args.add("--dist");
122-
args.add(versionCode);
123-
}
124-
125-
if (config.logLevel) {
126-
println args
127-
}
128-
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
129-
commandLine("cmd", "/c", *args)
130-
} else {
131-
commandLine(*args)
132-
}
133-
enabled true
73+
if (key == currentVariant) {
74+
currentRelease = release;
75+
}
76+
}
77+
78+
if (currentRelease == null) return;
79+
80+
def variant = currentRelease[0]
81+
def releaseName = currentRelease[1]
82+
def versionCodes = currentRelease[2]
83+
84+
def cliTask = tasks.create(
85+
name: bundleTask.getName() + variant + "SentryUpload",
86+
type: Exec) {
87+
description = "upload debug symbols to sentry"
88+
89+
def propertiesFile = "$reactRoot/android/sentry.properties";
90+
if (config.flavorAware) {
91+
propertiesFile = "$reactRoot/android/sentry-$variant"+".properties"
92+
println "For $variant using: $propertiesFile"
93+
} else {
94+
environment("SENTRY_PROPERTIES", propertiesFile)
95+
}
96+
Properties sentryProps = new Properties();
97+
try {
98+
sentryProps.load(new FileInputStream(propertiesFile));
99+
} catch (FileNotFoundException e) {
100+
println "File not found: $propertiesFile"
134101
}
102+
def cliExecutable = sentryProps.get("cli.executable", "$reactRoot/node_modules/sentry-cli-binary/bin/sentry-cli");
103+
104+
workingDir reactRoot
135105

136-
bundleTask.doLast {
137-
cliTask.execute();
106+
def args = [
107+
cliExecutable
108+
];
109+
if (config.logLevel) {
110+
args.push("--log-level");
111+
args.push(config.logLevel);
138112
}
139113

140-
cliTask.dependsOn(bundleTask)
114+
if (config.flavorAware) {
115+
args.push("--url");
116+
args.push(sentryProps.get("defaults.url"));
117+
args.push("--auth-token");
118+
args.push(sentryProps.get("auth.token"));
119+
}
120+
121+
args.push("react-native");
122+
args.push("gradle");
123+
args.push("--bundle");
124+
args.push(bundleOutput);
125+
args.push("--sourcemap");
126+
args.push(sourcemapOutput);
127+
args.push("--release");
128+
args.push(releaseName);
129+
130+
if (config.flavorAware) {
131+
args.push("--org");
132+
args.push(sentryProps.get("defaults.org"));
133+
args.push("--project");
134+
args.push(sentryProps.get("defaults.project"));
135+
}
136+
137+
versionCodes.each { versionCode ->
138+
args.add("--dist");
139+
args.add(versionCode);
140+
}
141+
142+
if (config.logLevel) {
143+
println args
144+
}
145+
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
146+
commandLine("cmd", "/c", *args)
147+
} else {
148+
commandLine(*args)
149+
}
150+
enabled true
141151
}
152+
153+
bundleTask.doLast {
154+
cliTask.execute();
155+
}
156+
157+
cliTask.dependsOn(bundleTask)
142158
}
143159
}

0 commit comments

Comments
 (0)