Skip to content

Commit

Permalink
style: Format code, add documentation and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
oissevalt committed Jul 31, 2024
1 parent d996b76 commit a1a96f3
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 17 deletions.
34 changes: 30 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
# Sealupd

Updater for [SealDice](https://github.com/sealdice), successor of [seal-updater](https://github.com/sealdice/seal-updater).
Updater for [SealDice], successor of [seal-updater].

# Usage

The program accepts to principal arguments: `--package` (short `-p`, alias `--upgrade`) and `--pid`. It waits for the process with the specified PID to terminate before extracting files from the provided package into the current directory. Finally, it tries to start the executable named `sealdice-core` or `sealdice-core.exe` unless the `--skip-launch` flag is set.

For a full definition of acceptable arguments and flags, see `src/cli.rs`.

# Development

> [!WARNING]
> Please use Rust 1.80.0 and above to build this project, as it uses new features such as `std::sync::LazyLock`.
> Please use Rust 1.80.0 and above to build this project, as it leverages new features such as `std::sync::LazyLock`.
## Style Guides

1. Use `cargo fmt` without any options to format the project. Do not use `rustfmt` as it formats on a per-file basis and removes disambiguate imports.

2. Import types directly; prefer `File` instead of `std::fs::File` or `fs::File`, unless ambiguity forbids (`Error` and `io::Error`).

3. Import functions by their immediate parent module; prefer `io::stdin()` instead of `std::io::stdin()` or `stdin()`.

## TODO

- [x] Implement business logic.
- [x] Add log information.
- [x] Add manifest and static build for Windows (via `embed_manifest` and `static_vcruntime`).
- [ ] CI/CD
- [ ] Tests
- [ ] CI/CD & auto-release
- [ ] Tests
- [ ] Make [SealDice] migrate from the old [seal-updater].

## Other Notes

- We might need a graceful way to log and print information rather than littering `debug!()` and `println!()` everywhere.

- The current implementation of `proc::wait()` is the best we can do, but feedback we got from [seal-updater] proves it is not always reliable.

[SealDice]: https://github.com/sealdice
[seal-updater]: https://github.com/sealdice/seal-updater
2 changes: 2 additions & 0 deletions src/decompress/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
};

use ::zip::result::ZipError;
use log::info;

mod tar;
mod util;
Expand All @@ -28,6 +29,7 @@ where
Err(format!("decompress zip: {}", err))?;
}

info!("Invalid ZIP archive detected, falling back to tarball.");
file.seek(SeekFrom::Start(0))?;
tar::decompress(file, dest).map_err(|e| format!("decompress tar: {}", e))?;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod cli;
mod consts;
mod decompress;
mod log;
mod proc_wait;
mod proc;
mod term_color;
mod util;

Expand All @@ -18,7 +18,7 @@ fn main() {
if CLI_ARGS.pid != 0 {
debug!("Starting to wait for process {}.", CLI_ARGS.pid);
println!("Waiting for process {} to finish.", CLI_ARGS.pid);
proc_wait::wait(CLI_ARGS.pid);
proc::wait(CLI_ARGS.pid);
}

debug!("Backing up old sealdice-core.");
Expand Down
File renamed without changes.
24 changes: 13 additions & 11 deletions src/term_color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ pub struct ColoredString {
impl ColoredString {
/// Creates a new [`ColoredString`] with an initial color code.
/// If the code is 0, it will be ignored.
pub fn new(s: &str, code: i32) -> Self {
let mut vec = Vec::new();
if code != 0 {
pub fn new(s: &str, codes: &[i32]) -> Self {
let mut vec = Vec::with_capacity(codes.len());
for &code in codes {
vec.push(code);
}

Expand All @@ -29,8 +29,10 @@ impl ColoredString {
}
}

fn append_color(&mut self, code: i32) {
self.codes.push(code);
fn append_colors(&mut self, codes: &[i32]) {
for &code in codes {
self.codes.push(code);
}
}
}

Expand Down Expand Up @@ -63,31 +65,31 @@ pub trait Colorable {
// Implementing for `&str` also implements for `String`.
impl<'a> Colorable for &'a str {
fn error(self) -> ColoredString {
ColoredString::new(self, 31)
ColoredString::new(self, &[31, 1])
}

fn warn(self) -> ColoredString {
ColoredString::new(self, 33)
ColoredString::new(self, &[33])
}

fn success(self) -> ColoredString {
ColoredString::new(self, 32)
ColoredString::new(self, &[32, 1])
}
}

impl Colorable for ColoredString {
fn error(mut self) -> ColoredString {
self.append_color(31);
self.append_colors(&[31, 1]);
self
}

fn warn(mut self) -> ColoredString {
self.append_color(33);
self.append_colors(&[33]);
self
}

fn success(mut self) -> ColoredString {
self.append_color(32);
self.append_colors(&[32, 1]);
self
}
}
3 changes: 3 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub fn backup_sealdice() -> Result<(), io::Error> {
fs::rename(cwd.join(EXE_NAME), cwd.join(old))
}

/// Attempts to run `chmod 755` on `sealdice-core` and then start it. Exit the program
/// if anything goes wrong.
#[cfg(target_family = "unix")]
pub fn restart_sealdice() {
use std::os::unix::{fs::PermissionsExt, process::CommandExt};
Expand Down Expand Up @@ -75,6 +77,7 @@ pub fn restart_sealdice() {
graceful_exit(1);
}

/// Attempts to start `sealdice-core`. Exit the program if anything goes wrong.
#[cfg(target_family = "windows")]
pub fn restart_sealdice() {
let dest = Path::new(".").join(EXE_NAME);
Expand Down

0 comments on commit a1a96f3

Please sign in to comment.