-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateReadme.gradle
41 lines (37 loc) · 1.29 KB
/
updateReadme.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.nio.charset.Charset
import java.nio.file.Files
import java.nio.file.Paths
/**
* Reads a file as string.
*
* @param path path to the file.
* @return the string with the file contents.
* @throws IOException if file don’t exist.
*/
private static String readFileAsString(String path) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path))
return new String(encoded, Charset.defaultCharset())
}
/**
* Replace every semantic version occurrence in specified file.
*
* @param newVersion version to replace.
* @param fileName the file to search.
* @throws IOException if file don’t exist.
*/
private static void replaceAllSemVerInFile(String newVersion, String fileName) throws IOException {
String readme = readFileAsString(fileName)
String readmeVersionReplaced = readme.replaceAll("(\\d+(\\.\\d+){2})(\\-[\\w\\d\\.\\-]*)?(\\+[\\w\\d\\.\\-]*)?", newVersion)
PrintWriter output = new PrintWriter(fileName)
output.print(readmeVersionReplaced)
output.close()
}
ext {
processUpdateReadme = { File resDir, String extension, String libraryVersionName ->
resDir.eachFileRecurse { file ->
if (file.name.endsWith(extension)) {
replaceAllSemVerInFile(libraryVersionName, file.absolutePath)
}
}
}
}