buildFromDirectory method

Future<ReleaseBundleZip> buildFromDirectory({
  1. Directory? rootDirectory,
  2. String? sourcePath,
  3. String? platform,
})

Implementation

Future<ReleaseBundleZip> buildFromDirectory(
    {Directory? rootDirectory, String? sourcePath, String? platform}) async {
  var configDirectory = this.configDirectory;

  if (rootDirectory == null) {
    if (sourcePath != null) {
      rootDirectory = Directory(joinPaths(configDirectory?.path, sourcePath));
    } else if (configDirectory != null) {
      rootDirectory = configDirectory;
    }
  }

  if (rootDirectory == null) {
    throw ArgumentError("Can't define `rootDirectory`!");
  }

  await prepare(rootDirectory, platform: platform);

  var filesWithCommand =
      getFiles(platform: platform).where((e) => e.hasCommands).toList();

  if (filesWithCommand.isNotEmpty) {
    print('»  Running files commands (${filesWithCommand.length}):');
    for (var f in filesWithCommand) {
      await f.executeCommands(this, rootDirectory, platform: platform);
    }
  }

  var files = rootDirectory.listSync(recursive: true);

  var rootPath = rootDirectory.path;

  print('»  Loading release bundle files from: $rootPath');

  var list = files
      .map((e) {
        var sourcePath = e.path;
        if (sourcePath.startsWith(rootPath)) {
          sourcePath = sourcePath.substring(rootPath.length);
        }

        sourcePath = ReleaseFile.normalizePath(sourcePath);

        var packFile = getFile(sourcePath, platform: platform);

        if (packFile == null) {
          return null;
        }

        var destinyPath = packFile.destinyPath;

        var file = File(e.path);
        var fileType = file.statSync().type;

        var releaseFiles = <ReleaseFile>[];

        if (fileType == FileSystemEntityType.directory) {
          var dir = Directory(file.path);
          var dirPath = dir.path;

          var dirFiles =
              dir.listSync(recursive: true).whereType<File>().toList();

          for (var f in dirFiles) {
            var filePath = f.path;
            if (filePath.startsWith(dirPath)) {
              filePath = filePath.substring(dirPath.length);
              while (filePath.startsWith(pack_path.separator)) {
                filePath = filePath.substring(1);
              }
            }

            var fileDestinyPath = pack_path.join(destinyPath, filePath);

            var data = f.readAsBytesSync();
            var time = f.lastModifiedSync();
            var exec = f.hasExecutablePermission ||
                ReleaseBundleZip.isExecutableFilePath(destinyPath);

            var releaseFile = ReleaseFile(fileDestinyPath, data,
                time: time, executable: exec);

            releaseFiles.add(releaseFile);
          }
        } else {
          var data = file.readAsBytesSync();
          var time = file.lastModifiedSync();
          var exec = file.hasExecutablePermission ||
              ReleaseBundleZip.isExecutableFilePath(destinyPath);

          var releaseFile =
              ReleaseFile(destinyPath, data, time: time, executable: exec);

          releaseFiles.add(releaseFile);
        }

        return releaseFiles;
      })
      .nonNulls
      .expand((e) => e)
      .toList();

  var release = Release(name, version, platform: platform);
  var releaseBundle = ReleaseBundleZip(release, files: list);

  await finalize(rootDirectory,
      releaseBundle: releaseBundle, platform: platform);

  return releaseBundle;
}