Skip to content

Commit

Permalink
updated docs to 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Morfly committed Jul 2, 2021
1 parent 96b0881 commit 02032d9
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 14 deletions.
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,17 @@ Run the migration.
Use [the documentation](docs/airin_gradle_migration.md) to learn more about the migration process.

## Installation

Current version: [![Maven Central](https://img.shields.io/maven-central/v/org.morfly.airin/airin-starlark.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22org.morfly.airin%22%20AND%20a:%22airin-starlark%22)


### Migration from Gradle

In the `buildSrc/build.gradle` file add the following:
```groovy
dependencies {
implementation "org.morfly.airin:airin-gradle:x.y.z"
// Gradle plugin
implementation "org.morfly.airin:airin-gradle:0.3.0"
// optional - Android specific extensions
implementation "org.morfly.airin:airin-gradle-android:x.y.z"
// Optional - Android specific extensions
implementation "org.morfly.airin:airin-gradle-android:0.3.0"
}
```
Then, in the root `build.gradle` file apply Airin Gradle plugin:
Expand All @@ -116,12 +114,23 @@ plugins {
In case you need only Starlark code generator:
```groovy
dependencies {
implementation "org.morfly.airin:airin-starlark:x.y.z"
// Starlark template engine
implementation "org.morfly.airin:airin-starlark:0.3.0"
// Collection of common rules and functions
implementation "org.morfly.airin:airin-starlark-stdlib:0.3.0"
// Optional - Starlark rules and functions generator
ksp "org.morfly.airin:airin-starlark-libgen:0.3.0"
}
```
Don't forget to add `id("com.google.devtools.ksp")` to `plugins` section of your `build.gradle(.kts)` file if you are
using `ksp`.

<br>

Now you are ready to [configure the plugin](docs/airin_gradle_migration.md).
Now you are ready for [the migration](docs/airin_gradle_migration.md).

Also, [learn more](docs/airin_starlark_libgen.md) about generating DSL for custom rules and functions.

## Examples

Expand Down
12 changes: 7 additions & 5 deletions docs/airin_gradle_migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ Create `buildSrc` directory in the root directory of your project.
Create `build.gradle` file inside and configure it with Kotlin and add `Airin` as a dependency.




```groovy
plugins {
id "org.jetbrains.kotlin.jvm" version "1.5.10"
Expand All @@ -30,10 +28,14 @@ repositories {
}
dependencies {
implementation "org.morfly.airin:airin-gradle:0.2.0"
implementation "org.morfly.airin:airin-gradle:0.3.0"
// optional - Android specific extensions
implementation "org.morfly.airin:airin-gradle-android:0.2.0"
// Optional - Android specific extensions
implementation "org.morfly.airin:airin-gradle-android:0.3.0"
// Optional - Android Gradle plugin
implementation "com.android.tools.build:gradle:<version>"
// Optional - Kotlin Gradle plugin if using Jetpack Compose
implementation "org.jetbrains.kotlin:kotlin-gradle-plugin:<version>"
}
```

Expand Down
189 changes: 189 additions & 0 deletions docs/airin_starlark_libgen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
# Starlark Library Generation
Airin provides by default a set of Bazel rules and functions that include Java, Kotlin, Android use cases and more.
All of them are available in `airin-starlark-stdlib` artifact.

These functions are represented with a typesafe Kotlin DSL. In case additional rules and functions required, Airin provides
the mechanism of generating them.

With the power of Kotlin symbol processing (KSP) `airin-starlark-libgen` artifact enables this functionallity.

## Getting started

In Gradle module that should contain the library, open `build.gradle(.kts)` file and add `ksp` plugin and required
repositories.
```groovy
plugins {
id "com.google.devtools.ksp" version "1.5.20-1.0.0-beta04"
}
repositories {
mavenCentral()
google()
}
```

In `dependencies` section add the following:
```groovy
// Starlark template engine
implementation "org.morfly.airin:airin-starlark:0.3.0"
// Library generator
ksp "org.morfly.airin:airin-starlark-libgen:0.3.0"
```

## Generating functions
In order to generate the function a `@LibraryFunction` annotation must be applied to the Kotlin `interface`.

```kotlin
@LibraryFunction(
name = "android_binary",
scope = [Build],
kind = Statement,
)
interface AndroidBinary {

@Argument(required = true)
val name: StringType
val srcs: ListType<Label?>?
val deps: ListType<Label?>?
val manifest_values: DictionaryType<Key, Value>?
}
```
The above configuration will generate two types of Kotlin DSL functions that use `round` and `curly` brackets.
Both will generate the same Starlark function.

The first one uses `round` backets and is more similar to the Sarlark one:
```kotlin
fun build() = BUILD.bazel {

android_binary(
name = "app",
srcs = glob("src/main/kotlin/**/*.kt"),
deps = list[":lib"],
manifest_values = dict {
"minSdkVersion" to "29"
}
)
}
```
The second one uses `curly` brackets and provides more flexibility for the template:
```kotlin
fun build(useLib: Boolean) = BUILD.bazel {

android_binary {
name = "app"
srcs = glob("src/main/kotlin/**/*.kt")
if(useLib) {
deps = list[":lib"]
}
manifest_values = dict {
"minSdkVersion" to "29"
}
}
}
```
You can use either of them depending on your use case.

### Generation configuration

The `@LibraryFunction` annotation has the following properties:

| param | type | enum options | description |
|:--------:|-----------------|--------------------------------|------------------------------------------------------------------------------------------------------|
| name | String | | Name of the generated function. |
| scope | [FunctionScope] | Build Workspace Starlark | Defines whether the function targets `BUILD`, `WORKSPACE` or `.bzl` files. Can be combined. |
| kind | FunctionKind | Statement Expression | `Statement` is more applicable to rules while `Expression` can be used for functions such as `glob`. |
| brackets | [BracketsKind] | Round Curly | Defines the type of DSL function by brackets. Can be combined. |

### Function arguments
Each propety of the interface will be considered as a function argument except the one annotated with `@Returns` annotation.

It is also possible to annotate properties with the `@Argument` annotation to adjust its behavior. However, it is not mandatory.

The `@Argument` annotation has the following properties:

| param | type | optional | description |
|:--------------:|---------|----------|-------------------------------------------------------------------------------------------------------------------------------------|
| underlyingName | String | true | Name that will be generated for the Starlark function. If not set the name will be taken from the property itself. |
| required | Boolean | true | Defines whether the argument is mandatory. |
| vararg | Boolean | true | Defines whether Kotlin DSL representation will generate this argument as `vararg`. The function can have only one such an argument. |

The example below shows the use of `vararg` parameter:
```kotlin
@LibraryFunction(
name = "glob",
scope = [Build, Workspace, Starlark],
kind = Expression
)
interface Glob {

@Argument(vararg = true)
val include: ListType<Label>

@Returns
val returns: ListType<Label>
}
```
This will generate the function named `glob` that has `include` argument as Kotlin `vararg`.
> Note. Airin will also generate the second `glob` function with the regular `include` arg of `ListType`.
```kotlin
val srcs: ListType<StringType> = glob(
"src/main/**/*.kt",
"src/main/**/*.java"
)
```

### Return type
The `@Returns` annotation specifies the return type of the generated function. Only function of `Expression` kind can have
explicitely specified return type. `Statement` functions have `Unit` return type by default.

The `@Returns` annotation has the following properties:

| param | type | optional | description |
|:-----:|------------|--------------|-------------------------------------------------------------------------------------------------------------------------------------------------------|
| kind | ReturnKind | Type Dynamic | Defines whether the generated Kotlin DSL function has a `specified return type`. Otherwise it will be `dynamically` inferred depending on the caller. |

The example below shows the use of a `Dynamic` return kind:

```kotlin
@LibraryFunction(
name = "select",
scope = [Build],
kind = Expression
)
interface Glob {

@Argument(underlyingName = "", inline)
val select: DictionaryType<Key, Value>?

// Property name and type are ignored for the Dynamic return kind.
@Returns(kind = Dynamic)
val returns: Any
}
```
This will generate the function return type of which will be infered depending on the caller.
```kotlin
val stringArg: StringType = select(dict{})

val listArg: ListType<StringType> = select(dict{})
```

## Allowed types
Arguments of generated functions could be of the following types:
- `StringType` and aliases: `Label`, `Name`
- `NumberType`
- `BooleanType`
- `ListType`
- `DictionaryType`
- `TupleType`
- `Any`

The types above that are named as `*Type` are imported from the `org.morfly.airin.starlark.lang` package.

Item type of `ListType` arguments should follow the same rule above.

In addition, types of keys and values of `DictionaryType` should be:
- `Key` for keys
- `Value` for values

The `Key` and `Value` types are also imported from the `org.morfly.airin.starlark.lang` package.
4 changes: 3 additions & 1 deletion docs/airin_starlark_template_engine.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Airin provides a declarative, typesafe Starlark template engine with which you can easily describe you Bazel build
scripts using Kotlin.

Also, [learn more](airin_starlark_libgen.md) about generating DSL for custom rules and functions.

## Overview

Here is the example of a basic usage of Airin Starlark template engine.
Expand Down Expand Up @@ -85,7 +87,7 @@ BUILD.bazel(relativePath = "some_dir") {
// your template code
}
```
All `BUILD`, `BUILD.bazel`, `WORKSPACE` and `WORKSPACE.bazel` functions allow specifying a relative path.
Both `BUILD` and `BUILD.bazel` functions allow specifying a relative path.

## Variables and Assignments

Expand Down

0 comments on commit 02032d9

Please sign in to comment.