summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 5e86c89768c151c58e08d81088827b3bd33b8b66 (plain)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
use std::{path::{Path, PathBuf}, fs, str::FromStr, sync::{Arc, Mutex}};
use anyhow::{Result, Context, anyhow};
use itertools::Itertools;
use rnix::{SyntaxKind, ast::{AttrpathValue, AttrSet, HasEntry, Entry::*}, SyntaxNode};
use rowan::{ast::AstNode, TextSize};
use serde::Deserialize;
use threadpool::ThreadPool;

struct StatusReportData {
    files: usize,
    items: usize,
    total_files: usize,
    total_items: usize,
    changed_items: usize,
    last_file: String,
    last_item: String,
}

impl StatusReportData {
    fn print(&self, clear: bool) {
        if clear {
            print!("\x1b[1F\x1b[2K\x1b[1F\x1b[2K");
        }
        println!("{}/{} files ({})", self.files, self.total_files, self.last_file);
        println!("{}/{} ({}) items ({})", self.items, self.total_items,
                 self.changed_items, self.last_item);
    }
}

struct StatusReport(Mutex<StatusReportData>);

impl StatusReport {
    fn new(total_files: usize, total_items: usize) -> Self {
        Self(Mutex::new(StatusReportData {
            files: 0,
            items: 0,
            total_files,
            total_items,
            changed_items: 0,
            last_file: "".to_string(),
            last_item: "".to_string(),
        }))
    }

    fn enter_file(&self, f: &str) {
        let mut m = self.0.lock().unwrap();
        m.files += 1;
        m.last_file = f.to_string();
        m.print(m.files > 1 || m.items >= 1);
    }

    fn enter_item(&self, i: String) {
        let mut m = self.0.lock().unwrap();
        m.items += 1;
        m.last_item = i;
        m.print(m.files >= 1 || m.items > 1);
    }

    fn update_item(&self, i: String) {
        let mut m = self.0.lock().unwrap();
        m.last_item = i;
        m.print(true);
    }

    fn changed_item(&self) {
        let mut m = self.0.lock().unwrap();
        m.changed_items += 1;
        m.print(true);
    }

    fn skip_items(&self, i: usize) {
        let mut m = self.0.lock().unwrap();
        m.items += i;
        m.print(m.files >= 1 || m.items >= 1);
    }
}

struct StatusPart<'a>(&'a StatusReport, usize);

impl<'a> StatusPart<'a> {
    fn enter_item(&mut self, i: String) {
        self.0.enter_item(i);
        self.1 -= 1;
    }

    fn update_item(&mut self, i: String) {
        self.0.update_item(i);
    }

    fn changed_item(&mut self) {
        self.0.changed_item();
    }
}

impl<'a> Drop for StatusPart<'a> {
    fn drop(&mut self) {
        self.0.skip_items(self.1);
    }
}



fn textsize_at_line(s: &str, line: usize) -> TextSize {
    s
        .split('\n')
        .map(|l|
            TextSize::of(l) + TextSize::new(1)
        )
        .take(line-1)
        .sum()
}


fn dig_to_kind(kind: SyntaxKind, node: &SyntaxNode) -> Option<SyntaxNode> {
    if node.kind() == kind {
        return Some(node.clone());
    }

    node.descendants()
        .filter(|node| node.kind() == kind)
        .next()
}

fn add_to_meta_block(rough_pos: SyntaxNode, content: &str, main_program: &str, file: &Path, line: usize) -> Result<(String, usize)> {
    let meta_node = dig_to_kind(SyntaxKind::NODE_ATTR_SET, &rough_pos).unwrap();
    let meta_set = AttrSet::cast(meta_node.clone()).unwrap();

    let description_entry = meta_set.entries()
         .filter(|entry| {
             match &entry {
                 Inherit(it) => it.attrs().any(|c| c.to_string() == "description"),
                 AttrpathValue(it) => it.attrpath().unwrap().to_string() == "description",
             }
         })
         .exactly_one().ok()
         .with_context(|| format!("meta node has no description attribute in {:?} at {}", file, line))?;
    let description = description_entry.syntax();

    let pos = description.text_range();
    let indent = content[..pos.start().into()].chars().rev().position(|c| c == '\n').unwrap();

    let patch = String::new()
        + "\n"
        + &" ".repeat(indent)
        + "mainProgram = \"" + main_program + "\";";

    Ok((patch, pos.end().into()))
}

fn edit_one(file: &Path, line: usize, main_program: &str, p: &StatusReport) -> Result<String> {
    let mut content = fs::read_to_string(file)?;
    let searchpos = textsize_at_line(&content, line);

    p.update_item(format!("doing {:?}", file));
    let parse = rnix::Root::parse(&content);
    if !parse.errors().is_empty() {
        anyhow::bail!("error: {:?}", parse.errors());
    }
    let tree = parse.tree();

    let pos_node = tree.syntax().descendants()
        .filter(|node| {
            if node.kind() == SyntaxKind::NODE_ATTRPATH_VALUE {
                let value = AttrpathValue::cast(node.clone()).unwrap();
                node.text_range().contains(searchpos) && value.attrpath().unwrap().to_string() == "meta"
            } else { false }
        })
        .exactly_one().ok();

    // do we have a meta attrset already?
    let (patch, insert_offset) = match pos_node {
        None => {
            let version_node = tree
                .syntax()
                .descendants()
                .filter(|node| {
                    if node.kind() == SyntaxKind::NODE_ATTRPATH_VALUE {
                        let value = AttrpathValue::cast(node.clone()).unwrap();
                        let name =  value.attrpath().unwrap().to_string();
                        node.text_range().contains(searchpos + TextSize::new(5))
                            && (name == "version" || name == "pname" || name == "name")
                    } else { false }
                })
                .exactly_one().ok()
                .with_context(|| format!("neither meta nor version node found for {:?} at {}", file, line))?;

            let attrset = version_node.parent().unwrap();
            if attrset.kind() != SyntaxKind::NODE_ATTR_SET {
                anyhow::bail!("name not in an attrset in {:?} at {}", file, line)
            }

            // does a meta block already exist?
            let maybe_meta_block = attrset
                .descendants()
                .filter(|node| {
                    if node.kind() == SyntaxKind::NODE_ATTRPATH_VALUE {
                        let value = AttrpathValue::cast(node.clone()).unwrap();
                        let name =  value.attrpath().unwrap().to_string();
                        name == "meta"
                    } else { false }
                })
                .exactly_one();

            if let Ok(meta) = maybe_meta_block {
                add_to_meta_block(meta.clone(), &content, main_program, file, line)?
            } else {
                let before_attrset_end = Into::<usize>::into(attrset.text_range().end())
                    - 1
                    - content[..attrset.text_range().end().into()]
                    .chars().rev().position(|c| c == '\n').unwrap();

                let indent = content[..version_node.text_range().start().into()]
                    .chars().rev().position(|c| c == '\n').unwrap();

                // some language specific build systems don't use meta as its own attrset
                // there's no good way to recognise these, but this seems to work fine
                let weird_nonstandard_meta = attrset
                    .descendants()
                    .any(|node| {
                        if node.kind() == SyntaxKind::NODE_ATTRPATH_VALUE {
                            let value = AttrpathValue::cast(node.clone()).unwrap();
                            let name =  value.attrpath().unwrap().to_string();
                            name == "description" || name == "homepage" || name == "license"
                        } else { false }
                    });
                let patch = String::new()
                    + "\n"
                    + &" ".repeat(indent)
                    + if weird_nonstandard_meta { "mainProgram = \"" } else { "meta.mainProgram = \"" }
                    + main_program + "\";";

                (patch, before_attrset_end)
            }
        },
        Some(pos) => {
            add_to_meta_block(pos.clone(), &content, main_program, file, line)?
        }
    };


    content = String::new()
        + &content[..insert_offset]
        + &patch
        + &content[insert_offset..];

    p.changed_item();
    Ok(content)
}


#[derive(Deserialize, Clone)]
struct TrivialProgram {
    pos: Option<String>,
    name: String
}

fn main() {

    let raw_inputs = fs::read_to_string("trivials.json").unwrap();
    let inputs: Vec<TrivialProgram> = serde_json::de::from_str::<Vec<_>>(&raw_inputs).unwrap();
        // .into_iter().take(200).collect();


    // TODO: group edits in the same file
    let pool = ThreadPool::new(16);

    let mut tasks: Vec<(TrivialProgram, PathBuf, usize)> = inputs.into_iter()
        .filter_map(|i| {
            if i.pos.is_none() {
                println!("no position for name {}", i.name);
                None
            } else {
                let pos = i.pos.as_ref().unwrap();
                let (filename, line) = {
                    let l = pos.split(':').collect::<Vec<_>>();
                    assert!(l.len() == 2);

                    (PathBuf::from_str(l[0]).unwrap(), l[1].parse().unwrap())
                };
                Some((i, filename, line))
            }
        })
        .collect();

    tasks.sort_by_key(|(_ ,filename, _)| filename.clone());

    let grouped_tasks: Vec<(TrivialProgram, PathBuf, Vec<(usize, String)>)> =
        tasks.into_iter()
             .map(|(i, f, l)| (i.clone(), f, vec![(l, i.name)]))
             .coalesce(|(i1, f1, l1), (i2, f2, l2)| {
                 if f1 == f2 {
                     if l1 == l2 && i1.name == i2.name {
                         Ok((i1, f1, l1))
                     } else {
                         Ok((i1, f1, l1.into_iter().chain(l2.into_iter()).collect()))
                     }
                 } else {
                     Err(((i1,f1,l1),(i2,f2,l2)))
                 }
             }).collect();

    let results = Arc::new(Mutex::new(vec![]));
    let printer = Arc::new(StatusReport::new(grouped_tasks.len(), grouped_tasks.len()));

    for (i, filename, sites) in grouped_tasks {
        pool.execute({
            let results = Arc::clone(&results);
            let printer = Arc::clone(&printer);

            move || {
                let pos = i.pos.unwrap();
                printer.enter_file(&pos);
                if sites.len() == 1 {
                    let result = edit_one(&filename, sites[0].0, &sites[0].1, &printer)
                        .map(|ok| (filename, ok));
                    results.lock().unwrap().push(result);
                } else {
                    results.lock().unwrap().push(Err(anyhow!("skipped {:?} as it has multiple edits", filename)));
                }
            }
        });
    }

    pool.join();

    println!("\n\nSummary:");
    let mut c_errors = 0;
    let mut c_total = 0;
    for r in results.lock().unwrap().iter() {
        match r {
            Err(e) => {
                println!(" {}", e);
                c_errors += 1;
            },
            _ => ()
        }
        c_total += 1;
    }

    println!("\n  ({c_total} sites total, {c_errors} errors, generated {} edits)", c_total - c_errors);

    let edits: Vec<_> = Arc::into_inner(results).unwrap().into_inner().unwrap()
        .into_iter()
        .filter_map(|r| r.ok())
        .collect();

    // check we didn't miss any duplicate edits
    let duplicates = edits.iter().duplicates_by(|(filename, _)| filename).count();
    println!("{duplicates} edits were not the only one in their file");

    println!("applying changes ...");
    for (filename, content) in edits {
        fs::write(&filename, content.as_bytes()).unwrap();
        // println!("{}", content);
    }
}