-
Notifications
You must be signed in to change notification settings - Fork 1
/
flutter.rs
82 lines (66 loc) · 2.2 KB
/
flutter.rs
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
use displaydoc::Display;
use serde::Deserialize;
use super::{remove_dirs, status_from_dirs, BuildStatus, BuildTool, BuildToolKind, BuildToolProbe};
use crate::build_tool_manager::BuildToolManager;
use std::{
fs,
path::{Path, PathBuf},
};
pub fn register(manager: &mut BuildToolManager) {
let probe = Box::new(FlutterProbe {});
manager.register(probe);
}
#[derive(Debug)]
pub struct FlutterProbe;
impl BuildToolProbe for FlutterProbe {
fn probe(&self, dir: &Path) -> Option<Box<dyn BuildTool>> {
let yaml_path = dir.join("pubspec.yaml");
Pubspec::try_from(yaml_path.as_path()).ok().map(|pubspec| {
Box::new(Flutter {
dir: dir.to_owned(),
pubspec,
}) as Box<dyn BuildTool>
})
}
fn applies_to(&self, kind: BuildToolKind) -> bool {
kind == BuildToolKind::Flutter
}
}
#[derive(Debug, Display)]
/// Flutter
pub struct Flutter {
dir: PathBuf,
pubspec: Pubspec,
}
static EPHEMERAL_DIRS: &[&str] = &["build", ".dart_tool"];
impl BuildTool for Flutter {
fn clean_project(&mut self, dry_run: bool) -> anyhow::Result<()> {
// `flutter clean` exists, but according to its documentation it would
// "Delete the build/ and .dart_tool/ directories" anyway. By doing this
// directly, we don't require flutter to be installed.
remove_dirs(&self.dir, EPHEMERAL_DIRS, dry_run)
}
fn status(&self) -> anyhow::Result<BuildStatus> {
status_from_dirs(&self.dir, EPHEMERAL_DIRS)
}
fn project_name(&self) -> Option<anyhow::Result<String>> {
Some(Ok(self.pubspec.name.clone()))
}
}
#[derive(Debug, Deserialize)]
struct Pubspec {
name: String,
// Increases confidence this is a Flutter project file
#[serde(rename(deserialize = "version"))]
_version: String,
// Increases confidence this is a Flutter project file
#[serde(rename(deserialize = "flutter"))]
_flutter: serde_yaml::Value,
}
impl TryFrom<&Path> for Pubspec {
type Error = anyhow::Error;
fn try_from(yaml_path: &Path) -> Result<Self, Self::Error> {
let pubspec: Pubspec = serde_yaml::from_str(&fs::read_to_string(yaml_path)?)?;
Ok(pubspec)
}
}