Skip to content

Commit

Permalink
fix: don't pollute stdout when running godot
Browse files Browse the repository at this point in the history
Fixes issues with scripts expecting to operate off of Godot's output.
  • Loading branch information
adalinesimonian committed Jan 18, 2025
1 parent adc0a20 commit 7d673bb
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 117 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ on:
paths-ignore:
- "gh-pages/**"

concurrency:
group: main
cancel-in-progress: true

jobs:
build-and-test:
runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -61,6 +65,16 @@ jobs:
shell: bash
run: scripts/find-missing-i18n.sh

- name: Check formatting
run: cargo fmt -- --check

- name: Check clippy
run: |
cargo clippy --all-targets --all-features -- \
-D clippy::suspicious -D clippy::style -D clippy::complexity \
-D clippy::perf -D clippy::dbg_macro -D clippy::todo \
-D clippy::unimplemented -D warnings
- name: Test
if: always() && (matrix.os == 'ubuntu-latest' && matrix.target == 'x86_64-unknown-linux-gnu') || (matrix.os == 'windows-latest' && matrix.target == 'x86_64-pc-windows-msvc') || (matrix.os == 'macos-latest' && matrix.target == 'x86_64-apple-darwin')
run: cargo test --target ${{ matrix.target }}
14 changes: 14 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ on:
paths-ignore:
- "gh-pages/**"

concurrency:
group: pr-${{ github.event.number }}
cancel-in-progress: true

jobs:
build-and-test:
runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -61,6 +65,16 @@ jobs:
shell: bash
run: scripts/find-missing-i18n.sh

- name: Check formatting
run: cargo fmt -- --check

- name: Check clippy
run: |
cargo clippy --all-targets --all-features -- \
-D clippy::suspicious -D clippy::style -D clippy::complexity \
-D clippy::perf -D clippy::dbg_macro -D clippy::todo \
-D clippy::unimplemented -D warnings
- name: Test
if: always() && (matrix.os == 'ubuntu-latest' && matrix.target == 'x86_64-unknown-linux-gnu') || (matrix.os == 'windows-latest' && matrix.target == 'x86_64-pc-windows-msvc') || (matrix.os == 'macos-latest' && matrix.target == 'x86_64-apple-darwin')
run: cargo test --target ${{ matrix.target }}
12 changes: 12 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ jobs:
run: scripts/find-missing-i18n.sh
continue-on-error: true

- name: Check formatting
run: cargo fmt -- --check
continue-on-error: true

- name: Check clippy
run: |
cargo clippy --all-targets --all-features -- \
-D clippy::suspicious -D clippy::style -D clippy::complexity \
-D clippy::perf -D clippy::dbg_macro -D clippy::todo \
-D clippy::unimplemented -D warnings
continue-on-error: true

- name: Test
if: (matrix.os == 'ubuntu-latest' && matrix.target == 'x86_64-unknown-linux-gnu') || (matrix.os == 'windows-latest' && matrix.target == 'x86_64-pc-windows-msvc') || (matrix.os == 'macos-latest' && matrix.target == 'x86_64-apple-darwin')
run: cargo test --target ${{ matrix.target }}
Expand Down
23 changes: 22 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,26 @@
},
"search.exclude": {
"**/target": true
}
},
"rust-analyzer.check.command": "clippy",
"rust-analyzer.check.extraArgs": [
"--no-default-features",
"--",
"-D",
"clippy::suspicious",
"-D",
"clippy::style",
"-D",
"clippy::complexity",
"-D",
"clippy::perf",
"-D",
"clippy::dbg_macro",
"-D",
"clippy::todo",
"-D",
"clippy::unimplemented",
"-D",
"warnings"
]
}
2 changes: 1 addition & 1 deletion scripts/find-missing-i18n.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ $rsFiles = Get-ChildItem -Path "$ScriptDir\..\src" -Recurse -Filter *.rs
$keys = @()

# Define the regex pattern with single-line and multi-line options
$pattern = '(?:i18n\.t(?:_args)?(?:_w)?\s*\(\s*|println_i18n!\s*\(\s*[^,\s]+,\s*)"([^"\\]*(?:\\.[^"\\]*)*)"'
$pattern = '(?:i18n\.t(?:_args)?(?:_w)?\s*\(\s*|[xe]?println_i18n!\s*\(\s*[^,\s]+,\s*)"([^"\\]*(?:\\.[^"\\]*)*)"'

foreach ($file in $rsFiles) {
# Read the entire file content as a single string
Expand Down
2 changes: 1 addition & 1 deletion scripts/find-missing-i18n.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck disable=SC2016
strings=$(find "$SCRIPT_DIR/../src" -type f -name '*.rs' -print0 | \
xargs -0 perl -0777 -ne '
while (/(?:i18n\.t(?:_args)?(?:_w)?\s*\(\s*|println_i18n!\s*\(\s*[^,\s]+,\s*)"([^"\\]*(?:\\.[^"\\]*)*)"/g) {
while (/(?:i18n\.t(?:_args)?(?:_w)?\s*\(\s*|[xe]?println_i18n!\s*\(\s*[^,\s]+,\s*)"([^"\\]*(?:\\.[^"\\]*)*)"/g) {
print "$1\n";
}
' | sort | uniq)
Expand Down
4 changes: 2 additions & 2 deletions src/download_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ use std::{

use indicatif::{ProgressBar, ProgressStyle};

use crate::{i18n::I18n, println_i18n};
use crate::{eprintln_i18n, i18n::I18n};

pub fn download_file(url: &str, dest: &Path, i18n: &I18n) -> Result<()> {
// Print downloading URL message
println_i18n!(i18n, "operation-downloading-url", [("url", url)]);
eprintln_i18n!(i18n, "operation-downloading-url", [("url", url)]);

let response = reqwest::blocking::get(url)?;

Expand Down
66 changes: 33 additions & 33 deletions src/godot_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use crate::download_utils::download_file;
use crate::println_i18n;
use crate::version_utils;
use crate::version_utils::GodotVersionDeterminate;
use crate::zip_utils;
use crate::{eprintln_i18n, println_i18n};
use crate::{i18n, project_version_detector};

use version_utils::{GodotVersion, GodotVersionDeterminateVecExt};
Expand Down Expand Up @@ -65,6 +65,7 @@ pub struct GodotManager<'a> {
i18n: &'a I18n,
}

#[allow(clippy::unimplemented)]
fn get_archive_name(version: &GodotVersionDeterminate, i18n: &I18n) -> String {
let is_csharp = version.is_csharp == Some(true);
let tag = version.to_remote_str();
Expand Down Expand Up @@ -151,8 +152,8 @@ fn verify_sha512(
);
let sum_file = file_path.with_extension("SHA512-SUMS");

if let Err(_) = download_file(&sha_url, &sum_file, &i18n) {
println_i18n!(i18n, "warning-sha-sums-missing");
if download_file(&sha_url, &sum_file, i18n).is_err() {
eprintln_i18n!(i18n, "warning-sha-sums-missing");
return Ok(());
}

Expand Down Expand Up @@ -238,7 +239,7 @@ fn find_godot_executable(version_dir: &Path, console: bool) -> Result<Option<Pat
}
});

return Ok(exe_candidate);
Ok(exe_candidate)
}

#[cfg(target_os = "macos")]
Expand All @@ -253,7 +254,7 @@ fn find_godot_executable(version_dir: &Path, console: bool) -> Result<Option<Pat
}
});

return Ok(app_candidate);
Ok(app_candidate)
}

#[cfg(not(any(target_os = "windows", target_os = "macos")))]
Expand All @@ -273,7 +274,7 @@ fn find_godot_executable(version_dir: &Path, console: bool) -> Result<Option<Pat
}
});

return Ok(unix_candidate);
Ok(unix_candidate)
}
}

Expand Down Expand Up @@ -343,7 +344,7 @@ impl<'a> GodotManager<'a> {

if std::env::current_exe().ok() != Some(exe_path.clone()) {
if let Err(err) = copy_over_binary_if_different(&gdvm_exe_source, &exe_path) {
println_i18n!(
eprintln_i18n!(
self.i18n,
"error-ensure-godot-binaries-failed",
[
Expand Down Expand Up @@ -380,8 +381,8 @@ impl<'a> GodotManager<'a> {

if version_path.exists() {
if force {
self.remove(&gv)?;
println_i18n!(
self.remove(gv)?;
eprintln_i18n!(
self.i18n,
"force-reinstalling-version",
[("version", gv.to_display_str())]
Expand All @@ -392,7 +393,7 @@ impl<'a> GodotManager<'a> {
}

if !gv.is_stable() {
println_i18n!(
eprintln_i18n!(
self.i18n,
"warning-prerelease",
[("branch", &gv.release_type)]
Expand All @@ -402,15 +403,15 @@ impl<'a> GodotManager<'a> {
fs::create_dir_all(&self.cache_path)?;

let release_tag = gv.to_remote_str();
let archive_name = get_archive_name(&gv, self.i18n);
let archive_name = get_archive_name(gv, self.i18n);
let download_url = get_download_url(&release_tag, &archive_name);
let cache_zip_path = self.cache_path.join(&archive_name);

if !redownload && cache_zip_path.exists() {
println_i18n!(self.i18n, "using-cached-zip");
eprintln_i18n!(self.i18n, "using-cached-zip");
} else {
if redownload && cache_zip_path.exists() {
println_i18n!(
eprintln_i18n!(
self.i18n,
"force-redownload",
[("version", gv.to_display_str())]
Expand All @@ -422,12 +423,12 @@ impl<'a> GodotManager<'a> {
.join(format!("{}.zip", gv.to_install_str()));

// Download the archive
download_file(&download_url, &tmp_file, &self.i18n)?;
verify_sha512(&tmp_file, &archive_name, &release_tag, &self.i18n)?;
download_file(&download_url, &tmp_file, self.i18n)?;
verify_sha512(&tmp_file, &archive_name, &release_tag, self.i18n)?;

// Move the verified zip to cache_dir
fs::rename(&tmp_file, &cache_zip_path)?;
println_i18n!(self.i18n, "cached-zip-stored");
eprintln_i18n!(self.i18n, "cached-zip-stored");
}

fs::create_dir_all(&version_path)?;
Expand All @@ -445,9 +446,8 @@ impl<'a> GodotManager<'a> {
let entry = entry?;
if entry.file_type()?.is_dir() {
let name = entry.file_name().to_string_lossy().to_string();
match GodotVersion::from_install_str(&name) {
Ok(gv) => versions.push(gv.to_determinate()),
Err(_) => (), // Ignore invalid version directories
if let Ok(gv) = GodotVersion::from_install_str(&name) {
versions.push(gv.to_determinate())
}
}
}
Expand Down Expand Up @@ -478,7 +478,7 @@ impl<'a> GodotManager<'a> {
&self,
gv: &GodotVersionDeterminate,
console: bool,
godot_args: &Vec<String>,
godot_args: &[String],
) -> Result<()> {
let version_dir = self.install_path.join(gv.to_install_str());
if !version_dir.exists() {
Expand Down Expand Up @@ -525,7 +525,7 @@ impl<'a> GodotManager<'a> {
// "No main scene set" message.
let current_dir = std::env::current_dir()?;
let project_file = current_dir.join("project.godot");
let mut godot_args = godot_args.clone();
let mut godot_args = godot_args.to_vec();
if project_file.exists() && godot_args.iter().all(|arg| !Path::new(arg).exists()) {
godot_args.push(project_file.to_string_lossy().to_string());
}
Expand Down Expand Up @@ -857,7 +857,7 @@ impl<'a> GodotManager<'a> {
let target_dir = self.install_path.join(version_str);

// Make sure bin directory exists
fs::create_dir_all(&symlink_dir.parent().unwrap())?;
fs::create_dir_all(symlink_dir.parent().unwrap())?;

if symlink_dir.exists() {
fs::remove_dir_all(&symlink_dir)?;
Expand Down Expand Up @@ -923,7 +923,7 @@ impl<'a> GodotManager<'a> {
pub fn determine_version(&self) -> Option<GodotVersion> {
let current_dir = std::env::current_dir().ok()?;

return project_version_detector::detect_godot_version_in_path(&self.i18n, current_dir);
project_version_detector::detect_godot_version_in_path(self.i18n, current_dir)
}

/// Pin a version to .gdvmrc in the current directory
Expand Down Expand Up @@ -954,7 +954,7 @@ impl<'a> GodotManager<'a> {

// Check if version is installed, if not, install
if !self.is_version_installed(&actual_version)? {
println_i18n!(
eprintln_i18n!(
self.i18n,
"auto-installing-version",
[("version", &actual_version.to_display_str())]
Expand Down Expand Up @@ -1023,10 +1023,10 @@ impl<'a> GodotManager<'a> {
progress.finish_and_clear();

if let Some(new_version) = new_version {
print!("\x1b[1;32m"); // Bold and green
println_i18n!(self.i18n, "upgrade-available", [("version", &new_version)]);
print!("\x1b[0m"); // Reset
println!();
eprint!("\x1b[1;32m"); // Bold and green
eprintln_i18n!(self.i18n, "upgrade-available", [("version", &new_version)]);
eprint!("\x1b[0m"); // Reset
eprintln!();

self.save_gdvm_cache(&GdvmCache {
last_update_check: now,
Expand All @@ -1042,14 +1042,14 @@ impl<'a> GodotManager<'a> {
if let Ok(new_version) = Version::parse(new_version.trim_start_matches('v')) {
let current_version = Version::parse(env!("CARGO_PKG_VERSION"))?;
if new_version > current_version {
print!("\x1b[1;32m"); // Bold and green
println_i18n!(
eprint!("\x1b[1;32m"); // Bold and green
eprintln_i18n!(
self.i18n,
"upgrade-available",
[("version", new_version.to_string())]
);
print!("\x1b[0m"); // Reset
println!();
eprint!("\x1b[0m"); // Reset
eprintln!();
} else {
self.save_gdvm_cache(&GdvmCache {
last_update_check: now,
Expand Down Expand Up @@ -1121,7 +1121,7 @@ impl<'a> GodotManager<'a> {

// Download the new binary
if let Err(err) = download_file(&bin_url, &out_file, self.i18n) {
println_i18n!(self.i18n, "upgrade-download-failed");
eprintln_i18n!(self.i18n, "upgrade-download-failed");
return Err(err);
}

Expand Down
Loading

0 comments on commit 7d673bb

Please sign in to comment.