-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add sentry properties reader
- Loading branch information
Showing
11 changed files
with
381 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import 'package:properties/properties.dart'; | ||
import 'package:yaml/yaml.dart'; | ||
import 'package:file/file.dart'; | ||
|
||
import '../injector.dart'; | ||
import '../log.dart'; | ||
import 'no_op_config_reader.dart'; | ||
import 'properties_config_reader.dart'; | ||
import 'yaml_config_reader.dart'; | ||
|
||
abstract class ConfigReader { | ||
String? getString(String key, {String? deprecatedKey}); | ||
bool? getBool(String key, {String? deprecatedKey}); | ||
bool contains(String key); | ||
|
||
/// By default this ConfigReader factory will try to load pubspec.yaml first. | ||
/// If the sentry config doesn't exist on pubspec.yaml it will use sentry.properties as fallback. | ||
factory ConfigReader() { | ||
// Attempt to retrieve the config from pubspec.yaml first | ||
final pubspec = getPubspec(); | ||
final sentryConfig = pubspec['sentry'] as YamlMap?; | ||
if (sentryConfig != null) { | ||
Log.info('retrieving config from pubspec.yaml'); | ||
return YamlConfigReader(sentryConfig); | ||
} else { | ||
Log.info('sentry config not found in pubspec.yaml'); | ||
} | ||
|
||
// If sentry config is not found in pubspec.yaml, try loading from sentry.properties | ||
final propertiesFile = injector.get<FileSystem>().file("sentry.properties"); | ||
if (propertiesFile.existsSync()) { | ||
Log.info('retrieving config from sentry.properties'); | ||
// Loads properties class via string as there are issues loading the file | ||
// from path if run in the test suite | ||
final properties = | ||
Properties.fromString(propertiesFile.readAsStringSync()); | ||
return PropertiesConfigReader(properties); | ||
} | ||
Log.error('no config found, please use sentry.properties or pubspec.yaml.'); | ||
return NoOpConfigReader(); | ||
} | ||
|
||
static dynamic getPubspec() { | ||
final file = injector.get<FileSystem>().file("pubspec.yaml"); | ||
if (!file.existsSync()) { | ||
Log.error("Pubspec not found: ${file.absolute.path}"); | ||
return {}; | ||
} | ||
final pubspecString = file.readAsStringSync(); | ||
final pubspec = loadYaml(pubspecString); | ||
return pubspec; | ||
} | ||
} | ||
|
||
extension Config on ConfigReader { | ||
T? get<T>( | ||
String name, String? deprecatedName, T? Function(String key) resolve) { | ||
if (deprecatedName != null && contains(deprecatedName)) { | ||
Log.warn( | ||
'Your config contains `$deprecatedName` which is deprecated. Consider switching to `$name`.'); | ||
} | ||
if (contains(name)) { | ||
return resolve(name); | ||
} else if (deprecatedName != null && contains(deprecatedName)) { | ||
return resolve(deprecatedName); | ||
} else { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'config_reader.dart'; | ||
|
||
class NoOpConfigReader implements ConfigReader { | ||
NoOpConfigReader(); | ||
|
||
@override | ||
bool? getBool(String key, {String? deprecatedKey}) { | ||
return null; | ||
} | ||
|
||
@override | ||
String? getString(String key, {String? deprecatedKey}) { | ||
return null; | ||
} | ||
|
||
@override | ||
bool contains(String key) { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import 'package:properties/properties.dart'; | ||
|
||
import 'config_reader.dart'; | ||
|
||
class PropertiesConfigReader implements ConfigReader { | ||
final Properties _properties; | ||
|
||
PropertiesConfigReader(Properties properties) : _properties = properties; | ||
|
||
@override | ||
bool? getBool(String key, {String? deprecatedKey}) { | ||
return get(key, deprecatedKey, (key) => _properties.getBool((key))); | ||
} | ||
|
||
@override | ||
String? getString(String key, {String? deprecatedKey}) { | ||
return get(key, deprecatedKey, (key) => _properties.get((key))); | ||
} | ||
|
||
@override | ||
bool contains(String key) { | ||
return _properties.contains(key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import 'package:yaml/yaml.dart'; | ||
|
||
import 'config_reader.dart'; | ||
|
||
class YamlConfigReader implements ConfigReader { | ||
final YamlMap? _yamlMap; | ||
|
||
YamlConfigReader(YamlMap? yamlMap) : _yamlMap = yamlMap; | ||
|
||
@override | ||
bool? getBool(String key, {String? deprecatedKey}) { | ||
return get(key, deprecatedKey, (key) => _yamlMap?[key] as bool?); | ||
} | ||
|
||
@override | ||
String? getString(String key, {String? deprecatedKey}) { | ||
return get(key, deprecatedKey, (key) => (_yamlMap?[key]).toString()); | ||
} | ||
|
||
@override | ||
bool contains(String key) { | ||
return _yamlMap?.containsKey(key) ?? false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.