class BuildFiles {

  public static JibContainerBuilder toJibContainerBuilder(
      Path projectRoot, Path buildFilePath, Map<String, String> templateParameters)
      throws InvalidImageReferenceException, IOException {
    BuildFileSpec buildFile = toBuildFileSpec(buildFilePath, templateParameters);

    JibContainerBuilder containerBuilder;
    if (buildFile.getFrom().isPresent()) {
      BaseImageSpec from = buildFile.getFrom().get();
      containerBuilder = Jib.from(from.getImage());
      if (!from.getPlatforms().isEmpty()) {
        containerBuilder.setPlatforms(
            from.getPlatforms()
                .stream()
                .map(
                    platformSpec ->
                        new Platform(platformSpec.getArchitecture(), platformSpec.getOs()))
                .collect(Collectors.toSet()));
      }
    } else {
      containerBuilder = Jib.fromScratch();
    }

    buildFile.getCreationTime().ifPresent(containerBuilder::setCreationTime);
    buildFile.getFormat().ifPresent(containerBuilder::setFormat);
    containerBuilder.setEnvironment(buildFile.getEnvironment());
    containerBuilder.setLabels(buildFile.getLabels());
    containerBuilder.setVolumes(buildFile.getVolumes());
    containerBuilder.setExposedPorts(buildFile.getExposedPorts());
    buildFile.getUser().ifPresent(containerBuilder::setUser);
    buildFile.getWorkingDirectory().ifPresent(containerBuilder::setWorkingDirectory);
    buildFile.getEntrypoint().ifPresent(containerBuilder::setEntrypoint);
    buildFile.getCmd().ifPresent(containerBuilder::setProgramArguments);

    if (buildFile.getLayers().isPresent()) {
      containerBuilder.setFileEntriesLayers(
          Layers.toLayers(projectRoot, buildFile.getLayers().get()));
    }
    return containerBuilder;
  }

}