Inject md5 sum into PKGBUILD

This commit is contained in:
Colin Woodbury
2020-06-10 20:03:19 -07:00
parent 574272b0e2
commit 1bf7d04a35
2 changed files with 16 additions and 13 deletions

View File

@@ -14,6 +14,7 @@ categories = ["cargo-plugins", "command-line-utilities"]
[dependencies]
auto_from = "0.3"
itertools = "0.9"
md5 = "0.7"
serde = "1.0"
serde_derive = "1.0"
toml = "0.5"

View File

@@ -4,11 +4,6 @@ use serde_derive::Deserialize;
use std::process::{self, Command};
use std::{fmt, fs, io};
// What it needs to do:
// 1. Read Cargo.toml and produce PKGBUILD.
// 2. Build a release binary if there isn't one.
// 3. Tar the release binary and copy it to the project root.
#[derive(Deserialize, Debug)]
struct Config {
package: Package,
@@ -25,6 +20,13 @@ struct Package {
license: String,
}
impl Package {
/// The name of the tarball that should be produced from this `Package`.
fn tarball(&self) -> String {
format!("{}-{}-x86_64.tar.gz", self.name, self.version)
}
}
#[derive(From)]
enum Error {
Io(io::Error),
@@ -61,9 +63,9 @@ fn work() -> Result<(), Error> {
let config = cargo_config()?;
release_build()?;
tarball(&config.package)?;
let md5 = md5sum()?;
let md5 = md5sum(&config.package)?;
let pkgbuild = pkgbuild(&config.package, &md5);
println!("{}", pkgbuild);
fs::write("PKGBUILD", pkgbuild)?;
Ok(())
}
@@ -77,8 +79,7 @@ fn cargo_config() -> Result<Config, Error> {
/// Produce a legal PKGBUILD.
fn pkgbuild(package: &Package, md5: &str) -> String {
format!(
r#"
{}
r#"{}
pkgname={}-bin
pkgver={}
pkgrel=1
@@ -124,13 +125,12 @@ fn release_build() -> Result<(), Error> {
}
fn tarball(package: &Package) -> Result<(), Error> {
let path = format!("{}-{}-x86_64.tar.gz", package.name, package.version);
let binary = format!("target/release/{}", package.name);
fs::copy(binary, &package.name)?;
Command::new("tar")
.arg("czf")
.arg(path)
.arg(package.tarball())
.arg(&package.name)
.status()?;
fs::remove_file(&package.name)?;
@@ -138,6 +138,8 @@ fn tarball(package: &Package) -> Result<(), Error> {
Ok(())
}
fn md5sum() -> Result<String, Error> {
Ok("dummy".to_string())
fn md5sum(package: &Package) -> Result<String, Error> {
let bytes = fs::read(package.tarball())?;
let digest = md5::compute(bytes);
Ok(format!("{:x}", digest))
}