Skip to content

Commit 809b0fb

Browse files
committed
refactor(build): extract common file path and extraction logic to lib
Move repeated file path construction and extraction checks into shared library functions Add new utility functions for target path generation and line ending normalization
1 parent 14b1e6c commit 809b0fb

4 files changed

Lines changed: 81 additions & 68 deletions

File tree

‎src/cmd_cbp/build/font.rs‎

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ pub fn execute(args: &ArgMatches) -> anyhow::Result<()> {
6060
.ok_or_else(|| anyhow::anyhow!("Download configuration not found"))?;
6161

6262
let temp_dir = tempfile::tempdir()?;
63-
let temp_file = if let Some(name) = dl_obj.get("download_name").and_then(|v| v.as_str()) {
64-
temp_dir.path().join(name)
65-
} else {
66-
temp_dir.path().join("download.tmp")
67-
};
63+
let temp_file = cbp::temp_download_path(&temp_dir, dl_obj);
6864

6965
// Download font file
7066
let url = dl_obj["url"]
@@ -74,11 +70,7 @@ pub fn execute(args: &ArgMatches) -> anyhow::Result<()> {
7470
cbp::download_file(url, &temp_file, &agent)?;
7571

7672
// Check if extraction is needed
77-
let needs_extract = url.ends_with(".zip")
78-
|| url.ends_with(".tar.gz")
79-
|| url.ends_with(".tar.xz")
80-
|| url.ends_with(".tar.bz2")
81-
|| dl_obj.get("extract").is_some();
73+
let needs_extract = cbp::needs_extract(url, dl_obj);
8274

8375
if needs_extract {
8476
cbp::extract_archive(&temp_dir, &temp_file, dl_obj)?;
@@ -101,17 +93,10 @@ pub fn execute(args: &ArgMatches) -> anyhow::Result<()> {
10193
// Find binary files
10294
let binary_paths = cbp::find_binary_files(temp_dir.path(), dl_obj)?;
10395

104-
// Create final font package
105-
let target_path = base_dir
106-
.canonicalize()?
107-
.join("binaries")
108-
.join(format!("{}.font.tar.gz", pkg))
109-
.display()
110-
.to_string();
96+
let target_path = cbp::target_font_path(&base_dir, pkg)?;
11197
let temp_path = temp_dir.path().canonicalize()?;
11298

113-
// Create binaries directory if it doesn't exist
114-
std::fs::create_dir_all(base_dir.join("binaries"))?;
99+
115100

116101
// Change to temp directory and collect files
117102
run_cmd!(

‎src/cmd_cbp/build/prebuild.rs‎

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,13 @@ pub fn execute(args: &ArgMatches) -> anyhow::Result<()> {
8989
.as_str()
9090
.ok_or_else(|| anyhow::anyhow!("URL not found"))?;
9191

92-
let temp_file = if let Some(name) =
93-
dl_obj.get("download_name").and_then(|v| v.as_str())
94-
{
95-
// Use specified download name
96-
temp_dir.path().join(name)
97-
} else {
98-
temp_dir.path().join("download.tmp")
99-
};
92+
let temp_file = cbp::temp_download_path(&temp_dir, dl_obj);
10093

10194
println!("-> Downloading from {}", url);
10295
cbp::download_file(url, &temp_file, &agent)?;
10396

10497
// Check if extraction is needed
105-
let needs_extract = url.ends_with(".zip")
106-
|| url.ends_with(".tar.gz")
107-
|| url.ends_with(".tar.xz")
108-
|| url.ends_with(".tar.bz2")
109-
|| dl_obj.get("extract").is_some();
98+
let needs_extract = cbp::needs_extract(url, dl_obj);
11099

111100
if needs_extract {
112101
cbp::extract_archive(&temp_dir, &temp_file, dl_obj)?;
@@ -132,14 +121,7 @@ pub fn execute(args: &ArgMatches) -> anyhow::Result<()> {
132121
std::fs::remove_file(&temp_file)?;
133122
}
134123

135-
// Create final package
136-
std::fs::create_dir_all(base_dir.join("binaries"))?;
137-
let target_path = base_dir
138-
.canonicalize()?
139-
.join("binaries")
140-
.join(format!("{}.{}.tar.gz", pkg, os_type))
141-
.display()
142-
.to_string();
124+
let target_path = cbp::target_binary_path(&base_dir, pkg, os_type)?;
143125
let temp_path = temp_dir.path().canonicalize()?;
144126

145127
// Add shebang option if enabled

‎src/cmd_cbp/build/source.rs‎

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,13 @@ pub fn execute(args: &ArgMatches) -> anyhow::Result<()> {
6565
.as_str()
6666
.ok_or_else(|| anyhow::anyhow!("Font URL not found"))?;
6767

68-
let temp_file =
69-
if let Some(name) = dl_obj.get("download_name").and_then(|v| v.as_str()) {
70-
// Use specified download name
71-
temp_dir.path().join(name)
72-
} else {
73-
temp_dir.path().join("download.tmp")
74-
};
68+
let temp_file = cbp::temp_download_path(&temp_dir, dl_obj);
7569

7670
// Process file after download
7771
println!("-> Downloading from {}", url);
7872
cbp::download_file(url, &temp_file, &agent)?;
7973

80-
let target_path = base_dir
81-
.canonicalize()?
82-
.join("sources")
83-
.join(format!("{}.tar.gz", pkg))
84-
.display()
85-
.to_string();
86-
std::fs::create_dir_all(base_dir.join("sources"))?;
74+
let target_path = cbp::target_source_path(&base_dir, pkg)?;
8775

8876
if dl_obj.len() == 1 {
8977
cbp::move_file_or_dir(&temp_file, std::path::Path::new(&target_path))?;
@@ -92,17 +80,13 @@ pub fn execute(args: &ArgMatches) -> anyhow::Result<()> {
9280
}
9381

9482
// Check if extraction is needed
95-
let needs_extract = url.ends_with(".zip")
96-
|| url.ends_with(".tar.gz")
97-
|| url.ends_with(".tar.xz")
98-
|| url.ends_with(".tar.bz2")
99-
|| dl_obj.get("extract").is_some();
83+
let needs_extract = cbp::needs_extract(url, dl_obj);
10084

10185
if needs_extract {
10286
println!("-> Processing source archive");
10387
cbp::extract_archive(&temp_dir, &temp_file, dl_obj)?;
10488
} else {
105-
normalize_line_endings(&temp_file)?;
89+
cbp::normalize_line_endings(&temp_file)?;
10690
}
10791

10892
let temp_path = temp_dir.path().canonicalize()?;
@@ -127,14 +111,7 @@ pub fn execute(args: &ArgMatches) -> anyhow::Result<()> {
127111
Ok(())
128112
}
129113

130-
/// Convert CRLF to LF for text files
131-
fn normalize_line_endings(path: &std::path::Path) -> anyhow::Result<()> {
132-
let content = std::fs::read_to_string(path)?;
133-
let normalized = content.replace("\r\n", "\n");
134-
std::fs::write(path, normalized)?;
135-
println!(" -> Normalized line endings: {}", path.display());
136-
Ok(())
137-
}
114+
138115

139116
// /// Get target directory name for archive
140117
// fn get_target_name(

‎src/libs/build.rs‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,28 @@ pub fn extract_archive(
9191
Ok(())
9292
}
9393

94+
pub fn needs_extract(
95+
url: &str,
96+
json_obj: &serde_json::Map<String, serde_json::Value>,
97+
) -> bool {
98+
url.ends_with(".zip")
99+
|| url.ends_with(".tar.gz")
100+
|| url.ends_with(".tar.xz")
101+
|| url.ends_with(".tar.bz2")
102+
|| json_obj.get("extract").is_some()
103+
}
104+
105+
pub fn temp_download_path(
106+
temp_dir: &tempfile::TempDir,
107+
json_obj: &serde_json::Map<String, serde_json::Value>,
108+
) -> std::path::PathBuf {
109+
if let Some(name) = json_obj.get("download_name").and_then(|v| v.as_str()) {
110+
temp_dir.path().join(name)
111+
} else {
112+
temp_dir.path().join("download.tmp")
113+
}
114+
}
115+
94116
/// Handle file renaming based on package configuration
95117
pub fn handle_rename(
96118
temp_dir: &tempfile::TempDir,
@@ -349,3 +371,50 @@ pub fn fix_shebang(path: &std::path::Path) -> anyhow::Result<()> {
349371

350372
Ok(())
351373
}
374+
375+
pub fn target_binary_path(
376+
base_dir: &std::path::Path,
377+
pkg: &str,
378+
os_type: &str,
379+
) -> anyhow::Result<String> {
380+
std::fs::create_dir_all(base_dir.join("binaries"))?;
381+
let path = base_dir
382+
.canonicalize()?
383+
.join("binaries")
384+
.join(format!("{}.{}.tar.gz", pkg, os_type))
385+
.display()
386+
.to_string();
387+
Ok(path)
388+
}
389+
390+
pub fn target_font_path(base_dir: &std::path::Path, pkg: &str) -> anyhow::Result<String> {
391+
std::fs::create_dir_all(base_dir.join("binaries"))?;
392+
let path = base_dir
393+
.canonicalize()?
394+
.join("binaries")
395+
.join(format!("{}.font.tar.gz", pkg))
396+
.display()
397+
.to_string();
398+
Ok(path)
399+
}
400+
401+
pub fn target_source_path(
402+
base_dir: &std::path::Path,
403+
pkg: &str,
404+
) -> anyhow::Result<String> {
405+
std::fs::create_dir_all(base_dir.join("sources"))?;
406+
let path = base_dir
407+
.canonicalize()?
408+
.join("sources")
409+
.join(format!("{}.tar.gz", pkg))
410+
.display()
411+
.to_string();
412+
Ok(path)
413+
}
414+
415+
pub fn normalize_line_endings(path: &std::path::Path) -> anyhow::Result<()> {
416+
let content = std::fs::read_to_string(path)?;
417+
let normalized = content.replace("\r\n", "\n");
418+
std::fs::write(path, normalized)?;
419+
Ok(())
420+
}

0 commit comments

Comments
 (0)