Skip to content

Commit

Permalink
add publish workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
yogh333 committed Dec 7, 2023
1 parent a1f124e commit 6490268
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Rust
name: Run cargo clippy, cargo fmt, build and Unit+Integration tests

on:
push:
Expand Down
105 changes: 105 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
name: Publish to Crates.io

on:
push:
tags:
- '*-v*.*.*' # Match tags like 'package1-v1.2.3'
workflow_dispatch: # Allow manual workflow dispatch

jobs:
test-dry-run:
runs-on: ubuntu-latest
if: github.event_name == 'workflow_dispatch' # Only run this job for manual triggers

steps:
- name: Checkout Code
uses: actions/checkout@v2

- name: Set Up Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable

- name: Retrieve Package Names from workspace root
id: get-root-packages
run: |
# Retrieve package names from the workspace root
PACKAGE_NAMES=$(cargo metadata --format-version=1 --no-deps | jq -r '.packages[].name' | tr '\n' ' ')
# Set the package names as an output
echo "Package names from root Cargo.toml: $PACKAGE_NAMES"
echo "::set-output name=package-names::$PACKAGE_NAMES"
shell: bash
working-directory: ${{ github.workspace }}

- name: Test Dry-Run Publish for Each Package
run: |
# Iterate through package names retrieved
PACKAGE_NAMES="${{ steps.get-root-packages.outputs.package-names }}"
for PACKAGE_NAME in $PACKAGE_NAMES; do
# Test a dry-run publish for each package within the workspace
cargo publish --dry-run --no-verify --token ${{ secrets.CARGO_REGISTRY_TOKEN }} --package "$PACKAGE_NAME"
done
env:
CARGO_TERM_COLOR: always
working-directory: ${{ github.workspace }}

build-and-publish:
runs-on: ubuntu-latest
if: github.event_name == 'push' # Only run this job for tag pushes

steps:
- name: Checkout Code
uses: actions/checkout@v2

- name: Set Up Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable

- name: Determine Package to Publish
id: determine-package
run: |
# Extract package name from the tag name
TAG_NAME="${{ github.ref }}"
PACKAGE_NAME=$(echo "$TAG_NAME" | cut -d'-' -f1)
PACKAGE_NAMES=$(cargo metadata --format-version=1 --no-deps | jq -r '.packages[].name' | tr '\n' ' ')
# Check if the extracted package name is a valid package in the workspace root Cargo.toml
if [[ ! " $PACKAGE_NAMES " =~ " $PACKAGE_NAME " ]]; then
echo "Invalid package name: $PACKAGE_NAME"
exit 1
fi
echo "Package to publish: $PACKAGE_NAME"
echo "::set-output name=package::$PACKAGE_NAME"
shell: bash
working-directory: ${{ github.workspace }}

- name: Check Package Version
id: check-version
run: |
PACKAGE_NAME="${{ steps.determine-package.outputs.package }}"
TAG_VERSION=$(echo "${{ github.ref }}" | cut -d 'v' -f 2)
MANIFEST_VERSION=$(cargo metadata --format-version=1 --no-deps | jq -r --arg PACKAGE_NAME "$PACKAGE_NAME" '.packages[] | select(.name == $PACKAGE_NAME) | .version')
if [ "$TAG_VERSION" != "$MANIFEST_VERSION" ]; then
echo "Package version in manifest does not match tag version."
exit 1
else
echo "Package version in manifest matches tag version."
fi
shell: bash
working-directory: ${{ github.workspace }}

- name: Build and Publish
run: |
PACKAGE_NAME="${{ steps.determine-package.outputs.package }}"
# Publish the specified package within the workspace to crates.io
cargo publish --no-verify --token ${{ secrets.CARGO_REGISTRY_TOKEN }} --package "$PACKAGE_NAME"
env:
CARGO_TERM_COLOR: always
working-directory: ${{ github.workspace }}
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
members = [
"ledger_device_sdk",
"ledger_secure_sdk_sys",
"include_gif"
"include_gif",
"testmacro"
]
resolver = "2"

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Ledger Device Rust SDK
This workspace contains the 3 crates members of Ledger Device Rust SDK
This workspace contains the 4 crates members of Ledger Device Rust SDK

* [ledger_device_sdk](./ledger_device_sdk): main Rust SDK crate used to build an application that runs on BOLOS OS,
* [ledger_secure_sdk_sys](./ledger_secure_sdk_sys): bindings to [ledger_secure_sdk](https://github.com/LedgerHQ/ledger-secure-sdk)
* [include_gif](./include_gif): procedural macro used to manage GIF.
* [include_gif](./include_gif): procedural macro used to manage GIF
* [testmacro](./testmacro): procedural macro used by unit and integrations tests
1 change: 1 addition & 0 deletions include_gif/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "include_gif"
version = "1.0.1"
edition = "2021"
license.workspace = true
repository.workspace = true
description = "procedural macro that packs a gif image into a byte representation"

[lib]
Expand Down
5 changes: 3 additions & 2 deletions ledger_device_sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
[package]
name = "ledger_device_sdk"
version = "1.2.0"
version = "1.2.1"
authors = ["yhql", "yogh333"]
edition = "2021"
license.workspace = true
repository.workspace = true
description = "Ledger device Rust SDK"

[dev-dependencies]
# enable the 'speculos' feature when testing
# https://github.com/rust-lang/cargo/issues/2911#issuecomment-749580481
ledger_device_sdk = { path = ".", features = ["speculos"] }

testmacro = { git = "https://github.com/yhql/testmacro"}
testmacro = { path = "../testmacro", version = "0.1.0"}

[dependencies]
ledger_secure_sdk_sys = {path = "../ledger_secure_sdk_sys", version = "1.0.2"}
Expand Down
1 change: 1 addition & 0 deletions ledger_secure_sdk_sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "1.0.2"
authors = ["yhql"]
edition = "2021"
license.workspace = true
repository.workspace = true
description = "Bindings to Ledger C SDK"

[build-dependencies]
Expand Down
15 changes: 15 additions & 0 deletions testmacro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "testmacro"
version = "0.1.0"
authors = ["yhql <[email protected]>"]
edition = "2018"
license.workspace = true
repository.workspace = true
description = "procedural macro used to run unit and integration tests"

[lib]
proc-macro = true

[dependencies]
syn = { version = "1.0", features = ["full"] }
quote = "1.0"
3 changes: 3 additions & 0 deletions testmacro/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# testmacro

A macro inspired from [Writing an OS in Rust](https://os.phil-opp.com/testing/) and [Rust Raspberry OS tutorials](https://github.com/rust-embedded/rust-raspberrypi-OS-tutorials/tree/master/13_integrated_testing) that helps building `#![no_std]` tests in some other projects.
23 changes: 23 additions & 0 deletions testmacro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;

#[proc_macro_attribute]
pub fn test_item(_attr: TokenStream, item: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(item as syn::ItemFn);
let name = &input.sig.ident.to_string();
let func = &input.block;

let r = quote! {
#[test_case]
const t: TestType = TestType {
modname: module_path!(),
name: #name,
f: || -> Result<(),()> {
#func
Ok(())
}
};
};
r.into()
}

0 comments on commit 6490268

Please sign in to comment.