Repository: cayenne
Updated Branches:
  refs/heads/master 05d279d8e -> 023547e6a


CAY-2334 cgen: option to force run from maven/gradle


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/023547e6
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/023547e6
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/023547e6

Branch: refs/heads/master
Commit: 023547e6a0faeeb7f151cff3028ff1552c7158c1
Parents: 05d279d
Author: Nikita Timofeev <stari...@gmail.com>
Authored: Thu Feb 1 12:37:40 2018 +0300
Committer: Nikita Timofeev <stari...@gmail.com>
Committed: Thu Feb 1 12:37:40 2018 +0300

----------------------------------------------------------------------
 RELEASE-NOTES.txt                               |   1 +
 cayenne-cgen/pom.xml                            |   6 ++
 .../cayenne/gen/ClassGenerationAction.java      | 101 +++++++++----------
 .../cayenne/gen/ClassGenerationActionTest.java  |  42 ++++++++
 .../java/org/apache/cayenne/tools/CgenTask.java |  34 +++++--
 .../cayenne/tools/CayenneGeneratorMojo.java     |  32 +++---
 6 files changed, 141 insertions(+), 75 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/023547e6/RELEASE-NOTES.txt
----------------------------------------------------------------------
diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt
index a41b221..746012c 100644
--- a/RELEASE-NOTES.txt
+++ b/RELEASE-NOTES.txt
@@ -13,6 +13,7 @@ Date:
 ----------------------------------
 Changes/New Features:
 
+CAY-2334 cgen: option to force run from maven/gradle
 CAY-2372 Extract new modules from cayenne-server
 CAY-2377 Cleanup deprecated code.
 CAY-2391 cdbimport: add option to skip user-defined relationships

http://git-wip-us.apache.org/repos/asf/cayenne/blob/023547e6/cayenne-cgen/pom.xml
----------------------------------------------------------------------
diff --git a/cayenne-cgen/pom.xml b/cayenne-cgen/pom.xml
index d95761d..518b319 100644
--- a/cayenne-cgen/pom.xml
+++ b/cayenne-cgen/pom.xml
@@ -59,6 +59,12 @@
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
+
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>

http://git-wip-us.apache.org/repos/asf/cayenne/blob/023547e6/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java
----------------------------------------------------------------------
diff --git 
a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java 
b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java
index eb122be..253ca06 100644
--- 
a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java
+++ 
b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java
@@ -19,7 +19,6 @@
 
 package org.apache.cayenne.gen;
 
-import org.apache.cayenne.CayenneException;
 import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.Embeddable;
@@ -81,6 +80,7 @@ public class ClassGenerationAction {
        protected String outputPattern;
        protected String encoding;
        protected boolean createPropertyNames;
+       protected boolean force; // force run generator
 
        // runtime ivars
        protected VelocityContext context;
@@ -88,7 +88,7 @@ public class ClassGenerationAction {
 
        public ClassGenerationAction() {
                this.outputPattern = "*.java";
-               this.timestamp = System.currentTimeMillis();
+               this.timestamp = 0L;
                this.usePkgPath = true;
                this.makePairs = true;
                this.context = new VelocityContext();
@@ -240,18 +240,16 @@ public class ClassGenerationAction {
                }
        }
 
-       protected Template getTemplate(TemplateType type) throws Exception {
+       protected Template getTemplate(TemplateType type) {
 
                String templateName = customTemplateName(type);
                if (templateName == null) {
                        templateName = defaultTemplateName(type);
                }
 
-               // Velocity < 1.5 has some memory problems, so we will create a
-               // VelocityEngine
-               // every time, and store templates in an internal cache, to 
avoid
-               // uncontrolled
-               // memory leaks... Presumably 1.5 fixes it.
+               // Velocity < 1.5 has some memory problems, so we will create a 
VelocityEngine every time,
+               // and store templates in an internal cache, to avoid 
uncontrolled memory leaks...
+               // Presumably 1.5 fixes it.
 
                Template template = templateCache.get(templateName);
 
@@ -274,9 +272,9 @@ public class ClassGenerationAction {
        }
 
        /**
-        * Validates the state of this class generator. Throws
-        * CayenneRuntimeException if it is in an inconsistent state. Called
-        * internally from "execute".
+        * Validates the state of this class generator.
+        * Throws CayenneRuntimeException if it is in an inconsistent state.
+        * Called internally from "execute".
         */
        protected void validateAttributes() {
                if (destDir == null) {
@@ -393,19 +391,8 @@ public class ClassGenerationAction {
                String filename = 
StringUtils.getInstance().replaceWildcardInStringWithString(WILDCARD, 
outputPattern, className);
                File dest = new File(mkpath(destDir, packageName), filename);
 
-               // Ignore if the destination is newer than the map
-               // (internal timestamp), i.e. has been generated after the map 
was
-               // last saved AND the template is older than the destination 
file
-               if (dest.exists() && !isOld(dest)) {
-
-                       if (superTemplate == null) {
-                               return null;
-                       }
-
-                       File superTemplateFile = new File(superTemplate);
-                       if (superTemplateFile.lastModified() < 
dest.lastModified()) {
-                               return null;
-                       }
+               if (dest.exists() && !fileNeedUpdate(dest, superTemplate)) {
+                       return null;
                }
 
                return dest;
@@ -434,19 +421,8 @@ public class ClassGenerationAction {
                                return null;
                        }
 
-                       // Ignore if the destination is newer than the map
-                       // (internal timestamp), i.e. has been generated after 
the map was
-                       // last saved AND the template is older than the 
destination file
-                       if (!isOld(dest)) {
-
-                               if (template == null) {
-                                       return null;
-                               }
-
-                               File templateFile = new File(template);
-                               if (templateFile.lastModified() < 
dest.lastModified()) {
-                                       return null;
-                               }
+                       if (!fileNeedUpdate(dest, template)) {
+                               return null;
                        }
                }
 
@@ -454,11 +430,31 @@ public class ClassGenerationAction {
        }
 
        /**
-        * Returns true if <code>file</code> parameter is older than internal
-        * timestamp of this class generator.
+        * Ignore if the destination is newer than the map
+        * (internal timestamp), i.e. has been generated after the map was
+        * last saved AND the template is older than the destination file
+        */
+       protected boolean fileNeedUpdate(File dest, String templateFileName) {
+               if(force) {
+                       return true;
+               }
+
+               if (isOld(dest)) {
+            if (templateFileName == null) {
+                               return false;
+            }
+
+            File templateFile = new File(templateFileName);
+                       return templateFile.lastModified() >= 
dest.lastModified();
+        }
+               return true;
+       }
+
+       /**
+        * Is file modified after internal timestamp (usually equal to mtime of 
datamap file)
         */
        protected boolean isOld(File file) {
-               return file.lastModified() <= timestamp;
+               return file.lastModified() > timestamp;
        }
 
        /**
@@ -500,8 +496,7 @@ public class ClassGenerationAction {
        }
 
        /**
-        * @param dataMap
-        *            The dataMap to set.
+        * @param dataMap The dataMap to set.
         */
        public void setDataMap(DataMap dataMap) {
                this.dataMap = dataMap;
@@ -509,16 +504,11 @@ public class ClassGenerationAction {
 
        /**
         * Adds entities to the internal entity list.
-        */
-
-       /**
-        *
-        * @param entities
-        * @throws CayenneException
+        * @param entities collection
         *
         * @since 4.0 throws exception
         */
-       public void addEntities(Collection<ObjEntity> entities) throws 
CayenneRuntimeException {
+       public void addEntities(Collection<ObjEntity> entities) {
                if (artifactsGenerationMode == ArtifactsGenerationMode.ENTITY
                                || artifactsGenerationMode == 
ArtifactsGenerationMode.ALL) {
                        if (entities != null) {
@@ -544,9 +534,8 @@ public class ClassGenerationAction {
                if (artifactsGenerationMode == ArtifactsGenerationMode.DATAMAP
                                || artifactsGenerationMode == 
ArtifactsGenerationMode.ALL) {
 
-                       // TODO: andrus 10.12.2010 - why not also check for 
empty query
-                       // list?? Or
-                       // create a better API for enabling DataMapArtifact
+                       // TODO: andrus 10.12.2010 - why not also check for 
empty query list??
+                       // Or create a better API for enabling DataMapArtifact
                        if (queries != null) {
                                artifacts.add(new DataMapArtifact(dataMap, 
queries));
                        }
@@ -586,4 +575,12 @@ public class ClassGenerationAction {
                        this.artifactsGenerationMode = 
ArtifactsGenerationMode.ALL;
                }
        }
+
+       public boolean isForce() {
+               return force;
+       }
+
+       public void setForce(boolean force) {
+               this.force = force;
+       }
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/023547e6/cayenne-cgen/src/test/java/org/apache/cayenne/gen/ClassGenerationActionTest.java
----------------------------------------------------------------------
diff --git 
a/cayenne-cgen/src/test/java/org/apache/cayenne/gen/ClassGenerationActionTest.java
 
b/cayenne-cgen/src/test/java/org/apache/cayenne/gen/ClassGenerationActionTest.java
index 8a1fbd6..721a129 100644
--- 
a/cayenne-cgen/src/test/java/org/apache/cayenne/gen/ClassGenerationActionTest.java
+++ 
b/cayenne-cgen/src/test/java/org/apache/cayenne/gen/ClassGenerationActionTest.java
@@ -29,6 +29,7 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
+import java.io.File;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.util.ArrayList;
@@ -36,8 +37,11 @@ import java.util.Collection;
 import java.util.List;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 public class ClassGenerationActionTest {
 
@@ -251,4 +255,42 @@ public class ClassGenerationActionTest {
                }
                return strings;
        }
+
+       @Test
+       public void testIsOld() {
+               File file = mock(File.class);
+               when(file.lastModified()).thenReturn(1000L);
+
+               action.setTimestamp(0);
+               assertTrue(action.isOld(file));
+
+               action.setTimestamp(2000L);
+               assertFalse(action.isOld(file));
+       }
+
+       @Test
+       public void testFileNeedUpdate() {
+               File file = mock(File.class);
+               when(file.lastModified()).thenReturn(1000L);
+
+               action.setTimestamp(0);
+               action.setForce(false);
+
+               assertFalse(action.fileNeedUpdate(file, null));
+
+               action.setTimestamp(2000L);
+               action.setForce(false);
+
+               assertTrue(action.fileNeedUpdate(file, null));
+
+               action.setTimestamp(0);
+               action.setForce(true);
+
+               assertTrue(action.fileNeedUpdate(file, null));
+
+               action.setTimestamp(2000L);
+               action.setForce(true);
+
+               assertTrue(action.fileNeedUpdate(file, null));
+       }
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/023547e6/cayenne-gradle-plugin/src/main/java/org/apache/cayenne/tools/CgenTask.java
----------------------------------------------------------------------
diff --git 
a/cayenne-gradle-plugin/src/main/java/org/apache/cayenne/tools/CgenTask.java 
b/cayenne-gradle-plugin/src/main/java/org/apache/cayenne/tools/CgenTask.java
index ee39a1c..b6b376e 100644
--- a/cayenne-gradle-plugin/src/main/java/org/apache/cayenne/tools/CgenTask.java
+++ b/cayenne-gradle-plugin/src/main/java/org/apache/cayenne/tools/CgenTask.java
@@ -20,7 +20,6 @@
 package org.apache.cayenne.tools;
 
 import java.io.File;
-import java.io.FilenameFilter;
 import java.util.Set;
 
 import groovy.lang.Reference;
@@ -108,6 +107,13 @@ public class CgenTask extends BaseCayenneTask {
     @Input
     private boolean createPropertyNames;
 
+    /**
+     * Force run (skip check for files modification time)
+     * @since 4.1
+     */
+    @Input
+    private boolean force;
+
     private String destDirName;
 
     @TaskAction
@@ -130,6 +136,10 @@ public class CgenTask extends BaseCayenneTask {
             DataMap dataMap = loaderAction.getMainDataMap();
 
             generator.setLogger(getLogger());
+
+            if(this.force || (getProject().findProperty("force") != null)) {
+                generator.setForce(true);
+            }
             generator.setTimestamp(dataMapFile.lastModified());
             generator.setDataMap(dataMap);
             generator.addEntities(filterAction.getFilteredEntities(dataMap));
@@ -150,13 +160,9 @@ public class CgenTask extends BaseCayenneTask {
             throw new GradleException("'additionalMaps' must be a directory.");
         }
 
-        FilenameFilter mapFilter = new FilenameFilter() {
-            public boolean accept(File dir, String name) {
-                return name != null && name.toLowerCase().endsWith(".map.xml");
-            }
-
-        };
-        return additionalMaps.listFiles(mapFilter);
+        return additionalMaps.listFiles(
+                (dir, name) -> name != null && 
name.toLowerCase().endsWith(".map.xml")
+        );
     }
 
     ClassGenerationAction newGeneratorInstance() {
@@ -447,4 +453,16 @@ public class CgenTask extends BaseCayenneTask {
         setCreatePropertyNames(createPropertyNames);
     }
 
+    public boolean isForce() {
+        return force;
+    }
+
+    public void setForce(boolean force) {
+        this.force = force;
+    }
+
+    public void force(boolean force) {
+        setForce(force);
+    }
+
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cayenne/blob/023547e6/maven-plugins/cayenne-maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
----------------------------------------------------------------------
diff --git 
a/maven-plugins/cayenne-maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
 
b/maven-plugins/cayenne-maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
index 6af8b0a..72c39b4 100644
--- 
a/maven-plugins/cayenne-maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
+++ 
b/maven-plugins/cayenne-maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
@@ -36,7 +36,6 @@ import org.apache.maven.plugins.annotations.Parameter;
 import org.slf4j.LoggerFactory;
 
 import java.io.File;
-import java.io.FilenameFilter;
 
 /**
  * Maven mojo to perform class generation from data map. This class is an Maven
@@ -185,6 +184,15 @@ public class CayenneGeneratorMojo extends AbstractMojo {
     @Parameter(defaultValue = "false")
     private boolean createPropertyNames;
 
+       /**
+        * If set to <code>true</code>, will skip file modification time 
validation and regenerate all.
+        * Default is <code>false</code>.
+        *
+        * @since 4.1
+        */
+       @Parameter(defaultValue = "false", property = "force")
+       private boolean force;
+
     private transient Injector injector;
 
        public void execute() throws MojoExecutionException, 
MojoFailureException {
@@ -212,14 +220,14 @@ public class CayenneGeneratorMojo extends AbstractMojo {
 
                        ClassGenerationAction generator = createGenerator();
                        generator.setLogger(logger);
+                       if(force) {
+                               // will (re-)generate all files
+                               generator.setForce(true);
+                       }
                        generator.setTimestamp(map.lastModified());
                        generator.setDataMap(dataMap);
                        
generator.addEntities(filterAction.getFilteredEntities(dataMap));
-                       // ksenia khailenko 15.10.2010
-                       // TODO add the "includeEmbeddables" and 
"excludeEmbeddables"
-                       // attributes
                        generator.addEmbeddables(dataMap.getEmbeddables());
-                       // TODO add the "includeQueries" and "excludeQueries" 
attributes
                        generator.addQueries(dataMap.getQueryDescriptors());
                        generator.execute();
                } catch (Exception e) {
@@ -237,18 +245,12 @@ public class CayenneGeneratorMojo extends AbstractMojo {
                }
 
                if (!additionalMaps.isDirectory()) {
-                       throw new MojoFailureException(
-                                       "'additionalMaps' must be a 
directory.");
+                       throw new MojoFailureException("'additionalMaps' must 
be a directory.");
                }
 
-        FilenameFilter mapFilter = new FilenameFilter() {
-            @Override
-            public boolean accept(File dir, String name) {
-                return name != null &&
-                       name.toLowerCase().endsWith(".map.xml");
-            }
-        };
-        return additionalMaps.listFiles(mapFilter);
+        return additionalMaps.listFiles(
+                       (dir, name) -> name != null && 
name.toLowerCase().endsWith(".map.xml")
+               );
        }
 
        /**

Reply via email to