|
| 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