<commit_before>// Copyright (c) 2014, 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.

library docgen.package_helpers;

import 'exports/source_mirrors.dart';
import 'generator.dart' show pubScript;

import 'dart:io';
import 'package:path/path.dart' as path;
import 'package:yaml/yaml.dart';

/// Helper accessor to determine the full pathname of the root of the dart
/// checkout. We can be in one of three situations:
/// 1) Running from pkg/docgen/bin/docgen.dart
/// 2) Running from a snapshot in a build,
///   e.g. xcodebuild/ReleaseIA32/dart-sdk/bin
/// 3) Running from a built distribution,
///   e.g. ...somename/dart-sdk/bin/snapshots
String get rootDirectory {
  if (_rootDirectoryCache != null) return _rootDirectoryCache;
  var scriptDir = path.absolute(path.dirname(Platform.script.toFilePath()));
  var root = scriptDir;
  var base = path.basename(root);
  var found = false;
  // When we find a version file or sdk we are one level below the root.
  while (!found && base != 'sdk' && base != 'pkg') {
    root = path.dirname(root);
    base = path.basename(root);
    // Look for something that looks like the dart-sdk directory, which we
    // expect to be in the path above us and to have a 'version' file containing
    // the same version as we are running.
    if (hasMatchingVersionFile(root)) {
      found = true;
    }
    if (root == base) {
      // We have reached the root of the filesystem without finding anything.
      throw new FileSystemException("Cannot find SDK directory starting from ",
          scriptDir);
    }
  }
  _rootDirectoryCache = path.dirname(root);
  return _rootDirectoryCache;
}
String _rootDirectoryCache;


/// Does this directory contain a version file that has the same version as
/// we do.
bool hasMatchingVersionFile(String root) {
  var versionFile = new File(path.join(root, 'version'));
  if (!versionFile.existsSync()) return false;
  var version = versionFile.readAsStringSync().trim();
  var vmVersion = Platform.version;
  return vmVersion.startsWith(version);
}

/// Given a LibraryMirror that is a library, return the name of the directory<commit_msg>Revert revision 41812 "Let docgen detect a dart-sdk directory that's differently named"<commit_after>// Copyright (c) 2014, 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.

library docgen.package_helpers;

import 'exports/source_mirrors.dart';
import 'generator.dart' show pubScript;

import 'dart:io';
import 'package:path/path.dart' as path;
import 'package:yaml/yaml.dart';

/// Helper accessor to determine the full pathname of the root of the dart
/// checkout. We can be in one of three situations:
/// 1) Running from pkg/docgen/bin/docgen.dart
/// 2) Running from a snapshot in a build,
///   e.g. xcodebuild/ReleaseIA32/dart-sdk/bin
/// 3) Running from a built distribution,
///   e.g. ...somename/dart-sdk/bin/snapshots
String get rootDirectory {
  if (_rootDirectoryCache != null) return _rootDirectoryCache;
  var scriptDir = path.absolute(path.dirname(Platform.script.toFilePath()));
  var root = scriptDir;
  var base = path.basename(root);
  // When we find dart-sdk or sdk we are one level below the root.
  while (base != 'dart-sdk' && base != 'sdk' && base != 'pkg') {
    root = path.dirname(root);
    base = path.basename(root);
    if (root == base) {
      // We have reached the root of the filesystem without finding anything.
      throw new FileSystemException("Cannot find SDK directory starting from ",
          scriptDir);
    }
  }
  _rootDirectoryCache = path.dirname(root);
  return _rootDirectoryCache;
}
String _rootDirectoryCache;

/// Given a LibraryMirror that is a library, return the name of the directory
{'fraction_non_alphanumeric': 0.10586154623547246, 'fraction_numerical': 0.005811015664477008, 'mean_word_length': 3.5349369988545245, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3091768', 'n_tokens_mistral': 1176, 'n_tokens_neox': 1141, 'n_words': 446}