Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit 9616968

Browse files
committed
build: do not build tree-sitter in configuration phase
1 parent fa176ea commit 9616968

File tree

6 files changed

+114
-26
lines changed

6 files changed

+114
-26
lines changed

build.gradle.kts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717

1818
import com.android.build.gradle.BaseExtension
19+
import com.itsaky.androidide.treesitter.BuildTreeSitterTask
20+
import com.itsaky.androidide.treesitter.CleanTreeSitterBuildTask
1921
import com.itsaky.androidide.treesitter.projectVersionCode
2022
import com.vanniktech.maven.publish.AndroidSingleVariantLibrary
2123
import com.vanniktech.maven.publish.MavenPublishBaseExtension
@@ -66,7 +68,12 @@ subprojects {
6668
}
6769
}
6870

71+
tasks.register<BuildTreeSitterTask>("buildTreeSitter")
72+
73+
tasks.register<CleanTreeSitterBuildTask>("cleanTreeSitterBuild")
74+
6975
tasks.register<Delete>("clean").configure {
76+
dependsOn("cleanTreeSitterBuild")
7077
delete(rootProject.buildDir)
7178
delete(rootProject.file("build/host"))
7279
delete(rootProject.file("tree-sitter-lib/cli/build"))

buildSrc/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ repositories {
2626
dependencies {
2727
implementation(gradleApi())
2828
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
29+
30+
compileOnly("com.android.tools.build:gradle:8.0.0")
2931
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* This file is part of android-tree-sitter.
3+
*
4+
* android-tree-sitter library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* android-tree-sitter library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with android-tree-sitter. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.itsaky.androidide.treesitter
19+
20+
import java.io.File
21+
import org.gradle.api.DefaultTask
22+
import org.gradle.api.tasks.TaskAction
23+
24+
/**
25+
* Task for building the tree-sitter lib.
26+
*
27+
* @author Akash Yadav
28+
*/
29+
abstract class BuildTreeSitterTask : DefaultTask() {
30+
31+
@TaskAction
32+
fun buildTsCli() {
33+
if (!BUILD_TS_CLI_FROM_SOURCE) {
34+
project.logger.warn(
35+
"Skipping tree-sitter-cli build as $ENV_TS_CLI_BUILD_FROM_SOURCE is set to 'false'")
36+
return
37+
}
38+
39+
var cliDir = project.rootProject.file("tree-sitter-lib/cli")
40+
var buildDir = File(cliDir, "build")
41+
42+
val cmd = arrayOf("cargo", "b", "--target-dir", buildDir.absolutePath, "--release")
43+
44+
project.logger.info(
45+
"Building tree-sitter-cli with command ${cmd.joinToString(separator = " ")}")
46+
47+
project.executeCommand(cliDir.absolutePath, command = cmd)
48+
}
49+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* This file is part of android-tree-sitter.
3+
*
4+
* android-tree-sitter library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* android-tree-sitter library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with android-tree-sitter. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.itsaky.androidide.treesitter
19+
20+
import org.gradle.api.DefaultTask
21+
import org.gradle.api.tasks.Delete
22+
import org.gradle.api.tasks.TaskAction
23+
24+
/**
25+
* Task for cleaning tree-sitter lib build.
26+
*
27+
* @author Akash Yadav
28+
*/
29+
abstract class CleanTreeSitterBuildTask : Delete() {
30+
31+
@TaskAction
32+
fun doClean() {
33+
delete(project.rootProject.file("tree-sitter-lib/cli/build"))
34+
}
35+
}

buildSrc/src/main/java/com/itsaky/androidide/treesitter/TreeSitterPlugin.kt

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.gradle.api.Project
2222
import org.gradle.api.tasks.Delete
2323
import org.gradle.api.tasks.compile.JavaCompile
2424
import org.gradle.kotlin.dsl.create
25+
import org.gradle.kotlin.dsl.maybeCreate
2526
import java.io.File
2627

2728
/**
@@ -31,36 +32,18 @@ import java.io.File
3132
*/
3233
class TreeSitterPlugin : Plugin<Project> {
3334
override fun apply(target: Project) {
34-
target.let { project ->
35-
project.tasks.register("buildForHost", BuildForHostTask::class.java) {
35+
target.run {
36+
tasks.register("buildForHost", BuildForHostTask::class.java) {
37+
dependsOn(rootProject.tasks.getByName("buildTreeSitter"))
3638
libName = project.name
3739
}
3840

39-
project.tasks.create("cleanHostBuild", type = Delete::class) {
41+
tasks.create("cleanHostBuild", type = Delete::class) {
4042
delete("src/main/cpp/host-build")
4143
}
4244

43-
project.tasks.named("clean").configure { dependsOn("cleanHostBuild") }
44-
project.tasks.withType(JavaCompile::class.java) { dependsOn("buildForHost") }
45+
tasks.named("clean").configure { dependsOn("cleanHostBuild") }
46+
tasks.named("preBuild") { dependsOn("buildForHost") }
4547
}
46-
47-
buildTsCli(project = target)
48-
}
49-
50-
private fun buildTsCli(project: Project) {
51-
if (!BUILD_TS_CLI_FROM_SOURCE) {
52-
project.logger.warn("Skipping tree-sitter-cli build as $ENV_TS_CLI_BUILD_FROM_SOURCE is set to 'false'")
53-
return
54-
}
55-
56-
var cliDir = project.rootProject.file("tree-sitter-lib/cli")
57-
var buildDir = File(cliDir, "build")
58-
59-
val cmd = arrayOf("cargo", "b", "--target-dir", buildDir.absolutePath, "--release")
60-
61-
project.logger.info(
62-
"Building tree-sitter-cli with command ${cmd.joinToString(separator = " ")}")
63-
64-
project.executeCommand(cliDir.absolutePath, command = cmd)
6548
}
6649
}

buildSrc/src/main/java/com/itsaky/androidide/treesitter/TsGrammarPlugin.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import java.io.File
2121
import org.gradle.api.Plugin
2222
import org.gradle.api.Project
2323
import org.gradle.api.logging.LogLevel.LIFECYCLE
24+
import org.gradle.api.tasks.compile.JavaCompile
2425

2526
/**
2627
* Plugin applied to grammar modules.
@@ -30,7 +31,17 @@ import org.gradle.api.logging.LogLevel.LIFECYCLE
3031
class TsGrammarPlugin : Plugin<Project> {
3132

3233
override fun apply(target: Project) {
33-
generateGrammar(target)
34+
target.run {
35+
tasks.register("generateTreeSitterGrammar") {
36+
doLast {
37+
generateGrammar(project = project)
38+
}
39+
}
40+
41+
tasks.withType(JavaCompile::class.java) {
42+
dependsOn("generateTreeSitterGrammar")
43+
}
44+
}
3445
}
3546

3647
fun generateGrammar(project: Project) {
@@ -59,7 +70,8 @@ class TsGrammarPlugin : Plugin<Project> {
5970
if (buildTimestamp.exists()) {
6071
buildTimestamp.setLastModified(System.currentTimeMillis())
6172
} else {
62-
buildTimestamp.createNewFile()
73+
buildTimestamp.parentFile.mkdirs()
74+
buildTimestamp.bufferedWriter().use { it.write("Tree Siter ${project.name} build timestamp") }
6375
}
6476
}
6577
}

0 commit comments

Comments
 (0)