summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: bd448ce02c1983f7aeb6d0acc698a286053cba79 (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
use std::{path::PathBuf, fs, process::exit};
use rowan::ast::AstNode;
use clap::{arg, Parser};

use queries::Query;
use changes::{apply_changes, format_range};

use crate::changes::format_range_json;

#[allow(dead_code)]
mod queries;
#[allow(dead_code)]
mod status_reporter;
mod batchmode;
#[allow(dead_code)]
mod util;
#[allow(dead_code)]
mod pipeline;
mod changes;


#[derive(clap::Parser)]
struct Args {
    query: String,
    path: Vec<PathBuf>,
    #[arg(short, long)]
    debug: bool,
    #[arg(long)]
    batchmode: bool,
    #[arg(long)]
    print_positions: bool,
    #[arg(long)]
    json: bool
}

fn main() {

    let args = Args::parse();

    let query = parse_query(&args.query);

    if args.debug {
        println!("{query:?}");
    }

    if args.batchmode {
        macro_rules! warn_arg {
            ($arg: ident, $text:literal) => { if args.$arg {
                eprintln!("Warning: option {} is ignored in batch mode.", $text);
            }}
        }

        warn_arg!(json, "--json");
        warn_arg!(print_positions, "--print-positions");

        batchmode::batchmode(args.path, query, args.debug);
    } else {
        for path in &args.path {
            handle_file(path, &args, &query)
        }
    }
}

fn handle_file(path: &PathBuf, args: &Args, query: &Query) {
    let (content, nexp) = match parse_nixfile(path) {
        Err(e) => {
            eprintln!("could not parse file: {e}");
            exit(2);
        },
        Ok(exp) => exp
    };

    let (changes, results) = query.apply(&content, nexp.syntax().clone()).unwrap();

    if args.debug {
        println!("{changes:?}");
    }

    if changes.len() == 0 {
        if args.print_positions {
            if args.json {
                let json = results
                    .iter()
                    .map(|result|
                         format_range_json(path, &content, result.text_range()))
                    .collect::<Vec<_>>();
                println!("{}", serde_json::to_string(&json).unwrap());
            } else {
                for result in results {
                    println!(
                        "{}",
                        format_range(path, &content, result.text_range())
                    );
                }
            }
        } else {
            for result in results {
                println!("{result}");
            }
        }
    } else {
        let changed = apply_changes(&content, changes, args.debug);

        println!("{changed}");
    }
}


fn parse_nixfile(path: &PathBuf) -> anyhow::Result<(String, rnix::Root)> {
    let content = fs::read_to_string(path)?;

    let tree = parse_nexpr(&content)?;
    Ok((content, tree))
}

fn parse_nexpr(code: &str) -> anyhow::Result<rnix::Root> {
    let parse = rnix::Root::parse(&code);
    if !parse.errors().is_empty() {
        anyhow::bail!("error: {:?}", parse.errors());
    }
    Ok(parse.tree())
}

fn parse_query(querystring: &str) -> Query {
    let parse = queries::parse(querystring);
    if parse.errors.len() != 0 {
        eprintln!(
            "syntax {}: \n  {}",
            if parse.errors.len() == 1 { "error" } else { "errors" },
            parse.errors.join("  \n")
        );
        exit(1);
    }

    let query = match parse.to_query() {
        Err(es) => {
            queries::print_query_errors(querystring, es);
            exit(1);
        },
        Ok(query) => query
    };

    query
}


macro_rules! test_change {
    ($name:ident, $query:literal, $before:literal, $after:literal) => {
        #[test]
        fn $name() {
            let nix = $before;
            let nexpr = parse_nexpr(&nix).unwrap();
            let query = parse_query($query);
            let (changes, _) = query.apply(&nix, nexpr.syntax().clone()).unwrap();
            let changed = apply_changes(&nix, changes, false);
            assert_eq!(changed, $after);
        }
    }
}

test_change!(
    test_remove_inherit,
    ">> Inherit >> mdDoc[remove]", r#"
{lib, ...}:
mkDerivation {
    pname = "dings";
    inherit (lib) mdDoc a b c mdDoc;
}"#,
    r#"
{lib, ...}:
mkDerivation {
    pname = "dings";
    inherit (lib) a b c;
}"#
);

test_change!(
    test_remove_attrpath,
    ">> mkDerivation >> pname[remove]",
    r#"
{lib, ...}:
mkDerivation {
    pname = "dings";
    meta.mainProgram = "blub";
}"#,
    r#"
{lib, ...}:
mkDerivation {
    meta.mainProgram = "blub";
}"#
);