getImportDirectives method
Implementation
List<String> getImportDirectives(
{Uri? uri, String? source, bool alsoImportOriginalFile = false}) {
if (uri != null && source != null) {
throw ArgumentError(
"either uri or source must be non-null, but not both");
}
if (uri == null && source == null) {
throw ArgumentError(
"either uri or source must be non-null, but not both");
}
if (alsoImportOriginalFile == true && uri == null) {
throw ArgumentError(
"flag 'alsoImportOriginalFile' may only be set if 'uri' is also set");
}
var fileUri = resolveUri(uri!);
final text = source ?? File.fromUri(fileUri).readAsStringSync();
final importRegex = RegExp("import [\\'\\\"]([^\\'\\\"]*)[\\'\\\"];");
final imports = importRegex.allMatches(text).map((m) {
var importedUri = Uri.parse(m.group(1)!);
if (importedUri.scheme != "package" && !importedUri.isAbsolute) {
throw ArgumentError(
"Cannot resolve relative URIs in file located at $uri. "
"Replace imported URIs with package or absolute URIs");
}
return text.substring(m.start, m.end);
}).toList();
if (alsoImportOriginalFile) {
imports.add("import '${uri}';");
}
return imports;
}