-
Notifications
You must be signed in to change notification settings - Fork 0
/
QrcBodyGenerator.groovy
62 lines (50 loc) · 1.81 KB
/
QrcBodyGenerator.groovy
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) 2019 Gonzalo Müller Bravo.
// Licensed under the MIT License (MIT), see LICENSE.txt
package all.shared.gradle.qt.task
import all.shared.gradle.qt.QrcFileGeneratorConfig
import groovy.transform.CompileStatic
import java.nio.file.FileSystem
import java.nio.file.FileSystems
import java.nio.file.Path
import org.gradle.api.file.ConfigurableFileTree
import org.gradle.api.file.FileVisitDetails
@CompileStatic
class QrcBodyGenerator {
private final FileSystem fileSystem = FileSystems.default
private final QrcFileGeneratorConfig config
public QrcBodyGenerator(final QrcFileGeneratorConfig config) {
this.config = config
}
public final Path calculateFilePath() {
return fileSystem.getPath(config.destinationFolder, config.destinationFile)
}
public final String createQrcBody(final ConfigurableFileTree fileTree) {
final StringBuilder body = new StringBuilder("<!DOCTYPE RCC>\n<RCC version=\"$config.rccVersion\">\n")
body << " <qresource${calculatePrefix()}>\n"
final String folder = calculateFilesFolder()
fileTree.visit { final FileVisitDetails fileInfo ->
if (!fileInfo.directory) {
body << " <file>${folder}${fileInfo.path}</file>\n"
}
}
body << ' </qresource>\n</RCC>\n'
return body.toString()
}
protected final String calculatePrefix() {
config.rccPrefix
? " prefix=\"${config.rccPrefix}\""
: ''
}
protected final String calculateFilesFolder() {
config.sourceFolder == config.destinationFolder
? ''
: "${relativizeFolders()}/"
}
protected final String relativizeFolders() {
final Path sourcePath = fileSystem.getPath(config.sourceFolder)
final Path destinationPath = fileSystem.getPath(config.destinationFolder)
return destinationPath
.relativize(sourcePath)
.toString()
}
}