Skip to content

Commit

Permalink
FORGE-1566: Add dependency to an addon given its ID
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Mar 14, 2014
1 parent 02e71ae commit 453300f
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@

/**
* Creates Furnace Addon projects
*
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*
*/
@SuppressWarnings("unchecked")
public class AddonProjectConfigurator
Expand Down Expand Up @@ -93,7 +93,7 @@ public void setupSimpleAddonProject(Project project, Version forgeVersion, Itera

/**
* Create a Furnace Project with the full structure (api,impl,tests,spi and addon)
*
*
* @throws FacetNotFoundException
* @throws FileNotFoundException
*/
Expand Down Expand Up @@ -177,15 +177,12 @@ private void generateReadme(Project project)
child.setContents(readmeTemplate);
}

private void installSelectedAddons(final Project project, Iterable<AddonId> addons, boolean managed)
public void installSelectedAddons(final Project project, Iterable<AddonId> addons, boolean managed)
{
if (addons != null)
for (AddonId addon : addons)
{
String[] mavenCoords = addon.getName().split(":");
DependencyBuilder dependency = DependencyBuilder.create().setGroupId(mavenCoords[0])
.setArtifactId(mavenCoords[1])
.setVersion(addon.getVersion().toString()).setClassifier(FORGE_ADDON_CLASSIFIER);
Dependency dependency = toDependency(addon);
if (managed)
{
dependencyInstaller.installManaged(project, dependency);
Expand All @@ -197,6 +194,24 @@ private void installSelectedAddons(final Project project, Iterable<AddonId> addo
}
}

public Dependency toDependency(AddonId addon)
{
String[] mavenCoords = addon.getName().split(":");
Dependency dependency = DependencyBuilder.create().setGroupId(mavenCoords[0])
.setArtifactId(mavenCoords[1])
.setVersion(addon.getVersion().toString()).setClassifier(FORGE_ADDON_CLASSIFIER);
return dependency;
}

/**
* Checks if the {@link Project} depends on the provided {@link AddonId}
*/
public boolean dependsOnAddon(final Project project, AddonId addonId)
{
Dependency dependency = toDependency(addonId);
return dependencyInstaller.isInstalled(project, dependency);
}

private Project createSubmoduleProject(final Project parent, String moduleName, String artifactId,
Class<? extends ProjectFacet>... requiredProjectFacets)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* Copyright 2014 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.addons.ui;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.inject.Inject;

import org.jboss.forge.addon.addons.facets.AddonClassifierFacet;
import org.jboss.forge.addon.addons.project.AddonProjectConfigurator;
import org.jboss.forge.addon.facets.constraints.FacetConstraint;
import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.projects.ProjectFactory;
import org.jboss.forge.addon.projects.facets.MetadataFacet;
import org.jboss.forge.addon.projects.ui.AbstractProjectCommand;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.UICompleter;
import org.jboss.forge.addon.ui.input.UIInput;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.metadata.WithAttributes;
import org.jboss.forge.addon.ui.result.Result;
import org.jboss.forge.addon.ui.result.Results;
import org.jboss.forge.addon.ui.util.Categories;
import org.jboss.forge.addon.ui.util.Metadata;
import org.jboss.forge.furnace.Furnace;
import org.jboss.forge.furnace.addons.AddonId;
import org.jboss.forge.furnace.repositories.AddonRepository;
import org.jboss.forge.furnace.util.Strings;

/**
* Adds an addon dependency to the current project
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
@FacetConstraint(AddonClassifierFacet.class)
public class AddAddonDependencyCommand extends AbstractProjectCommand
{
@Inject
private ProjectFactory projectFactory;

@Inject
private AddonProjectConfigurator configurator;

@Inject
private Furnace furnace;

@Inject
@WithAttributes(label = "Addon Coordinates", description = "Addon coordinates to be added as a dependency for the selected project", required = true)
private UIInput<AddonId> addon;

@Override
public void initializeUI(UIBuilder builder) throws Exception
{
final Set<AddonId> addonChoices = new TreeSet<AddonId>();
Project project = getSelectedProject(builder);
for (AddonRepository repository : furnace.getRepositories())
{
for (AddonId id : repository.listEnabled())
{
// TODO: Furnace should provide some way to detect if an addon is a Container type
boolean isContainerAddon = id.getName().contains("org.jboss.forge.furnace.container");
if (!isContainerAddon && !configurator.dependsOnAddon(project, id))
{
addonChoices.add(id);
}
}
}
addon.setCompleter(new UICompleter<AddonId>()
{

@Override
public Iterable<AddonId> getCompletionProposals(UIContext context, InputComponent<?, AddonId> input,
String value)
{
List<AddonId> addons = new ArrayList<>();
for (AddonId addonId : addonChoices)
{
if (Strings.isNullOrEmpty(value) || value.startsWith(addonId.toCoordinates()))
{
addons.add(addonId);
}
}
return addons;
}
});
builder.add(addon);
}

@Override
public Result execute(UIExecutionContext context) throws Exception
{
Project project = getSelectedProject(context);
AddonId addonId = addon.getValue();
configurator.installSelectedAddons(project, Collections.singleton(addonId), false);
return Results.success("Addon " + addonId + " added as a dependency to project "
+ project.getFacet(MetadataFacet.class).getProjectName());
}

@Override
protected boolean isProjectRequired()
{
return true;
}

@Override
public UICommandMetadata getMetadata(UIContext context)
{
return Metadata.forCommand(NewUICommandWizard.class)
.name("Addon: Add Dependency")
.description("Adds the provided addon as a dependency to the selected project")
.category(Categories.create("Forge", "Setup"));
}

@Override
protected ProjectFactory getProjectFactory()
{
return projectFactory;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@

/**
* Called when the Next button is pressed and the {@link FurnaceAddonProjectType} is selected in NewProjectWizard
*
*
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*
*/
public class FurnaceAddonSetupStep extends AbstractUICommand implements UIWizardStep
{
Expand Down

0 comments on commit 453300f

Please sign in to comment.