This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an initial dist xtask for packaging release artifacts
There's more work to be done here, but it's a start. Honestly, adding miniserde just to parse out the Cargo metadata seems way more difficult than it should be to get an accurate list of the binary artifacts for the project. There is an unstable `--out-dir` feature to copy artifacts for you after building, but it hasn't stabilized over the past couple of years. Not ideal. See: - rust-lang/cargo#3757 - rust-lang/cargo#6790 (comment) - matklad/cargo-xtask#3
- Loading branch information
1 parent
8540f62
commit 0d5b71b
Showing
6 changed files
with
191 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use std::env; | ||
use std::fs; | ||
use std::path::PathBuf; | ||
|
||
use anyhow::Result; | ||
use miniserde::{json, Deserialize}; | ||
use xshell::Shell; | ||
|
||
use crate::commands::cargo_cmd; | ||
use crate::utils::{project_root, verbose_cd}; | ||
use crate::Config; | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct Metadata { | ||
packages: Vec<Package>, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct Package { | ||
#[allow(dead_code)] | ||
name: String, | ||
targets: Vec<Target>, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct Target { | ||
kind: Vec<String>, | ||
name: String, | ||
} | ||
|
||
pub fn dist(config: &Config) -> Result<()> { | ||
let _ = fs::remove_dir_all(dist_dir()); | ||
fs::create_dir_all(dist_dir())?; | ||
|
||
dist_binary(config)?; | ||
// TODO: dist_manpage()?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn dist_binary(config: &Config) -> Result<()> { | ||
let sh = Shell::new()?; | ||
verbose_cd(&sh, project_root()); | ||
|
||
let cmd_option = cargo_cmd(config, &sh)?; | ||
if let Some(cmd) = cmd_option { | ||
let args = vec!["build", "--release", "--bins"]; | ||
cmd.args(args).run()?; | ||
} | ||
|
||
let binaries = project_binaries(config)?; | ||
for binary in &binaries { | ||
if binary == "xtask" { | ||
eprintln!("Ignoring xtask binary"); | ||
continue; | ||
} | ||
|
||
let binary_filename = if cfg!(target_os = "windows") { | ||
format!("{}.exe", binary) | ||
} else { | ||
binary.to_string() | ||
}; | ||
let src = release_dir().join(&binary_filename); | ||
let dest = dist_dir().join(&binary_filename); | ||
|
||
eprintln!("Copying {} to {}", src.display(), dest.display()); | ||
fs::copy(&src, &dest)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn project_binaries(config: &Config) -> Result<Vec<String>> { | ||
let sh = Shell::new()?; | ||
let mut binaries = Vec::new(); | ||
|
||
let cmd_option = cargo_cmd(config, &sh)?; | ||
if let Some(cmd) = cmd_option { | ||
let args = vec!["metadata", "--no-deps", "--format-version=1"]; | ||
let output = cmd.args(args).output()?; | ||
|
||
let metadata_json = String::from_utf8(output.stdout)?; | ||
let metadata: Metadata = json::from_str(&metadata_json)?; | ||
|
||
for package in metadata.packages { | ||
for target in &package.targets { | ||
if target.kind.contains(&"bin".to_string()) { | ||
eprintln!("{:?}", package); | ||
binaries.push(target.name.clone()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
Ok(binaries) | ||
} | ||
|
||
fn dist_dir() -> PathBuf { | ||
target_dir().join("dist") | ||
} | ||
|
||
fn release_dir() -> PathBuf { | ||
target_dir().join("release") | ||
} | ||
|
||
fn target_dir() -> PathBuf { | ||
let relative_path = env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| "target".to_string()); | ||
PathBuf::from(relative_path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters