Skip to content

Commit

Permalink
feat(plugin): implement grammar generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ObserverOfTime committed Jun 8, 2024
1 parent 4a70200 commit bc6e711
Show file tree
Hide file tree
Showing 24 changed files with 611 additions and 227 deletions.
1 change: 1 addition & 0 deletions .github/detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ style:
- "3"
- "10"
- "16"
- "0x7F"
ReturnCount:
max: 3
8 changes: 5 additions & 3 deletions .github/scripts/build-jni.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

& cmake -S ktreesitter -B ktreesitter/.cmake/build `
-DCMAKE_VERBOSE_MAKEFILE=ON `
-DCMAKE_INSTALL_PREFIX=ktreesitter/src/jvmMain/resources `
-DCMAKE_INSTALL_BINDIR="$env:CMAKE_INSTALL_LIBDIR"
& cmake --build ktreesitter/.cmake/build --config Debug
& cmake --install ktreesitter/.cmake/build --config Debug --prefix ktreesitter/src/jvmMain/resources
& cmake --install ktreesitter/.cmake/build --config Debug

foreach ($dir in Get-ChildItem -Directory -Path languages) {
& cmake -S "$dir" -B "$dir/.cmake/build" `
& cmake -S "$dir/build/generated" -B "$dir/.cmake/build" `
-DCMAKE_INSTALL_PREFIX="$dir/build/generated/src/jvmMain/resources" `
-DCMAKE_INSTALL_BINDIR="$env:CMAKE_INSTALL_LIBDIR"
& cmake --build "$dir/.cmake/build" --config Debug
& cmake --install "$dir/.cmake/build" --config Debug --prefix "$dir/src/jvmMain/resources"
& cmake --install "$dir/.cmake/build" --config Debug
}
9 changes: 5 additions & 4 deletions .github/scripts/build-jni.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ cmake -S ktreesitter -B ktreesitter/.cmake/build \
-DCMAKE_BUILD_TYPE=RelWithDebugInfo \
-DCMAKE_VERBOSE_MAKEFILE=ON \
-DCMAKE_OSX_ARCHITECTURES=arm64 \
-DCMAKE_INSTALL_PREFIX=ktreesitter/src/jvmMain/resources \
-DCMAKE_INSTALL_LIBDIR="$CMAKE_INSTALL_LIBDIR"

cmake --build ktreesitter/.cmake/build
cmake --install ktreesitter/.cmake/build --prefix ktreesitter/src/jvmMain/resources
cmake --install ktreesitter/.cmake/build

for dir in languages/*/; do
cmake -S "$dir" -B "${dir}.cmake/build" \
cmake -S "${dir}build/generated" -B "${dir}.cmake/build" \
-DCMAKE_BUILD_TYPE=RelWithDebugInfo \
-DCMAKE_OSX_ARCHITECTURES=arm64 \
-DCMAKE_INSTALL_PREFIX="${dir}build/generated/src/jvmMain/resources" \
-DCMAKE_INSTALL_LIBDIR="$CMAKE_INSTALL_LIBDIR"
cmake --build "${dir}.cmake/build"
cmake --install "${dir}.cmake/build" --prefix "${dir}src/jvmMain/resources"
cmake --install "${dir}.cmake/build"
done
55 changes: 48 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,78 @@ permissions:
contents: write

jobs:
generate:
runs-on: ubuntu-latest
name: Generate grammar files
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
submodules: true
- name: Set up Java
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 17
cache: gradle
cache-dependency-path: |
gradle/libs.versions.toml
gradle/wrapper/gradle-wrapper.properties
- name: Cache Kotlin/Native prebuilt
uses: actions/cache@v4
with:
path: ${{runner.tool_cache}}/konan/kotlin-native-prebuilt-*
key: konan-${{runner.os}}-prebuilt-1.9
- name: Generate files
run: ./gradlew --no-daemon generateGrammarFiles
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: generated-files
path: languages/*/build/generated/**
retention-days: 1
test:
runs-on: ${{matrix.os}}
name: >-
Test ${{matrix.platform}} platform
${{matrix.lib_platform && format('({0}-{1})', matrix.lib_platform, matrix.lib_arch)}}
needs: [generate]
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
platform: JVM
targets: jvmTest
targets: :ktreesitter:jvmTest
lib_platform: linux
lib_arch: x64
- os: windows-latest
platform: JVM
targets: jvmTest
targets: :ktreesitter:jvmTest
lib_platform: windows
lib_arch: x64
- os: macos-latest
platform: JVM
targets: jvmTest
targets: :ktreesitter:jvmTest
lib_platform: macos
lib_arch: aarch64
# - os: ubuntu-latest
# platform: Android
# targets: connectedDebugAndroidTest
# targets: :ktreesitter:connectedDebugAndroidTest
- os: ubuntu-latest
platform: Linux
targets: compileKotlinLinuxArm64 linuxX64Test
targets: >-
:ktreesitter:compileKotlinLinuxArm64
:ktreesitter:linuxX64Test
- os: windows-latest
platform: Windows
targets: mingwX64Test
targets: :ktreesitter:mingwX64Test
- os: macos-latest
platform: macOS/iOS
targets: macosX64Test macosArm64Test iosSimulatorArm64Test
targets: >-
:ktreesitter:macosX64Test
:ktreesitter:macosArm64Test
:ktreesitter:iosSimulatorArm64Test
steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand All @@ -81,6 +117,11 @@ jobs:
with:
path: ${{runner.tool_cache}}/konan/kotlin-native-prebuilt-*
key: konan-${{runner.os}}-prebuilt-1.9
- name: Download generated files
uses: actions/download-artifact@v4
with:
path: languages
name: generated-files
- name: Build JNI libraries
if: matrix.platform == 'JVM'
run: .github/scripts/build-jni.${{matrix.os == 'windows-latest' && 'ps1' || 'sh'}}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
path: ${{runner.tool_cache}}/konan/dependencies
key: konan-${{runner.os}}-dependencies
- name: Build documentation
run: ./gradlew --no-daemon :ktreesitter:dokkaHtml
run: ./gradlew --no-daemon generateFiles :ktreesitter:dokkaHtml
env:
KONAN_DATA_DIR: ${{runner.tool_cache}}/konan
- name: Upload pages artifact
Expand Down
28 changes: 26 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ jobs:
pattern: ktreesitter-lib-*
merge-multiple: true
- name: Build packages
shell: bash
run: ./gradlew --no-daemon ${{matrix.targets}}
env:
SIGNING_KEY: ${{secrets.SIGNING_KEY}}
Expand All @@ -160,7 +159,7 @@ jobs:
ktreesitter/build/repo/**
!ktreesitter/build/repo/**/maven-metadata.*
retention-days: 2
publish:
publish-library:
runs-on: ubuntu-latest
name: Publish packages on Maven Central
needs: [build-jars]
Expand Down Expand Up @@ -201,3 +200,28 @@ jobs:
run: gh release create $GITHUB_REF_NAME --generate-notes
env:
GH_TOKEN: ${{github.token}}
publish-plugin:
runs-on: ubuntu-latest
name: Publish Gradle plugin
needs: [build-jars]
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
submodules: true
- name: Set up Java
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 17
cache: gradle
cache-dependency-path: |
gradle/libs.versions.toml
gradle/wrapper/gradle-wrapper.properties
- name: Publish plugin
run: >-
./gradlew --no-daemon :ktreesitter-plugin:publishPlugins
-Pgradle.publish.key=$PUBLISH_KEY -Pgradle.publish.secret=$PUBLISH_SECRET
env:
PUBLISH_KEY: ${{secrets.GRADLE_PUBLISH_KEY}}
PUBLISH_SECRET: ${{secrets.GRADLE_PUBLISH_SECRET}}
1 change: 1 addition & 0 deletions .idea/conventionalCommit.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ Kotlin bindings to the Tree-sitter parsing library
- [ ] Swift
- [ ] Markdown
- [ ] CMake
- [ ] Gradle plugin
- [x] Gradle plugin
- [ ] Package
- [ ] DSL?
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ version.ref = "kotest"
id = "org.jetbrains.dokka"
version.ref = "dokka"

[plugins.gradle-publish]
id = "com.gradle.plugin-publish"
version = "1.2.1"

[bundles]
kotest-core = [
"kotest-engine",
Expand Down
40 changes: 40 additions & 0 deletions ktreesitter-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.util.Properties
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion

group = "io.github.tree-sitter"
version = with(Properties()) {
file("../gradle.properties").reader().use(::load)
getProperty("project.version")
}

plugins {
`kotlin-dsl`
id("com.gradle.plugin-publish") version "1.2.1"
}

repositories {
mavenCentral()
}

kotlin {
compilerOptions {
languageVersion.set(KotlinVersion.KOTLIN_1_9)
}
}

@Suppress("UnstableApiUsage")
gradlePlugin {
vcsUrl = "https://github.com/tree-sitter/kotlin-tree-sitter"
website = "https://github.com/tree-sitter/kotlin-tree-sitter/tree/master/plugin"
plugins.create("ktreesitter") {
id = "$group.${project.name}"
displayName = "KTreeSitter grammar plugin"
description = "A plugin that generates code for KTreeSitter grammar packages"
implementationClass = "io.github.treesitter.ktreesitter.plugin.GrammarPlugin"
tags = listOf("tree-sitter", "code", "generator")
}
}

tasks.validatePlugins {
enableStricterValidation.set(true)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.github.treesitter.ktreesitter.plugin

import java.io.File
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property

abstract class GrammarExtension {
abstract val baseDir: Property<File>
abstract val grammarName: Property<String>
abstract val files: Property<Array<File>>
abstract val interopName: Property<String>
abstract val libraryName: Property<String>
abstract val packageName: Property<String>
abstract val className: Property<String>
abstract val languageMethods: MapProperty<String, String>
}
Loading

0 comments on commit bc6e711

Please sign in to comment.