Skip to content

Commit c7fd4ee

Browse files
authored
Merge branch 'master' into addTests
2 parents 1abae8f + 3d0220e commit c7fd4ee

File tree

11 files changed

+176
-66
lines changed

11 files changed

+176
-66
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ apply from: "jacoco.gradle"
1010

1111
group "ru.endlesscode"
1212
description "Bukkit Gradle integration plugins"
13-
version "0.5.8"
13+
version "0.6.7"
1414

1515
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8
1616

src/main/groovy/ru/endlesscode/bukkitgradle/BukkitGradlePlugin.groovy

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@ import org.gradle.api.artifacts.ResolvableDependencies
88
import org.gradle.api.plugins.JavaPluginConvention
99
import org.gradle.api.tasks.compile.JavaCompile
1010
import ru.endlesscode.bukkitgradle.meta.PluginMetaPlugin
11-
import ru.endlesscode.bukkitgradle.server.TestServerPlugin
11+
import ru.endlesscode.bukkitgradle.server.DevServerPlugin
1212

1313
class BukkitGradlePlugin implements Plugin<Project> {
14+
final static String GROUP = "Bukkit"
15+
1416
Project project
1517

18+
static boolean isTesting() {
19+
System.properties['test'] == 'true'
20+
}
21+
1622
@Override
1723
void apply(Project project) {
1824
this.project = project
@@ -39,7 +45,7 @@ class BukkitGradlePlugin implements Plugin<Project> {
3945
apply("eclipse")
4046
apply("idea")
4147
apply(PluginMetaPlugin)
42-
apply(TestServerPlugin)
48+
apply(DevServerPlugin)
4349
}
4450

4551
convention.getPlugin(JavaPluginConvention).with {

src/main/groovy/ru/endlesscode/bukkitgradle/extension/RunConfiguration.groovy

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package ru.endlesscode.bukkitgradle.extension
22

3+
import groovy.xml.MarkupBuilder
34
import org.gradle.api.Project
5+
import ru.endlesscode.bukkitgradle.server.PrepareServer
6+
import ru.endlesscode.bukkitgradle.server.ServerCore
47

8+
import java.nio.file.Files
59
import java.nio.file.Path
610

711
class RunConfiguration {
@@ -56,4 +60,39 @@ class RunConfiguration {
5660
Path getDir() {
5761
project.projectDir.toPath().resolve(this.dir)
5862
}
63+
64+
/**
65+
* Builds and writes to file run configuration in IDEA .xml format
66+
*
67+
* @param configurationDir The configurations dir
68+
*/
69+
void buildIdeaConfiguration(Path configurationDir) {
70+
if (Files.notExists(configurationDir)) {
71+
return
72+
}
73+
74+
def taskName = "Run Server"
75+
def serverDir = (project.tasks.prepareServer as PrepareServer).serverDir.toRealPath()
76+
def args = this.bukkitArgs
77+
78+
def realDebug = this.debug
79+
this.debug = false
80+
def props = this.getJavaArgs()
81+
this.debug = realDebug
82+
83+
def runConfiguration = configurationDir.resolve("${taskName.replace(" ", "_")}.xml")
84+
def xml = new MarkupBuilder(runConfiguration.newWriter())
85+
xml.component(name: "ProjectRunConfigurationManager") {
86+
configuration(default: 'false', name: taskName, type: "JarApplication", factoryName: "JAR Application", singleton: "true") {
87+
option(name: 'JAR_PATH', value: "${serverDir.resolve(ServerCore.CORE_NAME)}")
88+
option(name: 'VM_PARAMETERS', value: props)
89+
option(name: 'PROGRAM_PARAMETERS', value: args)
90+
option(name: 'WORKING_DIRECTORY', value: serverDir)
91+
envs()
92+
method {
93+
option(name: "Gradle.BeforeRunTask", enabled: "true", tasks: "prepareServer", externalProjectPath: '$PROJECT_DIR$', vmOptions: "", scriptParameters: "")
94+
}
95+
}
96+
}
97+
}
5998
}

src/main/groovy/ru/endlesscode/bukkitgradle/meta/PluginMetaPlugin.groovy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ru.endlesscode.bukkitgradle.meta
33
import org.gradle.api.Plugin
44
import org.gradle.api.Project
55
import org.gradle.api.file.CopySpec
6+
import ru.endlesscode.bukkitgradle.BukkitGradlePlugin
67
import ru.endlesscode.bukkitgradle.extension.Bukkit
78

89
class PluginMetaPlugin implements Plugin<Project> {
@@ -15,6 +16,11 @@ class PluginMetaPlugin implements Plugin<Project> {
1516
metaFile new MetaFile(project)
1617
} as GenerateMeta
1718

19+
genMeta.configure {
20+
group = BukkitGradlePlugin.GROUP
21+
description = 'Generate plugin.yml file'
22+
}
23+
1824
tasks.processResources.dependsOn genMeta
1925
(tasks.processResources as CopySpec).from genMeta.target.toFile()
2026
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package ru.endlesscode.bukkitgradle.server
2+
3+
import org.gradle.api.Plugin
4+
import org.gradle.api.Project
5+
import org.gradle.internal.impldep.org.apache.maven.lifecycle.LifecycleExecutionException
6+
import ru.endlesscode.bukkitgradle.BukkitGradlePlugin
7+
8+
import java.nio.file.Files
9+
import java.nio.file.Path
10+
11+
class DevServerPlugin implements Plugin<Project> {
12+
Project project
13+
14+
@Override
15+
void apply(Project project) {
16+
this.project = project
17+
ServerCore serverCore = new ServerCore(project)
18+
project.task("runServer", type: RunServer, dependsOn: "prepareServer") {
19+
core serverCore
20+
}.configure {
21+
group = BukkitGradlePlugin.GROUP
22+
description = 'Run dev server'
23+
}
24+
25+
PrepareServer prepareServer = project.task("prepareServer", type: PrepareServer, dependsOn: ["build", "copyServerCore"]) {
26+
core serverCore
27+
}.configure {
28+
group = BukkitGradlePlugin.GROUP
29+
description = 'Prepare server ro run. Configure server and copy compiled plugin to plugins dir'
30+
} as PrepareServer
31+
32+
Path runConfigurationsDir = project.projectDir.toPath().resolve(".idea/runConfigurations")
33+
project.task("buildIdeaRun", dependsOn: "prepareServer").doLast {
34+
if (Files.notExists(runConfigurationsDir.parent)) {
35+
throw new LifecycleExecutionException("This task only for IntelliJ IDEA.")
36+
}
37+
38+
Files.createDirectories(runConfigurationsDir)
39+
prepareServer.run.buildIdeaConfiguration(runConfigurationsDir)
40+
}.configure {
41+
group = BukkitGradlePlugin.GROUP
42+
description = 'Configure IDEA server run configuration'
43+
}
44+
45+
project.afterEvaluate {
46+
prepareServer.run.buildIdeaConfiguration(runConfigurationsDir)
47+
}
48+
}
49+
}

src/main/groovy/ru/endlesscode/bukkitgradle/server/PrepareServer.groovy

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,36 @@ class PrepareServer extends DefaultTask {
1313
@Input
1414
ServerCore core
1515

16-
private Path serverDir
17-
private RunConfiguration run
16+
Closure<Path> serverDir
17+
RunConfiguration run
1818

19-
@TaskAction
20-
void prepareServer() {
21-
this.serverDir = core.serverDir
19+
void setCore(ServerCore core) {
20+
this.core = core
21+
this.serverDir = { core.serverDir }
2222
this.run = project.bukkit.run
23+
}
2324

25+
@TaskAction
26+
void prepareServer() {
2427
resolveEula()
2528
resolveOnlineMode()
2629
copyPluginToServerDir()
2730
}
2831

2932
void resolveEula() {
30-
Path eulaFile = serverDir.resolve("eula.txt")
33+
Path eulaFile = getServerDir().resolve("eula.txt")
34+
if (!Files.exists(eulaFile)) {
35+
Files.createFile(eulaFile)
36+
}
3137

32-
boolean eula = this.run.eula
33-
eulaFile.text = "eula=$eula"
38+
Properties properties = new Properties()
39+
properties.load(eulaFile.newReader())
40+
properties.setProperty("eula", "${this.run.eula}")
41+
properties.store(eulaFile.newWriter(), "By changing the setting below to TRUE you are indicating your agreement to our EULA (https://account.mojang.com/documents/minecraft_eula).")
3442
}
3543

3644
void resolveOnlineMode() {
37-
Path propsFile = serverDir.resolve("server.properties")
45+
Path propsFile = getServerDir().resolve("server.properties")
3846
if (!Files.exists(propsFile)) {
3947
Files.createFile(propsFile)
4048
}
@@ -48,9 +56,16 @@ class PrepareServer extends DefaultTask {
4856
void copyPluginToServerDir() {
4957
String pluginName = "${project.bukkit.meta.name}.jar"
5058
Path jar = project.jar.archivePath.toPath()
51-
Path pluginsDir = serverDir.resolve("plugins")
52-
Files.createDirectories(pluginsDir)
59+
if (!Files.exists(jar)) {
60+
return
61+
}
5362

63+
Path pluginsDir = getServerDir().resolve("plugins")
64+
Files.createDirectories(pluginsDir)
5465
Files.copy(jar, pluginsDir.resolve(pluginName), StandardCopyOption.REPLACE_EXISTING)
5566
}
67+
68+
Path getServerDir() {
69+
return serverDir.call()
70+
}
5671
}

src/main/groovy/ru/endlesscode/bukkitgradle/server/ServerCore.groovy

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package ru.endlesscode.bukkitgradle.server
22

33
import de.undercouch.gradle.tasks.download.DownloadExtension
44
import org.gradle.api.Project
5+
import org.gradle.internal.impldep.org.apache.maven.lifecycle.LifecycleExecutionException
6+
import ru.endlesscode.bukkitgradle.BukkitGradlePlugin
57
import ru.endlesscode.bukkitgradle.extension.Bukkit
68

79
import java.nio.file.Files
@@ -36,40 +38,40 @@ class ServerCore {
3638
* Registers needed tasks
3739
*/
3840
void registerTasks() {
39-
registerUpdateMetaTask()
4041
registerDownloadingTask()
4142
registerCoreCopyTask()
4243
}
4344

4445
/**
45-
* Registers updating server core metadata task
46+
* Registers core downloading task
4647
*/
47-
void registerUpdateMetaTask() {
48-
def task = project.task("updateServerCoreMetadata")
49-
task.extensions.create("download", DownloadExtension, project)
48+
void registerDownloadingTask() {
49+
project.task("downloadServerCore") {
50+
def skip = project.gradle.startParameter.isOffline() || BukkitGradlePlugin.isTesting()
51+
onlyIf { !skip }
52+
53+
if (skip) {
54+
return
55+
}
56+
57+
extensions.create("download", DownloadExtension, project)
5058

51-
task.doLast {
5259
download {
5360
src "https://hub.spigotmc.org/nexus/content/repositories/snapshots/org/bukkit/bukkit/$MAVEN_METADATA"
5461
dest downloadDir.toFile()
5562
quiet true
5663
}
57-
}
58-
}
59-
60-
/**
61-
* Registers core downloading task
62-
*/
63-
void registerDownloadingTask() {
64-
def task = project.task("downloadServerCore", dependsOn: "updateServerCoreMetadata")
65-
task.extensions.create("download", DownloadExtension, project)
6664

67-
task.doLast {
68-
download {
69-
src { "https://yivesmirror.com/files/spigot/${getCoreName()}" }
70-
dest downloadDir.toFile()
71-
onlyIfNewer true
65+
doLast {
66+
download {
67+
src "https://yivesmirror.com/files/spigot/${getCoreName()}"
68+
dest downloadDir.toFile()
69+
onlyIfNewer true
70+
}
7271
}
72+
}.configure {
73+
group = BukkitGradlePlugin.GROUP
74+
description = 'Download Spigot server core'
7375
}
7476
}
7577

@@ -83,6 +85,9 @@ class ServerCore {
8385
Path destination = getServerDir().resolve(CORE_NAME)
8486

8587
Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING)
88+
}.configure {
89+
group = BukkitGradlePlugin.GROUP
90+
description = 'Copy downloaded server core to server directory'
8691
}
8792
}
8893
}
@@ -117,6 +122,14 @@ class ServerCore {
117122
}
118123

119124
Path metaFile = downloadDir.resolve(MAVEN_METADATA)
125+
if (Files.notExists(metaFile)) {
126+
if (BukkitGradlePlugin.isTesting()) {
127+
return '1.11.0'
128+
}
129+
130+
throw new LifecycleExecutionException("Server cores meta not downloaded, make sure that Gradle isn't running in offline mode.")
131+
}
132+
120133
def metadata = new XmlSlurper().parse(metaFile.toFile())
121134
metadata.versioning.latest.toString()
122135
}

src/main/groovy/ru/endlesscode/bukkitgradle/server/TestServerPlugin.groovy

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/test/groovy/ru/endlesscode/bukkitgradle/TestBase.groovy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class TestBase {
1414

1515
@Before
1616
void setUp() throws Exception {
17+
System.properties.setProperty('test', 'true')
18+
1719
this.project = ProjectBuilder.builder()
1820
.withName("TestProject")
1921
.withProjectDir(new File("build/testProject"))
@@ -66,4 +68,16 @@ command:
6668

6769
task.execute()
6870
}
71+
72+
protected void configureRun() {
73+
this.project.bukkit.run.with {
74+
eula = true
75+
onlineMode = true
76+
debug = false
77+
dir = "devServer"
78+
encoding = "CP866"
79+
javaArgs = "-Xmx2G"
80+
bukkitArgs = "-s 2"
81+
}
82+
}
6983
}

src/test/groovy/ru/endlesscode/bukkitgradle/extension/RunConfigurationTest.groovy

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,4 @@ class RunConfigurationTest extends TestBase {
2929
assertEquals("-s 2", bukkitArgs)
3030
}
3131
}
32-
33-
void configureRun() {
34-
this.project.bukkit.run.with {
35-
eula = true
36-
onlineMode = true
37-
debug = false
38-
dir = "devServer"
39-
encoding = "CP866"
40-
javaArgs = "-Xmx2G"
41-
bukkitArgs = "-s 2"
42-
}
43-
}
4432
}

0 commit comments

Comments
 (0)