Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sentry_cli_version parameter #243

Merged
merged 6 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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? version) {
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, version ?? this.version, name],
buenaflor marked this conversation as resolved.
Show resolved Hide resolved
);
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
6 changes: 5 additions & 1 deletion 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,14 +96,14 @@ class ConfigurationValues {
binDir: sentryArguments['bin_dir'],
binPath: sentryArguments['bin_path'],
sentryCliCdnUrl: sentryArguments['sentry_cli_cdn_url'],
sentryCliVersion: sentryArguments['sentry_cli_version'],
);
}

factory ConfigurationValues.fromReader(ConfigReader configReader) {
return ConfigurationValues(
version: configReader.getString('version'),
name: configReader.getString('name'),

uploadDebugSymbols: configReader.getBool(
'upload_debug_symbols',
deprecatedKey: 'upload_native_symbols',
Expand All @@ -126,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 @@ -179,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
4 changes: 4 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-name');
Expand All @@ -55,6 +56,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 @@ -96,6 +98,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 @@ -136,6 +139,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();
}
Loading