Permalink
Browse files

RF-11132: Merge branch 'feature/cmdline-generator' into develop

  • Loading branch information...
2 parents 476021a + 1d94dda commit f1d97e2e7a594adc74a90bfc3b2e61dbdb953406 @lfryc lfryc committed May 15, 2012
@@ -0,0 +1,21 @@
+<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
+ <id>executable</id>
+ <formats>
+ <format>jar</format>
+ </formats>
+ <includeBaseDirectory>false</includeBaseDirectory>
+ <fileSets>
+ <fileSet>
+ <directory>target/classes</directory>
+ <outputDirectory>/</outputDirectory>
+ </fileSet>
+ </fileSets>
+ <dependencySets>
+ <dependencySet>
+ <outputDirectory>/</outputDirectory>
+ <scope>compile</scope>
+ <unpack>true</unpack>
+ </dependencySet>
+ </dependencySets>
+</assembly>
View
@@ -0,0 +1,65 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.richfaces.cdk</groupId>
+ <artifactId>parent</artifactId>
+ <version>4.3.0-SNAPSHOT</version>
+ <relativePath>../parent/pom.xml</relativePath>
+ </parent>
+
+ <artifactId>cmdln-generator</artifactId>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.richfaces.cdk</groupId>
+ <artifactId>generator</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.beust</groupId>
+ <artifactId>jcommander</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.codehaus.plexus</groupId>
+ <artifactId>plexus-utils</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.glassfish</groupId>
+ <artifactId>javax.faces</artifactId>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <finalName>cdk-cmdline-generator</finalName>
+ <plugins>
+ <plugin>
+ <artifactId>maven-assembly-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>assembly-jar</id>
+ <phase>package</phase>
+ <goals>
+ <goal>single</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <appendAssemblyId>false</appendAssemblyId>
+ <descriptors>
+ <descriptor>assembly-jar.xml</descriptor>
+ </descriptors>
+ <archive>
+ <manifest>
+ <mainClass>org.richfaces.cdk.GenerateMain</mainClass>
+ </manifest>
+ </archive>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
View
@@ -0,0 +1,26 @@
+#!/bin/sh
+
+while getopts ":p:" opt; do
+ case $opt in
+ p)
+ export PROJECT="$OPTARG"
+ ;;
+ esac
+done
+
+if [ -z "$PROJECT" ]; then
+ echo "Error: No project provided, use -p to provide project home"
+ exit 1
+fi
+
+if [ ! -f "$PROJECT/pom.xml" ]; then
+ echo "Error: project argument must be valid Maven project (contain pom.xml)"
+ exit 2
+fi
+
+if [ ! -d "$PROJECT/target/classes" -o ! -d "$PROJECT/target/dependency" ]; then
+ mvn -f "$PROJECT/pom.xml" compiler:compile dependency:unpack-dependencies
+fi
+
+TARGET=$(dirname $0)"/target"
+java -jar $TARGET/cdk-cmdline-generator.jar $*
@@ -0,0 +1,272 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright ${year}, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.richfaces.cdk;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.codehaus.plexus.util.DirectoryScanner;
+
+import com.beust.jcommander.Parameter;
+import com.google.common.base.Function;
+import com.google.common.collect.Collections2;
+
+/**
+ * Configurable command-line interface of CDK generator.
+ *
+ * This class is similar functionality as {@link org.richfaces.builder.mojo.GenerateMojo} from maven-cdk-plugin.
+ *
+ * @author Lukas Fryc
+ */
+public class CommandLineGenerator {
+
+ private static final String[] JAVA_INCLUDES = new String[] { "**/*.java" };
+ private static final String MAIN_CONFIG = "src/main/config";
+ private static final String MAIN_TEMPLATES = "src/main/templates";
+ private static final String[] EMPTY = new String[0];
+ private static final String[] XML_INCLUDES = new String[] { "**/*.xml" };
+
+ @Parameter(names = "-p")
+ String projectRoot;
+
+ @Parameter(names = "-d")
+ boolean debug = false;
+
+ @Parameter(names = "-n")
+ private String taglibNamespace;
+
+ private List<String> compileSourceRoots;
+ protected String[] sourceIncludes;
+ protected String[] sourceExcludes;
+ protected String templatesRoot;
+ protected String configRoot;
+
+ protected File outputDirectory;
+ protected File outputJavaDirectory;
+ protected File outputResourcesDirectory;
+ protected File outputTestDirectory;
+ protected File outputTestResourcesDirectory;
+
+ protected Map<String, String> options = new HashMap<String, String>();
+
+ private Logger logger;
+
+ private void setup() {
+ compileSourceRoots = Arrays.asList(projectRoot + "/src/main/java");
+
+ templatesRoot = projectRoot + "/" + MAIN_TEMPLATES;
+ configRoot = projectRoot + "/" + MAIN_CONFIG;
+
+ outputDirectory = new File(projectRoot, "target/classes");
+ outputJavaDirectory = new File(projectRoot, "target/generated-sources/main/java");
+ outputResourcesDirectory = new File(projectRoot, "target/generated-sources/main/resources");
+ outputTestDirectory = new File(projectRoot, "target/generated-sources/test/java");
+ outputTestResourcesDirectory = new File(projectRoot, "target/generated-sources/test/resources");
+
+ CustomLogger logger = new CustomLogger();
+ logger.setDebugEnabled(debug);
+ this.logger = logger;
+ }
+
+ public void execute() {
+ setup();
+
+ logger.info("[generate: " + projectRoot + "]");
+ long start = System.currentTimeMillis();
+
+ executeGenerator();
+
+ long end = System.currentTimeMillis();
+ logger.info("[total: " + (end - start) + " ms]");
+ }
+
+ public void executeGenerator() {
+ Generator generator = new Generator();
+ generator.setLog(logger);
+ generator.setLoader(createProjectClassLoader());
+
+ // Set source folders.
+ ArrayList<File> folders = new ArrayList<File>(compileSourceRoots.size());
+
+ for (String sourceFolder : compileSourceRoots) {
+ File folder = new File(sourceFolder);
+
+ if (folder.exists() && folder.isDirectory()) {
+ folders.add(folder);
+ }
+ }
+
+ generator.addSources(Sources.JAVA_SOURCES, findJavaFiles(), folders);
+ // detect templates and configs directories.
+ generator.addSources(Sources.RENDERER_TEMPLATES, findTemplateFiles(), null);
+ generator.addSources(Sources.FACES_CONFIGS, findFacesConfigFiles(), null);
+
+ // Setup output folders.
+ setOutput(generator, outputJavaDirectory, Outputs.JAVA_CLASSES);
+ setOutput(generator, outputResourcesDirectory, Outputs.RESOURCES);
+ setOutput(generator, outputTestDirectory, Outputs.TEST_JAVA_CLASSES);
+ setOutput(generator, outputTestResourcesDirectory, Outputs.TEST_RESOURCES);
+
+ // configure CDK workers.
+ setupPlugins(generator);
+
+ if (null != options) {
+
+ // TODO make it type safe.
+ generator.setOptions(options);
+ }
+
+ try {
+ if (taglibNamespace != null) {
+ generator.setNamespace(taglibNamespace);
+ }
+
+ // Build JSF library.
+ // LibraryBuilder builder = LibraryBuilder.createInstance(context);
+ generator.init();
+ generator.execute();
+
+ if (logger.getErrorCount() > 0) {
+ throw new IllegalStateException("Errors occurred while JSF library was built");
+ }
+ } catch (CdkException e) {
+ throw new IllegalStateException("CDK build error", e);
+ }
+ }
+
+ /**
+ * <p class="changed_added_4_0">
+ * </p>
+ *
+ * @param generator
+ * @throws MojoFailureException
+ */
+ private void setupPlugins(Generator generator) {
+ // TODO - get additional modules, as Maven components ?
+ }
+
+ /**
+ * <p class="changed_added_4_0">
+ * This utility method sets output directory for particular type. I such directory does not exist, it is created.
+ * </p>
+ *
+ * @param generator
+ * @param directory
+ * @param type
+ */
+ private static void setOutput(Generator generator, File directory, Outputs type) {
+ // if (!directory.exists()) {
+ // directory.mkdirs();
+ // }
+
+ generator.addOutputFolder(type, directory);
+ }
+
+ private Iterable<File> findTemplateFiles() {
+ String[] files = doScan(XML_INCLUDES, EMPTY, new File(templatesRoot));
+ return Collections2.transform(Arrays.asList(files), new StringToFile(templatesRoot));
+ }
+
+ private Iterable<File> findJavaFiles() {
+ Set<File> javaSources = new HashSet<File>();
+ String[] includes = null == sourceIncludes ? JAVA_INCLUDES : sourceIncludes;
+
+ for (String compileRoot : compileSourceRoots) {
+ File rootFolder = new File(compileRoot);
+ String[] sources = doScan(includes, sourceExcludes, rootFolder);
+ for (String src : sources) {
+ javaSources.add(new File(rootFolder, src));
+ }
+ }
+
+ return javaSources;
+ }
+
+ private Iterable<File> findFacesConfigFiles() {
+ String[] files = doScan(XML_INCLUDES, EMPTY, new File(configRoot));
+ return Collections2.transform(Arrays.asList(files), new StringToFile(configRoot));
+ }
+
+ CdkClassLoader createProjectClassLoader() {
+ CdkClassLoader classLoader = null;
+
+ try {
+ // This Mojo executed befor process-resources phase, therefore we have to use original resource folders.
+ List<File> urls = getClassPathElements();
+ classLoader = new CdkClassLoader(urls, this.getClass().getClassLoader());
+ } catch (MalformedURLException e) {
+ throw new IllegalStateException(e);
+ }
+
+ return classLoader;
+ }
+
+ List<File> getClassPathElements() {
+ List<File> files = new ArrayList<File>();
+
+ files.add(new File(projectRoot, "target/classes"));
+ files.add(new File(projectRoot, "target/dependency"));
+
+ return files;
+ }
+
+ protected String[] doScan(String[] includes, String[] excludes, File rootFolder) {
+ if (!rootFolder.exists()) {
+ return new String[] {};
+ }
+ try {
+ DirectoryScanner directoryScanner = new DirectoryScanner();
+
+ directoryScanner.setFollowSymlinks(true);
+ directoryScanner.setBasedir(rootFolder);
+ directoryScanner.setExcludes(excludes);
+ directoryScanner.setIncludes(includes);
+ directoryScanner.addDefaultExcludes();
+ directoryScanner.scan();
+
+ return directoryScanner.getIncludedFiles();
+ } catch (IllegalStateException e) {
+ throw new IllegalStateException("Error scanning source root: \'" + rootFolder + "\'", e);
+ }
+ }
+
+ private class StringToFile implements Function<String, File> {
+
+ private String root;
+
+ public StringToFile(String root) {
+ this.root = root;
+ }
+
+ @Override
+ public File apply(String input) {
+ return new File(root, input);
+ }
+ };
+}
Oops, something went wrong.

0 comments on commit f1d97e2

Please sign in to comment.