aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/repository/artifact_repository.rs
blob: d869156f3ad0c60ad69c5b6324a7626d24ac28fe (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//! ref: composer/src/Composer/Repository/ArtifactRepository.php

use crate::io::io_interface;
use std::path::Path;

use indexmap::IndexMap;
use shirabe_php_shim::{
    PhpMixed, RuntimeException, UnexpectedValueException, extension_loaded, hash_file,
};

use crate::io::io_interface::IOInterface;
use crate::json::json_file::JsonFile;
use crate::package::base_package::BasePackage;
use crate::package::loader::array_loader::ArrayLoader;
use crate::package::loader::loader_interface::LoaderInterface;
use crate::repository::array_repository::ArrayRepository;
use crate::repository::configurable_repository_interface::ConfigurableRepositoryInterface;
use crate::util::platform::Platform;
use crate::util::tar::Tar;
use crate::util::zip::Zip;

pub struct ArtifactRepository {
    inner: ArrayRepository,
    pub(crate) loader: Box<dyn LoaderInterface>,
    pub(crate) lookup: String,
    pub(crate) repo_config: IndexMap<String, PhpMixed>,
    io: Box<dyn IOInterface>,
}

impl std::fmt::Debug for ArtifactRepository {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ArtifactRepository")
            .field("lookup", &self.lookup)
            .field("repo_config", &self.repo_config)
            .finish()
    }
}

impl ArtifactRepository {
    pub fn new(
        repo_config: IndexMap<String, PhpMixed>,
        io: Box<dyn IOInterface>,
    ) -> anyhow::Result<Self> {
        if !extension_loaded("zip") {
            return Err(RuntimeException {
                message: "The artifact repository requires PHP's zip extension".to_string(),
                code: 0,
            }
            .into());
        }

        let url = repo_config["url"].as_string().unwrap_or("").to_string();
        let lookup = Platform::expand_path(&url);
        Ok(Self {
            inner: ArrayRepository::new(),
            loader: Box::new(ArrayLoader::new()),
            lookup,
            repo_config,
            io,
        })
    }

    pub fn get_repo_name(&self) -> String {
        format!("artifact repo ({})", self.lookup)
    }

    fn initialize(&mut self) -> anyhow::Result<()> {
        self.inner.initialize()?;
        let lookup = self.lookup.clone();
        self.scan_directory(&lookup)
    }

    fn scan_directory(&mut self, path: &str) -> anyhow::Result<()> {
        let entries = std::fs::read_dir(path)?;
        for entry in entries {
            let entry = entry?;
            let file_path = entry.path();

            if file_path.is_symlink() {
                let resolved = std::fs::canonicalize(&file_path)?;
                if resolved.is_dir() {
                    self.scan_directory(resolved.to_str().unwrap_or(""))?;
                    continue;
                }
            }

            if file_path.is_dir() {
                self.scan_directory(file_path.to_str().unwrap_or(""))?;
                continue;
            }

            if !file_path.is_file() {
                continue;
            }

            let ext = file_path
                .extension()
                .and_then(|e| e.to_str())
                .unwrap_or("")
                .to_lowercase();
            if !matches!(ext.as_str(), "zip" | "tar" | "gz" | "tgz") {
                continue;
            }

            let package = self.get_composer_information(&file_path)?;
            let basename = file_path.file_name().and_then(|n| n.to_str()).unwrap_or("");
            match package {
                None => {
                    self.io.write_error(
                        &format!(
                            "File <comment>{}</comment> doesn't seem to hold a package",
                            basename
                        ),
                        true,
                        io_interface::VERBOSE,
                    );
                }
                Some(package) => {
                    self.io.write_error(
                        &format!(
                            "Found package <info>{}</info> (<comment>{}</comment>) in file <info>{}</info>",
                            package.get_name(),
                            package.get_pretty_version(),
                            basename,
                        ),
                        true,
                        io_interface::VERBOSE,
                    );
                    self.inner.add_package(package);
                }
            }
        }
        Ok(())
    }

    fn get_composer_information(&self, file: &Path) -> anyhow::Result<Option<Box<BasePackage>>> {
        let mut json: Option<String> = None;
        let file_extension = file
            .extension()
            .and_then(|e| e.to_str())
            .unwrap_or("")
            .to_lowercase();

        let file_type: &str;
        if matches!(file_extension.as_str(), "gz" | "tar" | "tgz") {
            file_type = "tar";
        } else if file_extension == "zip" {
            file_type = "zip";
        } else {
            return Err(RuntimeException {
                message: format!(
                    "Files with \"{}\" extensions aren't supported. Only ZIP and TAR/TAR.GZ/TGZ archives are supported.",
                    file_extension
                ),
                code: 0,
            }
            .into());
        }

        let pathname = file.to_str().unwrap_or("");
        let get_result = if file_type == "tar" {
            Tar::get_composer_json(pathname)
        } else {
            Zip::get_composer_json(pathname)
        };
        match get_result {
            Ok(j) => json = j,
            Err(exception) => {
                self.io.write(
                    &format!("Failed loading package {}: {}", pathname, exception),
                    false,
                    io_interface::VERBOSE,
                );
            }
        }

        if json.is_none() {
            return Ok(None);
        }

        let mut package =
            JsonFile::parse_json(&json.unwrap(), &format!("{}#composer.json", pathname))?;
        let url_normalized = pathname.replace('\\', '/');
        let real_path = file
            .canonicalize()
            .ok()
            .and_then(|p| p.to_str().map(|s| s.to_string()))
            .unwrap_or_default();
        let shasum = hash_file("sha1", &real_path).unwrap_or_default();

        let mut dist = IndexMap::new();
        dist.insert(
            "type".to_string(),
            Box::new(PhpMixed::String(file_type.to_string())),
        );
        dist.insert(
            "url".to_string(),
            Box::new(PhpMixed::String(url_normalized)),
        );
        dist.insert("shasum".to_string(), Box::new(PhpMixed::String(shasum)));
        package.insert("dist".to_string(), Box::new(PhpMixed::Array(dist)));

        match self.loader.load(package, None) {
            Ok(package) => Ok(Some(package)),
            Err(exception) => Err(UnexpectedValueException {
                message: format!("Failed loading package in {}: {}", pathname, exception),
                code: 0,
            }
            .into()),
        }
    }
}

impl ConfigurableRepositoryInterface for ArtifactRepository {
    fn get_repo_config(&self) -> IndexMap<String, PhpMixed> {
        self.repo_config.clone()
    }
}