Skip to content

Commit 92970d5

Browse files
committed
Added MetaFile class
Also: - All java.io.File changed to java.nio.Path - Added test for MetaFile
1 parent 4117b83 commit 92970d5

File tree

8 files changed

+237
-176
lines changed

8 files changed

+237
-176
lines changed

src/main/groovy/ru/endlesscode/gradle/bukkit/BukkitPluginExtension.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class BukkitPluginExtension {
77
public static final String NAME = "bukkit"
88

99
private static final String LATEST_VERSION = "+"
10-
private static final String VERSION_SUFFIX = "-R0.1-SNAPSHOT"
10+
private static final String REVISION_SUFFIX = "-R0.1-SNAPSHOT"
1111

1212
private final project
1313

@@ -27,7 +27,7 @@ class BukkitPluginExtension {
2727
* @return Chosen Bukkit version
2828
*/
2929
String getVersion() {
30-
return version ? "$version$VERSION_SUFFIX" : LATEST_VERSION
30+
return version ? "$version$REVISION_SUFFIX" : LATEST_VERSION
3131
}
3232

3333
void meta(@DelegatesTo(PluginMeta) Closure<?> closure) {
Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,27 @@
11
package ru.endlesscode.gradle.bukkit.meta
22

33
import org.gradle.api.DefaultTask
4-
import org.gradle.api.GradleException
54
import org.gradle.api.tasks.Input
65
import org.gradle.api.tasks.TaskAction
76

87
import java.nio.file.Path
98

109
class GenerateMeta extends DefaultTask {
1110
@Input
12-
PluginMeta meta
11+
MetaFile metaFile
1312
@Input
1413
Path target
1514

1615
Path getTarget() {
17-
return this.target ?: temporaryDir.toPath().resolve(MetaFile.META_FILE)
16+
return this.target ?: temporaryDir.toPath().resolve(MetaFile.NAME)
1817
}
1918

2019
/**
21-
* Generates YAML from meta and writes it to target file
20+
* Writes meta to target file
2221
* @return
2322
*/
2423
@TaskAction
2524
def generateMeta() {
26-
def metaItems = meta.items
27-
validateMeta(metaItems)
28-
29-
List<String> lines = convertToYaml(metaItems)
30-
File file = PluginMetaPlugin.getMetaFile(project)
31-
if (file.exists()) {
32-
PluginMetaPlugin.removeAllMeta(file)
33-
lines.addAll(file.readLines())
34-
}
35-
36-
getTarget().withWriter { Writer writer ->
37-
lines.each { String line ->
38-
writer.write("$line\n")
39-
}
40-
}
41-
}
42-
43-
/**
44-
* Validates that meta contains all required attributes
45-
* If it isn't throw GradleException
46-
*
47-
* @param metaItems List of MetaItem
48-
*/
49-
private static validateMeta(List<MetaItem> metaItems) {
50-
for (MetaItem item in metaItems) {
51-
if (item.required && !metaItems.value) {
52-
throw new GradleException("Plugin metadata parse error: '$item.id' must not be null")
53-
}
54-
}
55-
}
56-
57-
/**
58-
* Converts given meta items to YAML format
59-
*
60-
* @param metaItems List of MetaItem
61-
* @return List of YAML lines
62-
*/
63-
private static List<String> convertToYaml(List<MetaItem> metaItems) {
64-
List<String> yaml = []
65-
metaItems.each { MetaItem item ->
66-
if (item.value != null) {
67-
yaml.add(item.entry)
68-
}
69-
}
70-
71-
return yaml
25+
metaFile.writeTo(getTarget())
7226
}
7327
}
Lines changed: 131 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,146 @@
11
package ru.endlesscode.gradle.bukkit.meta
22

3+
import org.gradle.api.GradleException
34
import org.gradle.api.Project
5+
import org.gradle.api.plugins.JavaPluginConvention
6+
7+
import java.nio.file.Files
8+
import java.nio.file.Path
49

510
class MetaFile {
6-
public static final String META_FILE = "plugin.yml"
7-
protected static final String[] META_ATTRIBUTES = [
11+
public static final String NAME = "plugin.yml"
12+
13+
private static final String[] ATTRIBUTES = [
814
"name", "description", "version", "author", "authors", "website", "main"
915
]
1016

11-
final List<String> lines
17+
final List<String> metaLines
18+
final List<String> staticLines
1219
final Project project
1320

14-
MetaFile(Project project) {
15-
this.lines = []
21+
private final PluginMeta meta
22+
private final Path metaFile
23+
24+
MetaFile(Project project, Path file = null) {
25+
this.metaLines = []
26+
this.staticLines = []
1627
this.project = project
28+
this.meta = project.bukkit.meta
29+
this.metaFile = file ?: this.findMetaFile()
30+
31+
this.filterMetaLines()
32+
}
33+
34+
/**
35+
* Finds and returns project metaFile even if it doesn't exist
36+
*
37+
* @return The File
38+
*/
39+
private Path findMetaFile() {
40+
def javaPlugin = project.convention.getPlugin(JavaPluginConvention)
41+
def mainSourceSet = javaPlugin.sourceSets.main
42+
def resourceDir = mainSourceSet.resources.srcDirs[0].toPath()
43+
44+
return resourceDir.resolve(NAME)
45+
}
46+
47+
/**
48+
* Validates and writes meta and static lines to target file
49+
* @param target
50+
*/
51+
void writeTo(Path target) {
52+
this.validateMeta()
53+
this.filterMetaLines()
54+
this.generateMetaLines()
55+
56+
writeLinesTo(target, metaLines, staticLines)
57+
}
58+
59+
/**
60+
* Validates that meta contains all required attributes
61+
* If it isn't throw GradleException
62+
*
63+
* @param metaItems List of MetaItem
64+
*/
65+
private validateMeta() {
66+
for (MetaItem item in meta.items) {
67+
if (!item.valid) {
68+
throw new GradleException("Plugin metadata parse error: '$item.id' must not be null")
69+
}
70+
}
71+
}
72+
73+
/**
74+
* Removes all meta lines from metaFile, and saves static lines to
75+
* list. If metaFile not exists only clears staticLines
76+
*/
77+
private void filterMetaLines() {
78+
staticLines.clear()
79+
if (!Files.exists(metaFile)) {
80+
return
81+
}
82+
83+
metaFile.eachLine { line ->
84+
line = line.trim()
85+
if (isStaticLine(line)) {
86+
staticLines << line
87+
}
88+
89+
return
90+
}
91+
92+
writeLinesTo(metaFile, staticLines)
93+
}
1794

18-
this.init()
95+
/**
96+
* Checks if line isn't dynamic meta or if line empty it isn't first
97+
*
98+
* @param line The line to check
99+
* @return true if line is static
100+
*/
101+
private boolean isStaticLine(String line) {
102+
return !isMetaLine(line) && (!line.empty || !staticLines.empty)
19103
}
20104

21-
void removeUnnecessary
105+
/**
106+
* Checks if line contains meta attributes
107+
*
108+
* @param line The line to check
109+
* @return true if line is dynamic meta, otherwise false
110+
*/
111+
private static boolean isMetaLine(String line) {
112+
for (String attribute in ATTRIBUTES) {
113+
if (line.startsWith("$attribute:")) {
114+
return true
115+
}
116+
}
117+
118+
return false
119+
}
120+
121+
/**
122+
* Generates meta lines from meta
123+
*/
124+
private void generateMetaLines() {
125+
metaLines.clear()
126+
meta.items.each { item ->
127+
if (item.value != null) {
128+
metaLines << item.entry
129+
}
130+
}
131+
}
132+
133+
/**
134+
* Writes lines to target file
135+
*
136+
* @param target The target to write
137+
* @param lineLists Lines to write
138+
*/
139+
private static void writeLinesTo(Path target, List<String>... lineLists) {
140+
target.withWriter("UTF-8") { writer ->
141+
lineLists.each { lineList ->
142+
lineList.each { line -> writer.println line }
143+
}
144+
}
145+
}
22146
}

src/main/groovy/ru/endlesscode/gradle/bukkit/meta/MetaItem.groovy

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,17 @@ class MetaItem {
2424
return resolve(this.value)
2525
}
2626

27-
private static String resolve(Object obj) {
27+
boolean isValid() {
28+
return !this.required || getValue() != null
29+
}
30+
31+
/**
32+
* Resolves all objects to String
33+
*
34+
* @param obj The object to resolve
35+
* @return Resolved string
36+
*/
37+
private static String resolve(def obj) {
2838
if (obj == null) {
2939
return null
3040
}

src/main/groovy/ru/endlesscode/gradle/bukkit/meta/PluginMeta.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ru.endlesscode.gradle.bukkit.meta
22

33
import org.gradle.api.Project
44

5+
@SuppressWarnings("GroovyUnusedDeclaration")
56
class PluginMeta {
67
final MetaItem name
78
final MetaItem description

src/main/groovy/ru/endlesscode/gradle/bukkit/meta/PluginMetaPlugin.groovy

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3,92 +3,20 @@ package ru.endlesscode.gradle.bukkit.meta
33
import org.gradle.api.Plugin
44
import org.gradle.api.Project
55
import org.gradle.api.file.CopySpec
6-
import org.gradle.api.plugins.JavaPluginConvention
76
import ru.endlesscode.gradle.bukkit.BukkitPluginExtension
87

98
class PluginMetaPlugin implements Plugin<Project> {
10-
11-
129
@Override
1310
void apply(Project project) {
1411
project.with {
1512
extensions.create(BukkitPluginExtension.NAME, BukkitPluginExtension, project)
16-
MetaFile meta = new MetaFile(project)
1713

1814
GenerateMeta genMeta = task("generatePluginMeta", type: GenerateMeta) {
19-
metaFile meta
15+
metaFile new MetaFile(project)
2016
} as GenerateMeta
2117

2218
tasks.processResources.dependsOn genMeta
2319
((CopySpec) tasks.processResources).from genMeta.target.toFile()
2420
}
2521
}
26-
27-
/**
28-
* Removes meta information from project meta if meta file
29-
* exists for project
30-
*
31-
* @param project The project
32-
*/
33-
static void processMetaFile(Project project) {
34-
File metaFile = getMetaFile(project)
35-
36-
if (metaFile.exists()) {
37-
removeAllMeta(metaFile)
38-
}
39-
}
40-
41-
/**
42-
* Gets project meta file even if it doesn't exist
43-
*
44-
* @param project The project
45-
* @return The meta file of project
46-
*/
47-
static File getMetaFile(Project project) {
48-
def java = project.convention.getPlugin(JavaPluginConvention)
49-
def resourceDir = java.sourceSets.main.resources.srcDirs[0]
50-
def metaFile = new File(resourceDir, MetaFile.META_FILE)
51-
52-
return metaFile
53-
}
54-
55-
/**
56-
* Removes all meta lines from file, also removes all lead
57-
* empty lines
58-
*
59-
* @param file The file with meta
60-
*/
61-
static void removeAllMeta(File file) {
62-
StringWriter tempWriter = new StringWriter()
63-
boolean firstLine = true
64-
file.filterLine(tempWriter) { String line ->
65-
line = line.trim()
66-
if (!isMetaLine(line) && (!line.isEmpty() || !firstLine)) {
67-
firstLine = false
68-
return true
69-
}
70-
71-
return false
72-
}
73-
74-
file.withWriter {
75-
it.write(tempWriter.toString())
76-
}
77-
}
78-
79-
/**
80-
* Checks if line contains meta attributes
81-
*
82-
* @param line The line to check
83-
* @return true if line is meta attribute, otherwise false
84-
*/
85-
static boolean isMetaLine(String line) {
86-
for (String attribute in MetaFile.META_ATTRIBUTES) {
87-
if (line.startsWith("$attribute:")) {
88-
return true
89-
}
90-
}
91-
92-
return false
93-
}
9422
}

0 commit comments

Comments
 (0)