forked from bodil/im-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile.toml
231 lines (214 loc) · 6.38 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
[tasks.prepare-release]
dependencies = ["copy-release", "patch-release"]
[tasks.patch-release]
script_runner = "@rust"
script = [
'''
use std::fs;
use std::io::Result;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
const FROM: &'static str = "extern crate im";
const TO: &'static str = "extern crate im_rc as im";
fn patch_file(path: &Path) -> Result<()> {
let orig = fs::read_to_string(path)?;
let mut bits: Vec<_> = orig.split(FROM).collect();
if bits.len() > 1 {
let out = bits.join(TO);
fs::write(path, &out)?;
}
Ok(())
}
fn fix_imports(path: &Path) -> Result<()> {
for entry in fs::read_dir(path)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
fix_imports(&path)?;
} else if path.extension() == Some(OsStr::new("rs")) {
patch_file(&path)?;
}
}
Ok(())
}
fn main() {
let path = PathBuf::from("dist").join("im-rc");
fix_imports(&path);
}
'''
]
[tasks.copy-release]
dependencies = ["sync"]
script_runner = "@rust"
script = [
'''
//! ```cargo
//! [dependencies]
//! toml_edit = "0.1"
//! fs_extra = "1.1"
//! glob = "0.2"
//! ```
#![allow(non_snake_case)]
extern crate toml_edit;
extern crate fs_extra;
extern crate glob;
use std::fs;
use std::path::{Path, PathBuf};
use toml_edit::{Document, value};
fn read_doc<P: AsRef<Path>>(name: P) -> Document {
let name: &Path = name.as_ref();
fs::read_to_string(name).expect(&format!("error reading {:?}", name))
.parse::<Document>()
.expect(&format!("failed to parse TOML in {:?}", name))
}
fn make_dist(root: &Path, target: &str, manifest: &str) -> PathBuf {
let path = root.join(target);
fs::create_dir_all(&path).unwrap();
let mut src = vec!["src".into(), "build.rs".into(), "proptest-regressions".into()];
for item in glob::glob("*.md").unwrap() {
if let Ok(path) = item {
src.push(path);
}
}
fs_extra::copy_items(&src, &path, &fs_extra::dir::CopyOptions::new())
.expect(&format!("unable to copy files to {:?} target", target));
fs::copy(manifest, path.join("Cargo.toml"))
.expect(&format!("unable to copy Cargo.toml to {:?} target", target));
path
}
fn main() {
// Prepare target folder
fs::remove_dir_all("dist");
let path = PathBuf::from("dist");
// Copy files into im and im-rc subfolders
make_dist(&path, "im", "Cargo.toml");
make_dist(&path, "im-rc", "rc/Cargo.toml");
// Patch im-rc/Cargo.toml paths
let rc_manifest_path = path.join("im-rc/Cargo.toml");
let mut doc = read_doc(&rc_manifest_path);
let build = value(doc["package"]["build"].as_str().unwrap()[1..].to_string());
doc["package"]["build"] = build;
doc["package"]["readme"] = value("../../README.md".to_string());
let libpath = value(doc["lib"]["path"].as_str().unwrap()[1..].to_string());
doc["lib"]["path"] = libpath;
fs::write(&rc_manifest_path, doc.to_string())
.expect(&format!("unable to write {:?}!", rc_manifest_path));
}
'''
]
[tasks.sync]
script_runner = "@rust"
script = [
'''
//! ```cargo
//! [dependencies]
//! toml_edit = "0.1"
//! ```
extern crate toml_edit;
use std::{env, process};
use std::fs::{read_to_string, write};
use toml_edit::{Document, Item, Table, Value};
fn read_doc(name: &str) -> Document {
read_to_string(name).expect(&format!("error reading {:?}", name))
.parse::<Document>()
.expect(&format!("failed to parse TOML in {:?}", name))
}
fn compare_values(left: &Value, right: &Value) -> bool {
if left.is_integer() && (left.as_integer() == right.as_integer()) {
return true;
}
if left.is_float() && (left.as_float() == right.as_float()) {
return true;
}
if left.is_bool() && (left.as_bool() == right.as_bool()) {
return true;
}
if left.is_str() && (left.as_str() == right.as_str()) {
return true;
}
if left.is_date_time() && (left.as_date_time() == right.as_date_time()) {
return true;
}
if let (Some(left), Some(right)) = (left.as_array(), right.as_array()) {
if left.len() != right.len() {
return false;
}
for (lvalue, rvalue) in left.iter().zip(right.iter()) {
if !compare_values(lvalue, rvalue) {
return false;
}
}
return true;
}
if let (Some(left), Some(right)) = (left.as_inline_table(), right.as_inline_table()) {
if left.len() != right.len() {
return false;
}
for (key, value) in left.iter() {
if !right.contains_key(key) {
return false;
}
if !compare_values(value, &right.get(key).unwrap()) {
return false;
}
}
return true;
}
false
}
fn compare_tables(left: &Table, right: &Table) -> bool {
if left.len() != right.len() {
return false;
}
for (key, value) in left.iter() {
if !right.contains_key(key) {
return false;
}
if !compare(value, &right[key]) {
return false;
}
}
true
}
fn compare(left: &Item, right: &Item) -> bool {
match (left, right) {
(&Item::Value(ref left), &Item::Value(ref right)) => compare_values(left, right),
(&Item::Table(ref left), &Item::Table(ref right)) => compare_tables(left, right),
(&Item::ArrayOfTables(ref left), &Item::ArrayOfTables(ref right)) => {
if left.len() != right.len() {
return false;
}
for (ltable, rtable) in left.iter().zip(right.iter()) {
if !compare_tables(ltable, rtable) {
return false;
}
}
true
}
_ => false
}
}
fn main() {
let args: Vec<String> = env::args().skip(1).collect();
let command = args.get(0).cloned().unwrap_or("sync".to_string());
let src = read_doc("./Cargo.toml");
let mut out = read_doc("./rc/Cargo.toml");
if &command == "sync" {
out["package"]["version"] = src["package"]["version"].clone();
out["dependencies"] = src["dependencies"].clone();
out["dev-dependencies"] = src["dev-dependencies"].clone();
out["build-dependencies"] = src["build-dependencies"].clone();
out["package.metadata.docs.rs"] = src["build-dependencies"].clone();
write("./rc/Cargo.toml", out.to_string()).expect("unable to write rc/Cargo.toml!");
} else if &command == "check" {
if !compare(&src["package"]["version"], &out["package"]["version"])
|| !compare(&src["dependencies"], &out["dependencies"])
|| !compare(&src["dev-dependencies"], &out["dev-dependencies"])
|| !compare(&src["build-dependencies"], &out["build-dependencies"]) {
eprintln!("*** ERROR: Cargo.toml files are out of sync!\n*** Please run `cargo make sync` and commit the changes.");
process::exit(1);
}
}
}
'''
]