mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-19 09:00:54 +00:00
attempt to resolve version from URL automatically
this might be a bit too heavy-weight for a lightweight tool but let's give it a try
This commit is contained in:
parent
87413806bf
commit
e41195e349
4 changed files with 158 additions and 1 deletions
|
|
@ -39,6 +39,11 @@ serde_json = "1.0"
|
||||||
# bao1x-api = { path = "../libs/bao1x-api" }
|
# bao1x-api = { path = "../libs/bao1x-api" }
|
||||||
bao1x-api = "0.1.0" # use crates.io to refer to this
|
bao1x-api = "0.1.0" # use crates.io to refer to this
|
||||||
|
|
||||||
|
# for remote resolution of github version in out-of-tree builds
|
||||||
|
reqwest = { version = "0.11", features = ["blocking", "json"] }
|
||||||
|
anyhow = "1.0"
|
||||||
|
toml = "0.8"
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "xous-copy-object"
|
name = "xous-copy-object"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,10 @@ extern crate crc;
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{Cursor, Write};
|
use std::io::{Cursor, Write};
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
use clap::{App, Arg};
|
use clap::{App, Arg};
|
||||||
|
use xous_semver::SemVer;
|
||||||
use xous_tools::elf::read_minielf;
|
use xous_tools::elf::read_minielf;
|
||||||
use xous_tools::sign_image::bin_to_uf2;
|
use xous_tools::sign_image::bin_to_uf2;
|
||||||
use xous_tools::swap_writer::SwapWriter;
|
use xous_tools::swap_writer::SwapWriter;
|
||||||
|
|
@ -107,12 +109,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let mut source = Cursor::new(Vec::new());
|
let mut source = Cursor::new(Vec::new());
|
||||||
args.write(&mut source).expect("Couldn't write out ELF files");
|
args.write(&mut source).expect("Couldn't write out ELF files");
|
||||||
|
|
||||||
|
let version = xous_tools::git_remote_version::get_xous_version().unwrap_or("v0.0.0".to_string());
|
||||||
|
let semver: [u8; 16] = SemVer::from_str(&version)?.into();
|
||||||
|
|
||||||
let result = xous_tools::sign_image::sign_image(
|
let result = xous_tools::sign_image::sign_image(
|
||||||
&source.get_ref(),
|
&source.get_ref(),
|
||||||
&private_key,
|
&private_key,
|
||||||
false,
|
false,
|
||||||
&None,
|
&None,
|
||||||
None,
|
Some(semver),
|
||||||
true,
|
true,
|
||||||
bao1x_api::signatures::SIGBLOCK_LEN,
|
bao1x_api::signatures::SIGBLOCK_LEN,
|
||||||
xous_tools::sign_image::Version::Bao1xV1,
|
xous_tools::sign_image::Version::Bao1xV1,
|
||||||
|
|
|
||||||
146
tools/src/git_remote_version.rs
Normal file
146
tools/src/git_remote_version.rs
Normal file
|
|
@ -0,0 +1,146 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
use anyhow::{Context, Result};
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct CargoToml {
|
||||||
|
dependencies: Option<HashMap<String, Dependency>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
enum Dependency {
|
||||||
|
Simple(String),
|
||||||
|
Detailed(DetailedDependency),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
|
struct DetailedDependency {
|
||||||
|
git: Option<String>,
|
||||||
|
rev: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct GitHubTag {
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_xous_core_revision(cargo_toml_path: &str) -> Result<Option<String>> {
|
||||||
|
// Read and parse Cargo.toml
|
||||||
|
let contents = fs::read_to_string(cargo_toml_path).context("Failed to read Cargo.toml")?;
|
||||||
|
|
||||||
|
let cargo: CargoToml = toml::from_str(&contents).context("Failed to parse Cargo.toml")?;
|
||||||
|
|
||||||
|
let Some(deps) = cargo.dependencies else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
|
||||||
|
let xous_core_url = "https://github.com/betrusted-io/xous-core";
|
||||||
|
let mut found_revisions = Vec::new();
|
||||||
|
|
||||||
|
// Search through dependencies for xous-core references
|
||||||
|
for (name, dep) in deps.iter() {
|
||||||
|
if let Dependency::Detailed(detailed) = dep {
|
||||||
|
if let (Some(git), Some(rev)) = (&detailed.git, &detailed.rev) {
|
||||||
|
if git == xous_core_url || git == &format!("{}.git", xous_core_url) {
|
||||||
|
found_revisions.push((name.clone(), rev.clone()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if let Dependency::Simple(simple) = dep {
|
||||||
|
unimplemented!("Can't handle dep: {}", simple);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if found_revisions.is_empty() {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
if found_revisions.len() > 1 {
|
||||||
|
eprintln!("Warning: Found multiple xous-core revisions:");
|
||||||
|
for (name, rev) in &found_revisions {
|
||||||
|
eprintln!(" {} -> {}", name, rev);
|
||||||
|
}
|
||||||
|
eprintln!("Using the first one: {}", found_revisions[0].1);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Some(found_revisions[0].1.clone()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_latest_tag_from_github(repo: &str) -> Result<String> {
|
||||||
|
let url = format!("https://api.github.com/repos/{}/tags", repo);
|
||||||
|
|
||||||
|
let client = reqwest::blocking::Client::builder()
|
||||||
|
.user_agent("xous-app-uf2-tool") // GitHub requires a user agent
|
||||||
|
.build()?;
|
||||||
|
|
||||||
|
let mut request = client.get(&url);
|
||||||
|
|
||||||
|
// Optional: use token if available (no token required by default)
|
||||||
|
let using_token = if let Ok(token) = std::env::var("GITHUB_TOKEN") {
|
||||||
|
request = request.header("Authorization", format!("token {}", token));
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
|
|
||||||
|
let response = request.send().context("Failed to fetch tags from GitHub")?;
|
||||||
|
|
||||||
|
// Check for rate limit errors
|
||||||
|
if response.status() == reqwest::StatusCode::FORBIDDEN {
|
||||||
|
// Check if this is a rate limit error
|
||||||
|
if let Some(remaining) = response.headers().get("x-ratelimit-remaining") {
|
||||||
|
if remaining.to_str().unwrap_or("") == "0" {
|
||||||
|
if using_token {
|
||||||
|
anyhow::bail!(
|
||||||
|
"GitHub API rate limit exceeded (5000 requests/hour with token).\n\
|
||||||
|
Please wait before trying again."
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
anyhow::bail!(
|
||||||
|
"GitHub API rate limit exceeded (60 requests/hour per IP without authentication).\n\
|
||||||
|
\n\
|
||||||
|
To increase your rate limit to 5000 requests/hour, set the GITHUB_TOKEN environment variable:\n\
|
||||||
|
\n\
|
||||||
|
1. Create a GitHub Personal Access Token at:\n\
|
||||||
|
https://github.com/settings/tokens\n\
|
||||||
|
2. No special permissions needed - you can leave all scopes unchecked\n\
|
||||||
|
3. Set the token:\n\
|
||||||
|
Linux/Mac: export GITHUB_TOKEN=your_token_here\n\
|
||||||
|
Windows: set GITHUB_TOKEN=your_token_here\n\
|
||||||
|
\n\
|
||||||
|
For more info: https://docs.github.com/en/rest/overview/rate-limits-for-the-rest-api"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !response.status().is_success() {
|
||||||
|
anyhow::bail!("GitHub API request failed: {}", response.status());
|
||||||
|
}
|
||||||
|
|
||||||
|
let tags: Vec<GitHubTag> = response.json().context("Failed to parse GitHub API response")?;
|
||||||
|
|
||||||
|
tags.first().map(|tag| tag.name.clone()).ok_or_else(|| anyhow::anyhow!("No tags found in repository"))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_xous_version() -> Result<String> {
|
||||||
|
// Assume we're running in the root directory
|
||||||
|
let cargo_toml_path = "Cargo.toml";
|
||||||
|
|
||||||
|
println!("Parsing {} for xous-core dependencies...", cargo_toml_path);
|
||||||
|
|
||||||
|
let rev = find_xous_core_revision(cargo_toml_path)?
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("No xous-core git dependency found in Cargo.toml"))?;
|
||||||
|
|
||||||
|
println!("Found xous-core revision: {}", rev);
|
||||||
|
println!("Fetching latest tag from GitHub...");
|
||||||
|
|
||||||
|
let version = get_latest_tag_from_github("betrusted-io/xous-core")?;
|
||||||
|
|
||||||
|
println!("Latest xous-core version: {}", version);
|
||||||
|
|
||||||
|
Ok(version)
|
||||||
|
}
|
||||||
|
|
@ -3,6 +3,7 @@ extern crate log;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub mod xous_arguments;
|
pub mod xous_arguments;
|
||||||
pub mod elf;
|
pub mod elf;
|
||||||
|
pub mod git_remote_version;
|
||||||
pub mod sign_image;
|
pub mod sign_image;
|
||||||
pub mod swap_writer;
|
pub mod swap_writer;
|
||||||
pub mod tags;
|
pub mod tags;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue