|
| 1 | +// cargo run --example list_crates |
| 2 | +// This list all crates that are generated. |
| 3 | + |
| 4 | +// cargo run --example list_crates -- 0.1.0 |
| 5 | +// If a version is passed in as an option, it will filter out any that |
| 6 | +// are already published to crates.io. |
| 7 | +// An updated clone of crates.io-index is required. |
| 8 | +// git clone https://github.com/rust-lang/crates.io-index |
| 9 | + |
| 10 | +use camino::{Utf8Path, Utf8PathBuf}; |
| 11 | +use serde::Deserialize; |
| 12 | +use std::io::BufRead; |
| 13 | +use std::{ |
| 14 | + fs::{self, File}, |
| 15 | + io::BufReader, |
| 16 | + str::FromStr, |
| 17 | +}; |
| 18 | +pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>; |
| 19 | + |
| 20 | +/// Get all directories below the given directory. |
| 21 | +fn get_dirs(dir: impl AsRef<Utf8Path>) -> Result<Vec<Utf8PathBuf>> { |
| 22 | + let mut dirs = Vec::new(); |
| 23 | + let paths = fs::read_dir(dir.as_ref())?; |
| 24 | + for path in paths { |
| 25 | + match path { |
| 26 | + Ok(path) => match Utf8Path::from_path(&path.path()) { |
| 27 | + Some(path) => { |
| 28 | + if path.is_dir() { |
| 29 | + dirs.push(path.to_path_buf()); |
| 30 | + } |
| 31 | + } |
| 32 | + None => (), |
| 33 | + }, |
| 34 | + Err(_) => (), |
| 35 | + } |
| 36 | + } |
| 37 | + Ok(dirs) |
| 38 | +} |
| 39 | + |
| 40 | +fn list_crate_names() -> Result<Vec<String>> { |
| 41 | + let mut names: Vec<_> = get_dirs("../mgmt")? |
| 42 | + .into_iter() |
| 43 | + .filter_map(|d| d.file_name().map(|d| format!("azure_mgmt_{}", d))) |
| 44 | + .collect(); |
| 45 | + names.extend( |
| 46 | + get_dirs("../svc")? |
| 47 | + .into_iter() |
| 48 | + .filter_map(|d| d.file_name().map(|d| format!("azure_svc_{}", d))), |
| 49 | + ); |
| 50 | + names.sort(); |
| 51 | + Ok(names) |
| 52 | +} |
| 53 | + |
| 54 | +fn main() -> Result<()> { |
| 55 | + let version = std::env::args().nth(1); |
| 56 | + |
| 57 | + let names = list_crate_names()?; |
| 58 | + match &version { |
| 59 | + Some(version) => { |
| 60 | + for name in names.iter() { |
| 61 | + if !has_version(name, version)? { |
| 62 | + println!("{}", name); |
| 63 | + // println!("cargo publish -p {}", name); |
| 64 | + // println!("Start-Sleep -Seconds 420"); |
| 65 | + // println!("cargo owner --add github:Azure:azure-sdk-publish-rust -- {}", name); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + None => { |
| 70 | + for (i, name) in names.iter().enumerate() { |
| 71 | + println!("{} {}", i + 1, name); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + Ok(()) |
| 77 | +} |
| 78 | + |
| 79 | +fn has_version(name: &str, version: &str) -> Result<bool> { |
| 80 | + Ok(get_versions(name)?.iter().any(|v| v.vers.as_str() == version)) |
| 81 | +} |
| 82 | + |
| 83 | +fn get_versions(name: &str) -> Result<Vec<CrateVersion>> { |
| 84 | + let path = format!("../../../crates.io-index/az/ur/{}", name); |
| 85 | + let path = Utf8PathBuf::from_str(&path)?; |
| 86 | + let mut versions = Vec::new(); |
| 87 | + if path.exists() { |
| 88 | + let file = File::open(path)?; |
| 89 | + let reader = BufReader::new(file); |
| 90 | + for line in reader.lines() { |
| 91 | + let version: CrateVersion = serde_json::from_str(&line?)?; |
| 92 | + versions.push(version); |
| 93 | + } |
| 94 | + } |
| 95 | + Ok(versions) |
| 96 | +} |
| 97 | + |
| 98 | +#[derive(Debug, Deserialize)] |
| 99 | +pub struct CrateVersion { |
| 100 | + pub name: String, |
| 101 | + pub vers: String, |
| 102 | +} |
0 commit comments