Skip to content

Commit

Permalink
Define protocol update as a Gradle task instead of script
Browse files Browse the repository at this point in the history
This avoids having to setup kotlin in Github Actions
(which currently has this problem: fwilhe2/setup-kotlin#85).
  • Loading branch information
joffrey-bion committed Jan 15, 2021
1 parent 2d7a488 commit 53e044e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 58 deletions.
25 changes: 10 additions & 15 deletions .github/workflows/update-protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,6 @@ jobs:
with:
java-version: 11

- name: Set up Kotlin
uses: fwilhe2/[email protected]
with:
version: 1.4.20

- name: Update protocol
run: kotlinc -script update-protocol.kts

- name: Read protocol version
uses: pCYSl5EDgo/cat@master
id: version
with:
path: protocol/version.txt
trim: true

- name: Gradle build cache
uses: actions/[email protected]
with:
Expand All @@ -46,6 +31,16 @@ jobs:
path: ~/.gradle/wrapper
key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('**/gradle-wrapper.properties') }}

- name: Update protocol
run: ./gradlew updateProtocolDefinitions

- name: Read protocol version
uses: pCYSl5EDgo/cat@master
id: version
with:
path: protocol/version.txt
trim: true

- name: Update public ABI descriptors
run: ./gradlew apiDump

Expand Down
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ kotlin {
}
}

val updateProtocolDefinitions by tasks.registering(org.hildan.chrome.devtools.build.UpdateProtocolDefinitionsTask::class)

val generateProtocolApi by tasks.registering(org.hildan.chrome.devtools.build.GenerateProtocolApiTask::class)

tasks {
Expand Down
54 changes: 54 additions & 0 deletions buildSrc/src/main/kotlin/UpdateProtocolDefinitionsTask.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.hildan.chrome.devtools.build

import org.gradle.api.DefaultTask
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import java.net.URL
import java.nio.file.Files
import java.nio.file.Path

private const val baseUrl = "https://raw.githubusercontent.com/ChromeDevTools/devtools-protocol"
private const val branch = "master"
private const val browserProtocolUrl = "$baseUrl/$branch/json/browser_protocol.json"
private const val jsProtocolUrl = "$baseUrl/$branch/json/js_protocol.json"
private const val packageJsonUrl = "$baseUrl/$branch/package.json"

private val protocolDescriptorUrls = listOf(browserProtocolUrl, jsProtocolUrl)

open class UpdateProtocolDefinitionsTask : DefaultTask() {

init {
// never consider this task up-to-date, the point is to check for updates
outputs.upToDateWhen { false }
}

@OutputDirectory
val outputDir: Path = project.rootDir.resolve("protocol").toPath()

@TaskAction
fun generate() {
Files.createDirectories(outputDir)

protocolDescriptorUrls.forEach { url ->
print("Downloading protocol JSON spec from $url ... ")
downloadTextFile(url, outputDir)
println("Done.")
}

val newVersion = fetchProtocolNpmVersion()
outputDir.resolve("version.txt").toFile().writeText(newVersion)
println("Chrome Devtools Protocol definition updated to $newVersion")
}
}

private fun fetchProtocolNpmVersion(): String {
val packageJson = URL(packageJsonUrl).readText()
return Regex(""""version"\s*:\s*"([^"]+)"""").find(packageJson)?.groupValues?.get(1) ?: "not-found"
}

private fun downloadTextFile(fileUrl: String, targetDir: Path) {
val url = URL(fileUrl)
val text = url.readText()
val outputFilePath = targetDir.resolve(url.path.substringAfterLast('/'))
outputFilePath.toFile().writeText(text)
}
43 changes: 0 additions & 43 deletions update-protocol.kts

This file was deleted.

0 comments on commit 53e044e

Please sign in to comment.