Skip to content

Commit

Permalink
feat: add support for custom compression options
Browse files Browse the repository at this point in the history
Signed-off-by: eXhumer <[email protected]>
  • Loading branch information
eXhumer committed Oct 5, 2024
1 parent 7237ba7 commit 5bb2808
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 12 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
outputs:
version: ${{ steps.version.outputs.version }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Rust cache for ${{ matrix.platform.release_for }}
uses: Swatinem/rust-cache@v2
Expand All @@ -58,7 +58,7 @@ jobs:
strip: true

- name: Upload binary
uses: actions/upload-artifact@v3.1.2
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.platform.name }}
path: target/${{ matrix.platform.target }}/release/decky
Expand All @@ -70,7 +70,7 @@ jobs:
contents: write
if: github.event_name != 'pull_request'
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Grab version
id: version
Expand All @@ -79,7 +79,7 @@ jobs:
echo "Version code is $version"
echo "version=$version" >> $GITHUB_OUTPUT
- uses: actions/download-artifact@v4.1.7
- uses: actions/download-artifact@v4
with:
path: artifacts

Expand Down
28 changes: 27 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ sha2 = "0.10.7"
tokio = { version = "1.24.2", features = ["full"] }
users = "0.11.0"
walkdir = "2.3.2"
zip = { version = "0.6.3", default-features = false }
zip = { version = "0.6.3", default-features = false, features = ["deflate"] }
which = "4.4.0"
dirs = "5"
18 changes: 18 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ impl ContainerEngine {
}


#[derive(clap::ValueEnum, Clone)]
pub enum CompressMethod {
Deflate,
Store,
}

#[derive(Subcommand)]
pub enum Command {
Plugin(PluginCLI),
Expand Down Expand Up @@ -70,6 +76,12 @@ pub enum PluginCommand {

#[arg(short = 'e', long = "engine", default_value = "docker")]
container_engine: ContainerEngine,

#[arg(short = 'm', long, default_value = "store")]
compression_method: CompressMethod,

#[arg(short = 'l', long)]
compression_level: Option<i32>,
},
New,
Deploy {
Expand All @@ -94,6 +106,12 @@ pub enum PluginCommand {
#[arg(short = 'e', long = "engine", default_value = "docker")]
container_engine: ContainerEngine,

#[arg(short = 'm', long, default_value = "store")]
compression_method: CompressMethod,

#[arg(short = 'l', long)]
compression_level: Option<i32>,

#[arg(short = 'S', long, default_value = "true")]
follow_symlinks: bool,

Expand Down
27 changes: 22 additions & 5 deletions src/cli/plugin/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ use std::{
path::{Path, PathBuf},
};
use walkdir::WalkDir;
use zip::{write::FileOptions, ZipWriter};
use zip::{write::FileOptions, CompressionMethod, ZipWriter};

use crate::{
cli::{FilenameSource, ContainerEngine},
cli::{CompressMethod, ContainerEngine, FilenameSource},
container_engine,
plugin::{CustomBackend, Plugin},
};
Expand All @@ -35,6 +35,8 @@ pub struct Builder {
pub follow_symlinks: bool,
pub output_filename_source: FilenameSource,
pub container_engine: ContainerEngine,
pub compression_method: CompressMethod,
pub compression_level: Option<i32>,
}

impl Builder {
Expand Down Expand Up @@ -216,7 +218,7 @@ impl Builder {
filename: &str,
path: PathBuf,
zip: &mut ZipWriter<File>,
perms: FileOptions,
opts: FileOptions,
) -> Result<()> {
let name = path
.strip_prefix(&self.tmp_build_root)
Expand All @@ -233,11 +235,22 @@ impl Builder {
if path.is_file() {
let bytes = std::fs::read(&path).unwrap();

zip.start_file(name.to_str().unwrap(), perms)?;
let method = match self.compression_method {
CompressMethod::Deflate => CompressionMethod::Deflated,
CompressMethod::Store => CompressionMethod::Stored,
};

let mut opts = opts.compression_method(method);

if method == CompressionMethod::Deflated && self.compression_level.is_some() {
opts = opts.compression_level(Some(self.compression_level.unwrap()));
}

zip.start_file(name.to_str().unwrap(), opts)?;

zip.write_all(&bytes)?;
} else if !name.as_os_str().is_empty() {
zip.add_directory(name.to_str().unwrap(), perms)?;
zip.add_directory(name.to_str().unwrap(), opts)?;
}

Ok(())
Expand Down Expand Up @@ -395,6 +408,8 @@ impl Builder {
follow_symlinks: bool,
output_filename_source: FilenameSource,
container_engine: ContainerEngine,
compression_method: CompressMethod,
compression_level: Option<i32>,
) -> Result<Self> {
if !output_root.exists() {
std::fs::create_dir(&output_root)?;
Expand All @@ -421,6 +436,8 @@ impl Builder {
follow_symlinks,
output_filename_source,
container_engine,
compression_method,
compression_level,
})
}
}
5 changes: 5 additions & 0 deletions src/cli/plugin/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use log::info;
use rand::distributions::{Alphanumeric, DistString};

use crate::cli::plugin::build::Builder;
use crate::cli::CompressMethod;
use crate::plugin::DeckFile;
use crate::{cli::FilenameSource, cli::ContainerEngine, plugin::Plugin};

Expand Down Expand Up @@ -231,6 +232,8 @@ impl Deployer {
follow_symlinks: bool,
output_filename_source: FilenameSource,
container_engine: ContainerEngine,
compression_method: CompressMethod,
compression_level: Option<i32>,
deck_ip: Option<String>,
deck_port: Option<String>,
deck_pass: Option<String>,
Expand All @@ -248,6 +251,8 @@ impl Deployer {
follow_symlinks,
output_filename_source,
container_engine,
compression_method,
compression_level,
)
.expect("Could not create builder");

Expand Down
10 changes: 9 additions & 1 deletion src/cli/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ pub async fn parse(args: &PluginCLI) -> Result<()> {
build_with_dev,
follow_symlinks,
output_filename_source,
container_engine
container_engine,
compression_method,
compression_level,
} => {
build::Builder::new(
plugin_path.into(),
Expand All @@ -25,6 +27,8 @@ pub async fn parse(args: &PluginCLI) -> Result<()> {
follow_symlinks.clone(),
output_filename_source.clone(),
container_engine.clone(),
compression_method.clone(),
compression_level.clone(),
)?
.run()
.await
Expand All @@ -44,6 +48,8 @@ pub async fn parse(args: &PluginCLI) -> Result<()> {
deck_pass,
deck_key,
deck_dir,
compression_method,
compression_level,
} => {
deploy::Deployer::new(
plugin_path.into(),
Expand All @@ -54,6 +60,8 @@ pub async fn parse(args: &PluginCLI) -> Result<()> {
follow_symlinks.clone(),
output_filename_source.clone(),
container_engine.clone(),
compression_method.clone(),
compression_level.clone(),
deck_ip.clone(),
deck_port.clone(),
deck_pass.clone(),
Expand Down

0 comments on commit 5bb2808

Please sign in to comment.