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
use cmake_parser_derive::CMake;

use crate::{
    command::common::{NewlineStyle, Permissions},
    doc::command_scope::{CommandScope, ToCommandScope},
    Token,
};

/// Copy a file to another location and modify its contents.
///
/// Reference: <https://cmake.org/cmake/help/v3.26/command/configure_file.html>
#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate")]
pub struct ConfigureFile<'t> {
    #[cmake(positional)]
    pub input: Token<'t>,
    #[cmake(positional)]
    pub output: Token<'t>,
    pub permissions: Option<Permissions<'t>>,
    #[cmake(rename = "COPYONLY")]
    pub copy_only: bool,
    pub escape_quotes: bool,
    #[cmake(rename = "@ONLY")]
    pub only: bool,
    pub newline_style: Option<NewlineStyle>,
}

impl<'t> ToCommandScope for ConfigureFile<'t> {
    fn to_command_scope(&self) -> CommandScope {
        CommandScope::Scripting
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::doc::cmake_parse::tests::{token, tokens_vec};
    use crate::*;
    use pretty_assertions::assert_eq;

    #[test]
    fn configure_file() {
        let src = include_bytes!("../../../../../fixture/commands/scripting/configure_file");
        let cmakelists = parse_cmakelists(src).unwrap();
        let doc = Doc::from(cmakelists);
        assert_eq!(
            doc.commands(),
            Ok(vec![
                Command::ConfigureFile(Box::new(ConfigureFile {
                    input: token(b"input1"),
                    output: token(b"output1"),
                    permissions: None,
                    copy_only: false,
                    escape_quotes: false,
                    only: false,
                    newline_style: None,
                })),
                Command::ConfigureFile(Box::new(ConfigureFile {
                    input: token(b"input1"),
                    output: token(b"output1"),
                    permissions: Some(Permissions::NoSource),
                    copy_only: false,
                    escape_quotes: false,
                    only: false,
                    newline_style: None,
                })),
                Command::ConfigureFile(Box::new(ConfigureFile {
                    input: token(b"input1"),
                    output: token(b"output1"),
                    permissions: Some(Permissions::UseSource),
                    copy_only: false,
                    escape_quotes: false,
                    only: false,
                    newline_style: None,
                })),
                Command::ConfigureFile(Box::new(ConfigureFile {
                    input: token(b"input1"),
                    output: token(b"output1"),
                    permissions: Some(Permissions::File(tokens_vec([b"file1", b"file2"]))),
                    copy_only: false,
                    escape_quotes: false,
                    only: false,
                    newline_style: None,
                })),
                Command::ConfigureFile(Box::new(ConfigureFile {
                    input: token(b"input1"),
                    output: token(b"output1"),
                    permissions: Some(Permissions::File(tokens_vec([b"file1", b"file2"]))),
                    copy_only: true,
                    escape_quotes: true,
                    only: true,
                    newline_style: Some(NewlineStyle::Unix),
                })),
                Command::ConfigureFile(Box::new(ConfigureFile {
                    input: token(b"input1"),
                    output: token(b"output1"),
                    permissions: None,
                    copy_only: false,
                    escape_quotes: false,
                    only: false,
                    newline_style: Some(NewlineStyle::Dos),
                })),
                Command::ConfigureFile(Box::new(ConfigureFile {
                    input: token(b"input1"),
                    output: token(b"output1"),
                    permissions: None,
                    copy_only: false,
                    escape_quotes: false,
                    only: false,
                    newline_style: Some(NewlineStyle::Win32),
                })),
                Command::ConfigureFile(Box::new(ConfigureFile {
                    input: token(b"input1"),
                    output: token(b"output1"),
                    permissions: None,
                    copy_only: false,
                    escape_quotes: false,
                    only: false,
                    newline_style: Some(NewlineStyle::Lf),
                })),
                Command::ConfigureFile(Box::new(ConfigureFile {
                    input: token(b"input1"),
                    output: token(b"output1"),
                    permissions: None,
                    copy_only: false,
                    escape_quotes: false,
                    only: false,
                    newline_style: Some(NewlineStyle::CrLf),
                })),
            ])
        )
    }
}