// Copyright (c) 2019, 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.md file.

import 'dart:io' show Directory, File, Platform, Process, ProcessResult;

String computeRepoDir() {
  ProcessResult result = Process.runSync(
      'git', ['rev-parse', '--show-toplevel'],
      runInShell: true,
      workingDirectory: new File.fromUri(Platform.script).parent.path);
  if (result.exitCode != 0) {
    throw "Git returned non-zero error code (${result.exitCode}):\n\n"
        "stdout: ${result.stdout}\n\n"
        "stderr: ${result.stderr}";
  }
  String dirPath = (result.stdout as String).trim();
  if (!new Directory(dirPath).existsSync()) {
    throw "The path returned by git ($dirPath) does not actually exist.";
  }
  return dirPath;
}

Uri computeRepoDirUri() {
  String dirPath = computeRepoDir();
  return new Directory(dirPath).uri;
}

// Copyright (c) 2019, 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.md file.

import 'dart:io' show Directory, File, Platform, Process, ProcessResult;

import 'package:_fe_analyzer_shared/src/util/filenames.dart';

String computeRepoDir() {
  Uri uri;
  if (Platform.script.hasAbsolutePath) {
    uri = Platform.script;
  } else if (Platform.packageConfig != null) {
    String packageConfig = Platform.packageConfig!;
    final String prefix = "file://";
    if (packageConfig.startsWith(prefix)) {
      uri = Uri.parse(packageConfig);
    } else {
      uri = Uri.base.resolve(nativeToUriPath(packageConfig));
    }
  } else {
    throw "Can't obtain the path to the SDK either via "
        "Platform.script or Platform.packageConfig";
  }
  String path = new File.fromUri(uri).parent.path;
  ProcessResult result = Process.runSync(
      'git', ['rev-parse', '--show-toplevel'],
      runInShell: true, workingDirectory: path);
  if (result.exitCode != 0) {
    throw "Git returned non-zero error code (${result.exitCode}):\n\n"
        "stdout: ${result.stdout}\n\n"
        "stderr: ${result.stderr}";
  }
  String dirPath = (result.stdout as String).trim();
  if (!new Directory(dirPath).existsSync()) {
    throw "The path returned by git ($dirPath) does not actually exist.";
  }
  return dirPath;
}

Uri computeRepoDirUri() {
  String dirPath = computeRepoDir();
  return new Directory(dirPath).uri;
}

Use 'packageConfig' instead of 'script' in an internal tool
[cfe] Use 'packageConfig' instead of 'script' in an internal tool

Change-Id: Ibcad91ddbaeb5b466c09a55c2be31f2a705d895f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/210461
Reviewed-by: Jens Johansen <05223a3c6bdf76af6f84755eae231abeebcf4530@google.com>
Commit-Queue: Dmitry Stefantsov <95a4b7e39916a54bd6477bec6a6de014b977db32@google.com>
