Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generator: Fix #230 Make it possible to clear generated files with a …
…maven goal. You can now use speedment:clear to remove all generated files. This solution uses a hash key for each file to determine if they have been modified or not. If the hash matches the content of the file, it is considered unchanged and can safely be regenerated.
- Loading branch information
Emil Forslund
committed
Jul 26, 2016
1 parent
484155a
commit 13a6e9f
Showing
4 changed files
with
331 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
generator/src/main/java/com/speedment/generator/internal/util/HashUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,100 @@ | |||
package com.speedment.generator.internal.util; | |||
|
|||
import com.speedment.runtime.exception.SpeedmentException; | |||
import static com.speedment.runtime.util.StaticClassUtil.instanceNotAllowed; | |||
import java.io.IOException; | |||
import java.nio.charset.Charset; | |||
import java.nio.charset.StandardCharsets; | |||
import java.nio.file.Files; | |||
import java.nio.file.Path; | |||
import java.security.MessageDigest; | |||
import java.security.NoSuchAlgorithmException; | |||
import java.util.Arrays; | |||
import java.util.List; | |||
import static java.util.stream.Collectors.joining; | |||
|
|||
/** | |||
* A utility method for creating and comparing hashes of files. | |||
* | |||
* @author Emil Forslund | |||
* @since 3.0.0 | |||
*/ | |||
public final class HashUtil { | |||
|
|||
private final static String ALGORITHM = "SHA-1"; | |||
private final static Charset CHARSET = StandardCharsets.UTF_8; | |||
|
|||
public static boolean compare(Path path, Path sha1) { | |||
final String expected = sha1(path); | |||
final String actual = load(sha1).get(0); | |||
return expected.equals(actual); | |||
} | |||
|
|||
public static boolean compare(Path path, String sha1) { | |||
final String expected = sha1(path); | |||
return expected.equals(sha1); | |||
} | |||
|
|||
public static boolean compare(String content, String sha1) { | |||
final String expected = sha1(content); | |||
return expected.equals(sha1); | |||
} | |||
|
|||
public static String sha1(Path path) { | |||
return sha1(load(path)); | |||
} | |||
|
|||
public static String sha1(String content) { | |||
return sha1(Arrays.asList(content.split("\\s+"))); | |||
} | |||
|
|||
private static String sha1(List<String> rows) { | |||
return sha1(rows.stream() | |||
.map(String::trim) | |||
.flatMap(s -> Arrays.asList(s.split("\\s+")).stream()) | |||
.collect(joining()) | |||
.getBytes(CHARSET) | |||
); | |||
} | |||
|
|||
private static String sha1(byte[] bytes) { | |||
final MessageDigest md; | |||
|
|||
try { | |||
md = MessageDigest.getInstance(ALGORITHM); | |||
} catch(final NoSuchAlgorithmException ex) { | |||
throw new SpeedmentException( | |||
"Could not find hashing algorithm '" + ALGORITHM + "'.", ex | |||
); | |||
} | |||
|
|||
return bytesToHex(md.digest(bytes)); | |||
} | |||
|
|||
private static String bytesToHex(byte[] bytes) { | |||
final StringBuilder result = new StringBuilder(); | |||
|
|||
for (int i = 0; i < bytes.length; i++) { | |||
result.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); | |||
} | |||
|
|||
return result.toString(); | |||
} | |||
|
|||
private static List<String> load(Path path) { | |||
try { | |||
return Files.readAllLines(path, CHARSET); | |||
} catch (final IOException ex) { | |||
throw new SpeedmentException( | |||
"Error reading file '" + path + "' for hashing.", ex | |||
); | |||
} | |||
} | |||
|
|||
/** | |||
* Utility classes should never be instantiated. | |||
*/ | |||
private HashUtil() { | |||
instanceNotAllowed(getClass()); | |||
} | |||
} |
Oops, something went wrong.