Skip to content

Commit

Permalink
yea
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlan404 committed Nov 11, 2023
1 parent d9524a4 commit 968004a
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 28 deletions.
8 changes: 3 additions & 5 deletions examples/quilt/server.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ type = "modrinth"
id = "create-fabric"
version = "wKEEi1qX"

[[mods]]
type = "modrinth"
id = "qsl"
version = "BTCxVi75"

[markdown]
files = ["README.md"]
auto_update = false

[options]
upload_to_mclogs = true
97 changes: 79 additions & 18 deletions src/hot_reload/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{process::Stdio, time::Duration, path::PathBuf, sync::{Mutex, Arc}};
use std::{process::{Stdio, ExitStatus}, time::Duration, path::PathBuf, sync::{Mutex, Arc}};

use anyhow::{Result, Context};
use anyhow::{Result, Context, bail, anyhow};
use console::style;
use dialoguer::theme::ColorfulTheme;
use indicatif::ProgressBar;
Expand Down Expand Up @@ -42,6 +42,20 @@ async fn try_read_line(opt: &mut Option<tokio::io::Lines<BufReader<tokio::proces
}
}

async fn try_wait_child(opt: &mut Option<Child>) -> Result<Option<ExitStatus>> {
match opt {
Some(c) => Ok(Some(c.wait().await?)),
None => Ok(None),
}
}

pub enum TestResult {
Ongoing,
Success,
Failed,

Check warning on line 55 in src/hot_reload/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

variant `Failed` is never constructed

warning: variant `Failed` is never constructed --> src/hot_reload/mod.rs:55:5 | 52 | pub enum TestResult { | ---------- variant in this enum ... 55 | Failed, | ^^^^^^
Crashed,
}

// TODO
// [x] fix stdout nesting for some reason
// [x] commands are not being sent properly
Expand Down Expand Up @@ -86,11 +100,12 @@ impl<'a> DevSession<'a> {

let mut is_stopping = false;
let mut test_passed = false;
let mut test_result = TestResult::Ongoing;
let mut exit_status = None;

let mut stdin_lines = tokio::io::BufReader::new(tokio::io::stdin()).lines();

loop {
'l: loop {
tokio::select! {
Some(cmd) = rx.recv() => {
match cmd {
Expand All @@ -112,7 +127,6 @@ impl<'a> DevSession<'a> {
exit_status = None;
}
Command::SendCommand(command) => {
self.builder.app.info(&format!("Sending command: {command}"))?;
if let Some(ref mut child) = &mut child {
if let Some(ref mut stdin) = &mut child.stdin {
let _ = stdin.write_all(command.as_bytes()).await;
Expand Down Expand Up @@ -181,7 +195,7 @@ impl<'a> DevSession<'a> {
}
Command::EndSession => {
self.builder.app.info("Ending session...")?;
break;
break 'l;
}
}
},
Expand All @@ -190,16 +204,24 @@ impl<'a> DevSession<'a> {

if self.test_mode
&& !is_stopping
&& !test_passed
&& s.contains("]: Done")
&& s.ends_with("For help, type \"help\"") {
test_passed = true;

self.builder.app.success("Test passed!");

tx.send(Command::SendCommand("stop\nend\n".to_owned())).await?;
tx.send(Command::WaitUntilExit).await?;
tx.send(Command::EndSession).await?;
&& !test_passed {

if s.contains("]: Done") && s.ends_with("For help, type \"help\"") {
test_passed = true;
test_result = TestResult::Success;

self.builder.app.success("Test passed!")?;

tx.send(Command::SendCommand("stop\nend\n".to_owned())).await?;
tx.send(Command::WaitUntilExit).await?;
tx.send(Command::EndSession).await?;
} else if s == "---- end of report ----" {
self.builder.app.info("Server crashed!")?;
test_result = TestResult::Crashed;

tx.send(Command::WaitUntilExit).await?;
tx.send(Command::EndSession).await?;
}
}

mp.suspend(|| {
Expand All @@ -219,10 +241,20 @@ impl<'a> DevSession<'a> {
}
}
},
status = try_wait_child(&mut child) => {
exit_status = status.unwrap_or(None);

self.builder.app.info("Server process exited")?;

if self.test_mode {
tx.send(Command::WaitUntilExit).await?;
tx.send(Command::EndSession).await?;
}
},
_ = tokio::signal::ctrl_c() => {
if !is_stopping {
self.builder.app.info("Stopping development session...")?;
break;
break 'l;
}
}
}
Expand Down Expand Up @@ -258,6 +290,15 @@ impl<'a> DevSession<'a> {
}
}

Check failure on line 291 in src/hot_reload/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this `else { if .. }` block can be collapsed

error: this `else { if .. }` block can be collapsed --> src/hot_reload/mod.rs:285:28 | 285 | } else { | ____________________________^ 286 | | if !status.success() { 287 | | println!( 288 | | " - Process didn't exit successfully" 289 | | ); 290 | | } 291 | | } | |_____________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_else_if help: collapse nested if block | 285 ~ } else if !status.success() { 286 + println!( 287 + " - Process didn't exit successfully" 288 + ); 289 + } |
}

match test_result {
TestResult::Crashed => {
println!(
" - Server crashed"
);
}
_ => {}
}

Check failure on line 301 in src/hot_reload/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let` --> src/hot_reload/mod.rs:294:17 | 294 | / match test_result { 295 | | TestResult::Crashed => { 296 | | println!( 297 | | " - Server crashed" ... | 300 | | _ => {} 301 | | } | |_________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_match = note: `#[deny(clippy::single_match)]` implied by `#[deny(clippy::all)]` help: try | 294 ~ if let TestResult::Crashed = test_result { 295 + println!( 296 + " - Server crashed" 297 + ); 298 + } |
});

if self.builder.app.server.options.upload_to_mclogs {
Expand All @@ -266,7 +307,27 @@ impl<'a> DevSession<'a> {

pb.enable_steady_tick(Duration::from_millis(250));

let log_path = self.builder.output_dir.join("logs").join("latest.log");
let log_path = match test_result {
TestResult::Crashed => {
let folder = self.builder.output_dir.join("crash-reports");
if !folder.exists() {
bail!("crash-reports folder doesn't exist, cant upload to mclo.gs");
}

// get latest crash report
let (report_path, _) = folder.read_dir()?
.into_iter()

Check failure on line 319 in src/hot_reload/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

useless conversion to the same type: `std::fs::ReadDir`

error: useless conversion to the same type: `std::fs::ReadDir` --> src/hot_reload/mod.rs:318:48 | 318 | let (report_path, _) = folder.read_dir()? | ________________________________________________^ 319 | | .into_iter() | |________________________________________^ help: consider removing `.into_iter()`: `folder.read_dir()?` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion = note: `#[deny(clippy::useless_conversion)]` implied by `#[deny(clippy::all)]`
.filter_map(|f| f.ok())

Check warning on line 320 in src/hot_reload/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

redundant closure

warning: redundant closure --> src/hot_reload/mod.rs:320:41 | 320 | ... .filter_map(|f| f.ok()) | ^^^^^^^^^^ help: replace the closure with the method itself: `std::result::Result::ok` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_for_method_calls
.filter_map(|f| Some((f.path(), f.metadata().ok()?.modified().ok()?)))
.max_by_key(|(_, t)| t.clone())

Check failure on line 322 in src/hot_reload/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `SystemTime` which implements the `Copy` trait

error: using `clone` on type `SystemTime` which implements the `Copy` trait --> src/hot_reload/mod.rs:322:50 | 322 | ... .max_by_key(|(_, t)| t.clone()) | ^^^^^^^^^ help: try dereferencing it: `*t` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
.ok_or(anyhow!("can't find crash report"))?;

report_path
}
_ => {
self.builder.output_dir.join("logs").join("latest.log")
}
};

if log_path.exists() {
let content = std::fs::read_to_string(&log_path)
Expand All @@ -276,7 +337,7 @@ impl<'a> DevSession<'a> {
drop(content);

pb.finish_and_clear();
self.builder.app.success("Log uploaded to mclo.gs");
self.builder.app.success("Log uploaded to mclo.gs")?;
mp.suspend(|| {
println!();
println!(" -- [ {} ] --", log.url);
Expand Down
16 changes: 11 additions & 5 deletions src/model/servertype/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl ServerType {
}
}

// TODO: move this to somewhere else, like BuildContext
pub async fn get_install_method(
&self,
app: &App,
Expand Down Expand Up @@ -196,6 +197,7 @@ impl ServerType {
})
}

// TODO: move this to somewhere else, like BuildContext
pub async fn get_startup_method(
&self,
app: &App,
Expand Down Expand Up @@ -231,6 +233,7 @@ impl ServerType {
})
}

// TODO: move to ModrinthAPI
pub fn get_modrinth_facets(&self, mcver: &str) -> Result<String> {

Check warning on line 237 in src/model/servertype/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

method `get_modrinth_facets` is never used

warning: method `get_modrinth_facets` is never used --> src/model/servertype/mod.rs:237:12 | 98 | impl ServerType { | --------------- method in this implementation ... 237 | pub fn get_modrinth_facets(&self, mcver: &str) -> Result<String> { | ^^^^^^^^^^^^^^^^^^^
let mut arr: Vec<Vec<String>> = vec![];

Expand All @@ -245,6 +248,7 @@ impl ServerType {
Ok(serde_json::to_string(&arr)?)
}

// TODO: move to ModrinthAPI
pub fn get_modrinth_name(&self) -> Option<String> {
match self {
Self::Fabric { .. } => Some("fabric"),
Expand All @@ -262,6 +266,7 @@ impl ServerType {
}.map(|o| o.to_owned())

Check warning on line 266 in src/model/servertype/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

redundant closure

warning: redundant closure --> src/model/servertype/mod.rs:266:15 | 266 | }.map(|o| o.to_owned()) | ^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `std::borrow::ToOwned::to_owned` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_for_method_calls
}

// TODO: move to HangarAPI
pub fn get_hangar_platform(&self) -> Option<mcapi::hangar::Platform> {
match self {
Self::Waterfall {} => Some(mcapi::hangar::Platform::Waterfall),
Expand All @@ -274,6 +279,7 @@ impl ServerType {
}
}

// TODO: move to HangarAPI
pub fn get_hangar_versions_filter(&self, mcver: &str) -> mcapi::hangar::VersionsFilter {
let platform = self.get_hangar_platform();
mcapi::hangar::VersionsFilter {
Expand Down Expand Up @@ -302,11 +308,11 @@ impl ToString for ServerType {
ServerType::Vanilla { } => String::from("Vanilla"),
ServerType::PaperMC { project, build } => format!("{project} build {build}"),
ServerType::Purpur { build } => format!("Purpur build {build}"),
ServerType::Fabric { loader, installer } => format!("Fabric v{loader}"),
ServerType::Quilt { loader, installer } => format!("Quilt v{loader}"),
ServerType::Fabric { loader, .. } => format!("Fabric v{loader}"),
ServerType::Quilt { loader, .. } => format!("Quilt v{loader}"),
ServerType::NeoForge { loader } => format!("NeoForge v{loader}"),
ServerType::Forge { loader } => format!("Forge v{loader}"),
ServerType::BuildTools { software, args } => format!("(BuildTools) {software}"),
ServerType::BuildTools { software, .. } => format!("(BuildTools) {software}"),
ServerType::Paper { } => format!("Paper"),

Check failure on line 316 in src/model/servertype/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

useless use of `format!`

error: useless use of `format!` --> src/model/servertype/mod.rs:316:39 | 316 | ServerType::Paper { } => format!("Paper"), | ^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `"Paper".to_string()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_format
ServerType::Velocity { } => format!("Velocity"),

Check failure on line 317 in src/model/servertype/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

useless use of `format!`

error: useless use of `format!` --> src/model/servertype/mod.rs:317:42 | 317 | ServerType::Velocity { } => format!("Velocity"), | ^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `"Velocity".to_string()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_format
ServerType::Waterfall { } => format!("Waterfall"),

Check failure on line 318 in src/model/servertype/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

useless use of `format!`

error: useless use of `format!` --> src/model/servertype/mod.rs:318:43 | 318 | ServerType::Waterfall { } => format!("Waterfall"), | ^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `"Waterfall".to_string()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_format
Expand All @@ -326,10 +332,10 @@ impl Resolvable for ServerType {
ServerType::PaperMC { project, build } => app.papermc().resolve_source(project, version, build).await,
ServerType::Purpur { build } => app.purpur().resolve_source(version, build).await,
ServerType::Fabric { loader, installer } => app.fabric().resolve_source(loader, installer).await,
ServerType::Quilt { loader, installer } => app.quilt().resolve_installer(installer).await,
ServerType::Quilt { installer, .. } => app.quilt().resolve_installer(installer).await,
ServerType::NeoForge { loader } => app.neoforge().resolve_source(loader).await,
ServerType::Forge { loader } => app.forge().resolve_source(loader).await,
ServerType::BuildTools { software, args } => buildtools().resolve_source(app).await,
ServerType::BuildTools { .. } => buildtools().resolve_source(app).await,
ServerType::Paper { } => app.papermc().resolve_source("paper", version, "latest").await,
ServerType::Velocity { } => app.papermc().resolve_source("velocity", version, "latest").await,
ServerType::Waterfall { } => app.papermc().resolve_source("waterfall", version, "latest").await,
Expand Down

0 comments on commit 968004a

Please sign in to comment.