Skip to content

Commit 9993d26

Browse files
authored
Merge pubspec.yaml and sentry.properties values (#295)
1 parent 142dbb1 commit 9993d26

File tree

5 files changed

+115
-15
lines changed

5 files changed

+115
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Enhancements
6+
7+
- Merge `pubspec.yaml` and `sentry.properties` values ([#295](https://github.com/getsentry/sentry-dart-plugin/pull/295))
8+
59
### Dependencies
610

711
- Bump CLI from v2.39.1 to v2.41.1 ([#290](https://github.com/getsentry/sentry-dart-plugin/pull/290))

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ dart run sentry_dart_plugin
3030
## Configuration (Optional)
3131

3232
This tool comes with a default configuration. You can configure it to suit your needs.
33-
By default the plugin will look for the Sentry configuration in the `pubspec.yaml` file.
34-
If the configuration doesn't exist, the plugin will look for a `sentry.properties` file.
35-
If the `sentry.properties` file doesn't exist, the plugin will look for environment variables.
33+
By default the plugin will look for the Sentry configuration in the `pubspec.yaml` and `sentry.properties` file.
34+
If a Sentry value does not exist in `pubspec.yaml`, the plugin will fallback to `sentry.properties` file.
35+
If a value exists in both, the `pubspec.yaml` takes precedence over the `sentry.properties` values.
36+
Environment and argument variables will take precedence over the file based ones.
3637

3738
### pubspec.yaml
3839

lib/src/utils/config-reader/config_reader.dart

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:properties/properties.dart';
2+
import 'package:sentry_dart_plugin/src/utils/config-reader/fallback_config_reader.dart';
23
import 'package:yaml/yaml.dart';
34
import 'package:file/file.dart';
45

@@ -13,31 +14,39 @@ abstract class ConfigReader {
1314
bool? getBool(String key, {String? deprecatedKey});
1415
bool contains(String key);
1516

16-
/// By default this ConfigReader factory will try to load pubspec.yaml first.
17-
/// If the sentry config doesn't exist on pubspec.yaml it will use sentry.properties as fallback.
17+
/// This factory will try to load both pubspec.yaml and sentry.properties.
18+
/// If a sentry config key doesn't exist on pubspec.yaml it will use sentry.properties as fallback.
1819
factory ConfigReader() {
19-
// Attempt to retrieve the config from pubspec.yaml first
20+
YamlConfigReader? pubspecReader;
21+
22+
Log.info('Searching for pubspec.yaml or sentry.properties config...');
23+
2024
final pubspec = getPubspec();
2125
final sentryConfig = pubspec['sentry'] as YamlMap?;
2226
if (sentryConfig != null) {
23-
Log.info('retrieving config from pubspec.yaml');
24-
return YamlConfigReader(sentryConfig);
25-
} else {
26-
Log.info('sentry config not found in pubspec.yaml');
27+
Log.info('Found config from pubspec.yaml');
28+
pubspecReader = YamlConfigReader(sentryConfig);
2729
}
2830

29-
// If sentry config is not found in pubspec.yaml, try loading from sentry.properties
31+
PropertiesConfigReader? propertiesReader;
32+
3033
final propertiesFile = injector.get<FileSystem>().file("sentry.properties");
3134
if (propertiesFile.existsSync()) {
32-
Log.info('retrieving config from sentry.properties');
35+
Log.info('Found config from sentry.properties');
3336
// Loads properties class via string as there are issues loading the file
3437
// from path if run in the test suite
3538
final properties =
3639
Properties.fromString(propertiesFile.readAsStringSync());
37-
return PropertiesConfigReader(properties);
40+
propertiesReader = PropertiesConfigReader(properties);
41+
}
42+
43+
if (pubspecReader == null && propertiesReader == null) {
44+
Log.info(
45+
'No file config found. Reading values from arguments or environment.');
46+
return NoOpConfigReader();
47+
} else {
48+
return FallbackConfigReader(pubspecReader, propertiesReader);
3849
}
39-
Log.error('no config found, please use sentry.properties or pubspec.yaml.');
40-
return NoOpConfigReader();
4150
}
4251

4352
static dynamic getPubspec() {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import 'config_reader.dart';
2+
3+
class FallbackConfigReader implements ConfigReader {
4+
FallbackConfigReader(this._configReader, this._fallbackConfigReader);
5+
6+
final ConfigReader? _configReader;
7+
final ConfigReader? _fallbackConfigReader;
8+
9+
@override
10+
bool? getBool(String key, {String? deprecatedKey}) {
11+
return _configReader?.getBool(key, deprecatedKey: deprecatedKey) ??
12+
_fallbackConfigReader?.getBool(key, deprecatedKey: deprecatedKey);
13+
}
14+
15+
@override
16+
String? getString(String key, {String? deprecatedKey}) {
17+
return _configReader?.getString(key, deprecatedKey: deprecatedKey) ??
18+
_fallbackConfigReader?.getString(key, deprecatedKey: deprecatedKey);
19+
}
20+
21+
@override
22+
bool contains(String key) {
23+
return _configReader?.contains(key) ??
24+
_fallbackConfigReader?.contains(key) ??
25+
false;
26+
}
27+
}

test/configureation_values_test.dart renamed to test/configuration_values_test.dart

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,65 @@ void main() {
207207
expect(sut.sentryCliVersion, '1.0.0');
208208
});
209209

210+
test('from config reader pubspec & properties', () {
211+
final sentryPubspec = '''
212+
version: pubspec-version
213+
name: pubspec-name
214+
upload_debug_symbols: true
215+
upload_source_maps: true
216+
''';
217+
218+
final sentryProperties = '''
219+
version=properties-version
220+
url=properties-url
221+
upload_debug_symbols=false
222+
upload_sources=true
223+
''';
224+
225+
FileSystem fs = MemoryFileSystem.test();
226+
fs.currentDirectory = fs.directory('/subdir')..createSync();
227+
injector.registerSingleton<FileSystem>(() => fs, override: true);
228+
229+
final propertiesConfig = ConfigFormatter.formatConfig(
230+
sentryProperties,
231+
ConfigFileType.sentryProperties,
232+
null,
233+
);
234+
final propertiesWriter = ConfigWriter(fs, 'fixture-name');
235+
propertiesWriter.write(
236+
'fixture-version',
237+
ConfigFileType.sentryProperties,
238+
propertiesConfig,
239+
);
240+
241+
final pubspecConfig = ConfigFormatter.formatConfig(
242+
sentryPubspec,
243+
ConfigFileType.pubspecYaml,
244+
null,
245+
);
246+
final pubspecWriter = ConfigWriter(fs, 'fixture-name');
247+
pubspecWriter.write(
248+
'fixture-version',
249+
ConfigFileType.pubspecYaml,
250+
pubspecConfig,
251+
);
252+
253+
final reader = ConfigReader();
254+
final sut = ConfigurationValues.fromReader(reader);
255+
256+
// string
257+
258+
expect(sut.version, 'pubspec-version'); // pubspec before properties
259+
expect(sut.name, 'pubspec-name'); // pubspec only
260+
expect(sut.url, 'properties-url'); // properties only
261+
262+
// bool
263+
264+
expect(sut.uploadDebugSymbols, true); // pubspec before properties
265+
expect(sut.uploadSourceMaps, true); // pubspec only
266+
expect(sut.uploadSources, true); // properties only
267+
});
268+
210269
test("fromPlatformEnvironment", () {
211270
final arguments = {
212271
'SENTRY_RELEASE': 'fixture-release',

0 commit comments

Comments
 (0)