Skip to content

Commit

Permalink
Add sentry_cli_version parameter (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
denrase authored Jun 18, 2024
1 parent 70eb0bf commit dedc5d0
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Add support for build files folder parameter ([#235](https://github.com/getsentry/sentry-dart-plugin/pull/235))
- Support SENTRYCLI_CDNURL env ([#230](https://github.com/getsentry/sentry-dart-plugin/pull/230))
- Add `sentry_cli_version` parameter ([#243](https://github.com/getsentry/sentry-dart-plugin/pull/243))

### Fixes

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ ignore_missing=true
| bin_dir | The folder where the plugin downloads the sentry-cli binary | .dart_tool/pub/bin/sentry_dart_plugin (string) | no | - |
| bin_path | Path to the sentry-cli binary to use instead of downloading. Make sure to use the correct version. | null (string) | no | - |
| sentry_cli_cdn_url | Alternative place to download sentry-cli | https://downloads.sentry-cdn.com/sentry-cli (string) | no | SENTRYCLI_CDNURL |

| sentry_cli_version | Override the sentry-cli version that should be downloaded. | (string) | no | - |

## Release

Expand Down
35 changes: 21 additions & 14 deletions lib/src/cli/setup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ class CLISetup {
HostPlatform platform,
String directory,
String downloadUrlPrefix,
String? versionOverride,
) async {
final dir = injector.get<FileSystem>().directory(directory);
await dir.create(recursive: true);
final file = dir.childFile('sentry-cli${platform.executableExtension}');

final source = _sources[platform]!;

if (!await _check(source, file)) {
await _download(source, file, downloadUrlPrefix);
if (!await _check(source, file, versionOverride)) {
await _download(source, file, downloadUrlPrefix, versionOverride);
}

return file.path;
Expand All @@ -35,24 +36,24 @@ class CLISetup {
HostPlatform platform,
String path,
String downloadUrlPrefix,
String? versionOverride,
) async {
final file = injector.get<FileSystem>().file(path);
final source = _sources[platform]!;
if (!await _check(source, file)) {
final downloadUrl = source.formatDownloadUrl(downloadUrlPrefix);
if (!await _check(source, file, versionOverride)) {
final downloadUrl =
source.formatDownloadUrl(downloadUrlPrefix, versionOverride);
Log.warn(
"Download Sentry CLI ${source.version} from '$downloadUrl' and update at path '${file.path}'.");
"Download Sentry CLI ${versionOverride ?? source.version} from '$downloadUrl' and update at path '${file.path}'.");
}
}

Future<void> _download(
CLISource source,
File file,
String downloadUrlPrefix,
) async {
final downloadUrl = source.formatDownloadUrl(downloadUrlPrefix);
Future<void> _download(CLISource source, File file, String downloadUrlPrefix,
String? versionOverride) async {
final downloadUrl =
source.formatDownloadUrl(downloadUrlPrefix, versionOverride);
Log.info(
"Downloading Sentry CLI ${source.version} from $downloadUrl to ${file.path}");
"Downloading Sentry CLI ${versionOverride ?? source.version} from $downloadUrl to ${file.path}");

final client = http.Client();
try {
Expand All @@ -64,18 +65,24 @@ class CLISetup {
client.close();
}

if (await _check(source, file)) {
if (await _check(source, file, versionOverride)) {
Log.info("Sentry CLI downloaded successfully.");
} else {
Log.error(
"Failed to download Sentry CLI: downloaded file doesn't match the expected checksum.");
}
}

Future<bool> _check(CLISource source, File file) async {
Future<bool> _check(
CLISource source, File file, String? versionOverride) async {
if (!await file.exists()) {
return false;
}
if (versionOverride != null) {
Log.info(
"Sentry CLI binary checksum verification skipped due to version override.");
return true;
}

final calculated = await _hash(file);
final expected = source.hash;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/cli/sources.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ class CLISource {

CLISource(this.name, this.version, this.hash);

Uri formatDownloadUrl(String prefix) {
Uri formatDownloadUrl(String prefix, String? versionOverride) {
if (prefix.endsWith('/')) {
prefix = prefix.substring(0, prefix.length - 2);
}

final parsed = Uri.parse(prefix);
final fullUrl = parsed.replace(
pathSegments: [...parsed.pathSegments, version, name],
pathSegments: [...parsed.pathSegments, versionOverride ?? version, name],
);
return fullUrl;
}
Expand Down
8 changes: 6 additions & 2 deletions lib/src/configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ class Configuration {
/// `https://downloads.sentry-cdn.com/sentry-cli`.
late String sentryCliCdnUrl;

/// Override the sentry-cli version that should be downloaded
late String? sentryCliVersion;

/// Loads the configuration values
Future<void> getConfigValues(List<String> cliArguments) async {
const taskName = 'reading config values';
Expand Down Expand Up @@ -153,6 +156,7 @@ class Configuration {
binPath = configValues.binPath;
sentryCliCdnUrl = configValues.sentryCliCdnUrl ??
'https://downloads.sentry-cdn.com/sentry-cli';
sentryCliVersion = configValues.sentryCliVersion;
}

/// Validates the configuration values and log an error if required fields
Expand Down Expand Up @@ -201,7 +205,7 @@ class Configuration {
if (platform != null) {
await injector
.get<CLISetup>()
.check(platform, binPath, sentryCliCdnUrl);
.check(platform, binPath, sentryCliCdnUrl, sentryCliVersion);
} else {
Log.warn('Host platform not supported. Cannot verify Sentry CLI.');
}
Expand Down Expand Up @@ -230,7 +234,7 @@ class Configuration {
}
final cliPath = await injector
.get<CLISetup>()
.download(platform, binDir, sentryCliCdnUrl);
.download(platform, binDir, sentryCliCdnUrl, sentryCliVersion);
if (!Platform.isWindows) {
final result =
await injector.get<ProcessManager>().run(['chmod', '+x', cliPath]);
Expand Down
5 changes: 5 additions & 0 deletions lib/src/configuration_values.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ConfigurationValues {
final String? binDir;
final String? binPath;
final String? sentryCliCdnUrl;
final String? sentryCliVersion;

ConfigurationValues({
this.version,
Expand All @@ -45,6 +46,7 @@ class ConfigurationValues {
this.binDir,
this.binPath,
this.sentryCliCdnUrl,
this.sentryCliVersion,
});

factory ConfigurationValues.fromArguments(List<String> arguments) {
Expand Down Expand Up @@ -94,6 +96,7 @@ class ConfigurationValues {
binDir: sentryArguments['bin_dir'],
binPath: sentryArguments['bin_path'],
sentryCliCdnUrl: sentryArguments['sentry_cli_cdn_url'],
sentryCliVersion: sentryArguments['sentry_cli_version'],
);
}

Expand Down Expand Up @@ -125,6 +128,7 @@ class ConfigurationValues {
binDir: configReader.getString('bin_dir'),
binPath: configReader.getString('bin_path'),
sentryCliCdnUrl: configReader.getString('sentry_cli_cdn_url'),
sentryCliVersion: configReader.getString('sentry_cli_version'),
);
}

Expand Down Expand Up @@ -178,6 +182,7 @@ class ConfigurationValues {
sentryCliCdnUrl: platformEnv.sentryCliCdnUrl ??
args.sentryCliCdnUrl ??
file.sentryCliCdnUrl,
sentryCliVersion: args.sentryCliVersion ?? file.sentryCliVersion,
);
}
}
16 changes: 16 additions & 0 deletions test/cli_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ void main() {
platform,
'.dart_tool/pub/bin/sentry_dart_plugin',
'https://downloads.sentry-cdn.com/sentry-cli',
null,
);
final suffix = platform.name.startsWith('windows') ? '.exe' : '';
expect(file, '.dart_tool/pub/bin/sentry_dart_plugin/sentry-cli$suffix');
expect(fs.file(file).existsSync(), isTrue);
});

test('version override', () async {
final fs = MemoryFileSystem.test();
injector.registerSingleton<FileSystem>(() => fs, override: true);
final cliSetup = CLISetup(sources);
final file = await cliSetup.download(
platform,
'.dart_tool/pub/bin/sentry_dart_plugin',
'https://downloads.sentry-cdn.com/sentry-cli',
'2.0.0',
);
final suffix = platform.name.startsWith('windows') ? '.exe' : '';
expect(file, '.dart_tool/pub/bin/sentry_dart_plugin/sentry-cli$suffix');
Expand Down
5 changes: 5 additions & 0 deletions test/configuration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ void main() {
binDir: 'binDir-args-config',
binPath: 'binPath-args-config',
sentryCliCdnUrl: 'sentryCliCdnUrl-args-config',
sentryCliVersion: '1.0.0-args-config',
);
final fileConfig = ConfigurationValues(
version: 'version-file-config',
Expand All @@ -95,6 +96,7 @@ void main() {
binDir: 'binDir-file-config',
binPath: 'binPath-file-config',
sentryCliCdnUrl: 'sentryCliCdnUrl-file-config',
sentryCliVersion: '1.0.0-file-config',
);

final sut = fixture.getSut(
Expand Down Expand Up @@ -129,6 +131,7 @@ void main() {
expect(sut.binDir, 'binDir-args-config');
expect(sut.binPath, 'binPath-args-config');
expect(sut.sentryCliCdnUrl, 'sentryCliCdnUrl-args-config');
expect(sut.sentryCliVersion, '1.0.0-args-config');
});

test("takes values from file config", () {
Expand All @@ -155,6 +158,7 @@ void main() {
binDir: 'binDir-file-config',
binPath: 'binPath-file-config',
sentryCliCdnUrl: 'sentryCliCdnUrl-file-config',
sentryCliVersion: '1.0.0-file-config',
);

final sut = fixture.getSut(
Expand Down Expand Up @@ -188,6 +192,7 @@ void main() {
expect(sut.binDir, 'binDir-file-config');
expect(sut.binPath, 'binPath-file-config');
expect(sut.sentryCliCdnUrl, 'sentryCliCdnUrl-file-config');
expect(sut.sentryCliVersion, '1.0.0-file-config');
});

test("falls back to default values", () {
Expand Down
5 changes: 5 additions & 0 deletions test/configureation_values_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ void main() {
"--sentry-define=ignore_missing=true",
"--sentry-define=bin_dir=fixture-bin_dir",
"--sentry-define=sentry_cli_cdn_url=fixture-sentry_cli_cdn_url",
"--sentry-define=sentry_cli_version=1.0.0",
];
final sut = ConfigurationValues.fromArguments(arguments);
expect(sut.name, 'fixture-sentry-name');
Expand All @@ -54,6 +55,7 @@ void main() {
expect(sut.ignoreMissing, true);
expect(sut.binDir, 'fixture-bin_dir');
expect(sut.sentryCliCdnUrl, 'fixture-sentry_cli_cdn_url');
expect(sut.sentryCliVersion, '1.0.0');
});

test("fromArguments supports deprecated fields", () {
Expand Down Expand Up @@ -94,6 +96,7 @@ void main() {
ignore_missing: true
bin_dir: fixture-bin_dir
sentry_cli_cdn_url: fixture-sentry_cli_cdn_url
sentry_cli_version: 1.0.0
''';

FileSystem fs = MemoryFileSystem.test();
Expand Down Expand Up @@ -154,6 +157,7 @@ void main() {
ignore_missing=true
bin_dir=fixture-bin_dir
sentry_cli_cdn_url=fixture-sentry_cli_cdn_url
sentry_cli_version=1.0.0
''';

FileSystem fs = MemoryFileSystem.test();
Expand Down Expand Up @@ -194,6 +198,7 @@ void main() {
expect(sut.ignoreMissing, true);
expect(sut.binDir, 'fixture-bin_dir');
expect(sut.sentryCliCdnUrl, 'fixture-sentry_cli_cdn_url');
expect(sut.sentryCliVersion, '1.0.0');
});

test("fromPlatformEnvironment", () {
Expand Down
2 changes: 2 additions & 0 deletions test/plugin_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ class MockCLI implements CLISetup {
HostPlatform platform,
String directory,
String cdnUrl,
String? overrideVersion,
) =>
Future.value(name);

Expand All @@ -448,6 +449,7 @@ class MockCLI implements CLISetup {
HostPlatform platform,
String path,
String cdnUrl,
String? overrideVersion,
) =>
Future.value();
}

0 comments on commit dedc5d0

Please sign in to comment.