Search in sources :

Example 1 with ComponentToWorkspaceApplier

use of org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentToWorkspaceApplier in project che-server by eclipse-che.

the class DevfileConverter method devFileToWorkspaceConfig.

/**
 * Converts given {@link Devfile} into {@link WorkspaceConfigImpl workspace config}.
 *
 * @param devfile initial devfile
 * @param contentProvider content provider for recipe-type component or plugin references
 * @return constructed workspace config
 * @throws DevfileException when general devfile error occurs
 * @throws DevfileException when devfile requires additional files content but the specified
 *     content provider does not support it
 * @throws DevfileFormatException when devfile format is invalid
 * @throws DevfileRecipeFormatException when content of the file specified in recipe type
 *     component is empty or its format is invalid
 */
public WorkspaceConfigImpl devFileToWorkspaceConfig(DevfileImpl devfile, FileContentProvider contentProvider) throws DevfileException {
    checkArgument(devfile != null, "Devfile must not be null");
    checkArgument(contentProvider != null, "Content provider must not be null");
    // make copy to avoid modification of original devfile
    devfile = new DevfileImpl(devfile);
    validateCurrentVersion(devfile);
    defaultEditorProvisioner.apply(devfile, contentProvider);
    WorkspaceConfigImpl config = new WorkspaceConfigImpl();
    config.setName(devfile.getName());
    for (Command command : devfile.getCommands()) {
        CommandImpl com = commandConverter.toWorkspaceCommand(command, contentProvider);
        if (com != null) {
            config.getCommands().add(com);
        }
    }
    // so, commands should be already converted
    for (ComponentImpl component : devfile.getComponents()) {
        ComponentToWorkspaceApplier applier = componentTypeToApplier.get(component.getType());
        if (applier == null) {
            throw new DevfileException(String.format("Devfile contains component `%s` with type `%s` that can not be converted to workspace", getIdentifiableComponentName(component), component.getType()));
        }
        applier.apply(config, component, contentProvider);
    }
    for (ProjectImpl project : devfile.getProjects()) {
        ProjectConfigImpl projectConfig = projectConverter.toWorkspaceProject(project);
        config.getProjects().add(projectConfig);
    }
    config.getAttributes().putAll(devfile.getAttributes());
    config.setDevfile(devfile);
    return config;
}
Also used : CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) Command(org.eclipse.che.api.core.model.workspace.devfile.Command) DevfileImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl) ProjectImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ProjectImpl) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) ComponentToWorkspaceApplier(org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentToWorkspaceApplier) ProjectConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ProjectConfigImpl)

Example 2 with ComponentToWorkspaceApplier

use of org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentToWorkspaceApplier in project devspaces-images by redhat-developer.

the class DevfileConverter method devFileToWorkspaceConfig.

/**
 * Converts given {@link Devfile} into {@link WorkspaceConfigImpl workspace config}.
 *
 * @param devfile initial devfile
 * @param contentProvider content provider for recipe-type component or plugin references
 * @return constructed workspace config
 * @throws DevfileException when general devfile error occurs
 * @throws DevfileException when devfile requires additional files content but the specified
 *     content provider does not support it
 * @throws DevfileFormatException when devfile format is invalid
 * @throws DevfileRecipeFormatException when content of the file specified in recipe type
 *     component is empty or its format is invalid
 */
public WorkspaceConfigImpl devFileToWorkspaceConfig(DevfileImpl devfile, FileContentProvider contentProvider) throws DevfileException {
    checkArgument(devfile != null, "Devfile must not be null");
    checkArgument(contentProvider != null, "Content provider must not be null");
    // make copy to avoid modification of original devfile
    devfile = new DevfileImpl(devfile);
    validateCurrentVersion(devfile);
    defaultEditorProvisioner.apply(devfile, contentProvider);
    WorkspaceConfigImpl config = new WorkspaceConfigImpl();
    config.setName(devfile.getName());
    for (Command command : devfile.getCommands()) {
        CommandImpl com = commandConverter.toWorkspaceCommand(command, contentProvider);
        if (com != null) {
            config.getCommands().add(com);
        }
    }
    // so, commands should be already converted
    for (ComponentImpl component : devfile.getComponents()) {
        ComponentToWorkspaceApplier applier = componentTypeToApplier.get(component.getType());
        if (applier == null) {
            throw new DevfileException(String.format("Devfile contains component `%s` with type `%s` that can not be converted to workspace", getIdentifiableComponentName(component), component.getType()));
        }
        applier.apply(config, component, contentProvider);
    }
    for (ProjectImpl project : devfile.getProjects()) {
        ProjectConfigImpl projectConfig = projectConverter.toWorkspaceProject(project);
        config.getProjects().add(projectConfig);
    }
    config.getAttributes().putAll(devfile.getAttributes());
    config.setDevfile(devfile);
    return config;
}
Also used : CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) Command(org.eclipse.che.api.core.model.workspace.devfile.Command) DevfileImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl) ProjectImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ProjectImpl) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) ComponentToWorkspaceApplier(org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentToWorkspaceApplier) ProjectConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ProjectConfigImpl)

Aggregations

Command (org.eclipse.che.api.core.model.workspace.devfile.Command)2 ComponentToWorkspaceApplier (org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentToWorkspaceApplier)2 DevfileException (org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)2 CommandImpl (org.eclipse.che.api.workspace.server.model.impl.CommandImpl)2 ProjectConfigImpl (org.eclipse.che.api.workspace.server.model.impl.ProjectConfigImpl)2 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)2 ComponentImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl)2 DevfileImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl)2 ProjectImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.ProjectImpl)2