Skip to content

Commit e52d02d

Browse files
committed
Refactor __main__ generation
1 parent 752910a commit e52d02d

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

src/python_files.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,26 @@ use crate::project_info::{ProjectInfo, ProjectManager};
77
use crate::utils::is_python_312_or_greater;
88

99
fn create_dunder_main_file(module: &str, is_async_project: bool) -> String {
10-
if is_async_project {
11-
format!(
12-
r#"from __future__ import annotations
10+
let mut file = "from __future__ import annotations\n\n".to_string();
1311

14-
import asyncio
12+
if is_async_project {
13+
file.push_str("import asyncio\n\n");
14+
}
1515

16-
from {module}.main import main # pragma: no cover
16+
file.push_str(&format!(
17+
r#"from {module}.main import main # pragma: no cover
1718
1819
if __name__ == "__main__":
19-
raise SystemExit(asyncio.run(main()))
2020
"#
21-
)
22-
} else {
23-
format!(
24-
r#"from __future__ import annotations
21+
));
2522

26-
from {module}.main import main # pragma: no cover
27-
28-
if __name__ == "__main__":
29-
raise SystemExit(main())
30-
"#
31-
)
23+
if is_async_project {
24+
file.push_str(" raise SystemExit(asyncio.run(main()))\n");
25+
} else {
26+
file.push_str(" raise SystemExit(main())\n");
3227
}
28+
29+
file
3330
}
3431

3532
fn create_main_file(is_async_project: bool) -> String {
@@ -142,6 +139,7 @@ fn save_pyo3_test_file(project_info: &ProjectInfo) -> Result<()> {
142139
fn create_project_init_file(module: &str, project_manager: &ProjectManager) -> String {
143140
match project_manager {
144141
ProjectManager::Maturin => {
142+
// 118 = the letter v
145143
let v_ascii: u8 = 118;
146144
if let Some(first_char) = module.chars().next() {
147145
if (first_char as u8) < v_ascii {

0 commit comments

Comments
 (0)