Skip to content

Commit 57bda87

Browse files
committedJul 26, 2021
Remove libraries submodule and add Gradle task to checkout libraries from the remote
1 parent eee13a0 commit 57bda87

File tree

10 files changed

+138
-16
lines changed

10 files changed

+138
-16
lines changed
 

‎.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@
1414
# Folders with build files
1515
out/
1616
/build/
17+
/build-tools/*/.idea/
1718
/build-tools/*/build/
1819
/*jupyter*/*/build/
1920
/api-examples/*/build/
2021
/teamcity-artifacts/
2122

23+
# Folder with library descriptors
24+
libraries/
25+
2226
# Gradle caches and internal files
2327
.gradle/
2428

‎.gitmodules

-3
This file was deleted.

‎build-tools/build-plugin/build.gradle.kts

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ repositories {
1111
}
1212

1313
dependencies {
14-
// implementation(projects.commonDependencies)
15-
implementation(libs.jupyter.commonDependencies)
14+
implementation(projects.commonDependencies)
1615
api(libs.bundles.allGradlePlugins)
1716
}
1817

‎build-tools/build-plugin/src/build/RootSettingsExtension.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ class RootSettingsExtension(
5252
val readmeFile: File = project.rootDir.resolve("docs").resolve("README.md")
5353
val readmeStubFile: File = project.rootDir.resolve("docs").resolve("README-STUB.md")
5454

55-
val librariesDir: File = project.rootDir.resolve("libraries")
56-
val librariesPropertiesFile: File = librariesDir.resolve(".properties")
55+
val librariesDir: File = BUILD_LIBRARIES.localLibrariesDir
5756

5857
val localInstallDir: File = run {
5958
val installPath = project.typedProperty<String?>("installPath")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package build
2+
3+
import org.gradle.api.DefaultTask
4+
import org.gradle.api.Project
5+
import org.gradle.api.provider.Property
6+
import org.gradle.api.tasks.Input
7+
import org.gradle.api.tasks.OutputDirectory
8+
import org.gradle.api.tasks.TaskAction
9+
import org.gradle.kotlin.dsl.create
10+
import org.gradle.kotlin.dsl.property
11+
import java.io.File
12+
13+
abstract class UpdateLibrariesTask : DefaultTask() {
14+
@get:Input
15+
val latestCommitHash: Property<String> = project.objects.property<String>().apply {
16+
set(project.provider { BUILD_LIBRARIES.latestCommitOnDefaultBranch })
17+
}
18+
19+
@get:OutputDirectory
20+
val librariesDir: Property<File> = project.objects.property<File>().apply {
21+
set(BUILD_LIBRARIES.localLibrariesDir)
22+
}
23+
24+
@TaskAction
25+
fun update() {
26+
val latestSha = latestCommitHash.get()
27+
if (BUILD_LIBRARIES.checkIfRefUpToDate(latestSha)) return
28+
BUILD_LIBRARIES.downloadLibraries(latestSha)
29+
}
30+
31+
companion object {
32+
fun getOrCreate(project: Project): UpdateLibrariesTask {
33+
val task = project.tasks.findByName(UPDATE_LIBRARIES_TASK_NAME)
34+
if (task != null) return task as UpdateLibrariesTask
35+
return project.tasks.create<UpdateLibrariesTask>(UPDATE_LIBRARIES_TASK_NAME)
36+
}
37+
}
38+
}

‎build-tools/build-plugin/src/build/kernelBuild.kt

+16-3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import java.io.OutputStream
4141

4242
@Serializable
4343
class NewPrData(
44+
@Suppress("unused")
4445
val title: String,
4546
@Suppress("unused")
4647
val head: String,
@@ -75,6 +76,8 @@ internal class KernelBuildConfigurator(private val project: Project) {
7576
setupVersionsPlugin()
7677
setupKtLintForAllProjects()
7778

79+
setupUpdateLibrariesTask()
80+
7881
println("##teamcity[buildNumber '${opts.pyPackageVersion}']")
7982
println("##teamcity[setParameter name='mavenVersion' value='${opts.mavenVersion}']")
8083

@@ -138,6 +141,10 @@ internal class KernelBuildConfigurator(private val project: Project) {
138141
}
139142
}
140143

144+
private fun setupUpdateLibrariesTask() {
145+
UpdateLibrariesTask.getOrCreate(project)
146+
}
147+
141148
private fun createCleanTasks() {
142149
listOf(true, false).forEach { local ->
143150
val dir = if (local) opts.localInstallDir else opts.distribBuildDir
@@ -156,9 +163,10 @@ internal class KernelBuildConfigurator(private val project: Project) {
156163
val groupName = if (local) LOCAL_INSTALL_GROUP else DISTRIBUTION_GROUP
157164
val cleanDirTask = tasks.getByName(makeTaskName(opts.cleanInstallDirTaskPrefix, local))
158165
val shadowJar = tasks.getByName(SHADOW_JAR_TASK)
166+
val updateLibrariesTask = tasks.named(UPDATE_LIBRARIES_TASK_NAME)
159167

160168
tasks.register<Copy>(makeTaskName(opts.copyLibrariesTaskPrefix, local)) {
161-
dependsOn(cleanDirTask)
169+
dependsOn(cleanDirTask, updateLibrariesTask)
162170
group = groupName
163171
from(opts.librariesDir)
164172
into(mainInstallPath.resolve(opts.librariesDir))
@@ -610,7 +618,7 @@ internal class KernelBuildConfigurator(private val project: Project) {
610618
}
611619

612620
private fun updateLibraryParam(libName: String, paramName: String, paramValue: String) {
613-
val libFile = project.file(opts.librariesDir).resolve("$libName.json")
621+
val libFile = project.file(opts.librariesDir).resolve(BUILD_LIBRARIES.descriptorFileName(libName))
614622
val libText = libFile.readText()
615623
val paramRegex = Regex("""^([ \t]*"$paramName"[ \t]*:[ \t]*")(.*)("[ \t]*,?)$""", RegexOption.MULTILINE)
616624
val newText = libText.replace(paramRegex, "$1$paramValue$3")
@@ -625,7 +633,8 @@ internal class KernelBuildConfigurator(private val project: Project) {
625633
opts.jvmTargetForSnippets?.let {
626634
add("jvmTargetForSnippets" to it)
627635
}
628-
val librariesProperties = readProperties(opts.librariesPropertiesFile)
636+
BUILD_LIBRARIES.downloadLatestPropertiesFile()
637+
val librariesProperties = readProperties(BUILD_LIBRARIES.localPropertiesFile)
629638
add("librariesFormatVersion" to librariesProperties["formatVersion"].orEmpty())
630639
}
631640

@@ -636,6 +645,8 @@ internal class KernelBuildConfigurator(private val project: Project) {
636645
}
637646

638647
private fun prepareReadmeTasks() {
648+
val updateTask = tasks.named(UPDATE_LIBRARIES_TASK_NAME)
649+
639650
val kotlinVersion = project.defaultVersionCatalog.versions.devKotlin
640651

641652
val readmeFile = opts.readmeFile
@@ -648,6 +659,8 @@ internal class KernelBuildConfigurator(private val project: Project) {
648659
inputs.dir(librariesDir)
649660
inputs.property("kotlinVersion", kotlinVersion)
650661
inputs.property("projectRepoUrl", opts.projectRepoUrl)
662+
663+
dependsOn(updateTask)
651664
}
652665

653666
val generateReadme = tasks.register(GENERATE_README_TASK) {

‎build-tools/build-plugin/src/build/taskNames.kt

+2
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ const val UNINSTALL_TASK = "uninstall"
3636

3737
const val MAKE_CHANGES_PR_TASK = "makeChangesPR"
3838
const val PREPARE_PACKAGE_TASK = "preparePackage"
39+
40+
const val UPDATE_LIBRARIES_TASK_NAME = "updateLibraryDescriptors"

‎build-tools/build-plugin/src/build/util.kt

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.gradle.api.plugins.ExtensionContainer
77
import org.gradle.kotlin.dsl.exclude
88
import org.gradle.kotlin.dsl.extra
99
import org.gradle.tooling.BuildException
10+
import org.jetbrains.kotlinx.jupyter.common.LibraryDescriptorsManager
1011
import java.io.File
1112
import java.nio.file.Path
1213
import java.util.Optional
@@ -15,6 +16,8 @@ import kotlin.reflect.KProperty
1516
import kotlin.reflect.KType
1617
import kotlin.reflect.typeOf
1718

19+
val BUILD_LIBRARIES = LibraryDescriptorsManager.getInstance()
20+
1821
fun makeTaskName(prefix: String, local: Boolean) = prefix + (if (local) "Local" else "Distrib")
1922

2023
fun makeDirs(dir: File) {

‎build.gradle.kts

+2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ tasks {
103103
classpath = files(shadowJar.get()) + classpath
104104
}
105105

106+
dependsOn(updateLibraryDescriptors.get())
107+
106108
systemProperties = mutableMapOf(
107109
"junit.jupiter.displayname.generator.default" to "org.junit.jupiter.api.DisplayNameGenerator\$ReplaceUnderscores",
108110

‎jupyter-lib/common-dependencies/src/main/kotlin/org/jetbrains/kotlinx/jupyter/common/libraries.kt

+71-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.jetbrains.kotlinx.jupyter.common
22

33
import kotlinx.serialization.json.JsonObject
44
import kotlinx.serialization.json.JsonPrimitive
5+
import kotlinx.serialization.json.jsonPrimitive
56
import org.slf4j.Logger
67
import org.slf4j.LoggerFactory
78
import java.io.File
@@ -32,17 +33,29 @@ class LibraryDescriptorsManager private constructor(
3233
val userLibrariesDir = userSettingsDir.resolve(userPath)
3334
val userCacheDir = userSettingsDir.resolve("cache")
3435
val localLibrariesDir = File(localPath)
36+
val defaultBranch = "master"
37+
val latestCommitOnDefaultBranch by lazy {
38+
getLatestCommitToLibraries(defaultBranch)!!.first
39+
}
40+
3541
fun homeLibrariesDir(homeDir: File? = null) = (homeDir ?: File("")).resolve(homePath)
3642

3743
val localPropertiesFile = localLibrariesDir.resolve(PROPERTIES_FILE)
44+
val commitHashFile by lazy {
45+
localLibrariesDir.resolve(COMMIT_HASH_FILE).also { file ->
46+
if (!file.exists()) {
47+
file.createDirsAndWrite()
48+
}
49+
}
50+
}
3851

3952
fun descriptorFileName(name: String) = "$name.$DESCRIPTOR_EXTENSION"
4053

4154
fun isLibraryDescriptor(file: File): Boolean {
4255
return file.isFile && file.name.endsWith(".$DESCRIPTOR_EXTENSION")
4356
}
4457

45-
fun getLatestCommitToLibraries(ref: String, sinceTimestamp: String?): Pair<String, String>? {
58+
fun getLatestCommitToLibraries(ref: String, sinceTimestamp: String? = null): Pair<String, String>? {
4659
return catchAll {
4760
var url = "$apiPrefix/commits?path=$remotePath&sha=$ref"
4861
if (sinceTimestamp != null) {
@@ -69,18 +82,69 @@ class LibraryDescriptorsManager private constructor(
6982
fun downloadLibraryDescriptor(ref: String, name: String): String {
7083
val url = "$apiPrefix/contents/$remotePath/$name.$DESCRIPTOR_EXTENSION?ref=$ref"
7184
logger.info("Requesting library descriptor at $url")
72-
val response = getHttp(url).jsonObject
73-
74-
val downloadURL = (response["download_url"] as JsonPrimitive).content
75-
val res = getHttp(downloadURL)
76-
return res.text
85+
return downloadSingleFile(url)
7786
}
7887

7988
fun checkRefExistence(ref: String): Boolean {
8089
val response = getHttp("$apiPrefix/contents/$remotePath?ref=$ref")
8190
return response.status.successful
8291
}
8392

93+
fun checkIfRefUpToDate(remoteRef: String): Boolean {
94+
if (!commitHashFile.exists()) return false
95+
val localRef = commitHashFile.readText()
96+
return localRef == remoteRef
97+
}
98+
99+
fun downloadLibraries(ref: String) {
100+
localLibrariesDir.mkdirs()
101+
102+
val url = "$apiPrefix/contents/$remotePath?ref=$ref"
103+
logger.info("Requesting library descriptors at $url")
104+
val response = getHttp(url).jsonArray
105+
106+
for (item in response) {
107+
item as JsonObject
108+
if (item["type"]?.jsonPrimitive?.content != "file") continue
109+
110+
val fileName = item["name"]!!.jsonPrimitive.content
111+
if (!fileName.endsWith(".$DESCRIPTOR_EXTENSION")) continue
112+
113+
val downloadUrl = item["download_url"]!!.jsonPrimitive.content
114+
val descriptorResponse = getHttp(downloadUrl)
115+
116+
val descriptorText = descriptorResponse.text
117+
val file = localLibrariesDir.resolve(fileName)
118+
file.writeText(descriptorText)
119+
}
120+
121+
saveLocalRef(ref)
122+
}
123+
124+
fun downloadLatestPropertiesFile() {
125+
val ref = latestCommitOnDefaultBranch
126+
val url = "$apiPrefix/contents/$remotePath/$PROPERTIES_FILE?ref=$ref"
127+
logger.info("Requesting $PROPERTIES_FILE file at $url")
128+
val text = downloadSingleFile(url)
129+
localPropertiesFile.createDirsAndWrite(text)
130+
}
131+
132+
private fun downloadSingleFile(contentsApiUrl: String): String {
133+
val response = getHttp(contentsApiUrl).jsonObject
134+
val downloadUrl = response["download_url"]!!.jsonPrimitive.content
135+
val res = getHttp(downloadUrl)
136+
return res.text
137+
}
138+
139+
private fun saveLocalRef(ref: String) {
140+
commitHashFile.createDirsAndWrite(ref)
141+
}
142+
143+
private fun File.createDirsAndWrite(text: String = "") {
144+
parentFile.mkdirs()
145+
writeText(text)
146+
}
147+
84148
private fun <T> catchAll(message: String = "", body: () -> T): T? = try {
85149
body()
86150
} catch (e: Throwable) {
@@ -92,6 +156,7 @@ class LibraryDescriptorsManager private constructor(
92156
private const val GITHUB_API_HOST = "api.github.com"
93157
private const val DESCRIPTOR_EXTENSION = "json"
94158
private const val PROPERTIES_FILE = ".properties"
159+
private const val COMMIT_HASH_FILE = "commit_sha"
95160

96161
fun getInstance(
97162
logger: Logger = LoggerFactory.getLogger(LibraryDescriptorsManager::class.java),

0 commit comments

Comments
 (0)
Please sign in to comment.