blob: 4192e3f6c248debb5f50866880c02a6186658d43 [file] [log] [blame]
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/generated/source.dart';
import 'package:path/path.dart';
String fileUriToNormalizedPath(Context context, Uri fileUri) {
assert(fileUri.isScheme('file'));
var path = context.fromUri(fileUri);
path = context.normalize(path);
return path;
}
/// If the [absoluteUri] is a `file` URI that has corresponding `package` URI,
/// return it. If the URI is not valid, e.g. has empty path segments, so
/// does not represent a valid file path, return `null`.
Uri? rewriteFileToPackageUri(SourceFactory sourceFactory, Uri absoluteUri) {
// Only file URIs get rewritten into package URIs.
if (!absoluteUri.isScheme('file')) {
return absoluteUri;
}
// It must be a valid URI, e.g. `file:///home/` is not.
var pathSegments = absoluteUri.pathSegments;
if (pathSegments.isEmpty || pathSegments.last.isEmpty) {
return null;
}
// We ask for Source only because `restoreUri` needs it.
// TODO(scheglov) Add more direct way to convert a path to URI.
var source = sourceFactory.forUri2(absoluteUri);
if (source == null) {
return null;
}
return sourceFactory.restoreUri(source);
}