Skip to content

Commit

Permalink
Fix/proguard setup (#885)
Browse files Browse the repository at this point in the history
* fix: proguard setup - don't duplicate the rules

* fix: proguard setup - support CRLF

* chore: update changelog

* test: ProGuard setup CRLF support
  • Loading branch information
vaind authored Jul 11, 2022
1 parent 266f79d commit 5053427
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixes

- Fix ProGuard setup if build.gradle uses CRLF (Windows) line breaks ([#885](https://github.com/getsentry/sentry-unity/pull/885))

### Features

- Bump CLI to v2.3.1 ([#875](https://github.com/getsentry/sentry-unity/pull/875))
Expand Down
15 changes: 11 additions & 4 deletions src/Sentry.Unity.Editor/Android/ProguardSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,19 @@ public void AddToGradleProject()
_logger.LogDebug("Writing proguard rule file to {0}.", ruleFile);
File.Copy(Path.GetFullPath($"Packages/{SentryPackageInfo.GetName()}/Plugins/Android/{RuleFileName}"), ruleFile, true);

var gradleNew = Regex.Replace(gradle, @"(\s+consumerProguardFiles .*)", "$1, '" + RuleFileName + "'");
if (gradle.Length == gradleNew.Length)
if (gradle.Contains(RuleFileName))
{
throw new Exception($"Couldn't add Proguard rule {RuleFileName} to {_gradleScriptPath} - no `consumerProguardFiles` found.");
_logger.LogDebug($"Proguard rule {RuleFileName} has already been added to {_gradleScriptPath} `consumerProguardFiles` in a previous build.");
}
else
{
var gradleNew = Regex.Replace(gradle, @"(\s+consumerProguardFiles [^\r\n]*)", "$1, '" + RuleFileName + "'");
if (gradle.Length == gradleNew.Length)
{
throw new Exception($"Couldn't add Proguard rule {RuleFileName} to {_gradleScriptPath} - no `consumerProguardFiles` found.");
}
File.WriteAllText(_gradleScriptPath, gradleNew);
}
File.WriteAllText(_gradleScriptPath, gradleNew);
}

private string LoadGradleScript()
Expand Down
22 changes: 18 additions & 4 deletions test/Sentry.Unity.Editor.Tests/Android/ProguardSetupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,32 @@ public void AddsRuleFile(bool existsBefore)
}

[Test]
public void AddsRuleToGradleScript()
[TestCase("\n")]
[TestCase("\r\n")]
public void AddsRuleToGradleScript(string lineSeparator)
{
var gradleScript = Path.Combine(_outputPath, "build.gradle");
var regex = new Regex($"consumerProguardFiles .*, '{ProguardSetup.RuleFileName}'");

// Update the file to have the expected separators for this test case.
File.WriteAllText(gradleScript, Regex.Replace(File.ReadAllText(gradleScript), "\r?\n", lineSeparator));

// Sanity check that the previous replacement worked.
StringAssert.Contains(lineSeparator, File.ReadAllText(gradleScript));
Assert.AreEqual(49, Regex.Matches(File.ReadAllText(gradleScript), lineSeparator).Count);

var sut = GetSut();

Assert.False(regex.Match(File.ReadAllText(gradleScript)).Success);
var regex = $"consumerProguardFiles [^\r\n]*, '{ProguardSetup.RuleFileName}'";
StringAssert.DoesNotMatch(regex, File.ReadAllText(gradleScript));

sut.AddToGradleProject();

Assert.True(regex.Match(File.ReadAllText(gradleScript)).Success);
var newContent = File.ReadAllText(gradleScript);
StringAssert.IsMatch(regex, newContent);

// Doesn't add again on the second run.
sut.AddToGradleProject();
Assert.AreEqual(File.ReadAllText(gradleScript), newContent);
}

[Test]
Expand Down

0 comments on commit 5053427

Please sign in to comment.