processSafeClasspath static method

Future<List<String>> processSafeClasspath(
  1. List<String> classpath,
  2. String workingDir
)

クラスパスを安全な形式に処理します。 長すぎるエントリはシンボリックリンクで置き換えるか、短縮します。

classpath 処理するクラスパスのリスト workingDir 作業ディレクトリ(シンボリックリンクの作成場所) return 処理されたクラスパスのリスト

Implementation

static Future<List<String>> processSafeClasspath(
  List<String> classpath,
  String workingDir,
) async {
  if (!Platform.isWindows) {
    return classpath; // Windowsでない場合は修正不要
  }

  final processedClasspath = <String>[];
  final symlinkDir = Directory(p.join(workingDir, 'classpath_symlinks'));

  // シンボリックリンク用ディレクトリが存在しない場合は作成
  if (!await symlinkDir.exists()) {
    await symlinkDir.create(recursive: true);
  }

  int symlinkIndex = 0;
  for (final path in classpath) {
    if (path.length <= _classpathEntryMaxLength) {
      // 短いパスはそのまま追加
      processedClasspath.add(path);
      continue;
    }

    try {
      // 長いパスの場合はシンボリックリンクを作成
      final fileName = p.basename(path);
      final symlinkPath = p.join(
        symlinkDir.path,
        'link_${symlinkIndex}_$fileName',
      );

      final linkFile = Link(symlinkPath);
      if (await linkFile.exists()) {
        await linkFile.delete();
      }

      await linkFile.create(
        ensureSafePath(path),
        recursive: true,
      );

      processedClasspath.add(symlinkPath);
      symlinkIndex++;

      debugPrint('Created symlink for long path: $path -> $symlinkPath');
    } catch (e) {
      debugPrint('Error creating symlink for long path: $e');
      // 失敗した場合は元のパスを使用
      processedClasspath.add(ensureSafePath(path));
    }
  }

  return processedClasspath;
}