Skip to content

Commit

Permalink
FORGEPLUGINS-124 Allow installation of the scaffold via Facets API
Browse files Browse the repository at this point in the history
This ensures that Event<InstallFacets>.fire would install the scaffold provider. The changes also ensure that the provider is enabled only when it has been set up instead of the project merely having the required facets.
  • Loading branch information
VineetReynolds committed Aug 9, 2013
1 parent a4a3028 commit fb3e660
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 32 deletions.
110 changes: 87 additions & 23 deletions src/main/java/org/jboss/forge/scaffold/angularjs/AngularScaffold.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
*/
package org.jboss.forge.scaffold.angularjs;

import static org.jboss.forge.scaffold.angularjs.ResourceProvider.getEntityTemplates;
import static org.jboss.forge.scaffold.angularjs.ResourceProvider.getGlobalTemplates;
import static org.jboss.forge.scaffold.angularjs.ResourceProvider.getStatics;
import static org.jboss.forge.scaffold.angularjs.ResourceProvider.*;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -31,9 +29,11 @@
import org.jboss.forge.env.Configuration;
import org.jboss.forge.parser.java.JavaClass;
import org.jboss.forge.parser.java.JavaSource;
import org.jboss.forge.parser.java.util.Strings;
import org.jboss.forge.project.facets.BaseFacet;
import org.jboss.forge.project.facets.DependencyFacet;
import org.jboss.forge.project.facets.MetadataFacet;
import org.jboss.forge.project.facets.PackagingFacet;
import org.jboss.forge.project.facets.WebResourceFacet;
import org.jboss.forge.project.facets.events.InstallFacets;
import org.jboss.forge.resources.DirectoryResource;
Expand All @@ -49,6 +49,7 @@
import org.jboss.forge.scaffoldx.facets.ScaffoldTemplateFacet;
import org.jboss.forge.scaffoldx.freemarker.FreemarkerClient;
import org.jboss.forge.scaffoldx.freemarker.TemplateLoaderConfig;
import org.jboss.forge.shell.PromptType;
import org.jboss.forge.shell.ShellMessages;
import org.jboss.forge.shell.ShellPrintWriter;
import org.jboss.forge.shell.ShellPrompt;
Expand Down Expand Up @@ -93,13 +94,13 @@ public class AngularScaffold extends BaseFacet implements ScaffoldProvider {

@Inject
private Event<CopyWebResourcesEvent> copyResourcesEvent;

@Inject
private Event<ProcessWithFreemarkerEvent> processWithFreemarkerEvent;

@Inject
private ResourceRegistry resourceRegistry;

@Inject
public AngularScaffold(final ShellPrompt prompt, final ShellPrintWriter writer,
final MetawidgetInspectorFacade metawidgetInspectorFacade, final InspectionResultProcessor angularResultEnhancer,
Expand All @@ -113,14 +114,39 @@ public AngularScaffold(final ShellPrompt prompt, final ShellPrintWriter writer,
}

@Override
@SuppressWarnings("unchecked")
public boolean install() {
// Required facet installation is already handled by the class-level @RequiresFacet annotation.
if (!isInstalled()) {
this.installTemplatesEvent.fire(new InstallFacets(WebResourceFacet.class, DependencyFacet.class,
PersistenceFacet.class, EJBFacet.class, CDIFacet.class, RestFacet.class));
String targetDir = selectTargetDir(this, null);
this.setup(targetDir, false , false);
}
return true;
}

@Override
@SuppressWarnings("unchecked")
public boolean isInstalled() {
return true;
if (project.hasAllFacets(WebResourceFacet.class, DependencyFacet.class, PersistenceFacet.class, EJBFacet.class,
CDIFacet.class, RestFacet.class)) {
String targetDir = configuration.getString(getTargetDirConfigKey(this));
if (targetDir == null) {
return false;
}
WebResourceFacet web = this.project.getFacet(WebResourceFacet.class);
boolean areResourcesInstalled = web.getWebResource(targetDir + GLYPHICONS_WHITE_PNG).exists()
&& web.getWebResource(targetDir + GLYPHICONS_PNG).exists()
&& web.getWebResource(targetDir + FORGE_LOGO_PNG).exists()
&& web.getWebResource(targetDir + ANGULAR_RESOURCE_JS).exists()
&& web.getWebResource(targetDir + ANGULAR_JS).exists()
&& web.getWebResource(targetDir + JQUERY_JS).exists()
&& web.getWebResource(targetDir + BOOTSTRAP_RESPONSIVE_CSS).exists()
&& web.getWebResource(targetDir + MAIN_CSS).exists()
&& web.getWebResource(targetDir + BOOTSTRAP_CSS).exists();
return areResourcesInstalled;
}
return false;
}

/**
Expand All @@ -145,15 +171,15 @@ public List<Resource<?>> setup(String targetDir, boolean overwrite, boolean inst
/**
* Generates the application's index aka landing page, among others. All artifacts that are generated once per scaffolding
* run are generated here.
*
*
* @param filteredClasses The list of {@link JavaClass}es that were scaffolded.
* @param targetDir The target directory for the generated scaffold artifacts.
* @param overwrite A flag that indicates whether existing resources should be overwritten or not.
* @return A list of generated {@link Resource}s
*/
public List<Resource<?>> generateIndex(List<JavaClass> filteredClasses, String targetDir, boolean overwrite) {
ArrayList<Resource<?>> result = new ArrayList<Resource<?>>();

/*
* TODO: Revert this change at a later date, if necessary. This is currently done to ensure that entities are picked up
* during invocation of the plugin from the Forge wizard in JBDS.
Expand All @@ -176,7 +202,7 @@ public boolean accept(Resource<?> resource)
return true;
}
};

WebResourceFacet web = this.project.getFacet(WebResourceFacet.class);
List<Resource<?>> resources = web.getWebResource(targetDir + "/views/").listResources(filter);
List<String> entityNames = new ArrayList<String>();
Expand Down Expand Up @@ -215,7 +241,7 @@ public List<Resource<?>> generateFrom(List<Resource<?>> resources, String target

// TODO: Provide a 'utility' class for allowing transliteration across language naming schemes
// We need this to use contextual naming schemes instead of performing toLowerCase etc. in FTLs.

// Prepare the Freemarker data model
Map<String, Object> root = new HashMap<String, Object>();
root.put("entityName", klass.getName());
Expand Down Expand Up @@ -278,7 +304,7 @@ private void displaySkippingResourceMsg(final JavaSource<?> entity) {
* Filters the provided {@link Resource}s to return a list of {@link JavaClass}es that will be inspected and scaffolded.
* {@link Resource}s that are not {@link JavaResource}s will be ignored. {@link JavaResource}s that are not JPA entities
* will also be ignored.
*
*
* @param targets The user-provided list of {@link Resource}s to be scaffolded.
* @return A list of {@link JavaClass}es that will be inspected and scaffolded.
*/
Expand Down Expand Up @@ -315,7 +341,7 @@ private List<JavaClass> filterResources(List<Resource<?>> targets) {
/**
* Obtains the root path for REST resources so that the AngularJS resource factory will be generated with the correct REST
* resource URL.
*
*
* @return The root path of the REST resources generated by the Forge REST plugin.
*/
private String getRootResourcePath() {
Expand All @@ -342,7 +368,7 @@ private void installTemplates() {
if (!project.hasFacet(ScaffoldTemplateFacet.class)) {
installTemplatesEvent.fire(new InstallFacets(ScaffoldTemplateFacet.class));
}

ScaffoldTemplateFacet templates = project.getFacet(ScaffoldTemplateFacet.class);
// Obtain a reference to the scaffold directory in the classpath
URL resource = getClass().getClassLoader().getResource("scaffold");
Expand All @@ -369,11 +395,11 @@ private void installTemplates() {
}
}
}

/**
* Produces a {@link TemplateLoaderConfig} used to configure the {@link FreemarkerClient} instance. If a template directory
* is present in the current project, it would be used over the provider-supplied templates.
*
*
* @return A {@link TemplateLoaderConfig} instance used to configure the {@link FreemarkerClient} on the locations to load
* the Freemarker templates.
*/
Expand All @@ -385,10 +411,10 @@ public TemplateLoaderConfig createTemplateLoaderConfig()
File templateBaseDir = getTemplateBaseDir(providerName);
return new TemplateLoaderConfig(templateBaseDir, getClass(), SCAFFOLD_DIR);
}

/**
* Provides the location of the templates directory for the specified scaffold provider.
*
* Provides the location of the templates directory for the specified scaffold provider.
*
* @param scaffoldProviderName The name of the scaffold provider. Used to distinguish between multiple scaffold providers
* installed in the project.
* @return The location of the templates directory if it is present in the current project. A null value is returned if the
Expand All @@ -405,12 +431,12 @@ private File getTemplateBaseDir(String scaffoldProviderName)
}
return null;
}

/**
* Provided a target directory, this method calculates the parent directories to re-create the path to the web resource
* root.
*
* @param targetDir The target directory that would be used as the basis for calculating the parent directories.
*
* @param targetDir The target directory that would be used as the basis for calculating the parent directories.
* @return The parent directories to traverse. Represented as a sequence of '..' characters with '/' to denote multiple
* parent directories.
*/
Expand Down Expand Up @@ -443,4 +469,42 @@ private int countOccurrences(String searchString, char charToSearch) {
return count;
}

private String selectTargetDir(ScaffoldProvider provider, String target)
{
if (provider == null)
{
throw new RuntimeException("Selected scaffold provider was null. Re-run with '--scaffoldType ...'");
}

String targetDirKey = getTargetDirConfigKey(provider);

if (Strings.isNullOrEmpty(target))
{
target = configuration.getString(targetDirKey);
if (Strings.isNullOrEmpty(target))
{
String finalName = project.getFacet(PackagingFacet.class).getFinalName();
target = prompt.promptCommon(
"Create scaffold in which sub-directory of web-root? (e.g. http://localhost:8080/"
+ finalName
+ "/DIR)", PromptType.FILE_PATH, "/");
}
}

if (!Strings.isNullOrEmpty(target))
{
configuration.setProperty(targetDirKey, target);
if (!target.startsWith("/"))
target = "/" + target;
if (target.endsWith("/"))
target = target.substring(0, target.length() - 1);
}
return target;
}

private String getTargetDirConfigKey(ScaffoldProvider provider)
{
return provider.getClass().getName() + "_targetDir";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@
*/
public class ResourceProvider {

private static final String GLYPHICONS_WHITE_PNG = "/img/glyphicons-halflings-white.png";
public static final String GLYPHICONS_WHITE_PNG = "/img/glyphicons-halflings-white.png";

private static final String GLYPHICONS_PNG = "/img/glyphicons-halflings.png";
public static final String GLYPHICONS_PNG = "/img/glyphicons-halflings.png";

private static final String FORGE_LOGO_PNG = "/img/forge-logo.png";
public static final String FORGE_LOGO_PNG = "/img/forge-logo.png";

private static final String ANGULAR_RESOURCE_JS = "/scripts/vendor/angular-resource.js";
public static final String ANGULAR_RESOURCE_JS = "/scripts/vendor/angular-resource.js";

private static final String ANGULAR_JS = "/scripts/vendor/angular.js";
public static final String ANGULAR_JS = "/scripts/vendor/angular.js";

private static final String JQUERY_JS = "/scripts/vendor/jquery-1.9.1.js";
public static final String JQUERY_JS = "/scripts/vendor/jquery-1.9.1.js";

private static final String BOOTSTRAP_RESPONSIVE_CSS = "/styles/bootstrap-responsive.css";
public static final String BOOTSTRAP_RESPONSIVE_CSS = "/styles/bootstrap-responsive.css";

private static final String MAIN_CSS = "/styles/main.css";
public static final String MAIN_CSS = "/styles/main.css";

private static final String BOOTSTRAP_CSS = "/styles/bootstrap.css";
public static final String BOOTSTRAP_CSS = "/styles/bootstrap.css";

/**
* Provides a list of {@link ScaffoldResource}s representing static files that are to be copied upon scaffolding setup.
Expand Down

0 comments on commit fb3e660

Please sign in to comment.