Skip to content

Commit

Permalink
parallel execution
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlan404 committed Jun 20, 2024
1 parent d4abb48 commit 3c740b6
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 16 deletions.
21 changes: 16 additions & 5 deletions src/api/app/actions/build/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::Path;
use std::{path::Path, sync::Arc};

use anyhow::Result;

Expand All @@ -19,17 +19,28 @@ impl App {
Ok(())
}

pub async fn action_install_addons(&self, base: &Path) -> Result<()> {
pub async fn action_install_addons(self: Arc<Self>, base: &Path) -> Result<()> {
let addons = self.collect_addons().await?;
let base = Arc::new(base.to_owned());

for addon in &addons {
self.action_install_addon(base, addon).await?;
let mut handles = vec![];

for addon in addons {
let app = self.clone();
let base = base.clone();
handles.push(tokio::spawn(async move {
app.action_install_addon(&base, &addon).await
}));
}

for handle in handles {
handle.await??;
}

Ok(())
}

pub async fn action_install_addon(&self, base: &Path, addon: &Addon) -> Result<()> {
pub async fn action_install_addon(self: Arc<Self>, base: &Path, addon: &Addon) -> Result<()> {
let steps = addon.resolve_steps(&self).await?;
let dir = base.join(addon.target.as_str());
self.execute_steps(&dir, &steps).await?;
Expand Down
3 changes: 2 additions & 1 deletion src/api/app/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ impl App {
tokio::fs::remove_file(&output_path).await?;
//return Ok(StepResult::Continue);
} else {
if let Some((format, mut hasher, content)) = metadata.get_hasher() {
let hasher = metadata.get_hasher();
if let Some((format, mut hasher, content)) = hasher {

Check warning on line 68 in src/api/app/step.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `format`

warning: unused variable: `format` --> src/api/app/step.rs:68:38 | 68 | if let Some((format, mut hasher, content)) = hasher { | ^^^^^^ help: if this is intentional, prefix it with an underscore: `_format`
let output_file = File::open(&output_path).await?;
let mut stream = ReaderStream::new(output_file);

Expand Down
2 changes: 1 addition & 1 deletion src/api/models/addon/addon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl Addon {
match &self.addon_type {
AddonType::Url { url } => resolve_steps_for_url(app, url, None).await,
AddonType::Modrinth { id, version } => app.modrinth().resolve_steps(id, version).await,
AddonType::Curseforge { id, version } => todo!(),
AddonType::Curseforge { id, version } => Ok(vec![]),

Check warning on line 21 in src/api/models/addon/addon.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `version`

warning: unused variable: `version` --> src/api/models/addon/addon.rs:21:41 | 21 | AddonType::Curseforge { id, version } => Ok(vec![]), | ^^^^^^^ help: try ignoring the field: `version: _`

Check warning on line 21 in src/api/models/addon/addon.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `id`

warning: unused variable: `id` --> src/api/models/addon/addon.rs:21:37 | 21 | AddonType::Curseforge { id, version } => Ok(vec![]), | ^^ help: try ignoring the field: `id: _`
AddonType::Spigot { id, version } => todo!(),

Check warning on line 22 in src/api/models/addon/addon.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `version`

warning: unused variable: `version` --> src/api/models/addon/addon.rs:22:37 | 22 | AddonType::Spigot { id, version } => todo!(), | ^^^^^^^ help: try ignoring the field: `version: _`

Check warning on line 22 in src/api/models/addon/addon.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `id`

warning: unused variable: `id` --> src/api/models/addon/addon.rs:22:33 | 22 | AddonType::Spigot { id, version } => todo!(), | ^^ help: try ignoring the field: `id: _`
AddonType::Hangar { id, version } => todo!(),

Check warning on line 23 in src/api/models/addon/addon.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `version`

warning: unused variable: `version` --> src/api/models/addon/addon.rs:23:37 | 23 | AddonType::Hangar { id, version } => todo!(), | ^^^^^^^ help: try ignoring the field: `version: _`
AddonType::GithubRelease { repo, version, filename } => app.github().resolve_steps(repo, version, filename).await,
Expand Down
2 changes: 1 addition & 1 deletion src/api/step/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct FileMeta {
}

impl FileMeta {
pub fn get_hasher(&self) -> Option<(HashFormat, Box<dyn DynDigest>, String)> {
pub fn get_hasher(&self) -> Option<(HashFormat, Box<dyn DynDigest + Send>, String)> {
get_best_hash(&self.hashes).map(|(format, content)| (format, format.get_digest(), content))
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/utils/hashing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub enum HashFormat {
}

impl HashFormat {
pub fn get_digest(&self) -> Box<dyn DynDigest> {
pub fn get_digest(&self) -> Box<dyn DynDigest + Send> {

Check warning on line 25 in src/api/utils/hashing/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this argument (1 byte) is passed by reference, but would be more efficient if passed by value (limit: 8 byte)

warning: this argument (1 byte) is passed by reference, but would be more efficient if passed by value (limit: 8 byte) --> src/api/utils/hashing/mod.rs:25:23 | 25 | pub fn get_digest(&self) -> Box<dyn DynDigest + Send> { | ^^^^^ help: consider passing by value instead: `self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref
match self {
HashFormat::Sha256 => Box::new(sha2::Sha256::new()),
HashFormat::Sha512 => Box::new(sha2::Sha512::new()),
Expand Down
4 changes: 2 additions & 2 deletions src/commands/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::Path;
use std::{path::Path, sync::Arc};

use anyhow::Result;

Expand All @@ -9,7 +9,7 @@ pub struct Args {

}

pub async fn run(mut app: App, args: Args) -> Result<()> {
pub async fn run(app: Arc<App>, args: Args) -> Result<()> {

Check warning on line 12 in src/commands/build.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `args`

warning: unused variable: `args` --> src/commands/build.rs:12:33 | 12 | pub async fn run(app: Arc<App>, args: Args) -> Result<()> { | ^^^^ help: if this is intentional, prefix it with an underscore: `_args`
let base = Path::new("./output/server");

app.action_install_jar(&base)

Check failure on line 15 in src/commands/build.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

error: this expression creates a reference which is immediately dereferenced by the compiler --> src/commands/build.rs:15:28 | 15 | app.action_install_jar(&base) | ^^^^^ help: change this to: `base` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
Expand Down
4 changes: 2 additions & 2 deletions src/commands/init.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::Path;
use std::{path::Path, sync::Arc};

use anyhow::Result;

Expand All @@ -11,7 +11,7 @@ pub struct Args {
name: Option<String>,
}

pub async fn run(mut app: App, args: Args) -> Result<()> {
pub async fn run(app: Arc<App>, args: Args) -> Result<()> {

Check warning on line 14 in src/commands/init.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `args`

warning: unused variable: `args` --> src/commands/init.rs:14:33 | 14 | pub async fn run(app: Arc<App>, args: Args) -> Result<()> { | ^^^^ help: if this is intentional, prefix it with an underscore: `_args`
app.action_install_addons(Path::new("./output/server"))
.await?;

Expand Down
4 changes: 3 additions & 1 deletion src/commands/sources/list.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::sync::Arc;

use anyhow::Result;

use crate::api::app::App;

#[derive(clap::Args)]
pub struct Args {}

pub async fn run(mut app: App, args: Args) -> Result<()> {
pub async fn run(app: Arc<App>, args: Args) -> Result<()> {

Check warning on line 10 in src/commands/sources/list.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `args`

warning: unused variable: `args` --> src/commands/sources/list.rs:10:33 | 10 | pub async fn run(app: Arc<App>, args: Args) -> Result<()> { | ^^^^ help: if this is intentional, prefix it with an underscore: `_args`
let sources = app.collect_sources().await?;

println!("{sources:#?}");
Expand Down
4 changes: 3 additions & 1 deletion src/commands/sources/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use anyhow::Result;

use crate::api::app::App;
Expand All @@ -9,7 +11,7 @@ pub enum Commands {
List(list::Args),
}

pub async fn run(mut app: App, args: Commands) -> Result<()> {
pub async fn run(app: Arc<App>, args: Commands) -> Result<()> {
match args {
Commands::List(args) => list::run(app, args).await,
}
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use anyhow::Result;
use api::app::App;
use clap::Parser;
Expand Down Expand Up @@ -27,7 +29,7 @@ enum Commands {
#[tokio::main]
async fn main() -> Result<()> {
let args = Cli::parse();
let app = App::new()?;
let app = Arc::new(App::new()?);

match args.command {
Commands::Init(args) => commands::init::run(app, args).await,
Expand Down

0 comments on commit 3c740b6

Please sign in to comment.