Skip to content

Commit

Permalink
Merge pull request #2 from piwonskp/scarb-2.8.2-standalone-plugin
Browse files Browse the repository at this point in the history
Cairo plugin as a shared library
  • Loading branch information
piwonskp authored Sep 23, 2024
2 parents 8d346ba + e6527ee commit 0c4811c
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 504 deletions.
34 changes: 11 additions & 23 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
[package]
name = "hello-cairo-plugin"
version = "0.0.0"
name = "hello_plugin"
version = "1.0.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.75"
cairo-lang-compiler = "~2.4.4"
cairo-lang-defs = "~2.4.4"
cairo-lang-runner = "~2.4.4"
cairo-lang-semantic = "~2.4.4"
cairo-lang-sierra = "~2.4.4"
cairo-lang-starknet = "~2.4.4"
cairo-lang-syntax = "~2.4.4"
cairo-lang-utils = "~2.4.4"
camino = "1.1.6"
clap = "4.4.8"
indoc = "2.0.4"
scarb = { git = "https://github.com/software-mansion/scarb", tag="v2.4.4" }
scarb-metadata = "1.8.0"
scarb-ui = "0.1.0"
semver = "1.0.20"
serde = "1.0.193"
serde_json = "1.0.108"
smol_str = "0.2.0"
tracing = "0.1.40"
url = "2.5.0"
cairo-lang-macro = "0.1.0"
cairo-lang-defs = "2.8.2"
cairo-lang-diagnostics = "2.8.2"
cairo-lang-parser = "2.8.2"
cairo-lang-syntax = "2.8.2"
regex = "1.10.6"
28 changes: 11 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,18 @@ An example project is located in [example directory](./example/).

### Execution

2. cd into example directory:
1. cd into project directory:

```
cd example/
```

2. Run the plugin to compile project:

```
cargo run build .
```

3. Run compiled code:
2. Run scarb with the hello-cairo-plugin:

```
scarb cairo-run --no-build
scarb cairo-run
```

4. You should see the output:
3. You should see the output:

```
Running examples
Expand All @@ -52,11 +45,12 @@ scarb cairo-run --no-build
Run completed successfully, returning []
```

## Project structure
## Development

The repository shows the implementation of cairo plugin with macros. For the implementation of macros see [src/lib.rs](./src/lib.rs). The file illustrates two approaches to developing attribute macros:
* hello macro using native cairo parser
* hello_regex macro implementing the same functionality using regex

The package consists of two parts: business logic and boilerplate code. Boilerplate code is stored in `src/main.rs` and `src/boilerplate` directory. Business logic is stored in `src/plugin`.
Plugin directory contains three files:
It is recommended especially for complex macros to use the native cairo parser as regexes are often error prone and have their limitations. They are lightweight however and may be enough for some macros.

- `config.rs` contains base properties of the plugin - its name, version and repository URL.
- `macro_plugin.rs` contains the plugin configuration. This is the place where you map item type to the function that processes it.
- `insert_hello.rs` contains the function that inserts hello message.
For more see [scarb's documentation on procedural macros](https://docs.swmansion.com/scarb/docs/reference/procedural-macro.html#procedural-macro-will-be-called-from-cairo-code).
4 changes: 2 additions & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "hello"
version = "0.0.0"
name = "hello_plugin"
version = "1.0.0"

[cairo-plugin]
6 changes: 3 additions & 3 deletions example/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ version = 1
name = "example"
version = "0.1.0"
dependencies = [
"hello",
"hello_plugin",
]

[[package]]
name = "hello"
version = "0.0.0"
name = "hello_plugin"
version = "1.0.0"
2 changes: 0 additions & 2 deletions example/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ hello = { path = "../" }
## Referencing compiled as part of this project:
## As for scarb 2.4.0 this is still an unsupported feature
# hello = ">=0.1.0"

[[target.hello]]
98 changes: 0 additions & 98 deletions src/boilerplate/compiler.rs

This file was deleted.

2 changes: 0 additions & 2 deletions src/boilerplate/mod.rs

This file was deleted.

110 changes: 0 additions & 110 deletions src/boilerplate/plugin_repository.rs

This file was deleted.

Loading

0 comments on commit 0c4811c

Please sign in to comment.