Read versions from Cargo.toml ref #3 #11

Merged
cadey merged 4 commits from cargo-version into main 2020-10-25 19:21:31 +00:00
9 changed files with 102 additions and 29 deletions

View File

@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased ## Unreleased
## 0.7.0
Support reading version data from `Cargo.toml`
## 0.6.1 ## 0.6.1
Fixed a logic error in release cutting. Fixed a logic error in release cutting.

40
Cargo.lock generated
View File

@ -98,6 +98,17 @@ version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "130aac562c0dd69c56b3b1cc8ffd2e17be31d0b6c25b61c96b76231aa23e39e1" checksum = "130aac562c0dd69c56b3b1cc8ffd2e17be31d0b6c25b61c96b76231aa23e39e1"
[[package]]
name = "cargo_toml"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "513d17226888c7b8283ac02a1c1b0d8a9d4cbf6db65dfadb79f598f5d7966fe9"
dependencies = [
"serde",
"serde_derive",
"toml",
]
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.54" version = "1.0.54"
@ -348,9 +359,10 @@ dependencies = [
[[package]] [[package]]
name = "gitea-release" name = "gitea-release"
version = "0.5.2" version = "0.7.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"cargo_toml",
"comrak", "comrak",
"elfs", "elfs",
"git2", "git2",
@ -365,6 +377,7 @@ dependencies = [
"structopt", "structopt",
"tempfile", "tempfile",
"tokio", "tokio",
"toml",
"url", "url",
] ]
@ -881,9 +894,9 @@ dependencies = [
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.17" version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1502d12e458c49a4c9cbff560d0fe0060c252bc29799ed94ca2ed4bb665a0101" checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
dependencies = [ dependencies = [
"unicode-xid", "unicode-xid",
] ]
@ -1100,18 +1113,18 @@ dependencies = [
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.111" version = "1.0.117"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9124df5b40cbd380080b2cc6ab894c040a3070d995f5c9dc77e18c34a8ae37d" checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.111" version = "1.0.117"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f2c3ac8e6ca1e9c80b8be1023940162bf81ae3cffbb1809474152f2ce1eb250" checksum = "cbd1ae72adb44aab48f325a02444a5fc079349a8d804c1fc922aed3f7454c74e"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1209,9 +1222,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.29" version = "1.0.48"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb37da98a55b1d08529362d9cbb863be17556873df2585904ab9d2bc951291d0" checksum = "cc371affeffc477f42a221a1e4297aedcea33d47d19b61455588bd9d8f6b19ac"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1353,6 +1366,15 @@ dependencies = [
"tokio", "tokio",
] ]
[[package]]
name = "toml"
version = "0.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75cf45bb0bef80604d001caaec0d09da99611b3c0fd39d3080468875cdb65645"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "tower-service" name = "tower-service"
version = "0.3.0" version = "0.3.0"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "gitea-release" name = "gitea-release"
version = "0.5.2" version = "0.7.0"
authors = ["Christine Dodrill <me@christine.website>"] authors = ["Christine Dodrill <me@christine.website>"]
edition = "2018" edition = "2018"
@ -8,6 +8,7 @@ edition = "2018"
[dependencies] [dependencies]
anyhow = "1.0" anyhow = "1.0"
cargo_toml = "0.8.1"
comrak = "0.7" comrak = "0.7"
git2 = "0.13" git2 = "0.13"
http = "0.2" http = "0.2"
@ -19,6 +20,7 @@ serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
structopt = { version = "0.3", default-features = false } structopt = { version = "0.3", default-features = false }
tokio = { version = "0.2", features = ["macros"] } tokio = { version = "0.2", features = ["macros"] }
toml = "0.5.7"
url = "2" url = "2"
gitea = { path = "./gitea" } gitea = { path = "./gitea" }

View File

@ -1 +0,0 @@
0.6.1

View File

@ -9,7 +9,7 @@ pub async fn run(
rm: ReleaseMeta, rm: ReleaseMeta,
) -> Result<()> { ) -> Result<()> {
let repo = git2::Repository::open(".")?; let repo = git2::Repository::open(".")?;
let tag = tag.unwrap_or(version::read_version("VERSION".into())?); let tag = tag.unwrap_or(version::read("VERSION".into())?);
let vtag = format!("v{}", tag); let vtag = format!("v{}", tag);
if git::has_tag(&repo, vtag.clone())? { if git::has_tag(&repo, vtag.clone())? {

View File

@ -1,16 +0,0 @@
use anyhow::Result;
use std::{fs, path::PathBuf};
pub(crate) fn read_version(fname: PathBuf) -> Result<String> {
let version = fs::read_to_string(fname)?;
Ok(version.trim().into())
}
#[cfg(test)]
mod tests {
#[test]
fn read_version() {
let version = super::read_version("./testdata/VERSION".into()).unwrap();
assert_eq!(version, "0.1.0");
}
}

38
src/version/cargo.rs Normal file
View File

@ -0,0 +1,38 @@
use anyhow::Result;
use cargo_toml::Manifest;
use std::fs::{self, File};
use std::io::Read;
fn get_file_as_byte_vec(filename: &str) -> Option<Vec<u8>> {
let f = File::open(&filename);
if f.is_err() {
return None;
}
let mut f = f.unwrap();
let metadata = fs::metadata(&filename).expect("unable to read metadata");
let mut buffer = vec![0; metadata.len() as usize];
f.read(&mut buffer).expect("buffer overflow");
Some(buffer)
}
pub fn read() -> Result<Option<String>> {
let bytes = get_file_as_byte_vec("Cargo.toml");
match bytes {
Some(bytes) => {
let pkg: Manifest = toml::from_slice(&bytes)?;
Ok(Some(pkg.package.unwrap().version))
}
None => Ok(None)
}
}
#[cfg(test)]
mod tests {
#[test]
fn read() {
use super::read;
read().unwrap().unwrap();
}
}

24
src/version/mod.rs Normal file
View File

@ -0,0 +1,24 @@
use anyhow::Result;
use std::{fs, path::PathBuf};
mod cargo;
pub(crate) fn read(fname: PathBuf) -> Result<String> {
let version = cargo::read()
.unwrap()
.unwrap_or(read_fs(fname)?);
Ok(version)
}
fn read_fs(fname: PathBuf) -> Result<String> {
Ok(fs::read_to_string(fname)?.trim().into())
}
#[cfg(test)]
mod tests {
#[test]
fn read_version() {
let version = super::read_fs("./testdata/VERSION".into()).unwrap();
assert_eq!(version, "0.1.0");
}
}

View File

@ -10,7 +10,7 @@ const TAG: &'static str = "0.1.0";
async fn cut() -> Result<()> { async fn cut() -> Result<()> {
let _ = pretty_env_logger::try_init(); let _ = pretty_env_logger::try_init();
let name = elfs::next(); let name = elfs::next();
let token = std::env::var("DOMO_GITEA_TOKEN")?; let token = std::env::var("DOMO_GITEA_TOKEN").expect("wanted envvar DOMO_GITEA_TOKEN");
let cli = gitea::Client::new("https://tulpa.dev".into(), token.clone(), APP_USER_AGENT)?; let cli = gitea::Client::new("https://tulpa.dev".into(), token.clone(), APP_USER_AGENT)?;
debug!("created gitea client"); debug!("created gitea client");