getRipgrepConfig function

RipgrepConfig getRipgrepConfig({
  1. bool? useSystemRipgrep,
  2. bool? isBundledMode,
  3. String? vendorRoot,
})

Get the ripgrep configuration. Determines whether to use system, builtin, or embedded ripgrep. Memoized after first call.

Implementation

RipgrepConfig getRipgrepConfig({
  bool? useSystemRipgrep,
  bool? isBundledMode,
  String? vendorRoot,
}) {
  if (_cachedConfig != null) return _cachedConfig!;

  final wantsSystem =
      useSystemRipgrep ?? _isEnvDefinedFalsy('USE_BUILTIN_RIPGREP');

  // Try system ripgrep if user wants it.
  if (wantsSystem) {
    final systemPath = _findExecutable('rg');
    if (systemPath != null) {
      _cachedConfig = const RipgrepConfig(
        mode: RipgrepMode.system,
        command: 'rg',
        args: [],
      );
      return _cachedConfig!;
    }
  }

  // In bundled (native) mode, ripgrep is statically compiled.
  if (isBundledMode == true) {
    _cachedConfig = RipgrepConfig(
      mode: RipgrepMode.embedded,
      command: Platform.resolvedExecutable,
      args: const ['--no-config'],
      argv0: 'rg',
    );
    return _cachedConfig!;
  }

  // Fall back to vendored binary.
  final rgRoot =
      vendorRoot ??
      p.join(p.dirname(Platform.resolvedExecutable), 'vendor', 'ripgrep');
  final platformDir = '${_getArch()}-${_getPlatformName()}';
  final rgBinary = Platform.isWindows ? 'rg.exe' : 'rg';
  final command = p.join(rgRoot, platformDir, rgBinary);

  _cachedConfig = RipgrepConfig(
    mode: RipgrepMode.builtin,
    command: command,
    args: const [],
  );
  return _cachedConfig!;
}