Skip to content

Commit e97eb69

Browse files
committed
Refactored build.gradle
1 parent 109d27d commit e97eb69

File tree

1 file changed

+57
-47
lines changed

1 file changed

+57
-47
lines changed

build.gradle

+57-47
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import com.beust.klaxon.JsonObject
1+
import groovy.json.JsonOutput
22

3+
import java.nio.file.Path
34
import java.nio.file.Paths
45
import java.util.regex.Pattern
56
import java.util.stream.Collectors
@@ -19,7 +20,6 @@ buildscript {
1920
//noinspection DifferentKotlinGradleVersion
2021
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
2122
classpath "com.github.jengelman.gradle.plugins:shadow:$shadowJarVersion"
22-
classpath "com.beust:klaxon:5.2"
2323
}
2424
}
2525

@@ -43,42 +43,54 @@ allprojects {
4343
testCompile "org.jetbrains.kotlin:kotlin-test:$kotlinVersion"
4444
}
4545

46-
String artifactsPathStr = rootProject.findProperty('artifactsPath') ?: 'artifacts'
47-
String buildCounterStr = rootProject.findProperty('build.counter') ?: '100500'
48-
String buildNumber = rootProject.findProperty('build.number') ?: ''
49-
String installPath = rootProject.findProperty('installPath')
50-
51-
ext.rootPath = rootDir.toPath()
52-
ext.packageName = 'kotlin-jupyter-kernel'
53-
ext.artifactsDir = rootPath.resolve(artifactsPathStr)
54-
ext.isProtectedBranch = isProtectedBranch()
55-
ext.versionFileName = "VERSION"
56-
57-
ext.installPathLocal = installPath ? Paths.get(installPath) :
58-
Paths.get(System.properties['user.home'].toString(), ".ipython", "kernels", "kotlin")
59-
ext.distributionPath = rootPath.resolve("distrib")
60-
ext.distribBuildPath = rootPath.resolve("distrib-build")
61-
ext.logosPath = getSubDir(rootPath, "resources", "logos")
62-
63-
String devAddition = isProtectedBranch ? '' : '.dev1'
64-
String defaultBuildNumber = "$baseVersion.$buildCounterStr$devAddition"
65-
String buildNumberRegex = "[0-9]+(\\.[0-9]+){3}(\\.dev[0-9]+)?"
66-
67-
if (!Pattern.matches(buildNumberRegex, buildNumber)) {
68-
def versionFile = artifactsDir.resolve(versionFileName).toFile()
69-
if (versionFile.exists()) {
70-
def lines = versionFile.readLines()
71-
assert !lines.empty, "There should be at least one line in VERSION file"
72-
buildNumber = lines.first().trim()
46+
ext {
47+
packageName = "kotlin-jupyter-kernel"
48+
versionFileName = "VERSION"
49+
50+
String artifactsPathStr = rootProject.findProperty('artifactsPath') ?: 'artifacts'
51+
String installPath = rootProject.findProperty('installPath')
52+
53+
//noinspection GroovyAssignabilityCheck
54+
rootPath = rootDir.toPath()
55+
//noinspection GroovyAssignabilityCheck
56+
artifactsDir = rootPath.resolve(artifactsPathStr)
57+
58+
//noinspection GroovyAssignabilityCheck
59+
installPathLocal = installPath ? Paths.get(installPath) :
60+
Paths.get(System.properties['user.home'].toString(), ".ipython", "kernels", "kotlin")
61+
//noinspection GroovyAssignabilityCheck
62+
distributionPath = rootPath.resolve("distrib")
63+
//noinspection GroovyAssignabilityCheck
64+
distribBuildPath = rootPath.resolve("distrib-build")
65+
//noinspection GroovyAssignabilityCheck
66+
logosPath = getSubDir(rootPath, "resources", "logos")
67+
68+
if (project == rootProject) {
69+
ext.isProtectedBranch = isProtectedBranch()
70+
String buildCounterStr = rootProject.findProperty('build.counter') ?: '100500'
71+
String buildNumber = rootProject.findProperty('build.number') ?: ''
72+
String devAddition = isProtectedBranch ? '' : '.dev1'
73+
String defaultBuildNumber = "$baseVersion.$buildCounterStr$devAddition"
74+
String buildNumberRegex = "[0-9]+(\\.[0-9]+){3}(\\.dev[0-9]+)?"
75+
76+
if (!Pattern.matches(buildNumberRegex, buildNumber)) {
77+
def versionFile = artifactsDir.resolve(versionFileName).toFile()
78+
if (versionFile.exists()) {
79+
def lines = versionFile.readLines()
80+
assert !lines.empty, "There should be at least one line in VERSION file"
81+
buildNumber = lines.first().trim()
82+
} else {
83+
buildNumber = defaultBuildNumber
84+
}
85+
}
86+
87+
project.version = buildNumber
88+
println("##teamcity[buildNumber '$version']")
7389
} else {
74-
buildNumber = defaultBuildNumber
90+
ext.isProtectedBranch = rootProject.isProtectedBranch
91+
project.version = rootProject.version
7592
}
76-
}
7793

78-
project.version = buildNumber
79-
println("##teamcity[buildNumber '$version']")
80-
81-
ext {
8294
debugPort = 1044
8395
debuggerConfig = "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$debugPort".toString()
8496

@@ -207,24 +219,24 @@ static String makeTaskName(String prefix, Boolean local) {
207219
return prefix + (local ? "Local" : "Distrib")
208220
}
209221

210-
static void makeDirs(java.nio.file.Path path) {
222+
static void makeDirs(Path path) {
211223
File dir = path.toFile()
212224
if (!dir.exists()) {
213225
dir.mkdirs()
214226
}
215227
}
216228

217-
static java.nio.file.Path getSubDir(java.nio.file.Path dir, String... subDir) {
229+
static Path getSubDir(Path dir, String... subDir) {
218230
def newDir = dir
219231
for (s in subDir) {
220232
newDir = newDir.resolve(s)
221233
}
222234
return newDir
223235
}
224236

225-
static void writeJson(Map<String, Object> json, java.nio.file.Path path) {
226-
def jsonString = new JsonObject(json).toJsonString(true, false)
227-
path.toFile().write(jsonString, 'UTF-8')
237+
static void writeJson(Map<String, Object> json, Path path) {
238+
def str = JsonOutput.prettyPrint(JsonOutput.toJson(json))
239+
path.toFile().write(str, 'UTF-8')
228240
}
229241

230242
void createCleanTasks() {
@@ -241,7 +253,7 @@ void createCleanTasks() {
241253
createCleanTasks()
242254

243255
@SuppressWarnings("unused")
244-
void createInstallTasks(Boolean local, java.nio.file.Path specPath, java.nio.file.Path mainInstallPath) {
256+
void createInstallTasks(Boolean local, Path specPath, Path mainInstallPath) {
245257
def groupName = local ? localGroup : distribGroup
246258
def cleanDirTask = getTasks().getByName(makeTaskName(cleanInstallDirTaskPrefix, local))
247259
def args = [type: Copy, dependsOn: cleanDirTask, group: groupName]
@@ -267,7 +279,7 @@ void createInstallTasks(Boolean local, java.nio.file.Path specPath, java.nio.fil
267279
}
268280
}
269281

270-
String createTaskForSpecs(Boolean debug, Boolean local, String group, Task cleanDir, java.nio.file.Path specPath, java.nio.file.Path mainInstallPath) {
282+
String createTaskForSpecs(Boolean debug, Boolean local, String group, Task cleanDir, Path specPath, Path mainInstallPath) {
271283
String taskName = makeTaskName(debug ? "createDebugSpecs" : "createSpecs", local)
272284
task([group: group], taskName) {
273285
dependsOn cleanDir, shadowJar
@@ -308,7 +320,7 @@ void createMainInstallTask(Boolean debug, Boolean local, String group, String sp
308320
}
309321
}
310322

311-
void makeKernelSpec(java.nio.file.Path installPath, Boolean localInstall) {
323+
void makeKernelSpec(Path installPath, Boolean localInstall) {
312324
def argv = localInstall ?
313325
Arrays.asList("python",
314326
installPath.resolve(runKernelPy).toString(),
@@ -329,7 +341,7 @@ void makeKernelSpec(java.nio.file.Path installPath, Boolean localInstall) {
329341
}
330342
}
331343

332-
void makeJarArgs(java.nio.file.Path installPath, String kernelJarPath, List<String> classPath, String debuggerConfig = "") {
344+
void makeJarArgs(Path installPath, String kernelJarPath, List<String> classPath, String debuggerConfig = "") {
333345
writeJson([
334346
"mainJar": kernelJarPath,
335347
"classPath": classPath,
@@ -346,8 +358,6 @@ task copyRunKernelPy(type: Copy, dependsOn: cleanInstallDirLocal, group: localGr
346358

347359
createInstallTasks(true, installPathLocal, installPathLocal)
348360

349-
task uninstall(dependsOn: cleanInstallDirLocal, group: localGroup) {
350-
351-
}
361+
task uninstall(dependsOn: cleanInstallDirLocal, group: localGroup)
352362

353363
apply from: 'distrib.gradle'

0 commit comments

Comments
 (0)