<commit_before>/*
 * The MIT License (MIT)
 * Copyright (c) 2016 <NAME>
 */

library websockets.environments.tools;


@GlobalQuantifyCapability(_ENV_LIBRARY_PREFIX, reflector)
import "package:reflectable/reflectable.dart";

import "environment_exception.dart";


const _ENV_LIBRARY_PREFIX = r"websockets.env.";


const Reflectable reflector = const WebSocketsEnvReflector();


LibraryMirror findLibrary(String library) {
  try {
    return reflector.findLibrary(library);
  } catch (_) {
    // TODO(nweiz): narrow the catch clause when issue 18532 is fixed.
    return null;
  }
}

/**
 * Since we got a GlobalQuantityCapability on all environment libraries,
 * we can find one if it is included.
 */
bool _envTested = false;
LibraryMirror _presentEnv;
LibraryMirror get presentEnv {
  if(!_envTested) {
    _presentEnv = reflector.libraries.values.firstWhere(
      (mirror) => mirror.qualifiedName.startsWith(_ENV_LIBRARY_PREFIX),
      orElse: () => null);
    _envTested = true;
  }
  return _presentEnv;
}

createWebSocketWithPresentEnv(String url,
  Iterable<String> protocols,
  Map<String, dynamic> headers) {
  LibraryMirror env = presentEnv;
  if(env == null)
    throw exception("No environment present!");
  // env classes only have a single class declared
  assert(env.declarations.length == 1);
  return newWebSocketInstance(env.declarations[env.declarations.keys.single],
    url, protocols, headers);
}

/**
 * Invoke the static connect method of the given WebSocket subclass.
 */
newWebSocketInstance(ClassMirror envClass, String url,<commit_msg>Fix regex to match env libraries<commit_after>/*
 * The MIT License (MIT)
 * Copyright (c) 2016 <NAME>
 */

library websockets.environments.tools;


@GlobalQuantifyCapability(_ENV_LIBRARY_PREFIX, reflector)
import "package:reflectable/reflectable.dart";

import "environment_exception.dart";


const _ENV_LIBRARY_PREFIX = r"^websockets\.env\..*$";
final _ENV_LIBRARY_PREFIX_REGEX = new RegExp(_ENV_LIBRARY_PREFIX);


const Reflectable reflector = const WebSocketsEnvReflector();


LibraryMirror findLibrary(String library) {
  try {
    return reflector.findLibrary(library);
  } catch (_) {
    // TODO(nweiz): narrow the catch clause when issue 18532 is fixed.
    return null;
  }
}

/**
 * Since we got a GlobalQuantityCapability on all environment libraries,
 * we can find one if it is included.
 */
bool _envTested = false;
LibraryMirror _presentEnv;
LibraryMirror get presentEnv {
  if(!_envTested) {
    _presentEnv = reflector.libraries.values.firstWhere(
      (mirror) => _ENV_LIBRARY_PREFIX_REGEX.hasMatch(mirror.qualifiedName),
      orElse: () => null);
    _envTested = true;
  }
  return _presentEnv;
}

createWebSocketWithPresentEnv(String url,
  Iterable<String> protocols,
  Map<String, dynamic> headers) {
  LibraryMirror env = presentEnv;
  if(env == null)
    throw exception("No environment present!");
  // env classes only have a single class declared
  assert(env.declarations.length == 1);
  return newWebSocketInstance(env.declarations[env.declarations.keys.single],
    url, protocols, headers);
}

/**
 * Invoke the static connect method of the given WebSocket subclass.
 */
newWebSocketInstance(ClassMirror envClass, String url,
{'fraction_non_alphanumeric': 0.10235732009925558, 'fraction_numerical': 0.00620347394540943, 'mean_word_length': 4.638111888111888, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '7367899', 'n_tokens_mistral': 1031, 'n_tokens_neox': 974, 'n_words': 258}