runPubGetIndirectly static method

Future runPubGetIndirectly(
  1. String forDirectory, {
  2. required StdoutSession stdoutSession,
  3. bool forceUseFlutterTool = false,
})

runs pub get indirectly by creating a temporary package that refers to forDirectory and copying the result back

Implementation

static Future runPubGetIndirectly(
  String forDirectory, {
  required StdoutSession stdoutSession,
  bool forceUseFlutterTool = false,
}) async {
  final forDirectoryPubspecPath = path.join(forDirectory, 'pubspec.yaml');
  final forDirectoryPubspec =
      Pubspec.parse(File(forDirectoryPubspecPath).readAsStringSync());
  final packageName = forDirectoryPubspec.name;
  final isFlutter = forDirectoryPubspec.flutter != null;

  String potentialFlutterDependency = isFlutter
      ? '''
flutter:
  sdk: flutter
'''
      : '';

  final tempDirectory = Directory.systemTemp.createTempSync();
  final tempPackagePath = path.join(tempDirectory.path, 'temp_package');
  final tempPackagePubspecPath = path.join(tempPackagePath, 'pubspec.yaml');
  Directory(tempPackagePath).createSync(recursive: true);
  File(tempPackagePubspecPath).writeAsStringSync(
    '''
name: temp_package
version: 0.0.1
publish_to: none

environment:
sdk: '>=3.5.0 <4.0.0'

dependencies:
$packageName:
  path: $forDirectory
$potentialFlutterDependency
''',
  );
  await DartInteraction.runDartOrFlutterCommand(tempPackagePath,
      args: ['pub', 'get'],
      stdoutSession: stdoutSession,
      forceUseFlutterTool: forceUseFlutterTool);
  await DartInteraction.transferPackageConfig(
    fromPackage: tempPackagePath,
    toPackage: forDirectory,
    stdoutSession: stdoutSession,
    packageNameReplacementInfo: (
      oldPackageName: 'temp_package',
      newPackageName: packageName
    ),
  );
  await Directory(tempDirectory.path).delete(recursive: true);
}