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
use std::{
    path::{Path, PathBuf},
    sync::Arc,
};

use eyre::Context;

pub struct RustSource {
    client: Arc<dagger_sdk::Query>,

    exclude: Vec<String>,
}

impl RustSource {
    pub fn new(client: Arc<dagger_sdk::Query>) -> Self {
        Self {
            client,
            exclude: vec!["node_modules/", ".git/", "target/", ".cuddle/"]
                .into_iter()
                .map(|s| s.to_string())
                .collect(),
        }
    }

    pub fn with_exclude(
        &mut self,
        exclude: impl IntoIterator<Item = impl Into<String>>,
    ) -> &mut Self {
        self.exclude = exclude.into_iter().map(|s| s.into()).collect();

        self
    }

    pub fn append_exclude(
        &mut self,
        exclude: impl IntoIterator<Item = impl Into<String>>,
    ) -> &mut Self {
        self.exclude
            .append(&mut exclude.into_iter().map(|s| s.into()).collect::<Vec<_>>());

        self
    }

    pub async fn get_rust_src<T, I>(
        &self,
        source: Option<T>,
        crate_paths: I,
    ) -> eyre::Result<(dagger_sdk::Directory, dagger_sdk::Directory)>
    where
        T: Into<PathBuf>,
        T: Clone,
        I: IntoIterator,
        I::Item: Into<String>,
    {
        let source_path = match source.clone() {
            Some(s) => s.into(),
            None => PathBuf::from("."),
        };

        let (skeleton_files, _crates) = self
            .get_rust_skeleton_files(&source_path, crate_paths)
            .await?;

        let src = self.get_src(source.clone()).await?;
        let rust_src = self.get_rust_dep_src(source).await?;
        let rust_src = rust_src.with_directory(".", skeleton_files.id().await?);

        Ok((src, rust_src))
    }

    pub async fn get_src(
        &self,
        source: Option<impl Into<PathBuf>>,
    ) -> eyre::Result<dagger_sdk::Directory> {
        let source = source.map(|s| s.into()).unwrap_or(PathBuf::from("."));

        let directory = self.client.host().directory_opts(
            source.display().to_string(),
            dagger_sdk::HostDirectoryOptsBuilder::default()
                .exclude(self.exclude.iter().map(|s| s.as_str()).collect::<Vec<_>>())
                .build()?,
        );

        Ok(directory)
    }

    pub async fn get_rust_dep_src(
        &self,
        source: Option<impl Into<PathBuf>>,
    ) -> eyre::Result<dagger_sdk::Directory> {
        let source = source.map(|s| s.into()).unwrap_or(PathBuf::from("."));

        let directory = self.client.host().directory_opts(
            source.display().to_string(),
            dagger_sdk::HostDirectoryOptsBuilder::default()
                .include(vec!["**/Cargo.toml", "**/Cargo.lock"])
                .build()?,
        );

        Ok(directory)
    }

    pub async fn get_rust_target_src(
        &self,
        source_path: &Path,
        container: dagger_sdk::Container,
        crate_paths: impl IntoIterator<Item = impl Into<String>>,
    ) -> eyre::Result<dagger_sdk::Directory> {
        let (_skeleton_files, crates) = self
            .get_rust_skeleton_files(source_path, crate_paths)
            .await?;

        let exclude = crates
            .iter()
            .map(|c| format!("**/*{}*", c.replace('-', "_")))
            .collect::<Vec<_>>();

        let exclude = exclude.iter().map(|c| c.as_str()).collect();

        let incremental_dir = self.client.directory().with_directory_opts(
            ".",
            container.directory("target").id().await?,
            dagger_sdk::DirectoryWithDirectoryOpts {
                exclude: Some(exclude),
                include: None,
            },
        );

        return Ok(incremental_dir);
    }

    pub async fn get_rust_skeleton_files(
        &self,
        source_path: &Path,
        crate_paths: impl IntoIterator<Item = impl Into<String>>,
    ) -> eyre::Result<(dagger_sdk::Directory, Vec<String>)> {
        let paths = crate_paths
            .into_iter()
            .map(|s| s.into())
            .collect::<Vec<String>>();

        let mut crates = Vec::new();
        for path in paths {
            if path.ends_with("/*") {
                let mut dirs = tokio::fs::read_dir(source_path.join(path.trim_end_matches("/*")))
                    .await
                    .context(format!("failed to find path: {}", path.clone()))?;
                while let Some(entry) = dirs.next_entry().await? {
                    if entry.metadata().await?.is_dir() {
                        crates.push(entry.path());
                    }
                }
            } else {
                crates.push(PathBuf::from(path));
            }
        }

        fn create_skeleton_files(
            directory: dagger_sdk::Directory,
            path: &Path,
        ) -> eyre::Result<dagger_sdk::Directory> {
            let main_content = r#"
        #[allow(dead_code)]
        fn main() { panic!("should never be executed"); }"#;
            let lib_content = r#"
        #[allow(dead_code)]
        fn some() { panic!("should never be executed"); }"#;

            let directory = directory.with_new_file(
                path.join("src").join("main.rs").display().to_string(),
                main_content,
            );
            let directory = directory.with_new_file(
                path.join("src").join("lib.rs").display().to_string(),
                lib_content,
            );

            Ok(directory)
        }

        let mut directory = self.client.directory();
        let mut crate_names = Vec::new();

        for rust_crate in crates.iter() {
            if let Some(file_name) = rust_crate.file_name() {
                crate_names.push(file_name.to_str().unwrap().to_string());
            }
            directory = create_skeleton_files(
                directory,
                rust_crate.strip_prefix(source_path).unwrap_or(&rust_crate),
            )?;
        }

        Ok((directory, crate_names))
    }
}