Skip to content

Commit 444c6c1

Browse files
committed
refactor(cli): use early returns instead of elses
1 parent 30a5b52 commit 444c6c1

6 files changed

Lines changed: 82 additions & 67 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ Use a mask to assign an icon to a folder:
2929
folderify mask.png /path/to/folder
3030
```
3131

32+
Use the same mask for multiple folders in one run:
33+
34+
```shell
35+
folderify mask.png /path/to/folder-1 /path/to/folder-2 /path/to/folder-3
36+
```
37+
3238
Generate `mask.icns` and `mask.iconset` files:
3339

3440
```shell

src/args.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ struct FolderifyArgs {
2323
#[clap(verbatim_doc_comment)]
2424
mask: Option<PathBuf>,
2525

26-
/// Target file or folder. If a target is specified, the resulting icon will
27-
/// be applied to the target file/folder. Else (unless --output-icns or
26+
/// Target files or folders. If any targets are specified, the resulting icon
27+
/// will be applied to each target. Else (unless --output-icns or
2828
/// --output-iconset is specified), a .iconset folder and .icns file will be
2929
/// created in the same folder as the mask (you can use "Get Info" in Finder
3030
/// to copy the icon from the .icns file).
3131
#[clap(verbatim_doc_comment)]
32-
target: Option<PathBuf>,
32+
targets: Vec<PathBuf>,
3333

3434
/// Write the `.icns` file to the given path.
3535
/// (Will be written even if a target is also specified.)
@@ -142,7 +142,7 @@ pub struct Options {
142142
pub mask_path: PathBuf,
143143
pub color_scheme: ColorScheme,
144144
pub no_trim: bool,
145-
pub target: Option<PathBuf>,
145+
pub targets: Vec<PathBuf>,
146146
pub folder_style: FolderStyle,
147147
pub empty_folder: bool,
148148
pub output_icns: Option<PathBuf>,
@@ -263,7 +263,7 @@ pub fn get_options() -> Options {
263263
mask_path: mask,
264264
color_scheme: map_color_scheme_auto(args.color_scheme, folder_style),
265265
no_trim: args.no_trim,
266-
target: args.target,
266+
targets: args.targets,
267267
folder_style,
268268
empty_folder: args.empty_folder,
269269
output_icns: args.output_icns,

src/icon_conversion.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ impl ProgressBarType {
2323
ProgressBarType::Input => 1,
2424
ProgressBarType::Conversion => 13 + if options.badge.is_some() { 1 } else { 0 },
2525
ProgressBarType::OutputWithAssignment => {
26-
2 + if matches!(options.set_icon_using, SetIconUsing::Rez) {
27-
7
28-
} else {
29-
0
30-
}
26+
let assignment_steps = match options.set_icon_using {
27+
SetIconUsing::Rez => 7,
28+
_ => 1,
29+
};
30+
1 + assignment_steps * options.targets.len() as u64
3131
}
3232
ProgressBarType::OutputIcns => 1,
3333
}
@@ -643,20 +643,21 @@ impl IconConversion {
643643
let args = CommandArgs::new();
644644
run_command(OSASCRIPT_COMMAND, &args, Some(stdin.as_bytes()))?;
645645

646-
if target_metadata.is_dir() {
647-
// TODO: check for network volume first, only then the appropriate path.
648-
if metadata(target_path.join("Icon\r")).is_err()
649-
&& metadata(target_path.join(".VolumeIcon.icns")).is_err()
650-
{
651-
eprintln!("Icon was not successfully assigned to the target folder.");
652-
exit(1);
653-
}
654-
} else if options.target.is_some() {
646+
if !target_metadata.is_dir() {
655647
// TODO: this is usually overwritten by the progress bars.
656648
eprintln!(
657649
"Target is not a folder. Please check manually if the icon was assigned correctly."
658650
);
659-
};
651+
return Ok(());
652+
}
653+
654+
// TODO: check for network volume first, only then the appropriate path.
655+
if metadata(target_path.join("Icon\r")).is_err()
656+
&& metadata(target_path.join(".VolumeIcon.icns")).is_err()
657+
{
658+
eprintln!("Icon was not successfully assigned to the target folder.");
659+
exit(1);
660+
}
660661

661662
Ok(())
662663
}

src/main.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,10 @@ fn main() {
9292
handles.push(handle);
9393
}
9494

95-
let output_iconset_only = match (
96-
&options.target,
97-
&options.output_icns,
98-
&options.output_iconset,
99-
) {
100-
(None, None, Some(output_iconset)) => Some(output_iconset),
101-
_ => None,
102-
};
95+
let mut output_iconset_only = None;
96+
if options.targets.is_empty() && options.output_icns.is_none() {
97+
output_iconset_only = options.output_iconset.as_ref();
98+
}
10399

104100
// Deduplicate this `match` with the one that happens after handle joining.
105101
let output_progress_bar_type = match output_iconset_only {
@@ -133,20 +129,16 @@ fn main() {
133129
)
134130
.unwrap();
135131

136-
let icns_assignment_path = options
137-
.target
138-
.as_ref()
139-
.unwrap_or(&final_output_paths.icns_path);
140-
141-
output_icon_conversion
142-
.assign_icns(
143-
&options,
144-
&final_output_paths.icns_path,
145-
icns_assignment_path,
146-
)
147-
.unwrap();
132+
for target in &options.targets {
133+
output_icon_conversion
134+
.assign_icns(&options, &final_output_paths.icns_path, target)
135+
.unwrap();
136+
}
148137

149-
icns_assignment_path
138+
options
139+
.targets
140+
.first()
141+
.unwrap_or(&final_output_paths.icns_path)
150142
}
151143
};
152144

src/output_paths.rs

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,47 @@ impl PotentialOutputPaths {
2020
iconset_dir: None,
2121
icns_path: None,
2222
};
23-
match (
24-
&options.target,
25-
&options.output_iconset,
26-
&options.output_icns,
27-
) {
28-
(Some(target), output_iconset, output_icns) => {
23+
if !options.targets.is_empty() {
24+
for target in &options.targets {
2925
println!(
3026
"[{}] => assign to [{}]",
3127
options.mask_path.display(),
3228
target.display()
3329
);
34-
Self::alt_outputs(options, &mut output_paths, output_iconset, output_icns);
35-
}
36-
(None, None, None) => {
37-
let iconset_dir_value = options.mask_path.with_extension("iconset");
38-
let icns_path_value = options.mask_path.with_extension("icns");
39-
println!(
40-
"[{}] => [{}]",
41-
options.mask_path.display(),
42-
iconset_dir_value.display()
43-
);
44-
println!(
45-
"[{}] => [{}]",
46-
options.mask_path.display(),
47-
icns_path_value.display()
48-
);
49-
output_paths.iconset_dir = Some(iconset_dir_value);
50-
output_paths.icns_path = Some(icns_path_value);
51-
}
52-
(None, output_iconset, output_icns) => {
53-
Self::alt_outputs(options, &mut output_paths, output_iconset, output_icns);
5430
}
31+
Self::alt_outputs(
32+
options,
33+
&mut output_paths,
34+
&options.output_iconset,
35+
&options.output_icns,
36+
);
37+
return output_paths;
38+
}
39+
40+
if options.output_iconset.is_none() && options.output_icns.is_none() {
41+
let iconset_dir_value = options.mask_path.with_extension("iconset");
42+
let icns_path_value = options.mask_path.with_extension("icns");
43+
println!(
44+
"[{}] => [{}]",
45+
options.mask_path.display(),
46+
iconset_dir_value.display()
47+
);
48+
println!(
49+
"[{}] => [{}]",
50+
options.mask_path.display(),
51+
icns_path_value.display()
52+
);
53+
output_paths.iconset_dir = Some(iconset_dir_value);
54+
output_paths.icns_path = Some(icns_path_value);
55+
return output_paths;
5556
}
57+
58+
Self::alt_outputs(
59+
options,
60+
&mut output_paths,
61+
&options.output_iconset,
62+
&options.output_icns,
63+
);
5664
output_paths
5765
}
5866

test/test-behaviour.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ test("Assign folder icon", async () => {
5959
expect(await tempDir.join("Icon\r").exists()).toBe(true);
6060
});
6161

62+
test("Assign the same folder icon to multiple targets", async () => {
63+
const tempDir1 = await Path.makeTempDir();
64+
const tempDir2 = await Path.makeTempDir();
65+
await shellOut([EXAMPLES.join("src/apple.png"), tempDir1, tempDir2]);
66+
expect(await tempDir1.join("Icon\r").exists()).toBe(true);
67+
expect(await tempDir2.join("Icon\r").exists()).toBe(true);
68+
});
69+
6270
test("Assign folder icon using Rez", async () => {
6371
const tempDir = await Path.makeTempDir();
6472
await shellOut([

0 commit comments

Comments
 (0)