initializePuro function

Future<void> initializePuro(
  1. SdkInitializerContext context
)

Implementation

Future<void> initializePuro(SdkInitializerContext context) async {
  // Create folder for flutter sdk symlink
  final symlinkPath = flutterSdkSymlink();

  Directory? puroRootDir;
  final puroPath = getPuroPath();
  bool checkForUpdates = false;

  // Check if puro is already installed
  if (puroPath != null && puroPath.existsSync()) {
    printVerbose(
      'Puro is already installed at ${puroPath.parent.parent.absolute.path}',
    );
    puroRootDir = puroPath.parent.parent;
    checkForUpdates = true;
  } else {
    // Install puro
    puroRootDir = installPuro();
  }
  dcli.env['PURO_ROOT'] = puroRootDir.absolute.path;

  // Check if puro is up to date
  if (checkForUpdates && !_alreadyCheckedForUpdate) {
    _alreadyCheckedForUpdate = true;
    printVerbose('Checking for updates...');
    final latestVersion = Version.parse(getLatestPuroVersion());
    printVerbose('Latest Puro version: $latestVersion');

    final currentPuro = await getCurrentPuroVersion();
    final currentVersion = Version.parse(currentPuro.version);
    printVerbose(
      'Current Puro version: $currentVersion - Standalone: ${currentPuro.standalone}',
    );
    if (currentVersion < latestVersion) {
      print('Puro is outdated. Updating to $latestVersion');
      if (currentPuro.standalone) {
        puroRootDir = installPuro();
      } else {
        await puro(['upgrade-puro'], progress: Progress.print());
        puroRootDir = puroPath!.parent.parent;
      }
      dcli.env['PURO_ROOT'] = puroRootDir.absolute.path;
    } else {
      printVerbose('Puro is up to date.');
    }
  }

  // getting the puro flutter sdk path only work when no bin override is set
  // https://github.com/pingbird/puro/blob/46d9753ffe8e60f0efa7256fad8e5efbf107e39a/puro/lib/src/config.dart#L147
  dcli.env['PURO_FLUTTER_BIN'] = null;

  final packageDir = context.packageDir?.root ?? SidekickContext.projectRoot;
  final versions = await VersionParser(
    packagePath: packageDir,
    projectRoot: SidekickContext.projectRoot,
    useBeta: true,
  ).getMaxFlutterSdkVersionFromPubspec();

  // Setup puro environment
  await _createPuroEnvironment(packageDir, versions.flutterVersion);
  await _binPuroToProject(packageDir, versions.flutterVersion);

  // Create symlink to puro flutter sdk
  final flutterPath = await puroFlutterSdkPath(packageDir);
  final flutterBinPath = Directory(flutterPath).directory('bin');
  printVerbose('Use Puro Flutter SDK: $flutterPath');

  dcli.env['PURO_FLUTTER_BIN'] = flutterBinPath.absolute.path;
  createSymlink(symlinkPath, flutterPath);

  print(
    'Using Flutter ${versions.flutterVersion} (Dart ${versions.dartVersion})',
  );
}