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
#![type_length_limit = "2149570"]
#[cfg(target_os = "macos")]
extern crate tempdir;

pub mod errors {
    pub use anyhow::{anyhow, bail, Context, Error, Result};
}

mod android;
pub mod config;
pub mod device;
mod host;
#[cfg(target_os = "macos")]
mod ios;
pub mod overlay;
pub mod platform;
pub mod project;
mod script;
mod ssh;
mod toolchain;
pub mod utils;

pub use crate::config::Configuration;

use crate::config::PlatformConfiguration;
#[cfg(target_os = "macos")]
use crate::ios::IosManager;
use crate::platform::regular_platform::RegularPlatform;
use crate::project::Project;
use anyhow::{anyhow, Context};
use dyn_clone::DynClone;
use std::fmt::Display;
use std::{path, sync};

use crate::errors::Result;

pub struct Dinghy {
    devices: Vec<sync::Arc<Box<dyn Device>>>,
    platforms: Vec<(String, sync::Arc<Box<dyn Platform>>)>,
}

impl Dinghy {
    pub fn probe(conf: &sync::Arc<Configuration>) -> Result<Dinghy> {
        let mut managers: Vec<Box<dyn PlatformManager>> = vec![];
        if let Some(man) = host::HostManager::probe(conf) {
            managers.push(Box::new(man));
        }
        if let Some(man) = android::AndroidManager::probe() {
            managers.push(Box::new(man));
        }
        if let Some(man) = script::ScriptDeviceManager::probe(conf.clone()) {
            managers.push(Box::new(man));
        }
        if let Some(man) = ssh::SshDeviceManager::probe(conf.clone()) {
            managers.push(Box::new(man));
        }
        #[cfg(target_os = "macos")]
        {
            std::thread::sleep(std::time::Duration::from_millis(100));
            if let Some(man) = IosManager::new().context("Could not initialize iOS manager")? {
                managers.push(Box::new(man));
            }
        }

        let mut devices = vec![];
        let mut platforms = vec![];
        for man in managers.into_iter() {
            devices.extend(
                man.devices()
                    .context("Could not list devices")?
                    .into_iter()
                    .map(|it| sync::Arc::new(it)),
            );
            platforms.extend(
                man.platforms()
                    .context("Could not list platforms")?
                    .into_iter()
                    .map(|it| (it.id(), sync::Arc::new(it))),
            );
        }
        for (platform_name, platform_conf) in &conf.platforms {
            if platform_name == "host" {
                continue;
            }
            let rustc_triple = platform_conf
                .rustc_triple
                .as_ref()
                .ok_or_else(|| anyhow!("Platform {} has no rustc_triple", platform_name))?;
            let pf = RegularPlatform::new(
                platform_conf.clone(),
                platform_name.to_string(),
                rustc_triple.clone(),
                platform_conf
                    .toolchain
                    .clone()
                    .map(|it| path::PathBuf::from(it))
                    .or(dirs::home_dir()
                        .map(|it| it.join(".dinghy").join("toolchain").join(platform_name)))
                    .with_context(|| format!("Toolchain missing for platform {}", platform_name))?,
            )
            .with_context(|| format!("Could not assemble platform {}", platform_name))?;
            platforms.push((pf.id(), sync::Arc::new(pf)));
        }
        Ok(Dinghy { devices, platforms })
    }

    pub fn devices(&self) -> Vec<sync::Arc<Box<dyn Device>>> {
        self.devices.clone()
    }

    pub fn host_platform(&self) -> sync::Arc<Box<dyn Platform>> {
        self.platforms[0].1.clone()
    }

    pub fn platforms(&self) -> Vec<sync::Arc<Box<dyn Platform>>> {
        self.platforms
            .iter()
            .map(|&(_, ref platform)| platform.clone())
            .collect()
    }

    pub fn platform_by_name(
        &self,
        platform_name_filter: &str,
    ) -> Option<sync::Arc<Box<dyn Platform>>> {
        self.platforms
            .iter()
            .filter(|&&(ref platform_name, _)| platform_name == platform_name_filter)
            .map(|&(_, ref platform)| platform.clone())
            .next()
    }
}

pub trait Device: std::fmt::Debug + Display + DeviceCompatibility + DynClone {
    fn clean_app(&self, build_bundle: &BuildBundle) -> Result<()>;

    fn debug_app(
        &self,
        project: &Project,
        build: &Build,
        args: &[&str],
        envs: &[&str],
    ) -> Result<BuildBundle>;

    fn id(&self) -> &str;

    fn name(&self) -> &str;

    fn run_app(
        &self,
        project: &Project,
        build: &Build,
        args: &[&str],
        envs: &[&str],
    ) -> Result<BuildBundle>;
}

dyn_clone::clone_trait_object!(Device);

pub trait DeviceCompatibility {
    fn is_compatible_with_regular_platform(&self, _platform: &RegularPlatform) -> bool {
        false
    }

    fn is_compatible_with_host_platform(&self, _platform: &host::HostPlatform) -> bool {
        false
    }

    #[cfg(target_os = "macos")]
    fn is_compatible_with_ios_platform(&self, _platform: &ios::IosPlatform) -> bool {
        false
    }
}

pub trait Platform: std::fmt::Debug {
    fn setup_env(&self, project: &Project, setup_args: &SetupArgs) -> Result<()>;

    fn id(&self) -> String;

    fn is_compatible_with(&self, device: &dyn Device) -> bool;

    fn is_host(&self) -> bool;
    fn rustc_triple(&self) -> &str;

    fn strip(&self, build: &mut Build) -> Result<()>;
    fn sysroot(&self) -> Result<Option<path::PathBuf>>;
}

impl Display for dyn Platform {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(fmt, "{}", self.id())
    }
}

pub trait PlatformManager {
    fn devices(&self) -> Result<Vec<Box<dyn Device>>>;
    fn platforms(&self) -> Result<Vec<Box<dyn Platform>>>;
}

#[derive(Clone, Debug)]
pub struct Build {
    pub setup_args: SetupArgs,
    pub dynamic_libraries: Vec<path::PathBuf>,
    pub runnable: Runnable,
    pub target_path: path::PathBuf,
    pub files_in_run_args: Vec<path::PathBuf>,
}

#[derive(Clone, Debug)]
pub struct SetupArgs {
    pub verbosity: i8,
    pub forced_overlays: Vec<String>,
    pub envs: Vec<String>,
    pub cleanup: bool,
    pub strip: bool,
    pub device_id: Option<String>,
}

impl SetupArgs {
    pub fn get_runner_command(&self, platform_id: &str) -> String {
        let mut extra_args = String::new();
        if self.verbosity > 0 {
            for _ in 0..self.verbosity {
                extra_args.push_str("-v ")
            }
        }
        if self.verbosity < 0 {
            for _ in 0..-self.verbosity {
                extra_args.push_str("-q ")
            }
        }
        if self.cleanup {
            extra_args.push_str("--cleanup ")
        }
        if self.strip {
            extra_args.push_str("--strip ")
        }
        if let Some(device_id) = &self.device_id {
            extra_args.push_str("-d ");
            extra_args.push_str(&device_id);
            extra_args.push(' ');
        }
        for env in &self.envs {
            extra_args.push_str("-e ");
            extra_args.push_str(env);
            extra_args.push(' ');
        }

        format!(
            "{} -p {} {}runner --",
            std::env::current_exe().unwrap().to_str().unwrap(),
            platform_id,
            extra_args
        )
    }
}

#[derive(Clone, Debug, Default)]
pub struct BuildBundle {
    pub id: String,
    pub bundle_dir: path::PathBuf,
    pub bundle_exe: path::PathBuf,
    pub lib_dir: path::PathBuf,
    pub root_dir: path::PathBuf,
}

impl BuildBundle {
    fn replace_prefix_with<P: AsRef<path::Path>>(&self, path: P) -> Result<Self> {
        Ok(BuildBundle {
            id: self.id.clone(),
            bundle_dir: utils::normalize_path(
                &path
                    .as_ref()
                    .to_path_buf()
                    .join(self.bundle_dir.strip_prefix(&self.root_dir)?),
            ),
            bundle_exe: utils::normalize_path(
                &path
                    .as_ref()
                    .to_path_buf()
                    .join(self.bundle_exe.strip_prefix(&self.root_dir)?),
            ),
            lib_dir: utils::normalize_path(
                &path
                    .as_ref()
                    .to_path_buf()
                    .join(self.lib_dir.strip_prefix(&self.root_dir)?),
            ),
            root_dir: path.as_ref().to_path_buf(),
        })
    }
}

#[derive(Clone, Debug, Default)]
pub struct Runnable {
    pub id: String,
    pub package_name: String,
    pub exe: path::PathBuf,
    pub source: path::PathBuf,
}