Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Packaging and Publishing Regex Utility as a Cairo Library #201 #207

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions docs/CAIRO_LIBRARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Publishing a Cairo package as library

**This document explains how to organize and publish Cairo package that can be imported and used as dependency by other developers**

## Getting started
First, you need a Cairo package. You can create one with:
```sh
scarb new project_name
```

For more details, check out [this documentation](https://github.com/KaizeNodeLabs/starkiro/blob/main/README.md).

## Preparing your package
### Defining the library target
In your Scarb.toml manifest, add a `[lib]` section. This tells Scarb that your package exports a reusable library.

### Adding package metadata
In the `[package]` section, you can include basic fields that provide useful information for other developers. Although these fields are optional, it is recommended that you add:
- `description`: A short explanation of your package's purpose
- `documentation`: A link to the package’s documentation
- `repository`: A URL to the GitHub repository where the source code is hosted
- `license`: The license under which the package is distributed
- `homepage`: A link to the project's website or landing page

For a complete list of available metadata fields, please refer to the [scarb documentation](https://docs.swmansion.com/scarb/docs/reference/manifest.html).

## Publishing your package

### Official Scarb registry (recommended)

The official [Scarb registry](https://scarbs.xyz/) is a central hub where developers can list, discover, and retrieve packages. Publishing your package here makes it easy for others to use your library.

To publish your package:

1. **Generate an API Token**
Log into the registry dashboard and create a token with the `publish` scope.

2. **Run the Publish Command**
```sh
SCARB_REGISTRY_AUTH_TOKEN=scrb_mytoken scarb publish
```
The publish command automatically packages and verifies your package. Then it will publish your package to the official scarbs.xyz registry. **Once the package is published, it's impossible to unpublish it.**

### Alternative Option
If your package is hosted on GitHub, you can directly add it as a dependency in your project without publishing to the official registry. For example:

```sh
scarb add your_package --git https://github.com/username/your_package.git
```

This method pulls the package directly from the GitHub repository. However, using the official registry is recommended for better discoverability and integration.

## Resources
- [Scarb Documentation](https://docs.swmansion.com/scarb/docs/guides/dependencies.html)
- [Scarb Package Registery](https://scarbs.xyz/)
- [Cairo Library Example](https://github.com/KaizeNodeLabs/starkiro/tree/main/examples/cairo/scripts/regex)
44 changes: 44 additions & 0 deletions examples/cairo/scripts/regex/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Regex

**A Cairo library providing a simplified regex engine supporting essential pattern matching features**

## Prerequisites
Install [Scarb](https://docs.swmansion.com/scarb/) (we recommend using [asdf](https://asdf-vm.com/) version manager).

## Installation

In your project directory, run the following command to add the library as a dependency:

```sh
scarb add [email protected]
```

Alternatively, you can manually add the dependency. In your Scarb.toml file, include:

```toml
[dependencies]
regex = "0.1.0"
```

## Usage

Import and use the library in your Cairo file:

```cairo
use regex::RegexTrait;

fn main() {
// Create a new Regex instance
let mut pattern = RegexTrait::new("H.llo");

// Sample text
let text = "Hello, World!";

// Check if the text matches the pattern
let is_match = pattern.matches(text.into());

println!("Match Found: {}", is_match);
}
```

For a detailed example of how to integrate and use this library in a Cairo project, check the [examples](./examples) folder.
6 changes: 6 additions & 0 deletions examples/cairo/scripts/regex/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
[package]
name = "regex"
version = "0.1.0"
description = "A cairo library providing a simplified regex engine supporting essential pattern matching features"
repository = "https://github.com/KaizeNodeLabs/starkiro/tree/main/examples/cairo/scripts/regex"
edition = "2024_07"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

Create-type = ["lib"]

[dependencies]

[dev-dependencies]
cairo_test = "2.9.2"

[lib]