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()) {
    print(
      '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) {
    print('Checking for updates...');
    final latestVersion = Version.parse(getLatestPuroVersion());
    print('Latest Puro version: $latestVersion');

    final currentPuro = await getCurrentPuroVersion();
    final currentVersion = Version.parse(currentPuro.version);
    print(
      '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 {
      print('Puro is up to date.');
    }
  }

  // Setup puro environment
  await _setupFlutterEnvironment(context);

  // Create symlink to puro flutter sdk
  final packageDir = context.packageDir?.root ?? SidekickContext.projectRoot;
  final flutterPath = await puroFlutterSdkPath(packageDir);

  final flutterBinPath = Directory(flutterPath).directory('bin');

  dcli.env['PURO_FLUTTER_BIN'] = flutterBinPath.absolute.path;
  print('Use Puro Flutter SDK: $flutterPath');
  createSymlink(symlinkPath, flutterPath);
}