-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile.toml
130 lines (113 loc) · 3.66 KB
/
Makefile.toml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
[tasks.fmt]
description = "Format source code"
workspace = false
install_script = ['''
#!/usr/bin/env bash
rustup which rustfmt --toolchain nightly
if [ $? -ne 0 ]; then
rustup install nightly
fi
''']
script = '''
#!/usr/bin/env bash
cargo +nightly fmt
'''
[tasks.sort-dependencies]
workspace = false
script_runner = "@rust"
script = '''
//! ```cargo
//! [dependencies]
//! toml_edit = "0.22.0"
//! ```
use std::fs;
use std::path::Path;
use toml_edit::{Document, Item, Table, InlineTable};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let cargo_toml_paths = [
"Cargo.toml",
"utils/Cargo.toml",
"message-derive/Cargo.toml",
"core/Cargo.toml",
"remote/Cargo.toml",
"cluster/Cargo.toml",
];
for path in cargo_toml_paths.iter() {
let path = Path::new(path);
if path.exists() {
backup_cargo_toml(path)?;
sort_dependencies(path)?;
} else {
println!("Warning: {} not found", path.display());
}
}
Ok(())
}
// Create a backup of Cargo.toml -> Cargo.toml.bak
fn backup_cargo_toml(file_path: &Path) -> Result<(), Box<dyn std::error::Error>> {
let backup_path = file_path.with_extension("toml.bak");
fs::copy(file_path, &backup_path)?;
println!("Backup created: {}", backup_path.display());
Ok(())
}
fn sort_table(table: &mut Table) {
let mut keys: Vec<_> = table.iter().map(|(k, _)| k.to_string()).collect();
keys.sort();
let sorted_table = keys.into_iter()
.filter_map(|k| table.get(&k).map(|v| (k, v.clone())))
.collect();
*table = sorted_table;
}
fn sort_inline_table(table: &mut InlineTable) {
let mut keys: Vec<_> = table.iter().map(|(k, _)| k.to_string()).collect();
keys.sort();
let sorted_table = keys.into_iter()
.filter_map(|k| table.get(&k).map(|v| (k, v.clone())))
.collect();
*table = sorted_table;
}
fn sort_dependencies(file_path: &Path) -> Result<(), Box<dyn std::error::Error>> {
let content = fs::read_to_string(file_path)?;
let mut doc = content.parse::<Document>()?;
// Sort workspace dependencies
if let Some(workspace) = doc.get_mut("workspace") {
if let Some(deps) = workspace.get_mut("dependencies") {
match deps {
Item::Table(table) => {
sort_table(table);
println!("Sorted workspace.dependencies in {}", file_path.display());
},
Item::Value(value) => {
if let Some(inline_table) = value.as_inline_table_mut() {
sort_inline_table(inline_table);
println!("Sorted workspace.dependencies in {}", file_path.display());
}
},
_ => {}
}
}
}
// Sort other dependency sections
let sections = ["dependencies", "dev-dependencies", "build-dependencies"];
for section in sections.iter() {
if let Some(deps) = doc.get_mut(section) {
match deps {
Item::Table(table) => {
sort_table(table);
println!("Sorted {} in {}", section, file_path.display());
},
Item::Value(value) => {
if let Some(inline_table) = value.as_inline_table_mut() {
sort_inline_table(inline_table);
println!("Sorted {} in {}", section, file_path.display());
}
},
_ => {}
}
}
}
fs::write(file_path, doc.to_string())?;
println!("Updated {} successfully", file_path.display());
Ok(())
}
'''