Skip to content

Commit

Permalink
Reuse some code
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed May 17, 2024
1 parent 0c87a92 commit 65e42ba
Showing 1 changed file with 35 additions and 47 deletions.
82 changes: 35 additions & 47 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -48,84 +48,72 @@ val commonJVMSettings = Seq(
},
)

def compileTreeSitter(config: Configuration): Def.Initialize[Task[Seq[File]]] = Def.task {
val output = os.Path((config / resourceManaged).value)

val version = "0.22.6"
// returns directory we built in
def downloadAndBuild(name: String, version: String, repoUrl: String): os.Path = {
val binaryName = System.mapLibraryName(name)

val downloadTo = os.Path(IO.createTemporaryDirectory)

println(s"Downloading tree-sitter $version to $downloadTo")
println(s"Downloading $name $version to $downloadTo")

import sys.process._

requests
.get(s"https://github.com/tree-sitter/tree-sitter/archive/v$version.tar.gz")
.get(s"$repoUrl/archive/v$version.tar.gz")
.readBytesThrough { bytes =>
val cmd = s"tar -xzf - --directory $downloadTo"

(cmd #< bytes).!!
}

val binaryName = System.mapLibraryName("tree-sitter")
val binaryPathCompiled = downloadTo / s"tree-sitter-$version" / binaryName
val extracted = downloadTo / s"$name-$version"

Process(
command = List("make", binaryName),
cwd = Some((downloadTo / s"tree-sitter-$version").toIO),
cwd = Some(extracted.toIO),
).!!

val binaryPathGenerated = output / binaryName
println("writing tree-sitter file")
extracted
}

def copyLibrary(name: String, from: os.Path, to: os.Path): os.Path = {
val binaryName = System.mapLibraryName(name)

val source = from / binaryName
val target = to / binaryName

os.copy
.over(
binaryPathCompiled,
binaryPathGenerated,
source,
target,
createFolders = true,
)

List(binaryPathGenerated.toIO)
target
}

def compileBindingsPython(config: Configuration): Def.Initialize[Task[Seq[File]]] = Def.task {
def compileTreeSitter(config: Configuration): Def.Initialize[Task[Seq[File]]] = Def.task {
val output = os.Path((config / resourceManaged).value)

val version = "0.21.0"

val downloadTo = os.Path(IO.createTemporaryDirectory)

println(s"Downloading tree-sitter-python $version to $downloadTo")

import sys.process._

requests
.get(s"https://github.com/tree-sitter/tree-sitter-python/archive/v$version.tar.gz")
.readBytesThrough { bytes =>
val cmd = s"tar -xzf - --directory $downloadTo"

(cmd #< bytes).!!
}

val binaryName = System.mapLibraryName("tree-sitter-python")
val binaryPathCompiled = downloadTo / s"tree-sitter-python-$version" / binaryName
val extracted = downloadAndBuild(
name = "tree-sitter",
version = "0.22.6",
repoUrl = "https://github.com/tree-sitter/tree-sitter",
)

Process(
command = List("make", binaryName),
cwd = Some((downloadTo / s"tree-sitter-python-$version").toIO),
).!!
List(copyLibrary("tree-sitter", extracted, output).toIO)
}

val binaryPathGenerated = output / binaryName
println("writing tree-sitter-python file")
def compilePythonGrammar(config: Configuration): Def.Initialize[Task[Seq[File]]] = Def.task {
val output = os.Path((config / resourceManaged).value)

os.copy
.over(
binaryPathCompiled,
binaryPathGenerated,
createFolders = true,
)
val extracted = downloadAndBuild(
name = "tree-sitter-python",
version = "0.21.0",
repoUrl = "https://github.com/tree-sitter/tree-sitter-python",
)

List(binaryPathGenerated.toIO)
List(copyLibrary("tree-sitter-python", extracted, output).toIO)
}

lazy val core = crossProject(JVMPlatform)
Expand All @@ -146,7 +134,7 @@ lazy val bindingsPython = crossProject(JVMPlatform)
.settings(
name := "language-python",
commonSettings,
Compile / resourceGenerators += compileBindingsPython(Compile).taskValue,
Compile / resourceGenerators += compilePythonGrammar(Compile).taskValue,
)
.dependsOn(core)
.jvmSettings(commonJVMSettings)
Expand Down

0 comments on commit 65e42ba

Please sign in to comment.