Files
sdk/pkg/analyzer_utilities/lib/package_root.dart
T
Konstantin Shcheglov 76387a9c5b Support for google3 locations in 'packageRoot' and KernelCompilationService.
Change-Id: I90910bc3c966f648ade107d10d4c7f593c67c7a4
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/242162
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
2022-04-25 19:46:13 +00:00

38 lines
1.5 KiB
Dart

// Copyright (c) 2017, 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 'dart:io';
import 'package:path/path.dart' as pathos;
/// Returns a path to the directory containing source code for packages such as
/// kernel, front_end, and analyzer.
String get packageRoot {
// If the package root directory is specified on the command line using
// -DpkgRoot=..., use it.
const pkgRootVar =
bool.hasEnvironment('pkgRoot') ? String.fromEnvironment('pkgRoot') : null;
if (pkgRootVar != null) {
var path = pathos.join(Directory.current.path, pkgRootVar);
if (!path.endsWith(pathos.separator)) path += pathos.separator;
return path;
}
// Otherwise try to guess based on the script path.
var scriptPath = pathos.fromUri(Platform.script);
var parts = pathos.split(scriptPath);
var pkgIndex = parts.indexOf('pkg');
if (pkgIndex != -1) {
return pathos.joinAll(parts.sublist(0, pkgIndex + 1)) + pathos.separator;
}
// Try google3 environment. We expect that all packages that will be
// accessed via this root are configured in the BUILD file, and located
// inside this single root.
final runFiles = Platform.environment['RUNFILES'];
final analyzerPackagesRoot = Platform.environment['ANALYZER_PACKAGES_ROOT'];
if (runFiles != null && analyzerPackagesRoot != null) {
return pathos.join(runFiles, analyzerPackagesRoot);
}
throw StateError('Unable to find sdk/pkg/ in $scriptPath');
}