Skip to content

Commit 6831c2b

Browse files
committed
small refactor
1 parent 5a758d7 commit 6831c2b

File tree

2 files changed

+51
-53
lines changed

2 files changed

+51
-53
lines changed

src/build.rs

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::bsconfig;
22
use crate::bsconfig::OneOrMore;
33
use crate::helpers;
44
use crate::helpers::get_bs_build_path;
5-
use crate::helpers::get_build_path;
65
use crate::helpers::get_package_path;
76
use crate::package_tree;
87
use crate::package_tree::Package;
@@ -15,7 +14,6 @@ use log::{debug, error};
1514
use log::{info, log_enabled};
1615
use rayon::prelude::*;
1716
use std::fs;
18-
use std::fs::File;
1917
use std::io::stdout;
2018
use std::io::Write;
2119
use std::io::{self, BufRead};
@@ -54,6 +52,11 @@ pub struct Module {
5452
pub interface_last_modified: Option<SystemTime>,
5553
}
5654

55+
fn read_lines(filename: String) -> io::Result<io::Lines<io::BufReader<fs::File>>> {
56+
let file = fs::File::open(filename)?;
57+
Ok(io::BufReader::new(file).lines())
58+
}
59+
5760
fn get_res_path_from_ast(ast_file: &str) -> Option<String> {
5861
if let Ok(lines) = read_lines(ast_file.to_string()) {
5962
// we skip the first line with is some null characters
@@ -75,50 +78,6 @@ fn get_res_path_from_ast(ast_file: &str) -> Option<String> {
7578
return None;
7679
}
7780

78-
fn get_compiler_asset(
79-
source_file: &str,
80-
package_name: &str,
81-
namespace: &Option<String>,
82-
root_path: &str,
83-
extension: &str,
84-
) -> String {
85-
let namespace = match extension {
86-
"ast" | "asti" => &None,
87-
_ => namespace,
88-
};
89-
90-
get_build_path(root_path, package_name)
91-
+ "/"
92-
+ &helpers::file_path_to_compiler_asset_basename(source_file, namespace)
93-
+ "."
94-
+ extension
95-
}
96-
97-
fn get_bs_compiler_asset(
98-
source_file: &str,
99-
package_name: &str,
100-
namespace: &Option<String>,
101-
root_path: &str,
102-
extension: &str,
103-
) -> String {
104-
let namespace = match extension {
105-
"ast" | "iast" => &None,
106-
_ => namespace,
107-
};
108-
let dir = std::path::Path::new(source_file)
109-
.strip_prefix(get_package_path(root_path, &package_name))
110-
.unwrap()
111-
.parent()
112-
.unwrap();
113-
114-
std::path::Path::new(&get_bs_build_path(root_path, &package_name))
115-
.join(dir)
116-
.join(helpers::file_path_to_compiler_asset_basename(source_file, namespace) + extension)
117-
.to_str()
118-
.unwrap()
119-
.to_owned()
120-
}
121-
12281
fn remove_compile_assets(
12382
source_file: &str,
12483
package_name: &str,
@@ -129,15 +88,15 @@ fn remove_compile_assets(
12988
// optimization
13089
// only issue cmti if htere is an interfacce file
13190
for extension in &["cmj", "cmi", "cmt", "cmti", "ast", "iast"] {
132-
let _ = std::fs::remove_file(get_compiler_asset(
91+
let _ = std::fs::remove_file(helpers::get_compiler_asset(
13392
source_file,
13493
package_name,
13594
namespace,
13695
root_path,
13796
extension,
13897
));
13998
if ["cmj", "cmi", "cmt", "cmti"].contains(&extension) {
140-
let _ = std::fs::remove_file(get_bs_compiler_asset(
99+
let _ = std::fs::remove_file(helpers::get_bs_compiler_asset(
141100
source_file,
142101
package_name,
143102
namespace,
@@ -343,11 +302,6 @@ fn generate_ast(
343302
ast_path
344303
}
345304

346-
fn read_lines(filename: String) -> io::Result<io::Lines<io::BufReader<File>>> {
347-
let file = File::open(filename)?;
348-
Ok(io::BufReader::new(file).lines())
349-
}
350-
351305
// Namespaces work like the following: The build system will generate a file
352306
// called `MyModule.mlmap` which contains all modules that are in the namespace
353307
//

src/helpers.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,47 @@ pub fn string_ends_with_any(s: &PathBuf, suffixes: &[&str]) -> bool {
130130
== suffix
131131
})
132132
}
133+
134+
pub fn get_compiler_asset(
135+
source_file: &str,
136+
package_name: &str,
137+
namespace: &Option<String>,
138+
root_path: &str,
139+
extension: &str,
140+
) -> String {
141+
let namespace = match extension {
142+
"ast" | "asti" => &None,
143+
_ => namespace,
144+
};
145+
146+
get_build_path(root_path, package_name)
147+
+ "/"
148+
+ &file_path_to_compiler_asset_basename(source_file, namespace)
149+
+ "."
150+
+ extension
151+
}
152+
153+
pub fn get_bs_compiler_asset(
154+
source_file: &str,
155+
package_name: &str,
156+
namespace: &Option<String>,
157+
root_path: &str,
158+
extension: &str,
159+
) -> String {
160+
let namespace = match extension {
161+
"ast" | "iast" => &None,
162+
_ => namespace,
163+
};
164+
let dir = std::path::Path::new(source_file)
165+
.strip_prefix(get_package_path(root_path, &package_name))
166+
.unwrap()
167+
.parent()
168+
.unwrap();
169+
170+
std::path::Path::new(&get_bs_build_path(root_path, &package_name))
171+
.join(dir)
172+
.join(file_path_to_compiler_asset_basename(source_file, namespace) + extension)
173+
.to_str()
174+
.unwrap()
175+
.to_owned()
176+
}

0 commit comments

Comments
 (0)