aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/installer/project_installer.rs
blob: 83ffbb245493d5c0f733aae0bb4e6752d92ad936 (plain)
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
//! ref: composer/src/Composer/Installer/ProjectInstaller.php

use crate::downloader::DownloadManagerInterface;
use crate::installer::InstallerInterface;
use crate::package::PackageInterfaceHandle;
use crate::repository::InstalledRepositoryInterface;
use crate::util::Filesystem;
use shirabe_php_shim::{InvalidArgumentException, PhpMixed};

#[derive(Debug)]
pub struct ProjectInstaller {
    install_path: String,
    download_manager: std::rc::Rc<std::cell::RefCell<dyn DownloadManagerInterface>>,
    filesystem: std::rc::Rc<std::cell::RefCell<Filesystem>>,
}

impl ProjectInstaller {
    pub fn new(
        install_path: &str,
        dm: std::rc::Rc<std::cell::RefCell<dyn DownloadManagerInterface>>,
        fs: std::rc::Rc<std::cell::RefCell<Filesystem>>,
    ) -> Self {
        let install_path = format!("{}/", install_path.replace('\\', "/").trim_end_matches('/'));
        Self {
            install_path,
            download_manager: dm,
            filesystem: fs,
        }
    }
}

#[async_trait::async_trait(?Send)]
impl InstallerInterface for ProjectInstaller {
    fn supports(&self, _package_type: &str) -> bool {
        true
    }

    fn is_installed(
        &mut self,
        _repo: &dyn InstalledRepositoryInterface,
        _package: PackageInterfaceHandle,
    ) -> bool {
        false
    }

    async fn download(
        &mut self,
        package: PackageInterfaceHandle,
        prev_package: Option<PackageInterfaceHandle>,
    ) -> anyhow::Result<Option<PhpMixed>> {
        let install_path = &self.install_path;
        if std::path::Path::new(install_path).exists()
            && !self.filesystem.borrow().is_dir_empty(install_path)
        {
            return Err(InvalidArgumentException {
                message: format!("Project directory {} is not empty.", install_path),
                code: 0,
            }
            .into());
        }
        if !std::path::Path::new(install_path).is_dir() {
            std::fs::create_dir_all(install_path)?;
        }

        self.download_manager
            .borrow()
            .download(package, install_path, prev_package)
            .await
    }

    async fn prepare(
        &mut self,
        r#type: &str,
        package: PackageInterfaceHandle,
        prev_package: Option<PackageInterfaceHandle>,
    ) -> anyhow::Result<Option<PhpMixed>> {
        self.download_manager
            .borrow()
            .prepare(r#type, package, &self.install_path, prev_package)
            .await
    }

    async fn cleanup(
        &mut self,
        r#type: &str,
        package: PackageInterfaceHandle,
        prev_package: Option<PackageInterfaceHandle>,
    ) -> anyhow::Result<Option<PhpMixed>> {
        self.download_manager
            .borrow()
            .cleanup(r#type, package, &self.install_path, prev_package)
            .await
    }

    async fn install(
        &mut self,
        _repo: &mut dyn InstalledRepositoryInterface,
        package: PackageInterfaceHandle,
    ) -> anyhow::Result<Option<PhpMixed>> {
        self.download_manager
            .borrow()
            .install(package, &self.install_path)
            .await
    }

    async fn update(
        &mut self,
        _repo: &mut dyn InstalledRepositoryInterface,
        _initial: PackageInterfaceHandle,
        _target: PackageInterfaceHandle,
    ) -> anyhow::Result<Option<PhpMixed>> {
        Err(InvalidArgumentException {
            message: "not supported".to_string(),
            code: 0,
        }
        .into())
    }

    async fn uninstall(
        &mut self,
        _repo: &mut dyn InstalledRepositoryInterface,
        _package: PackageInterfaceHandle,
    ) -> anyhow::Result<Option<PhpMixed>> {
        Err(InvalidArgumentException {
            message: "not supported".to_string(),
            code: 0,
        }
        .into())
    }

    fn get_install_path(&mut self, _package: PackageInterfaceHandle) -> Option<String> {
        Some(self.install_path.clone())
    }
}