Skip to content

Commit 056865a

Browse files
feat(makefile): run all or specific bindings example (#165)
* feat(makefile): run all or specific bindings example * add `bindings-example` * go examples * split off chain ID * rework kotlin into examples folder * kotlin * remove kotlin chain id * fix CI * Update bindings/python/examples/chain_id.py Co-authored-by: Thoralf-M <[email protected]> * add make examples * add Python readme * nit * add licenses * camelize example argument * nit * remove extraneous example --------- Co-authored-by: Thoralf-M <[email protected]>
1 parent 1565f30 commit 056865a

File tree

12 files changed

+184
-35
lines changed

12 files changed

+184
-35
lines changed

.github/workflows/bindings.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
- name: Checks for uncommitted changes
2828
run: git diff --exit-code
2929
- name: Run the example
30-
run: make test-go
30+
run: make go-examples
3131
kotlin:
3232
runs-on: ubuntu-latest
3333
steps:
@@ -43,7 +43,7 @@ jobs:
4343
source "$HOME/.sdkman/bin/sdkman-init.sh"
4444
sdk install gradle
4545
- name: Run the example
46-
run: make test-kotlin
46+
run: make kotlin-examples
4747
python:
4848
runs-on: ubuntu-latest
4949
steps:
@@ -54,4 +54,4 @@ jobs:
5454
- name: Checks for uncommitted changes
5555
run: git diff --exit-code
5656
- name: Run the example
57-
run: make test-python
57+
run: make python-examples

Makefile

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,17 @@ bindings: ## Build all bindings
6868
$(MAKE) kotlin
6969
$(MAKE) python
7070

71-
.PHONY: test-bindings
72-
test-bindings: ## Test all bindings
73-
$(MAKE) test-go
74-
$(MAKE) test-kotlin
75-
$(MAKE) test-python
71+
.PHONY: bindings-examples
72+
bindings-examples: ## Run all bindings examples
73+
$(MAKE) go-examples
74+
$(MAKE) kotlin-examples
75+
$(MAKE) python-examples
76+
77+
.PHONY: bindings-example
78+
bindings-example: ## Run a specific example for all bindings. Usage: make bindings-example example
79+
$(MAKE) go-example $(word 2,$(MAKECMDGOALS))
80+
$(MAKE) kotlin-example $(word 2,$(MAKECMDGOALS))
81+
$(MAKE) python-example $(word 2,$(MAKECMDGOALS))
7682

7783
# Build ffi crate and detect platform
7884
define build_binding
@@ -102,22 +108,49 @@ python: ## Build Python bindings
102108
cargo run --bin iota_sdk_bindings -- generate --library "target/release/libiota_sdk_ffi$${LIB_EXT}" --language python --out-dir bindings/python/lib --no-format; \
103109
cp target/release/libiota_sdk_ffi$${LIB_EXT} bindings/python/lib/
104110

105-
.PHONY: test-go
106-
test-go: ## Test Go bindings
107-
cd bindings/go/; \
108-
LD_LIBRARY_PATH="../../target/release" CGO_LDFLAGS="-liota_sdk_ffi -L../../target/release" go run test.go \
111+
.PHONY: go-example
112+
go-example: ## Run a specific Go example. Usage: make go-example example
113+
%:
114+
@true
115+
go-example:
116+
cd bindings/go/examples; \
117+
LD_LIBRARY_PATH="../../../target/release" CGO_LDFLAGS="-liota_sdk_ffi -L../../../target/release" go run $(word 2,$(MAKECMDGOALS)).go \
109118
cd -
110119

111-
.PHONY: test-kotlin
112-
test-kotlin: ## Test Kotlin bindings
120+
.PHONY: go-examples
121+
go-examples: ## Run all Go bindings examples
122+
@for example in $$(find bindings/go/examples -name "*.go" -exec basename {} .go \;); do \
123+
$(MAKE) go-example "$$example"; \
124+
done
125+
126+
.PHONY: kotlin-example
127+
kotlin-example: ## Run a specific Kotlin example. Usage: make kotlin-example example
128+
%:
129+
@true
130+
kotlin-example:
113131
cd bindings/kotlin; \
114132
./gradlew build clean; \
115-
LD_LIBRARY_PATH=./lib ./gradlew run -q; \
133+
LD_LIBRARY_PATH=./lib ./gradlew example -Pexample=$(word 2,$(MAKECMDGOALS)) -q; \
116134
cd -
117135

118-
.PHONY: test-python
119-
test-python: ## Test Python bindings
120-
python3 bindings/python/test.py
136+
.PHONY: kotlin-examples
137+
kotlin-examples: ## Run all Kotlin bindings examples
138+
@for example in $$(find bindings/kotlin/examples -name "*.kt" -exec basename {} .kt \;); do \
139+
$(MAKE) kotlin-example "$$example"; \
140+
done
141+
142+
.PHONY: python-example
143+
python-example: ## Run a specific Python example. Usage: make python-example example
144+
%:
145+
@true
146+
python-example:
147+
PYTHONPATH=bindings/python python3 bindings/python/examples/$(word 2,$(MAKECMDGOALS)).py
148+
149+
.PHONY: python-examples
150+
python-examples: ## Run all Python bindings examples
151+
@for example in $$(find bindings/python/examples -name "*.py" -exec basename {} .py \;); do \
152+
$(MAKE) python-example "$$example"; \
153+
done
121154

122155
.PHONY: help
123156
help: ## Show this help

bindings/go/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,14 @@ uniffi-bindgen-go --library target/release/libiota_sdk_ffi.so --out-dir ./bindin
3232
uniffi-bindgen-go --library target/release/libiota_sdk_ffi.dll --out-dir ./bindings/go --no-format
3333
```
3434

35-
# Test it
35+
# Run an example
3636

3737
```sh
38-
LD_LIBRARY_PATH="../../target/release" CGO_LDFLAGS="-liota_sdk_ffi -L../../target/release" go run test.go
38+
LD_LIBRARY_PATH="../../target/release" CGO_LDFLAGS="-liota_sdk_ffi -L../../target/release" go run examples/chain_id.go
39+
```
40+
41+
or
42+
43+
```sh
44+
make go-example chain_id
3945
```

bindings/go/examples/chain_id.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2025 IOTA Stiftung
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package main
5+
6+
import (
7+
"fmt"
8+
"log"
9+
10+
sdk "example.com/bindings/iota_sdk_ffi"
11+
)
12+
13+
func isNilError(err error) bool {
14+
if sdkErr, ok := err.(*sdk.SdkFfiError); ok {
15+
return sdkErr == nil
16+
}
17+
return false
18+
}
19+
20+
func main() {
21+
client := sdk.GraphQlClientNewDevnet()
22+
23+
chainID, err := client.ChainId()
24+
if !isNilError(err) {
25+
log.Fatalf("Failed to get chain ID: %v", err)
26+
}
27+
fmt.Println("Chain ID:", chainID)
28+
}
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ func isNilError(err error) bool {
1717
func main() {
1818
client := sdk.GraphQlClientNewDevnet()
1919

20-
chainID, err := client.ChainId()
21-
if !isNilError(err) {
22-
log.Fatalf("Failed to get chain ID: %v", err)
23-
}
24-
fmt.Println("Chain ID:", chainID)
25-
2620
address, err := sdk.AddressFromHex("0xb14f13f5343641e5b52d144fd6f106a7058efe2f1ad44598df5cda73acf0101f")
2721
if err != nil {
2822
log.Fatalf("Failed to parse address: %v", err)

bindings/kotlin/README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Kotlin
1+
# IOTA SDK - Kotlin Bindings
22

33
## Generate Binding
44

@@ -19,7 +19,7 @@ cargo run --bin iota_sdk_bindings -- generate --library "target/release/libiota_
1919
cp target/release/libiota_sdk_ffi${LIB_EXT} bindings/$LANGUAGE/lib/
2020
```
2121

22-
# Run example
22+
# Run an example
2323

2424
Install gradle:
2525

@@ -35,5 +35,11 @@ sdk install gradle
3535
cd bindings/kotlin
3636

3737
./gradlew build clean
38-
LD_LIBRARY_PATH=./lib ./gradlew run -q
38+
LD_LIBRARY_PATH=./lib ./gradlew example -Pexample=chain_id
39+
```
40+
41+
or
42+
43+
```sh
44+
make kotlin-example chain_id
3945
```

bindings/kotlin/build.gradle.kts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,28 @@ dependencies {
1616

1717
kotlin { jvmToolchain(21) }
1818

19-
application { mainClass.set("ExampleKt") }
19+
// Generic task to run any example
20+
tasks.register<JavaExec>("example") {
21+
classpath = sourceSets["main"].runtimeClasspath
22+
jvmArgs = listOf("-Djna.library.path=${projectDir}/lib")
23+
24+
// Get the example name from the command line argument -Pexample=<name>
25+
val exampleProperty = "example"
26+
inputs.property(exampleProperty, project.findProperty(exampleProperty) ?: "example")
27+
28+
mainClass.set(provider {
29+
val example = project.findProperty(exampleProperty)?.toString() ?: "example"
30+
// Convert snake_case to CamelCase and append Kt
31+
val className = example.split('_')
32+
.map { it.replaceFirstChar { c -> c.uppercaseChar() } }
33+
.joinToString("")
34+
"${className}Kt"
35+
})
36+
}
2037

2138
sourceSets {
2239
main {
23-
kotlin { srcDirs("lib", "src/main/kotlin") }
40+
kotlin { srcDirs("lib", "examples") }
2441
// Explicitly disable Java source sets since we only have Kotlin
2542
java { setSrcDirs(emptyList<String>()) }
2643
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2025 IOTA Stiftung
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import iota_sdk.GraphQlClient
5+
import kotlinx.coroutines.runBlocking
6+
7+
fun main() = runBlocking {
8+
try {
9+
val client = GraphQlClient.newDevnet()
10+
val chainId = client.chainId()
11+
println("Chain ID: $chainId")
12+
13+
} catch (e: Exception) {
14+
e.printStackTrace()
15+
}
16+
}

bindings/kotlin/src/main/kotlin/example.kt renamed to bindings/kotlin/examples/example.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import kotlinx.coroutines.runBlocking
1010
fun main() = runBlocking {
1111
try {
1212
val client = GraphQlClient.newDevnet()
13-
val chainId = client.chainId()
14-
println("Chain ID: $chainId")
1513

1614
val myAddress =
1715
Address.fromHex(

bindings/python/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# IOTA SDK - Python Bindings
2+
3+
```sh
4+
cargo build --all-features -p iota-sdk-ffi --lib --release
5+
```
6+
7+
# Generate the Python bindings
8+
9+
## MacOS
10+
11+
```sh
12+
cargo run --bin iota_sdk_bindings -- generate --library "target/release/libiota_sdk_ffi.dylib" --language python --out-dir bindings/python/lib --no-format
13+
```
14+
15+
## Linux
16+
17+
```sh
18+
cargo run --bin iota_sdk_bindings -- generate --library "target/release/libiota_sdk_ffi.so" --language python --out-dir bindings/python/lib --no-format
19+
```
20+
21+
## Windows
22+
23+
```sh
24+
cargo run --bin iota_sdk_bindings -- generate --library "target/release/libiota_sdk_ffi.dll" --language python --out-dir bindings/python/lib --no-format
25+
```
26+
27+
# Run an example
28+
29+
```sh
30+
PYTHONPATH=. python3 examples/chain_id.py
31+
```
32+
33+
or
34+
35+
```sh
36+
make python-example chain_id
37+
```

0 commit comments

Comments
 (0)