pesde 0.7.3

A package manager for the Luau programming language, supporting multiple runtimes including Roblox and Lune
Documentation
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
use crate::{
	all_packages_dirs,
	download::DownloadGraphOptions,
	engine::runtime::Engines,
	graph::{
		DependencyGraph, DependencyGraphNode, DependencyGraphNodeWithTarget,
		DependencyGraphWithTarget,
	},
	linking::generator::get_file_types,
	manifest::DependencyType,
	reporters::{DownloadsReporter, PatchesReporter},
	scripts::{execute_script, ExecuteScriptHooks, ScriptName},
	source::{
		ids::PackageId,
		traits::{GetTargetOptions, PackageRef as _, PackageSource as _},
	},
	Project, RefreshedSources, LINK_LIB_NO_FILE_FOUND, SCRIPTS_LINK_FOLDER,
};
use fs_err::tokio as fs;
use futures::TryStreamExt as _;

use std::{
	collections::{HashMap, VecDeque},
	convert::Infallible,
	ffi::OsString,
	future::{self, Future},
	num::NonZeroUsize,
	path::{Path, PathBuf},
	sync::Arc,
};
use tokio::{
	pin,
	task::{spawn_blocking, JoinSet},
};
use tracing::{instrument, Instrument as _};

/// Hooks to perform actions after certain events during download and linking.
#[allow(unused_variables)]
pub trait DownloadAndLinkHooks: Send + Sync {
	/// The error type for the hooks.
	type Error: std::error::Error + Send + Sync + 'static;

	/// Called after scripts have been downloaded. The `downloaded_graph`
	/// contains all downloaded packages.
	fn on_scripts_downloaded(
		&self,
		graph: &DependencyGraphWithTarget,
	) -> impl Future<Output = Result<(), Self::Error>> + Send {
		future::ready(Ok(()))
	}

	/// Called after binary dependencies have been downloaded. The
	/// `downloaded_graph` contains all downloaded packages.
	fn on_bins_downloaded(
		&self,
		graph: &DependencyGraphWithTarget,
	) -> impl Future<Output = Result<(), Self::Error>> + Send {
		future::ready(Ok(()))
	}

	/// Called after all dependencies have been downloaded. The
	/// `downloaded_graph` contains all downloaded packages.
	fn on_all_downloaded(
		&self,
		graph: &DependencyGraphWithTarget,
	) -> impl Future<Output = Result<(), Self::Error>> + Send {
		future::ready(Ok(()))
	}
}

impl DownloadAndLinkHooks for () {
	type Error = Infallible;
}

/// Options for which dependencies to install.
#[derive(Debug, Clone, Copy)]
pub enum InstallDependenciesMode {
	/// Install all dependencies
	All,
	/// Install all dependencies, then remove [DependencyType::Dev] dependencies
	Prod,
	/// Only install dependencies which are [DependencyType::Dev]
	Dev,
}

impl InstallDependenciesMode {
	fn fits(self, dep_ty: DependencyType) -> bool {
		match (self, dep_ty) {
			(InstallDependenciesMode::Prod, DependencyType::Dev) => false,
			(InstallDependenciesMode::Dev, dep_ty) => dep_ty == DependencyType::Dev,

			_ => true,
		}
	}
}

/// Options for downloading and linking.
#[derive(Debug)]
pub struct DownloadAndLinkOptions<Reporter = (), Hooks = ()> {
	/// The reqwest client.
	pub reqwest: reqwest::Client,
	/// The downloads reporter.
	pub reporter: Option<Arc<Reporter>>,
	/// The download and link hooks.
	pub hooks: Option<Arc<Hooks>>,
	/// The refreshed sources.
	pub refreshed_sources: RefreshedSources,
	/// Which dependencies to install.
	pub install_dependencies_mode: InstallDependenciesMode,
	/// The max number of concurrent network requests.
	pub network_concurrency: NonZeroUsize,
	/// Whether to re-install all dependencies even if they are already installed
	pub force: bool,
	/// The engines this project is using
	pub engines: Arc<Engines>,
}

impl<Reporter, Hooks> DownloadAndLinkOptions<Reporter, Hooks>
where
	Reporter: DownloadsReporter + PatchesReporter + Send + Sync + 'static,
	Hooks: DownloadAndLinkHooks + Send + Sync + 'static,
{
	/// Creates a new download options with the given reqwest client and reporter.
	#[must_use]
	pub fn new(reqwest: reqwest::Client) -> Self {
		Self {
			reqwest,
			reporter: None,
			hooks: None,
			refreshed_sources: Default::default(),
			install_dependencies_mode: InstallDependenciesMode::All,
			network_concurrency: NonZeroUsize::new(16).unwrap(),
			force: false,
			engines: Default::default(),
		}
	}

	/// Sets the downloads reporter.
	#[must_use]
	pub fn reporter(mut self, reporter: impl Into<Arc<Reporter>>) -> Self {
		self.reporter.replace(reporter.into());
		self
	}

	/// Sets the download and link hooks.
	#[must_use]
	pub fn hooks(mut self, hooks: impl Into<Arc<Hooks>>) -> Self {
		self.hooks.replace(hooks.into());
		self
	}

	/// Sets the refreshed sources.
	#[must_use]
	pub fn refreshed_sources(mut self, refreshed_sources: RefreshedSources) -> Self {
		self.refreshed_sources = refreshed_sources;
		self
	}

	/// Sets which dependencies to install
	#[must_use]
	pub fn install_dependencies_mode(
		mut self,
		install_dependencies: InstallDependenciesMode,
	) -> Self {
		self.install_dependencies_mode = install_dependencies;
		self
	}

	/// Sets the max number of concurrent network requests.
	#[must_use]
	pub fn network_concurrency(mut self, network_concurrency: NonZeroUsize) -> Self {
		self.network_concurrency = network_concurrency;
		self
	}

	/// Sets whether to re-install all dependencies even if they are already installed
	#[must_use]
	pub fn force(mut self, force: bool) -> Self {
		self.force = force;
		self
	}

	/// Sets the engines this project is using
	#[must_use]
	pub fn engines(mut self, engines: impl Into<Arc<Engines>>) -> Self {
		self.engines = engines.into();
		self
	}
}

impl Clone for DownloadAndLinkOptions {
	fn clone(&self) -> Self {
		Self {
			reqwest: self.reqwest.clone(),
			reporter: self.reporter.clone(),
			hooks: self.hooks.clone(),
			refreshed_sources: self.refreshed_sources.clone(),
			install_dependencies_mode: self.install_dependencies_mode,
			network_concurrency: self.network_concurrency,
			force: self.force,
			engines: self.engines.clone(),
		}
	}
}

impl Project {
	/// Downloads a graph of dependencies and links them in the correct order
	#[instrument(
		skip_all,
		fields(install_dependencies = debug(options.install_dependencies_mode)),
		level = "debug"
	)]
	pub async fn download_and_link<Reporter, Hooks>(
		&self,
		graph: &DependencyGraph,
		options: DownloadAndLinkOptions<Reporter, Hooks>,
	) -> Result<DependencyGraphWithTarget, errors::DownloadAndLinkError<Hooks::Error>>
	where
		Reporter: DownloadsReporter + PatchesReporter + 'static,
		Hooks: DownloadAndLinkHooks + 'static,
	{
		let DownloadAndLinkOptions {
			reqwest,
			reporter,
			hooks,
			refreshed_sources,
			install_dependencies_mode,
			network_concurrency,
			force,
			engines,
		} = options;

		let reqwest = reqwest.clone();
		let manifest = self.deser_manifest().await?;

		if force {
			async fn remove_dir(dir: PathBuf) -> std::io::Result<()> {
				tracing::debug!("force deleting the `{}` folder", dir.display());

				match fs::remove_dir_all(dir).await {
					Ok(()) => Ok(()),
					Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
					Err(e) => Err(e),
				}
			}

			let mut tasks = all_packages_dirs()
				.into_iter()
				.map(|folder| remove_dir(self.package_dir().join(&folder)))
				.chain(std::iter::once(remove_dir(
					self.package_dir().join(SCRIPTS_LINK_FOLDER),
				)))
				.collect::<JoinSet<_>>();

			while let Some(task) = tasks.join_next().await {
				task.unwrap()?;
			}
		}

		// step 1. download dependencies
		let graph_to_download = {
			let mut download_graph_options = DownloadGraphOptions::<Reporter>::new(reqwest.clone())
				.refreshed_sources(refreshed_sources.clone())
				.network_concurrency(network_concurrency);

			if let Some(reporter) = reporter.clone() {
				download_graph_options = download_graph_options.reporter(reporter);
			}

			let correct_deps = if matches!(install_dependencies_mode, InstallDependenciesMode::All)
			{
				graph.clone()
			} else {
				let mut queue = graph
					.iter()
					.filter(|(_, node)| {
						node.direct
							.as_ref()
							.is_some_and(|(_, _, ty)| install_dependencies_mode.fits(*ty))
					})
					.collect::<VecDeque<_>>();

				let mut correct_deps = DependencyGraph::new();
				while let Some((id, node)) = queue.pop_front() {
					if correct_deps.insert(id.clone(), node.clone()).is_some() {
						// prevent an infinite loop with recursive dependencies
						continue;
					}

					node.dependencies
						.values()
						.filter_map(|(id, _)| graph.get(id).map(|node| (id, node)))
						.for_each(|x| queue.push_back(x));
				}

				correct_deps
			};

			let mut downloaded_graph = HashMap::new();

			let graph_to_download = if force {
				correct_deps
			} else {
				let mut tasks = correct_deps
					.into_iter()
					.map(|(id, node)| {
						let container_folder: Arc<Path> = node
							.container_folder_from_project(&id, self, manifest.target.kind())
							.into();

						async move {
							return (
								// force local packages to be updated
								if node.pkg_ref.is_local() {
									None
								} else {
									fs::metadata(&container_folder)
										.await
										.is_ok()
										.then_some(container_folder)
								},
								id,
								node,
							);
						}
					})
					.collect::<JoinSet<_>>();

				let mut graph_to_download = DependencyGraph::new();
				while let Some(task) = tasks.join_next().await {
					let (install_path, id, node) = task.unwrap();
					if let Some(install_path) = install_path {
						downloaded_graph.insert(id, (node, install_path, false));
						continue;
					}

					graph_to_download.insert(id, node);
				}

				graph_to_download
			};

			let span = tracing::debug_span!("download");
			let _guard = span.enter();

			let downloaded =
				self.download_graph(&graph_to_download, download_graph_options.clone())?;
			pin!(downloaded);

			let mut tasks = JoinSet::new();

			while let Some((id, node, fs)) = downloaded.try_next().await? {
				let container_folder =
					node.container_folder_from_project(&id, self, manifest.target.kind());

				let project = self.clone();

				tasks.spawn(async move {
					fs::create_dir_all(&container_folder).await?;

					fs.write_to(&container_folder, project.cas_dir(), true)
						.await
						.map_err(errors::DownloadAndLinkError::<Hooks::Error>::from)?;

					Ok::<_, errors::DownloadAndLinkError<Hooks::Error>>((
						id,
						(node, container_folder.into(), false),
					))
				});
			}

			while let Some(task) = tasks.join_next().await {
				let (id, data) = task.unwrap()?;
				downloaded_graph.insert(id, data);
			}

			#[cfg(feature = "patches")]
			{
				use crate::patches::apply_patch;
				let mut tasks = manifest
					.patches
					.iter()
					.flat_map(|(name, versions)| {
						versions
							.iter()
							.map(|(v_id, path)| (PackageId::new(name.clone(), v_id.clone()), path))
					})
					.filter_map(|(id, patch_path)| {
						downloaded_graph.get(&id).map(|(_, container_folder, _)| {
							(id, container_folder.clone(), patch_path)
						})
					})
					.map(|(id, container_folder, patch_path)| {
						let patch_path = patch_path.to_path(self.package_dir());
						let reporter = reporter.clone();

						async move {
							match reporter {
								Some(reporter) => {
									apply_patch(
										&id,
										container_folder,
										&patch_path,
										reporter.clone(),
									)
									.await
								}
								None => {
									apply_patch(&id, container_folder, &patch_path, ().into()).await
								}
							}
						}
					})
					.collect::<JoinSet<_>>();

				while let Some(task) = tasks.join_next().await {
					task.unwrap()?;
				}
			}

			downloaded_graph
		};

		let graph_download_data = graph_to_download
			.iter()
			.map(|(id, (_, install_path, _))| (id.clone(), install_path.clone()))
			.collect::<HashMap<_, _>>();

		let (wally_graph_to_download, other_graph_to_download) =
			graph_to_download
				.into_iter()
				.partition::<HashMap<_, _>, _>(|(_, (node, _, _))| node.pkg_ref.is_wally_package());

		let mut graph = DependencyGraphWithTarget::new();

		async fn get_graph_targets<Hooks: DownloadAndLinkHooks>(
			graph: &mut DependencyGraphWithTarget,
			project: &Project,
			downloaded_graph: HashMap<PackageId, (DependencyGraphNode, Arc<Path>, bool)>,
			engines: &Arc<Engines>,
		) -> Result<Vec<Vec<OsString>>, errors::DownloadAndLinkError<Hooks::Error>> {
			let mut tasks = downloaded_graph
				.into_iter()
				.map(|(id, (node, install_path, local))| {
					let source = node.pkg_ref.source();

					let id = Arc::new(id);
					let project = project.clone();
					let engines = engines.clone();

					async move {
						let target = source
							.get_target(
								&node.pkg_ref,
								&GetTargetOptions {
									project: project.clone(),
									path: install_path.clone(),
									id: id.clone(),
									engines: engines.clone(),
								},
							)
							.await?;

						let sync_config_args = Some(&target)
							.filter(|_| !node.pkg_ref.is_wally_package() && !local)
							.and_then(|t| t.build_files())
							.map(|build_files| {
								std::iter::once(install_path.as_os_str().to_os_string())
									.chain(build_files.iter().map(OsString::from))
									.collect::<Vec<_>>()
							});

						Ok::<_, errors::DownloadAndLinkError<Hooks::Error>>((
							Arc::into_inner(id).unwrap(),
							DependencyGraphNodeWithTarget { target, node },
							sync_config_args,
						))
					}
				})
				.collect::<JoinSet<_>>();

			let mut sync_config_args = vec![];

			while let Some(task) = tasks.join_next().await {
				let (id, node, args) = task.unwrap()?;
				graph.insert(id, node);
				if let Some(args) = args {
					sync_config_args.push(args);
				}
			}

			Ok(sync_config_args)
		}

		// step 2. get targets for non Wally packages (Wally packages require the scripts packages to be downloaded first)
		let sync_config_args =
			get_graph_targets::<Hooks>(&mut graph, self, other_graph_to_download, &engines)
				.instrument(tracing::debug_span!("get targets (non-wally)"))
				.await?;

		self.link(&graph, &manifest, &Default::default(), false)
			.instrument(tracing::debug_span!("link (non-wally)"))
			.await?;

		#[derive(Debug, Clone, Copy)]
		struct LinkingExecuteScriptHooks;

		impl ExecuteScriptHooks for LinkingExecuteScriptHooks {
			fn not_found(&self, script: ScriptName) {
				tracing::warn!(
					"not having a `{script}` script in the manifest might cause issues with linking"
				);
			}
		}

		let mut tasks = sync_config_args
			.into_iter()
			.map(|args| {
				let project = self.clone();
				let engines = engines.clone();

				async move {
					execute_script(
						ScriptName::RobloxSyncConfigGenerator,
						&project,
						&engines,
						LinkingExecuteScriptHooks,
						args,
						false,
					)
					.await
					.map(|_| ())
				}
			})
			.collect::<JoinSet<_>>();

		while let Some(result) = tasks.join_next().await {
			result.unwrap()?;
		}

		if let Some(hooks) = &hooks {
			hooks
				.on_scripts_downloaded(&graph)
				.await
				.map_err(errors::DownloadAndLinkError::Hook)?;

			hooks
				.on_bins_downloaded(&graph)
				.await
				.map_err(errors::DownloadAndLinkError::Hook)?;
		}

		// step 3. get targets for Wally packages
		get_graph_targets::<Hooks>(&mut graph, self, wally_graph_to_download, &engines)
			.instrument(tracing::debug_span!("get targets (wally)"))
			.await?;

		let mut tasks = graph_download_data
			.into_iter()
			.map(|(package_id, install_path)| {
				let span =
					tracing::info_span!("extract types", package_id = package_id.to_string());

				let node = graph[&package_id].clone();

				async move {
					let Some(lib_file) = node.target.lib_path() else {
						return Ok((package_id, vec![]));
					};

					let types = if lib_file.as_str() == LINK_LIB_NO_FILE_FOUND {
						vec![]
					} else {
						let lib_file = lib_file.to_path(&install_path);

						let contents =
							match fs::read_to_string(&lib_file).await {
								Ok(contents) => contents,
								Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
									return Err(errors::DownloadAndLinkError::<Hooks::Error>::LibFileNotFound(
									lib_file.into_boxed_path(),
								));
								}
								Err(e) => return Err(e.into()),
							};

						let types = spawn_blocking(move || get_file_types(&contents))
							.await
							.unwrap();

						tracing::debug!("contains {} exported types", types.len());

						types
					};

					Ok((package_id, types))
				}
				.instrument(span)
			})
			.collect::<JoinSet<_>>();

		let mut package_types = HashMap::<PackageId, Vec<String>>::default();

		while let Some(task) = tasks.join_next().await {
			let (id, types) = task.unwrap()?;
			package_types.insert(id, types);
		}

		// step 4. link ALL dependencies. do so with types
		self.link(&graph, &manifest, &package_types, true)
			.instrument(tracing::debug_span!("link (all)"))
			.await?;

		if let Some(hooks) = &hooks {
			hooks
				.on_all_downloaded(&graph)
				.await
				.map_err(errors::DownloadAndLinkError::Hook)?;
		}

		if matches!(install_dependencies_mode, InstallDependenciesMode::Prod) || !force {
			self.remove_unused(&graph).await?;
		}

		Ok(graph)
	}
}

/// Errors that can occur when downloading and linking dependencies
pub mod errors {
	use std::path::Path;

	use thiserror::Error;

	/// An error that can occur when downloading and linking dependencies
	#[derive(Debug, Error)]
	#[non_exhaustive]
	pub enum DownloadAndLinkError<E> {
		/// Reading the manifest failed
		#[error("error reading manifest")]
		ManifestRead(#[from] crate::errors::ManifestReadError),

		/// An error occurred while downloading the graph
		#[error("error downloading graph")]
		DownloadGraph(#[from] crate::download::errors::DownloadGraphError),

		/// An error occurred while linking dependencies
		#[error("error linking dependencies")]
		Linking(#[from] crate::linking::errors::LinkingError),

		/// An error occurred while executing the pesde callback
		#[error("error executing hook")]
		Hook(#[source] E),

		/// IO error
		#[error("io error")]
		Io(#[from] std::io::Error),

		/// Error getting a target
		#[error("error getting target")]
		GetTarget(#[from] crate::source::errors::GetTargetError),

		/// Removing unused dependencies failed
		#[error("error removing unused dependencies")]
		RemoveUnused(#[from] crate::linking::incremental::errors::RemoveUnusedError),

		/// Patching a package failed
		#[cfg(feature = "patches")]
		#[error("error applying patch")]
		Patch(#[from] crate::patches::errors::ApplyPatchError),

		/// Executing a script failed
		#[error("error executing script")]
		ExecuteScript(#[from] crate::scripts::errors::ExecuteScriptError),

		/// The library file was not found
		#[error("library file at `{0}` not found")]
		LibFileNotFound(Box<Path>),
	}
}