Skip to content

Commit 0fe7563

Browse files
authored
Merge pull request #14 from EndlessCodeGroup/feature/localProperties
Local properties
2 parents dfae361 + 9c9284b commit 0fe7563

File tree

8 files changed

+100
-47
lines changed

8 files changed

+100
-47
lines changed

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,21 @@ Also you can add custom (unsupported by BukkitGradle) attributes like a `depend`
113113
Just create `plugin.yml` file and put custom attributes into.
114114

115115
### Running Dev server
116-
Before running server you should configure path to BuildTools:
117-
```groovy
118-
bukkit {
119-
buildtools = '/path/to/BuildTools.jar' // It can be only local directory
120-
}
116+
Before running server you should configure BuildTools and dev server location.
117+
118+
You can define it in `local.properties` file (that was automatically created in project root on refresh):
119+
```properties
120+
# Absolute path to directory that contains BuildTools.jar
121+
buildtools.dir=/path/to/buildtools/
122+
# Absolute path to dev server
123+
server.dir=/path/to/buildtools/
121124
```
125+
Or you can define it globally (for all projects that uses BukkitGradle) with environment variables `BUKKIT_DEV_SERVER_HOME`
126+
and `BUILDTOOLS_HOME`.
122127

123128
##### On IntelliJ IDEA
124-
Run `:buildIdeaRun` task. To your IDE will be added Run Configuration that will dynamically refreshes when you change server configurations.
129+
Run `:buildIdeaRun` task. To your IDE will be added Run Configuration that will dynamically refreshes when you change
130+
server configurations.
125131

126132
![Run Configuration](http://image.prntscr.com/image/1a12a03b8ac54fccb7d5b70a335fa996.png)
127133

@@ -138,8 +144,6 @@ bukkit {
138144
eula = false
139145
// Set online-mode flag
140146
onlineMode = false
141-
// Path to deploy server (relative)
142-
dir = "server"
143147
// Debug mode (listen 5005 port, if you use running from IDEA this option will be ignored)
144148
debug = true
145149
// Set server encoding (flag -Dfile.encoding)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class Bukkit {
1111
private final Project project
1212

1313
String version
14-
String buildtools = ''
1514

1615
final PluginMeta meta
1716
final RunConfiguration run

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ class RunConfiguration {
1818
boolean onlineMode
1919
boolean debug
2020
String encoding
21-
String dir
2221
String javaArgs
2322
String bukkitArgs
2423

@@ -29,7 +28,6 @@ class RunConfiguration {
2928
this.onlineMode = false
3029
this.debug = true
3130
this.encoding = 'UTF-8'
32-
this.dir = 'server'
3331

3432
this.javaArgs = '-Xmx1G'
3533
this.bukkitArgs = ''
@@ -53,15 +51,6 @@ class RunConfiguration {
5351
return bukkitArgs ?: ''
5452
}
5553

56-
/**
57-
* Returns servers dir
58-
*
59-
* @return The directory
60-
*/
61-
Path getDir() {
62-
project.projectDir.toPath().resolve(this.dir)
63-
}
64-
6554
/**
6655
* Builds and writes to file run configuration in IDEA .xml format
6756
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class MetaFile {
7676
*/
7777
private void filterMetaLines() {
7878
staticLines.clear()
79-
if (!Files.exists(metaFile)) {
79+
if (Files.notExists(metaFile)) {
8080
return
8181
}
8282

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

Lines changed: 84 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,31 @@ import ru.endlesscode.bukkitgradle.BukkitGradlePlugin
88
import ru.endlesscode.bukkitgradle.extension.Bukkit
99
import ru.endlesscode.bukkitgradle.util.MavenApi
1010

11+
import javax.annotation.Nullable
1112
import java.nio.file.Files
1213
import java.nio.file.Path
1314
import java.nio.file.Paths
1415

1516
class ServerCore {
1617
public static final String CORE_NAME = "core.jar"
18+
public static final String SERVER_HOME_PROPERTY = "server.dir"
19+
public static final String SERVER_HOME_ENV = "BUKKIT_DEV_SERVER_HOME"
20+
public static final String BUILDTOOLS_NAME = "BuildTools.jar"
21+
public static final String BUILDTOOLS_HOME_PROPERTY = "buildtools.dir"
22+
public static final String BUILDTOOLS_HOME_ENV = "BUILDTOOLS_HOME"
1723

1824
private static final String MAVEN_METADATA = "maven-metadata.xml"
1925

2026
private final Project project
2127

2228
private Path bukkitGradleDir
2329
private boolean forceRebuild = false
30+
private Properties localProps = new Properties()
2431

2532
ServerCore(Project project) {
2633
this.project = project
2734

2835
MavenApi.init(project)
29-
3036
this.initDir()
3137

3238
project.afterEvaluate {
@@ -47,8 +53,8 @@ class ServerCore {
4753
*/
4854
void registerTasks() {
4955
registerBukkitMetaTask()
50-
registerCoreCopyTask()
5156
registerBuildServerCoreTask()
57+
registerCoreCopyTask()
5258
}
5359

5460
/**
@@ -83,6 +89,11 @@ class ServerCore {
8389
group = BukkitGradlePlugin.GROUP
8490
description = 'Copy built server core to server directory'
8591

92+
if (!tasks.buildServerCore.enabled) {
93+
enabled = false
94+
return
95+
}
96+
8697
def coreName = getCoreName()
8798
from MavenApi.getSpigotDir(realVersion)
8899
include coreName
@@ -109,17 +120,23 @@ class ServerCore {
109120
return !MavenApi.hasSpigot(getRealVersion())
110121
}
111122

112-
def path = Paths.get(project.bukkit.buildtools as String)
123+
if (buildToolsPath == null || serverDir == null) {
124+
project.logger.warn("You can't use server running feature.")
125+
enabled = false
126+
return
127+
}
128+
129+
def path = buildToolsPath.resolve(BUILDTOOLS_NAME)
113130
def absolutePath = path.toAbsolutePath().toString()
114-
if (Files.notExists(path) || !Files.isRegularFile(path)) {
131+
if (Files.notExists(path) || Files.isDirectory(path)) {
115132
project.logger.warn("BuildTools not found on path: '$absolutePath'\n" +
116-
'It should be path to .jar file of BuildTools.')
133+
'BuildTools directory should contains BuildTools.jar file.')
117134
enabled = false
118135
return
119136
}
120137

121138
main = '-jar'
122-
args absolutePath, '--rev', getSimpleVersion()
139+
args(absolutePath, '--rev', getSimpleVersion())
123140
workingDir = path.getParent().toAbsolutePath().toString()
124141
standardInput = System.in
125142
}
@@ -150,6 +167,67 @@ class ServerCore {
150167
return getRealVersion().replace(Bukkit.REVISION_SUFFIX, '')
151168
}
152169

170+
/**
171+
* Returns server directory
172+
*
173+
* @return Server directory or null if dev server location not defined
174+
*/
175+
@Nullable
176+
Path getServerDir() {
177+
return getDirFromPropsOrEnv(SERVER_HOME_PROPERTY, SERVER_HOME_ENV, "Dev server location")
178+
}
179+
180+
private @Nullable
181+
Path getBuildToolsPath() {
182+
return getDirFromPropsOrEnv(BUILDTOOLS_HOME_PROPERTY, BUILDTOOLS_HOME_ENV, "BuildTools location")
183+
}
184+
185+
private @Nullable
186+
Path getDirFromPropsOrEnv(String propertyName, String envVariable, String comment) {
187+
this.initLocalProps()
188+
189+
def localProp = localProps.getProperty(propertyName)
190+
def globalEnv = System.getenv(envVariable)
191+
if (localProp == null && globalEnv == null) {
192+
project.logger.warn("$comment not found. It can be fixed by two ways:\n" +
193+
" 1. Define location with '$propertyName' in the local.properties file\n" +
194+
" 2. Define $envVariable environment variable")
195+
return null
196+
}
197+
def dir = Paths.get(localProp ?: globalEnv)
198+
Files.createDirectories(dir)
199+
200+
return dir
201+
}
202+
203+
private initLocalProps() {
204+
Path propsFile = this.project.file("local.properties").toPath()
205+
if (Files.exists(propsFile)) {
206+
localProps.load(propsFile.newReader())
207+
return
208+
}
209+
210+
project.logger.info("Local properties file not found. Creating...")
211+
Files.createFile(propsFile)
212+
localProps.load(propsFile.newReader())
213+
214+
if (System.getenv(SERVER_HOME_ENV) == null) {
215+
localProps.setProperty(SERVER_HOME_PROPERTY, project.file("build/server").absolutePath)
216+
}
217+
218+
if (System.getenv(BUILDTOOLS_HOME_ENV) == null) {
219+
localProps.setProperty(BUILDTOOLS_HOME_PROPERTY, project.file("build/buildtools").absolutePath)
220+
}
221+
222+
localProps.store(propsFile.newWriter(), " This file should *NOT* be checked into Version Control Systems,\n" +
223+
" as it contains information specific to your local configuration.\n" +
224+
" \n" +
225+
" Location of the dev server and BuildTools.\n" +
226+
" For customization when using a Version Control System, please read the\n" +
227+
" header note."
228+
)
229+
}
230+
153231
/**
154232
* Resolves and returns dynamic version
155233
*
@@ -178,16 +256,4 @@ class ServerCore {
178256
def metadata = new XmlSlurper().parse(metaFile.toFile())
179257
return metadata.versioning.latest.toString()
180258
}
181-
182-
/**
183-
* Returns server directory
184-
*
185-
* @return Server directory
186-
*/
187-
Path getServerDir() {
188-
Path serverDir = this.project.bukkit.run.dir.resolve(getSimpleVersion())
189-
Files.createDirectories(serverDir)
190-
191-
return serverDir
192-
}
193259
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ abstract class SystemScript {
2222
*/
2323
void buildOn(Path dir) {
2424
Path scriptFile = dir.resolve(getFileName())
25-
if (!Files.exists(scriptFile)) {
25+
if (Files.notExists(scriptFile)) {
2626
Files.createFile(scriptFile)
2727
}
2828

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class PrepareServer extends DefaultTask {
3333

3434
void resolveEula() {
3535
Path eulaFile = getServerDir().resolve("eula.txt")
36-
if (!Files.exists(eulaFile)) {
36+
if (Files.notExists(eulaFile)) {
3737
Files.createFile(eulaFile)
3838
}
3939

@@ -45,7 +45,7 @@ class PrepareServer extends DefaultTask {
4545

4646
void resolveOnlineMode() {
4747
Path propsFile = getServerDir().resolve("server.properties")
48-
if (!Files.exists(propsFile)) {
48+
if (Files.notExists(propsFile)) {
4949
Files.createFile(propsFile)
5050
}
5151

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ class TestBase {
2727
description = "Test project description"
2828
version = "1.0"
2929
ext.url = "https://www.example.ru/"
30-
31-
bukkit {
32-
buildtools = "/path/to/buildtools"
33-
}
3430
}
3531
}
3632

@@ -78,7 +74,6 @@ command:
7874
eula = true
7975
onlineMode = true
8076
debug = false
81-
dir = 'devServer'
8277
encoding = 'CP866'
8378
javaArgs = '-Xmx2G'
8479
bukkitArgs = '-s 2'

0 commit comments

Comments
 (0)