feat: add vpm list repos

This commit is contained in:
anatawa12 2023-01-23 18:26:34 +09:00
commit e51d51fd52
No known key found for this signature in database
GPG key ID: 9CA909848B8E4EA6
4 changed files with 136 additions and 40 deletions

View file

@ -23,7 +23,7 @@ Ports
- [ ] `vpm install templates`
- [ ] `vpm list`
- [ ] `vpm list unity`
- [ ] `vpm list repos`
- [x] `vpm list repos`
- [ ] `vpm list repo`
- [ ] `vpm list templates`
- [ ] `vpm check`

81
src/commands/list.rs Normal file
View file

@ -0,0 +1,81 @@
use crate::version::Version;
use crate::vpm::VersionSelector;
use clap::{Parser, Subcommand};
use reqwest::Url;
use std::path::{Path, PathBuf};
#[derive(Subcommand)]
pub enum Command {
Repos(Repos),
Repo(Repo),
}
impl Command {
pub async fn run(self) {
match self {
Command::Repos(cmd) => cmd.run().await,
Command::Repo(cmd) => cmd.run().await,
}
}
}
#[derive(Parser)]
pub struct Repos {}
impl Repos {
pub async fn run(self) {
let client = crate::create_client();
let mut env = crate::vpm::Environment::load_default(client)
.await
.expect("loading global config");
env.get_repos(|repo| {
let mut name = None;
let mut r#type = None;
let mut local_path = None;
if let Some(description) = &repo.description {
name = name.or(description.name.as_deref());
r#type = r#type.or(description.r#type.as_deref());
}
if let Some(creation_info) = &repo.creation_info {
name = name.or(creation_info.name.as_deref());
local_path = local_path.or(creation_info.local_path.as_deref());
}
println!(
"{} | {} (at {})",
name.unwrap_or("(unnamed)"),
r#type.unwrap_or("(unknown type)"),
local_path.unwrap_or(Path::new("(unknown)")).display(),
);
Ok(())
})
.await
.expect("error listing repo");
}
}
#[derive(Parser)]
pub struct Repo {
/// Name of Package
#[arg()]
path_or_url: String,
}
impl Repo {
pub async fn run(self) {
let client = crate::create_client();
let mut env = crate::vpm::Environment::load_default(client)
.await
.expect("loading global config");
if let Ok(url) = Url::parse(&self.path_or_url) {
env.add_remote_repo(url).await.expect("adding repository")
} else {
env.add_local_repo(Path::new(&self.path_or_url))
.await
.expect("adding repository")
}
env.save().await.expect("saving settings file");
}
}

View file

@ -1,12 +1,15 @@
use clap::Parser;
mod add;
mod list;
mod resolve;
#[derive(Parser)]
pub enum Command {
#[command(subcommand)]
Add(add::Command),
#[command(subcommand)]
List(list::Command),
Resolve(resolve::Command),
}
@ -14,6 +17,7 @@ impl Command {
pub async fn run(self) {
match self {
Command::Add(cmd) => cmd.run().await,
Command::List(cmd) => cmd.run().await,
Command::Resolve(cmd) => cmd.run().await,
}
}

View file

@ -120,54 +120,40 @@ impl Environment {
.next())
}
pub(crate) async fn find_packages(&mut self, package: &str) -> io::Result<Vec<PackageJson>> {
let mut list = Vec::new();
pub async fn get_repos(
&mut self,
mut callback: impl FnMut(&LocalCachedRepository) -> io::Result<()>,
) -> io::Result<()> {
let official = self
.repo_cache
.get_or_create_repo(
&self.get_official_path(),
VRC_OFFICIAL_URL,
Some("Official"),
)
.await?;
callback(official)?;
fn append<'a>(
list: &mut Vec<PackageJson>,
package: &str,
repo: &'a LocalCachedRepository,
) -> io::Result<()> {
if let Some(version) = repo.cache.get(package) {
list.extend(
from_value::<PackageVersions>(version.clone())?
.versions
.values()
.cloned(),
);
}
Ok(())
}
append(
&mut list,
package,
self.repo_cache
.get_or_create_repo(
&self.get_official_path(),
VRC_OFFICIAL_URL,
Some("Official"),
)
.await?,
)?;
append(
&mut list,
package,
self.repo_cache
.get_or_create_repo(&self.get_curated_path(), VRC_CURATED_URL, Some("Curated"))
.await?,
)?;
let curated = self
.repo_cache
.get_or_create_repo(&self.get_curated_path(), VRC_CURATED_URL, Some("Curated"))
.await?;
callback(curated)?;
let mut uesr_repo_file_names = HashSet::new();
let repos_base = self.get_repos_dir();
let user_repos = self.get_user_repos()?;
for x in &user_repos {
append(&mut list, package, self.repo_cache.get_user_repo(x).await?)?;
callback(self.repo_cache.get_user_repo(x).await?)?;
if let Ok(relative) = x.local_path.strip_prefix(&repos_base) {
if let Some(file_name) = relative.file_name() {
if relative.parent().is_none() {
if relative
.parent()
.map(|x| x.as_os_str().is_empty())
.unwrap_or(true)
{
// the file must be in direct child of
uesr_repo_file_names.insert(file_name.to_owned());
}
@ -189,10 +175,35 @@ impl Environment {
.repo_cache
.get_repo(&path, || async { unreachable!() })
.await?;
append(&mut list, package, repo)?;
callback(repo)?;
}
}
Ok(())
}
pub(crate) async fn find_packages(&mut self, package: &str) -> io::Result<Vec<PackageJson>> {
let mut list = Vec::new();
fn append<'a>(
list: &mut Vec<PackageJson>,
package: &str,
repo: &'a LocalCachedRepository,
) -> io::Result<()> {
if let Some(version) = repo.cache.get(package) {
list.extend(
from_value::<PackageVersions>(version.clone())?
.versions
.values()
.cloned(),
);
}
Ok(())
}
self.get_repos(|repo| append(&mut list, package, repo))
.await?;
// user package folders
for x in self.get_user_package_folders()? {
if let Some(package_json) =