locateOrCreateRunnerDir function

Directory locateOrCreateRunnerDir(
  1. String originPath,
  2. String platformSubDir, [
  3. String? customRunnerPath
])

Original function adapted for robustness and using path package.

Tries to find or create a "runner" directory (case-insensitive) within a given base path. Defaults to looking inside originPath/platform/runner.

  • originPath: The root path of the project.
  • platformSubDir: The platform directory name (e.g., "macos", "linux", "windows").
  • customRunnerPath: An optional full path that might already point to or include "runner". If provided, this function will try to use it or find "runner" within it.

This revised function aims to be more robust for locating the conventional runner directory.

Implementation

Directory locateOrCreateRunnerDir(
  String originPath,
  String platformSubDir, [
  String? customRunnerPath,
]) {
  String basePath;

  if (customRunnerPath != null && customRunnerPath.isNotEmpty) {
    basePath = customRunnerPath;
  } else {
    basePath = path.join(originPath, platformSubDir);
  }

  // Normalize using the path package
  basePath = path.normalize(basePath);

  // Check if basePath itself ends with "runner" (case-insensitive)
  if (path.basename(basePath).toLowerCase() == "runner") {
    return Directory(basePath); // basePath is already the runner directory
  }

  // If not, look for a "runner" (or "Runner" on macOS) subdirectory
  String runnerDirName = "runner";
  if (Platform.isMacOS &&
      Directory(path.join(basePath, "Runner")).existsSync()) {
    // Check for macOS "Runner" first if on macOS and it exists
    runnerDirName = "Runner";
  } else if (Directory(path.join(basePath, "runner")).existsSync()) {
    // Check for lowercase "runner" if uppercase wasn't found or not on macOS
    runnerDirName = "runner";
  } else {
    // Neither "Runner" nor "runner" exists as a subdirectory,
    runnerDirName = Platform.isMacOS ? "Runner" : "runner";
  }

  final runnerPath = path.join(basePath, runnerDirName);
  return Directory(runnerPath);
}