createFromConfig function

Future<void> createFromConfig(
  1. Config flutterConfigs,
  2. bool generateSettings,
  3. bool mergeHexFiles
)

Create the requested files from the information from provided in the yaml file.

Implementation

Future<void> createFromConfig(
  Config flutterConfigs,
  bool generateSettings,
  bool mergeHexFiles
) async {
  String? key;
  Signing? signer;
  String exportPath = flutterConfigs.exportPath ?? '';
  final iconsDir = createDirIfNotExist(path.join(exportPath));

  // Generates Icons for given platform
  if(flutterConfigs.hasKeyfileConfig && flutterConfigs.keyfileConfig!.generate){
    SigningKeyData data = Signing.generateKey(logger: logger);
    signer = Signing(
      privateKey: data.privateKey,
      publicKey: data.publicKeyPem,
    );
    await saveBytes(
      path: iconsDir.path,
      printName: 'nrfutil_keys',
      fileType: 'zip',
      bytes: data.zipFile
    );
  }
  else if(flutterConfigs.hasKeyfileConfig){
    signer = Signing(
      privateKey: getFirmware(flutterConfigs.keyfileConfig?.privateKey),
      publicKey: getFirmware(flutterConfigs.keyfileConfig?.publicKey),
    );
  }

  if(generateSettings || (flutterConfigs.settingsConfig != null && flutterConfigs.settingsConfig!.generate)){
    BLDFUSettings sett = BLDFUSettings();
    if(flutterConfigs.settingsConfig?.path != null){
      sett.fromHexFile(flutterConfigs.settingsConfig!.path!);
    }
    String value = sett.generate(
      arch: flutterConfigs.settingsConfig?.arch ?? 'NRF52',
      appFile: getFirmware(flutterConfigs.applicationConfig.path),
      sdFile: getFirmware(flutterConfigs.softdeviceConfig.path),
      sdValType: ValidationType.getValTypeFromString(flutterConfigs.settingsConfig?.sdValType),
      appValType: ValidationType.getValTypeFromString(flutterConfigs.settingsConfig?.appValType),
      blSettVersion: flutterConfigs.settingsConfig?.blSettVersion ?? 1,
      blVersion: flutterConfigs.bootloaderConfig.version,
      appVersion: flutterConfigs.applicationConfig.version,
      backupAddress: flutterConfigs.settingsConfig?.backupAddress,
      customBootSettAddr: flutterConfigs.settingsConfig?.customBootSettAddr,
      noBackup: flutterConfigs.settingsConfig?.noBackup ?? false,
      signer: signer
    );

    await saveString(
      printName: 'settings_package',
      fileType: 'hex',
      bytes: value,
      path: iconsDir.path
    );
  }
  else if(
      flutterConfigs.applicationConfig.path != null ||
      flutterConfigs.bootloaderConfig.path != null ||
      flutterConfigs.softdeviceConfig.path != null
  ){
    Uint8List value = await NRFUTIL(
      mode: flutterConfigs.debug?NRFUtilMode.debug:NRFUtilMode.release,
      applicationFirmware: getFirmware(flutterConfigs.applicationConfig.path),
      bootloaderFirmware: getFirmware(flutterConfigs.bootloaderConfig.path),
      softDeviceFirmware: getFirmware(flutterConfigs.softdeviceConfig.path),
      hardwareVersion: flutterConfigs.hardwareVersion,
      bootloaderVersion: flutterConfigs.bootloaderConfig.version,
      applicationVersion: flutterConfigs.applicationConfig.version,
      keyFile: key,
      signer: signer,
      softDeviceReqType: NRFPackage.getSoftDeviceTypesFromString(flutterConfigs.sofDeviceReqType),
      comment: flutterConfigs.comment,
    ).generate();

    await saveBytes(
      printName: 'dfu_package',
      fileType: 'zip',
      bytes: value,
      path: iconsDir.path
    );
  }

  if(mergeHexFiles){
    String? app = getFirmware(flutterConfigs.applicationConfig.path);
    String? sd = getFirmware(flutterConfigs.softdeviceConfig.path);
    String? boot = getFirmware(flutterConfigs.bootloaderConfig.path);
    String? sett = generateSettings?getFirmware('${iconsDir.path}/settings_package.hex'):getFirmware(flutterConfigs.settingsConfig?.path);

    String value = IntelHex.mergeHex([sd,boot,sett,app]);
    if(value != ''){
      await saveString(
        printName: 'merged',
        fileType: 'hex',
        bytes: value,
        path: iconsDir.path
      );
    }
    else{
      stderr.writeln('\n✕ Could not merge files');
      exit(2);
    }
  }
}