Skip to content

Commit

Permalink
FORGE-2425: Migrated git addon to simple container
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Aug 6, 2015
1 parent 29c3b53 commit 8f880fe
Show file tree
Hide file tree
Showing 21 changed files with 253 additions and 247 deletions.
2 changes: 1 addition & 1 deletion git/addon/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

<dependency>
<groupId>org.jboss.forge.furnace.container</groupId>
<artifactId>cdi</artifactId>
<artifactId>simple</artifactId>
<classifier>forge-addon</classifier>
<scope>provided</scope>
</dependency>
Expand Down
2 changes: 1 addition & 1 deletion git/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<dependency>
<groupId>org.jboss.forge.furnace.container</groupId>
<artifactId>cdi</artifactId>
<artifactId>simple</artifactId>
<classifier>forge-addon</classifier>
<scope>provided</scope>
</dependency>
Expand Down
2 changes: 1 addition & 1 deletion git/impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<dependency>
<groupId>org.jboss.forge.furnace.container</groupId>
<artifactId>cdi</artifactId>
<artifactId>simple</artifactId>
<classifier>forge-addon</classifier>
<scope>provided</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,47 @@

package org.jboss.forge.addon.git.facet;

import javax.inject.Inject;

import org.jboss.forge.addon.dependencies.Dependency;
import org.jboss.forge.addon.dependencies.builder.DependencyBuilder;
import org.jboss.forge.addon.facets.AbstractFacet;
import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.projects.dependencies.DependencyInstaller;
import org.jboss.forge.furnace.addons.AddonRegistry;
import org.jboss.forge.furnace.container.simple.lifecycle.SimpleContainer;

/**
* @author <a href="mailto:jevgeni.zelenkov@gmail.com">Jevgeni Zelenkov</a>
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
public class GitAPIFacetImpl extends AbstractFacet<Project> implements GitAPIFacet
public class GitAPIFacetImpl extends AbstractFacet<Project>implements GitAPIFacet
{
private static final Dependency GIT_DEPENDENCY = DependencyBuilder.create()
.setGroupId("org.eclipse.jgit")
.setArtifactId("org.eclipse.jgit.pgm");

@Inject
public DependencyInstaller installer;

@Override
public boolean install()
{
installer.install(getFaceted(), GIT_DEPENDENCY);
getDependencyInstaller().install(getFaceted(), GIT_DEPENDENCY);
return true;
}

@Override
public boolean isInstalled()
{
return installer.isInstalled(getFaceted(), GIT_DEPENDENCY);
return getDependencyInstaller().isInstalled(getFaceted(), GIT_DEPENDENCY);
}

private DependencyInstaller getDependencyInstaller()
{
if (installer == null)
{
AddonRegistry addonRegistry = SimpleContainer.getFurnace(getClass().getClassLoader()).getAddonRegistry();
installer = addonRegistry.getServices(DependencyInstaller.class).get();
}
return installer;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@

import static org.jboss.forge.addon.git.constants.GitConstants.GIT_DIRECTORY;

import javax.inject.Inject;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.InitCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.jboss.forge.addon.facets.AbstractFacet;
import org.jboss.forge.addon.git.GitUtils;
import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.resource.DirectoryResource;
import org.jboss.forge.addon.resource.FileResource;
Expand All @@ -24,30 +23,29 @@
* @author <a href="mailto:jevgeni.zelenkov@gmail.com">Jevgeni Zelenkov</a>
*
*/
public class GitFacetImpl extends AbstractFacet<Project> implements GitFacet
public class GitFacetImpl extends AbstractFacet<Project>implements GitFacet
{

@Inject
private GitUtils gitUtils;
static final Logger logger = Logger.getLogger(GitFacetImpl.class.getName());

@Override
public boolean install()
{
Project project = getFaceted();
// init git repo
final DirectoryResource rootDirectory = project.getRootDirectory();
FileResource<?> gitDir = rootDirectory.getChildDirectory(GIT_DIRECTORY).reify(FileResource.class);
DirectoryResource rootDirectory = project.getRoot().reify(DirectoryResource.class);
FileResource<?> gitDir = rootDirectory.getChild(GIT_DIRECTORY).reify(FileResource.class);
if (!gitDir.exists())
{
InitCommand init = Git.init();
init.setDirectory(rootDirectory.getUnderlyingResourceObject());

try
{
gitUtils.close(init.call());
init.call().close();
}
catch (GitAPIException e)
catch (Exception e)
{
logger.log(Level.SEVERE, "Error while initializing directory", e);
}
}

Expand All @@ -57,7 +55,8 @@ public boolean install()
@Override
public boolean isInstalled()
{
return getFaceted().getRootDirectory().getChildDirectory(GIT_DIRECTORY).exists();
DirectoryResource root = getFaceted().getRoot().reify(DirectoryResource.class);
return root != null && root.getChildDirectory(GIT_DIRECTORY).exists();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import java.util.LinkedList;
import java.util.List;

import javax.inject.Inject;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.jboss.forge.addon.facets.AbstractFacet;
Expand All @@ -32,19 +30,15 @@
import org.jboss.forge.addon.resource.Resource;
import org.jboss.forge.addon.resource.ResourceFactory;
import org.jboss.forge.addon.resource.ResourceFilter;
import org.jboss.forge.furnace.addons.AddonRegistry;
import org.jboss.forge.furnace.container.simple.lifecycle.SimpleContainer;
import org.jboss.forge.furnace.util.Streams;

@FacetConstraint(GitFacet.class)
public class GitIgnoreFacetImpl extends AbstractFacet<Project> implements GitIgnoreFacet
public class GitIgnoreFacetImpl extends AbstractFacet<Project>implements GitIgnoreFacet
{

@Inject
private GitIgnoreConfig config;

@Inject
private GitUtils gitUtils;

@Inject
private ResourceFactory factory;

@Override
Expand All @@ -53,24 +47,22 @@ public boolean install()
try
{
DirectoryResource cloneDir = cloneDir();
String repo = config.remoteRepository();
// TODO ShellMessages.info(shell, "Cloning " + repo + " into " + cloneDir.getFullyQualifiedName());
Git git = gitUtils.clone(cloneDir, repo);
gitUtils.close(git);
String repo = getIgnoreConfig().remoteRepository();
Git git = getGitUtils().clone(cloneDir, repo);
getGitUtils().close(git);
return true;
}
catch (Exception e)
{
// TODO ShellMessages.error(shell, "Failed to checkout gitignore: " + e);
return false;
}
}

@Override
public boolean isInstalled()
{
File clone = config.localRepository();
Resource<File> cloneDir = factory.create(clone);
File clone = getIgnoreConfig().localRepository();
Resource<File> cloneDir = getResourceFactory().create(clone);
return cloneDir.exists() && cloneDir.getChild(GIT_DIRECTORY).exists();
}

Expand Down Expand Up @@ -105,9 +97,10 @@ public String contentOf(String template)
@Override
public void update() throws IOException, GitAPIException
{
Git git = gitUtils.git(cloneDir());
gitUtils.pull(git, 10000);
gitUtils.close(git);
try (Git git = getGitUtils().git(cloneDir()))
{
getGitUtils().pull(git, 10000);
}
}

private List<String> listGitignores(DirectoryResource dir)
Expand All @@ -132,7 +125,37 @@ public boolean accept(Resource<?> resource)

private DirectoryResource cloneDir()
{
return factory.create(DirectoryResource.class, config.localRepository());
return getResourceFactory().create(DirectoryResource.class, getIgnoreConfig().localRepository());
}

private ResourceFactory getResourceFactory()
{
if (factory == null)
{
AddonRegistry addonRegistry = SimpleContainer.getFurnace(getClass().getClassLoader()).getAddonRegistry();
factory = addonRegistry.getServices(ResourceFactory.class).get();
}
return factory;
}

private GitIgnoreConfig getIgnoreConfig()
{
if (config == null)
{
AddonRegistry addonRegistry = SimpleContainer.getFurnace(getClass().getClassLoader()).getAddonRegistry();
config = addonRegistry.getServices(GitIgnoreConfig.class).get();
}
return config;
}

private GitUtils getGitUtils()
{
if (gitUtils == null)
{
AddonRegistry addonRegistry = SimpleContainer.getFurnace(getClass().getClassLoader()).getAddonRegistry();
gitUtils = addonRegistry.getServices(GitUtils.class).get();
}
return gitUtils;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@

import java.io.File;

import javax.inject.Inject;

import org.jboss.forge.addon.configuration.Configuration;
import org.jboss.forge.addon.configuration.ConfigurationFactory;
import org.jboss.forge.furnace.addons.AddonRegistry;
import org.jboss.forge.furnace.container.simple.lifecycle.SimpleContainer;

public class GitIgnoreConfig
{

@Inject
private ConfigurationFactory configFactory;

public String defaultRemoteRepository()
Expand Down Expand Up @@ -67,7 +65,17 @@ public void setLocalRepository(String location)

private Configuration userConfig()
{
return configFactory.getUserConfiguration();
return getConfigFactory().getUserConfiguration();
}

private ConfigurationFactory getConfigFactory()
{
if (configFactory == null)
{
AddonRegistry addonRegistry = SimpleContainer.getFurnace(getClass().getClassLoader()).getAddonRegistry();
configFactory = addonRegistry.getServices(ConfigurationFactory.class).get();
}
return configFactory;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@

import static org.jboss.forge.addon.git.constants.GitConstants.GITIGNORE;

import javax.inject.Inject;

import org.jboss.forge.addon.facets.FacetFactory;
import org.jboss.forge.addon.git.GitUtils;
import org.jboss.forge.addon.git.gitignore.resources.GitIgnoreResource;
import org.jboss.forge.addon.projects.ProjectFactory;
import org.jboss.forge.addon.projects.ui.AbstractProjectCommand;
import org.jboss.forge.addon.resource.DirectoryResource;
import org.jboss.forge.addon.resource.ResourceFactory;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.input.InputComponentFactory;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.util.Categories;
import org.jboss.forge.addon.ui.util.Metadata;
import org.jboss.forge.furnace.addons.AddonRegistry;
import org.jboss.forge.furnace.container.simple.lifecycle.SimpleContainer;

abstract class AbstractGitCommand extends AbstractProjectCommand
{

@Inject
private ProjectFactory projectFactory;
private InputComponentFactory inputComponentFactory;
private GitUtils gitUtils;
private ResourceFactory resourceFactory;
private FacetFactory facetFactory;

@Override
public UICommandMetadata getMetadata(UIContext context)
Expand All @@ -39,12 +47,17 @@ public void initializeUI(UIBuilder builder) throws Exception
@Override
protected ProjectFactory getProjectFactory()
{
if (projectFactory == null)
{
AddonRegistry addonRegistry = SimpleContainer.getFurnace(getClass().getClassLoader()).getAddonRegistry();
projectFactory = addonRegistry.getServices(ProjectFactory.class).get();
}
return projectFactory;
}

protected GitIgnoreResource gitIgnoreResource(UIContext context)
{
GitIgnoreResource resource = getSelectedProject(context).getRootDirectory().getChildOfType(
GitIgnoreResource resource = getSelectedProject(context).getRoot().reify(DirectoryResource.class).getChildOfType(
GitIgnoreResource.class,
GITIGNORE);
if (resource == null || !resource.exists())
Expand All @@ -58,4 +71,44 @@ protected boolean isGitIgnoreSelected(UIContext context)
{
return context.getInitialSelection().get() instanceof GitIgnoreResource;
}

protected InputComponentFactory getInputComponentFactory()
{
if (inputComponentFactory == null)
{
AddonRegistry addonRegistry = SimpleContainer.getFurnace(getClass().getClassLoader()).getAddonRegistry();
inputComponentFactory = addonRegistry.getServices(InputComponentFactory.class).get();
}
return inputComponentFactory;
}

protected GitUtils getGitUtils()
{
if (gitUtils == null)
{
AddonRegistry addonRegistry = SimpleContainer.getFurnace(getClass().getClassLoader()).getAddonRegistry();
gitUtils = addonRegistry.getServices(GitUtils.class).get();
}
return gitUtils;
}

protected ResourceFactory getResourceFactory()
{
if (resourceFactory == null)
{
AddonRegistry addonRegistry = SimpleContainer.getFurnace(getClass().getClassLoader()).getAddonRegistry();
resourceFactory = addonRegistry.getServices(ResourceFactory.class).get();
}
return resourceFactory;
}

protected FacetFactory getFacetFactory()
{
if (facetFactory == null)
{
AddonRegistry addonRegistry = SimpleContainer.getFurnace(getClass().getClassLoader()).getAddonRegistry();
facetFactory = addonRegistry.getServices(FacetFactory.class).get();
}
return facetFactory;
}
}

0 comments on commit 8f880fe

Please sign in to comment.