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
extern crate curl;
extern crate dirs;
extern crate unzip;

use curl::easy::Easy;
use std::{
    fs::{self, File},
    io::{BufReader, Write},
    path::{Path, PathBuf},
    process::Command,
    sync::{mpsc, Mutex},
    thread,
};

pub mod util;

pub use util::{get_flutter_version, Error};

#[derive(PartialEq, Copy, Clone)]
enum Target {
    Linux,
    Windows,
    MacOS,
}

pub fn download(version: &str) -> Result<mpsc::Receiver<(f64, f64)>, Error> {
    let dir = home_download_path();
    download_to(version, &dir)
}

pub fn download_to(version: &str, dir: &Path) -> Result<mpsc::Receiver<(f64, f64)>, Error> {
    let url = download_url(version);
    let dir = dir.to_path_buf().join(version);

    if !should_download(&dir) {
        println!("Flutter engine already exist. Download not necessary");
        return Err(Error::AlreadyDownloaded);
    }

    let (tx, rx) = mpsc::channel();
    thread::spawn(move || {
        // TODO: less unwrap, more error handling

        // Write the contents of rust-lang.org to stdout
        tx.send((0.0, 0.0)).unwrap();
        // create target dir

        fs::create_dir_all(&dir).unwrap();

        let download_file = dir.join("engine.zip");

        let mut file = File::create(&download_file).unwrap();

        let tx = Mutex::new(tx);

        let mut easy = Easy::new();

        println!("Starting download from {}", url);
        easy.url(&url).unwrap();
        easy.follow_location(true).unwrap();
        easy.progress(true).unwrap();
        easy.progress_function(move |total, done, _, _| {
            tx.lock().unwrap().send((total, done)).unwrap();
            true
        })
        .unwrap();
        easy.write_function(move |data| Ok(file.write(data).unwrap()))
            .unwrap();
        easy.perform().unwrap();

        println!("Download finished");

        println!("Extracting...");
        let zip_file = File::open(&download_file).unwrap();
        let reader = BufReader::new(zip_file);
        let unzipper = unzip::Unzipper::new(reader, &dir);
        unzipper.unzip().unwrap();

        // mac framework file is a double zip file
        if target() == Target::MacOS {
            Command::new("unzip")
                .args(&["FlutterEmbedder.framework.zip", "-d", "FlutterEmbedder.framework"])
                .current_dir(&dir)
                .status()
                .unwrap();

            // TODO: fixme
            // unzip bug! Extracted file corrupted!
            // let zip_file = File::open(dir.join("FlutterEmbedder.framework.zip")).unwrap();
            // let reader = BufReader::new(zip_file);
            // let unzipper = unzip::Unzipper::new(reader, dir.join("FlutterEmbedder.framework"));
            // unzipper.unzip().unwrap();
        }
    });

    Ok(rx)
}

pub fn home_download_path() -> PathBuf {
    let mut dir = dirs::home_dir().unwrap();
    dir.push(".flutter-rs");
    dir
}

pub fn download_url(version: &str) -> String {
    let url = match target() {
        Target::Linux => {
            "{base_url}/flutter_infra/flutter/{version}/linux-x64/linux-x64-embedder"
        }
        Target::MacOS => {
            "{base_url}/flutter_infra/flutter/{version}/darwin-x64/FlutterEmbedder.framework.zip"
        }
        Target::Windows => {
            "{base_url}/flutter_infra/flutter/{version}/windows-x64/windows-x64-embedder.zip"
        }
    };
    let base_url = std::env::var("FLUTTER_STORAGE_BASE_URL");
    let base_url = base_url
        .as_ref()
        .map(String::as_str)
        .unwrap_or("https://storage.googleapis.com");
    url.replace("{base_url}", base_url)
        .replace("{version}", version)
}

fn should_download(path: &Path) -> bool {
    match target() {
        Target::Linux => !path.join("libflutter_engine.so").exists(),
        Target::MacOS => !path.join("FlutterEmbedder.framework").exists(),
        Target::Windows => !path.join("flutter_engine.dll").exists(),
    }
}

fn target() -> Target {
    let target = std::env::var("TARGET").expect("Cannot determine target");
    if target.contains("linux") {
        Target::Linux
    } else if target.contains("apple") {
        Target::MacOS
    } else if target.contains("windows") {
        Target::Windows
    } else {
        panic!("Unknown target {}", target)
    }
}