polyhorn_cli/android/tasks/
find_android_studio.rs

1#![allow(unused_imports, unused_variables, unused_mut)]
2
3use std::path::Path;
4
5use super::{AndroidContext, AndroidError};
6use crate::core::{Manager, Task};
7
8/// This task attempts to locate Android Studio and uses the result to locate
9/// the Android SDK and Java home. The Android Studio itself isn't used, but
10/// it's the easiest way of obtaining a working installation of the SDK, NDK and
11/// Java that are all compatible.
12pub struct FindAndroidStudio;
13
14impl Task for FindAndroidStudio {
15    type Context = AndroidContext;
16    type Error = AndroidError;
17
18    fn verb(&self) -> &str {
19        "Configuring"
20    }
21
22    fn message(&self) -> &str {
23        "Android Studio"
24    }
25
26    fn detail(&self) -> &str {
27        ""
28    }
29
30    fn run(
31        &self,
32        mut context: AndroidContext,
33        _manager: &mut Manager,
34    ) -> Result<AndroidContext, AndroidError> {
35        #[cfg(target_os = "macos")]
36        {
37            // We start by looking for Android Studio itself.
38            let path = Path::new("/Applications/Android Studio.app").to_path_buf();
39            let studio = match path.exists() {
40                true => path,
41                false => return Err(AndroidError::AndroidStudioNotFound(path)),
42            };
43
44            // Android Studio ships with a compatible release of OpenJDK 8.
45            let mut java_home = studio.clone();
46            java_home.push("Contents/jre/jdk/Contents/Home");
47
48            match java_home.exists() {
49                true => context.java_home = Some(java_home),
50                false => return Err(AndroidError::JavaNotFound(java_home)),
51            }
52
53            // If Android Studio is installed, and the user has opted to install
54            // the default components, the SDK will have been installed in
55            // `~/Library/Android/sdk`.
56            let android_sdk_root = Path::new(&format!(
57                "{}/Library/Android/sdk",
58                dirs::home_dir().unwrap().to_str().unwrap()
59            ))
60            .to_path_buf();
61
62            match android_sdk_root.exists() {
63                true => context.android_sdk_root = Some(android_sdk_root),
64                false => return Err(AndroidError::AndroidSDKNotFound(android_sdk_root)),
65            }
66
67            return Ok(context);
68        }
69
70        // TODO: we don't have heuristics to find Android Studio on non-macOS
71        // systems yet.
72        #[allow(unreachable_code)]
73        Err(AndroidError::UnsupportedHostOS(
74            "We can't yet automatically locate Android Studio on your OS.",
75        ))
76    }
77}