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
use crate::cargo::Cargo;
use crate::engine::Build;
use crate::error::Error;
use std::path::{Path, PathBuf};
use std::process::Command;

pub struct Flutter {
    root: PathBuf,
}

impl Flutter {
    pub fn new() -> Result<Self, Error> {
        let root = if let Ok(root) = std::env::var("FLUTTER_ROOT") {
            PathBuf::from(root)
        } else {
            let flutter = which::which("flutter").expect("flutter not found");
            let flutter = std::fs::canonicalize(flutter)?;
            flutter
                .parent()
                .ok_or(Error::FlutterNotFound)?
                .parent()
                .ok_or(Error::FlutterNotFound)?
                .to_owned()
        };
        Ok(Flutter { root })
    }

    pub fn root(&self) -> &Path {
        &self.root
    }

    pub fn engine_version(&self) -> Result<String, Error> {
        let path = self
            .root
            .join("bin")
            .join("internal")
            .join("engine.version");
        Ok(std::fs::read_to_string(path).map(|v| v.trim().to_owned())?)
    }

    pub fn bundle(&self, cargo: &Cargo, build: Build) -> Result<(), Error> {
        let flag = match build {
            Build::Debug => "--debug",
            Build::Release => "--release",
            Build::Profile => "--profile",
        };

        let status = Command::new("flutter")
            .current_dir(cargo.workspace().root())
            .arg("build")
            .arg("bundle")
            .arg(flag)
            .arg("--track-widget-creation")
            .arg("--asset-dir")
            .arg(cargo.build_dir().join("flutter_assets"))
            .arg("--depfile")
            .arg(cargo.build_dir().join("snapshot_blob.bin.d"))
            .status()
            .expect("flutter build bundle");
        if status.code() != Some(0) {
            return Err(Error::FlutterError);
        }
        Ok(())
    }

    pub fn attach(&self, cargo: &Cargo, debug_uri: &str) -> Result<(), Error> {
        let debug_uri = format!("--debug-uri={}", debug_uri);
        let status = Command::new("flutter")
            .current_dir(cargo.workspace().root())
            .arg("attach")
            .arg("--device-id=flutter-tester")
            .arg(debug_uri)
            .status()
            .expect("Success");
        if status.code() != Some(0) {
            return Err(Error::FlutterError);
        }
        Ok(())
    }

    pub fn aot(
        &self,
        cargo: &Cargo,
        host_engine_path: &Path,
        target_engine_path: &Path,
    ) -> Result<(), Error> {
        let root = cargo.workspace().root();
        let build_dir = cargo.build_dir();
        let host_engine_dir = host_engine_path.parent().unwrap();
        let target_engine_dir = target_engine_path.parent().unwrap();
        let snapshot = build_dir.join("kernel_snapshot.dill");

        let status = Command::new(host_engine_dir.join("dart"))
            .current_dir(root)
            .arg(
                host_engine_dir
                    .join("gen")
                    .join("frontend_server.dart.snapshot"),
            )
            .arg("--sdk-root")
            .arg(host_engine_dir.join("flutter_patched_sdk"))
            .arg("--target=flutter")
            .arg("--aot")
            .arg("--tfa")
            .arg("-Ddart.vm.product=true")
            .arg("--packages")
            .arg(".packages")
            .arg("--output-dill")
            .arg(&snapshot)
            .arg(root.join("lib").join("main.dart"))
            .status()
            .expect("Success");

        if status.code() != Some(0) {
            return Err(Error::FlutterError);
        }

        let gen_snapshot = [
            "gen_snapshot",
            "gen_snapshot_x64",
            "gen_snapshot_x86",
            "gen_snapshot_host_targeting_host",
        ]
        .iter()
        .map(|bin| target_engine_dir.join(bin))
        .find(|path| path.exists())
        .ok_or(Error::GenSnapshotNotFound)?;

        let status = Command::new(gen_snapshot)
            .current_dir(root)
            .arg("--causal_async_stacks")
            .arg("--deterministic")
            .arg("--snapshot_kind=app-aot-elf")
            .arg("--strip")
            .arg(format!("--elf={}", build_dir.join("app.so").display()))
            .arg(&snapshot)
            .status()
            .expect("Success");

        if status.code() != Some(0) {
            return Err(Error::FlutterError);
        }

        Ok(())
    }
}