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
|
//! ref: composer/src/Composer/Downloader/ArchiveDownloader.php
use anyhow::Result;
use indexmap::IndexMap;
use shirabe_external_packages::symfony::component::finder::Finder;
use shirabe_php_shim::{
DIRECTORY_SEPARATOR, RuntimeException, bin2hex, file_exists, is_dir, random_bytes, realpath,
};
use crate::dependency_resolver::operation::InstallOperation;
use crate::downloader::DownloaderInterface;
use crate::downloader::FileDownloader;
use crate::package::PackageInterface;
use crate::util::Platform;
pub trait ArchiveDownloader {
fn inner(&self) -> &FileDownloader;
fn inner_mut(&mut self) -> &mut FileDownloader;
fn cleanup_executed(&self) -> &IndexMap<String, bool>;
fn cleanup_executed_mut(&mut self) -> &mut IndexMap<String, bool>;
async fn extract(
&self,
package: &dyn PackageInterface,
file: &str,
path: &str,
) -> Result<Option<PhpMixed>>;
async fn prepare(
&mut self,
r#type: &str,
package: &dyn PackageInterface,
path: &str,
prev_package: Option<&dyn PackageInterface>,
) -> Result<Option<PhpMixed>> {
self.cleanup_executed_mut().remove(package.get_name());
self.inner_mut()
.prepare(r#type, package, path, prev_package)
}
async fn cleanup(
&mut self,
r#type: &str,
package: &dyn PackageInterface,
path: &str,
prev_package: Option<&dyn PackageInterface>,
) -> Result<Option<PhpMixed>> {
self.cleanup_executed_mut()
.insert(package.get_name().to_string(), true);
self.inner_mut()
.cleanup(r#type, package, path, prev_package)
}
/// @inheritDoc
///
/// @throws \RuntimeException
/// @throws \UnexpectedValueException
async fn install(
&mut self,
package: &dyn PackageInterface,
path: &str,
output: bool,
) -> Result<Option<PhpMixed>> {
if output {
self.inner().io.write_error(&format!(
" - {}{}",
InstallOperation::format(package, false),
self.get_install_operation_appendix(package, path)
));
}
let vendor_dir = self
.inner()
.config
.borrow_mut()
.get("vendor-dir")
.as_string()
.unwrap_or("")
.to_string();
// clean up the target directory, unless it contains the vendor dir, as the vendor dir contains
// the archive to be extracted. This is the case when installing with create-project in the current directory
// but in that case we ensure the directory is empty already in ProjectInstaller so no need to empty it here.
if !self
.inner()
.filesystem
.borrow()
.normalize_path(&vendor_dir)
.contains(
&self
.inner()
.filesystem
.borrow()
.normalize_path(&format!("{}{}", path, DIRECTORY_SEPARATOR)),
)
{
self.inner_mut()
.filesystem
.borrow_mut()
.empty_directory(path, true);
}
let temporary_dir = loop {
let candidate = format!("{}/composer/{}", vendor_dir, bin2hex(&random_bytes(4)));
if !is_dir(&candidate) {
break candidate;
}
};
self.inner_mut().add_cleanup_path(package, &temporary_dir);
// avoid cleaning up $path if installing in "." for eg create-project as we can not
// delete the directory we are currently in on windows
if !is_dir(path) || realpath(path) != Some(Platform::get_cwd(false).unwrap_or_default()) {
self.inner_mut().add_cleanup_path(package, path);
}
self.inner_mut()
.filesystem
.borrow_mut()
.ensure_directory_exists(&temporary_dir);
let file_name = self.inner().get_file_name(package, path);
let _ = file_name;
let promise = self.extract(package, "", &temporary_dir)?;
// TODO(phase-b): the original PHP chains React promise `.then(onFulfilled, onRejected)`
// callbacks that capture `$this`, `$filesystem`, `$package`, `$path`, `$temporaryDir`,
// `$fileName`, and a recursive `$renameRecursively` closure. PromiseInterface::then in
// Rust expects `FnOnce(Option<PhpMixed>) -> Option<PhpMixed>` and the callbacks here
// need both `&mut self` access and to return another promise. This needs a structural
// rework (likely splitting the trait or adding a `then_boxed_result` adapter), plus a
// way to share `&mut self` with the closure (probably `Rc<RefCell<...>>`).
let _ = (&promise, &temporary_dir, package, path);
todo!(
"ArchiveDownloader::install: rewire .then(onFulfilled, onRejected) chain to match PromiseInterface signature"
)
}
/// @inheritDoc
fn get_install_operation_appendix(&self, _package: &dyn PackageInterface, _path: &str) -> &str {
": Extracting archive"
}
}
|