Skip to content

Commit

Permalink
feat: get versions from the repo directly (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
patriciobcs authored Apr 17, 2024
1 parent c41261f commit 1969933
Show file tree
Hide file tree
Showing 19 changed files with 2,102 additions and 2,994 deletions.
1,531 changes: 1,389 additions & 142 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
log = "0.4.21"
env_logger = "0.11.3"
reqwest = { version = "0.12.3", features = ["json"] }
toml = "0.8.12"
tokio = { version = "1.37.0", features = ["full"] }

[dev-dependencies]
tokio-test = "0.4"
mockito = "0.30"
34 changes: 22 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
# Polkadot SDK Version Manager

This is a simple tool to manage and update the Polkadot SDK dependencies in any Cargo.toml file.
This is a simple tool to manage and update the Polkadot SDK dependencies in any Cargo.toml file. It will automatically update the Polkadot SDK dependencies to their correct crates.io version.

## Installation

From [GitHub](https://github.com/paritytech/psvm):

```sh
cargo install --git https://github.com/paritytech/psvm psvm
```

From [crates.io](https://crates.io/crates/psvm):

```sh
cargo install psvm
```

## Usage

Go to the directory containing the Cargo.toml file you want to update and run `psvm`. This will automatically update the Polkadot SDK dependencies in the Cargo.toml file to their correct crates.io version.
Expand All @@ -19,18 +27,20 @@ If you want to update the dependencies to a specific Polkadot SDK version, you c
```sh
# Go to the directory containing the Cargo.toml file you want to update
cd <cargo-toml-dir>
# Run the psvm command to update the dependencies
psvm
# You can also update it using the path to the Cargo.toml file
psvm -p <cargo-toml-dir>/Cargo.toml
# Overwrite local dependencies with crates.io versions
psvm -o
# Update to a specific Polkadot SDK version (default 1.3.0)
psvm -v "1.7.0"
# Update to a specific Polkadot SDK version
psvm -v "1.3.0"
# You can also update an specific Cargo.toml file by passing its path
psvm -v "1.4.0" -p <cargo-toml-dir>/Cargo.toml
# Overwrite local dependencies (with same name as Polkadot SDK crates) with crates.io versions
psvm -v "1.7.0" -o
# List all available Polkadot SDK versions
psvm -l
```

## Maintenance
> Listing all available Polkadot SDK versions requires querying the GitHub API, so your IP may be rate-limited. If a rate limit is reached, the tool will fallback to the GitHub CLI to list the versions. Ensure you have the GitHub CLI installed and authenticated to avoid any issue.
## Workflow

To update the Polkadot SDK dependencies mapping, run `./scripts/update.sh <polkadot-sdk repo path>`. This will update the dependencies `json` files located in `src/versions` with the latest versions defined in Polkadot SDK crates-io branches.
To update a `Cargo.toml`, the tool will fetch the `Plan.toml` file (used to publish crates into crates.io) from the release branch in Polkadot SDK associated to the version input (`--version` argument), generate a mapping (crate -> version) filtering all crates that were not published (i.e. `publish = false`), and overwrite the input Cargo.toml file to match the version from the mapping (i.e [v1.6.0 `Plan.toml`](https://raw.githubusercontent.com/paritytech/polkadot-sdk/release-crates-io-v1.6.0/Plan.toml)).

You can add more branches in the `BRANCHES` list from `./scripts/update.sh` file. Once you have process the branches, you need to index them in the `src/versions.rs` file.
In specific versions, the `Plan.toml` file may not exists (i.e. v1.3.0). In this case, the tool will fallback to the `Cargo.lock` file (i.e. [v1.3.0 `Cargo.lock`](https://raw.githubusercontent.com/paritytech/polkadot-sdk/release-crates-io-v1.3.0/Cargo.lock)) from the branch, generate a mapping using this file and overwrite the input Cargo.toml file to match the version from the mapping. The only concern to be aware in this scenario is that the `Cargo.lock` file may contain dependencies that are not published in crates.io, and the tool will not be able to filter them out cause it is not possible to determine if a crate is published or not (with this file). If you have a local dependency with a name similar to a crate not published, the tool will overwrite it, so be careful. Currently, this only happens with v1.3.0, but as the branches can change at any time, it is important to be aware of this. The tool will alert with a message "Failed to get Plan.toml, falling back to Cargo.lock." if this happens.
46 changes: 0 additions & 46 deletions scripts/process.sh

This file was deleted.

37 changes: 0 additions & 37 deletions scripts/update.sh

This file was deleted.

39 changes: 28 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ mod versions;

use clap::Parser;
use env_logger::Env;
use serde_json::from_str;
use std::collections::BTreeMap;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use toml_edit::DocumentMut;
use versions::get_version_mapping;
use versions::get_release_branches_versions;
use versions::get_version_mapping_with_fallback;

pub const DEFAULT_GIT_SERVER: &str = "https://raw.githubusercontent.com";

/// Polkadot SDK Version Manager.
///
Expand All @@ -36,25 +38,40 @@ struct Command {
#[clap(short, long, default_value = "Cargo.toml")]
path: PathBuf,

/// Specifies the Polkadot SDK version.
#[clap(short, long)]
version: String,
/// Specifies the Polkadot SDK version. Use '--list' flag to display available versions.
#[clap(short, long, required_unless_present = "list")]
version: Option<String>,

/// Overwrite local dependencies (using path).
/// Overwrite local dependencies (using path) with same name as the ones in the Polkadot SDK.
#[clap(short, long)]
overwrite: bool,

/// List available versions.
#[clap(short, long)]
list: bool,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let cmd = Command::parse();

if cmd.list {
let crates_versions = get_release_branches_versions().await?;
println!("Available versions:");
for version in crates_versions.iter() {
println!("- {}", version);
}
return Ok(());
}

let version = cmd.version.unwrap(); // Safe to unwrap due to `required_unless_present`

let cargo_toml_path = validate_workspace_path(cmd.path)?;

// Decide which branch data to use based on the branch name
let crates_versions_data = get_version_mapping(&cmd.version);

let crates_versions: BTreeMap<String, String> = from_str(crates_versions_data)?;
let crates_versions: BTreeMap<String, String> =
get_version_mapping_with_fallback(DEFAULT_GIT_SERVER, &version).await?;

update_dependencies(&cargo_toml_path, &crates_versions, cmd.overwrite)?;

Expand Down Expand Up @@ -127,7 +144,7 @@ fn update_dependencies_impl(
}
}

fn update_table_dependencies(
pub fn update_table_dependencies(
dep_table: &mut toml_edit::Table,
crates_versions: &BTreeMap<String, String>,
overwrite: bool,
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 1969933

Please sign in to comment.