Skip to content

Commit ebc913f

Browse files
committed
extracted
1 parent 2a294fe commit ebc913f

2 files changed

Lines changed: 81 additions & 38 deletions

File tree

src/main/java/io/openapiprocessor/maven/ProcessMojo.java

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@
1919
import org.apache.maven.plugin.*;
2020
import org.apache.maven.plugins.annotations.*;
2121
import org.apache.maven.plugins.annotations.Mojo;
22-
import org.apache.maven.shared.utils.io.DirectoryScanner;
2322

2423
import java.io.File;
2524
import java.util.*;
2625

2726
/**
28-
* Mojo to run the processor.
27+
* run an openapi-processor.
2928
*
3029
* @author Martin Hauner
3130
*/
@@ -52,45 +51,13 @@ public void execute () throws MojoExecutionException, MojoFailureException {
5251
try {
5352
Map<String, Object> properties = createProperties ();
5453

55-
DirectoryScanner sourceScanner = new DirectoryScanner ();
5654
File sourceRoot = apiPath.getParentFile ();
57-
sourceScanner.setBasedir (sourceRoot);
58-
sourceScanner.setIncludes ("**/*.yaml", "**/*.yml");
59-
sourceScanner.scan ();
60-
String[] sourceFiles = sourceScanner.getIncludedFiles ();
61-
62-
long lastModified = 0;
63-
for (String source : sourceFiles) {
64-
File current = new File (sourceRoot, source);
65-
66-
if (current.exists () && current.lastModified () > lastModified) {
67-
lastModified = current.lastModified ();
68-
}
69-
}
70-
55+
File targetRoot = new File((String) properties.get (TARGET_DIR));
7156

72-
DirectoryScanner targetScanner = new DirectoryScanner ();
73-
File targetDir = new File((String) properties.get (TARGET_DIR));
74-
targetScanner.setBasedir (targetDir);
75-
targetScanner.setIncludes ("**/*", "**/*");
76-
targetScanner.scan ();
77-
String[] targetFiles = targetScanner.getIncludedFiles ();
78-
79-
boolean outdated = false;
80-
if (targetFiles.length == 0) {
81-
outdated = true;
82-
}
83-
84-
for (String target : targetFiles) {
85-
File current = new File (targetDir, target);
86-
87-
if (current.exists () && current.lastModified () < lastModified) {
88-
outdated = true;
89-
break;
90-
}
91-
}
57+
UpToDateCheck upToDateCheck = new UpToDateCheck ();
58+
boolean upToDate = upToDateCheck.isUpToDate (sourceRoot, targetRoot);
9259

93-
if (outdated) {
60+
if (!upToDate) {
9461
getLog().info( "Changes detected - generating target files!" );
9562

9663
new ProcessorRunner (id, properties)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2020 the original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.openapiprocessor.maven;
18+
19+
import org.apache.maven.shared.utils.io.DirectoryScanner;
20+
21+
import java.io.File;
22+
23+
/**
24+
* check if any input y(a)ml files is newer than any output file.
25+
*
26+
* @author Martin Hauner
27+
*/
28+
public class UpToDateCheck {
29+
30+
/**
31+
* checks if any *.yaml or *.yml input file is newer than any output file.
32+
*
33+
* It returns true if the last modified date of any input file is younger than all modified
34+
* dates of the outputs or if the outputs directory is empty.
35+
*
36+
* @param input root directory of the input files
37+
* @param output target directory of the output files
38+
* @return true in case the input is younger than the output, otherwise false
39+
*/
40+
public boolean isUpToDate (File input, File output) {
41+
long lastModifiedInput = getLastModified (input, scanInput (input));
42+
long lastModifiedOutput = getLastModified (output, scanOutput (output));
43+
return lastModifiedInput <= lastModifiedOutput;
44+
}
45+
46+
private String[] scanInput (File source) {
47+
DirectoryScanner sourceScanner = new DirectoryScanner ();
48+
sourceScanner.setBasedir (source);
49+
sourceScanner.setIncludes ("**/*.yaml", "**/*.yml");
50+
sourceScanner.scan ();
51+
return sourceScanner.getIncludedFiles ();
52+
}
53+
54+
private String[] scanOutput (File output) {
55+
DirectoryScanner targetScanner = new DirectoryScanner ();
56+
targetScanner.setBasedir (output);
57+
targetScanner.setIncludes ("**/*", "**/*");
58+
targetScanner.scan ();
59+
return targetScanner.getIncludedFiles ();
60+
}
61+
62+
private long getLastModified (File root, String[] files) {
63+
long lastModified = 0;
64+
65+
for (String file : files) {
66+
File current = new File (root, file);
67+
68+
if (current.exists () && current.lastModified () > lastModified) {
69+
lastModified = current.lastModified ();
70+
}
71+
}
72+
73+
return lastModified;
74+
}
75+
76+
}

0 commit comments

Comments
 (0)