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
#![warn(missing_docs)]
//! A package manager for the Luau programming language, supporting multiple runtimes including Roblox and Lune.
//! pesde has its own registry, however it can also use Wally, and Git repositories as package sources.
//! It has been designed with multiple targets in mind, namely Roblox, Lune, and Luau.

use crate::{
	lockfile::Lockfile,
	manifest::{target::TargetKind, Manifest},
	source::{
		traits::{PackageSource as _, RefreshOptions},
		PackageSources,
	},
};
use async_stream::try_stream;
use fs_err::tokio as fs;
use futures::Stream;
use gix::sec::identity::Account;
use semver::{Version, VersionReq};
use std::{
	collections::{HashMap, HashSet},
	fmt::Debug,
	hash::{Hash as _, Hasher as _},
	path::{Path, PathBuf},
	sync::Arc,
};
use tokio::io::AsyncReadExt as _;
use tracing::instrument;
use wax::Pattern as _;

/// Downloading packages
pub mod download;
/// Utility for downloading and linking in the correct order
pub mod download_and_link;
/// Handling of engines
pub mod engine;
/// Graphs
pub mod graph;
/// Linking packages
pub mod linking;
/// Lockfile
pub mod lockfile;
/// Manifest
pub mod manifest;
/// Package names
pub mod names;
/// Patching packages
#[cfg(feature = "patches")]
pub mod patches;
pub mod reporters;
/// Resolving packages
pub mod resolver;
/// Running scripts
pub mod scripts;
/// Package sources
pub mod source;
pub(crate) mod util;

/// The name of the manifest file
pub const MANIFEST_FILE_NAME: &str = "pesde.toml";
/// The name of the lockfile
pub const LOCKFILE_FILE_NAME: &str = "pesde.lock";
/// The name of the default index
pub const DEFAULT_INDEX_NAME: &str = "default";
/// The name of the packages container
pub const PACKAGES_CONTAINER_NAME: &str = ".pesde";
pub(crate) const LINK_LIB_NO_FILE_FOUND: &str = "____pesde_no_export_file_found";
/// The folder in which scripts are linked
pub const SCRIPTS_LINK_FOLDER: &str = ".pesde";

pub(crate) fn default_index_name() -> String {
	DEFAULT_INDEX_NAME.into()
}

#[derive(Debug, Default)]
struct AuthConfigShared {
	tokens: HashMap<gix::Url, String>,
	git_credentials: Option<Account>,
}

/// Struct containing the authentication configuration
#[derive(Debug, Clone, Default)]
pub struct AuthConfig {
	shared: Arc<AuthConfigShared>,
}

impl AuthConfig {
	/// Create a new `AuthConfig`
	#[must_use]
	pub fn new() -> Self {
		AuthConfig::default()
	}

	/// Set the tokens
	/// Panics if the `AuthConfig` is shared
	#[must_use]
	pub fn with_tokens<I: IntoIterator<Item = (gix::Url, S)>, S: AsRef<str>>(
		mut self,
		tokens: I,
	) -> Self {
		Arc::get_mut(&mut self.shared).unwrap().tokens = tokens
			.into_iter()
			.map(|(url, s)| (url, s.as_ref().to_string()))
			.collect();
		self
	}

	/// Set the git credentials
	/// Panics if the `AuthConfig` is shared
	#[must_use]
	pub fn with_git_credentials(mut self, git_credentials: Option<Account>) -> Self {
		Arc::get_mut(&mut self.shared).unwrap().git_credentials = git_credentials;
		self
	}

	/// Get the tokens
	#[must_use]
	pub fn tokens(&self) -> &HashMap<gix::Url, String> {
		&self.shared.tokens
	}

	/// Get the git credentials
	#[must_use]
	pub fn git_credentials(&self) -> Option<&Account> {
		self.shared.git_credentials.as_ref()
	}
}

#[derive(Debug)]
struct ProjectShared {
	package_dir: PathBuf,
	workspace_dir: Option<PathBuf>,
	data_dir: PathBuf,
	cas_dir: PathBuf,
	auth_config: AuthConfig,
}

/// The main struct of the pesde library, representing a project
/// Unlike `ProjectShared`, this struct is `Send` and `Sync` and is cheap to clone because it is `Arc`-backed
#[derive(Debug, Clone)]
pub struct Project {
	shared: Arc<ProjectShared>,
}

impl Project {
	/// Create a new `Project`
	#[must_use]
	pub fn new(
		package_dir: impl AsRef<Path>,
		workspace_dir: Option<impl AsRef<Path>>,
		data_dir: impl AsRef<Path>,
		cas_dir: impl AsRef<Path>,
		auth_config: AuthConfig,
	) -> Self {
		Project {
			shared: ProjectShared {
				package_dir: package_dir.as_ref().to_path_buf(),
				workspace_dir: workspace_dir.map(|d| d.as_ref().to_path_buf()),
				data_dir: data_dir.as_ref().to_path_buf(),
				cas_dir: cas_dir.as_ref().to_path_buf(),
				auth_config,
			}
			.into(),
		}
	}

	/// The directory of the package
	#[must_use]
	pub fn package_dir(&self) -> &Path {
		&self.shared.package_dir
	}

	/// The directory of the workspace this package belongs to, if any
	#[must_use]
	pub fn workspace_dir(&self) -> Option<&Path> {
		self.shared.workspace_dir.as_deref()
	}

	/// The directory to store general-purpose data
	#[must_use]
	pub fn data_dir(&self) -> &Path {
		&self.shared.data_dir
	}

	/// The CAS (content-addressable storage) directory
	#[must_use]
	pub fn cas_dir(&self) -> &Path {
		&self.shared.cas_dir
	}

	/// The authentication configuration
	#[must_use]
	pub fn auth_config(&self) -> &AuthConfig {
		&self.shared.auth_config
	}

	/// Read the manifest file
	#[instrument(skip(self), ret(level = "trace"), level = "debug")]
	pub async fn read_manifest(&self) -> Result<String, errors::ManifestReadError> {
		let string = fs::read_to_string(self.package_dir().join(MANIFEST_FILE_NAME)).await?;
		Ok(string)
	}

	// TODO: cache the manifest
	/// Deserialize the manifest file
	#[instrument(skip(self), ret(level = "trace"), level = "debug")]
	pub async fn deser_manifest(&self) -> Result<Manifest, errors::ManifestReadError> {
		deser_manifest(self.package_dir()).await
	}

	/// Deserialize the manifest file of the workspace root
	#[instrument(skip(self), ret(level = "trace"), level = "debug")]
	pub async fn deser_workspace_manifest(
		&self,
	) -> Result<Option<Manifest>, errors::ManifestReadError> {
		let Some(workspace_dir) = self.workspace_dir() else {
			return Ok(None);
		};

		deser_manifest(workspace_dir).await.map(Some)
	}

	/// Write the manifest file
	#[instrument(skip(self, manifest), level = "debug")]
	pub async fn write_manifest<S: AsRef<[u8]>>(&self, manifest: S) -> Result<(), std::io::Error> {
		fs::write(
			self.package_dir().join(MANIFEST_FILE_NAME),
			manifest.as_ref(),
		)
		.await
	}

	/// Deserialize the lockfile
	#[instrument(skip(self), ret(level = "trace"), level = "debug")]
	pub async fn deser_lockfile(&self) -> Result<Lockfile, errors::LockfileReadError> {
		let string = fs::read_to_string(self.package_dir().join(LOCKFILE_FILE_NAME)).await?;
		lockfile::parse_lockfile(&string).map_err(Into::into)
	}

	/// Write the lockfile
	#[instrument(skip(self, lockfile), level = "debug")]
	pub async fn write_lockfile(
		&self,
		lockfile: &Lockfile,
	) -> Result<(), errors::LockfileWriteError> {
		let lockfile = toml::to_string(lockfile)?;
		let lockfile = format!(
			r"# This file is automatically @generated by pesde.
# It is not intended for manual editing.
format = {}
{lockfile}",
			lockfile::CURRENT_FORMAT
		);

		fs::write(self.package_dir().join(LOCKFILE_FILE_NAME), lockfile).await?;
		Ok(())
	}

	/// Get the workspace members
	#[instrument(skip(self), level = "debug")]
	pub async fn workspace_members(
		&self,
		can_ref_self: bool,
	) -> Result<
		impl Stream<Item = Result<(PathBuf, Manifest), errors::WorkspaceMembersError>>,
		errors::WorkspaceMembersError,
	> {
		let dir = self.workspace_dir().unwrap_or(self.package_dir());
		let manifest = deser_manifest(dir).await?;

		let members = matching_globs(
			dir,
			manifest.workspace_members.iter().map(String::as_str),
			false,
			can_ref_self,
		)
		.await?;

		Ok(try_stream! {
			for path in members {
				let manifest = deser_manifest(&path).await?;
				yield (path, manifest);
			}
		})
	}
}

/// Gets all matching paths in a directory
#[instrument(ret, level = "trace")]
pub async fn matching_globs<'a, P: AsRef<Path> + Debug, I: IntoIterator<Item = &'a str> + Debug>(
	dir: P,
	globs: I,
	relative: bool,
	can_ref_self: bool,
) -> Result<HashSet<PathBuf>, errors::MatchingGlobsError> {
	let (negative_globs, mut positive_globs): (HashSet<&str>, _) =
		globs.into_iter().partition(|glob| glob.starts_with('!'));

	let include_self = positive_globs.remove(".") && can_ref_self;

	let negative_globs = wax::any(
		negative_globs
			.into_iter()
			.map(|glob| wax::Glob::new(&glob[1..]))
			.collect::<Result<Vec<_>, _>>()?,
	)?;
	let positive_globs = wax::any(
		positive_globs
			.into_iter()
			.map(wax::Glob::new)
			.collect::<Result<Vec<_>, _>>()?,
	)?;

	let mut read_dirs = vec![fs::read_dir(dir.as_ref().to_path_buf()).await?];
	let mut paths = HashSet::new();

	if include_self {
		paths.insert(if relative {
			PathBuf::new()
		} else {
			dir.as_ref().to_path_buf()
		});
	}

	while let Some(mut read_dir) = read_dirs.pop() {
		while let Some(entry) = read_dir.next_entry().await? {
			let path = entry.path();
			if entry.file_type().await?.is_dir() {
				read_dirs.push(fs::read_dir(&path).await?);
			}

			let relative_path = path.strip_prefix(dir.as_ref()).unwrap();

			if positive_globs.is_match(relative_path) && !negative_globs.is_match(relative_path) {
				paths.insert(if relative {
					relative_path.to_path_buf()
				} else {
					path.clone()
				});
			}
		}
	}

	Ok(paths)
}

/// A struct containing sources already having been refreshed
#[derive(Debug, Clone, Default)]
pub struct RefreshedSources(Arc<tokio::sync::Mutex<HashSet<u64>>>);

impl RefreshedSources {
	/// Create a new empty `RefreshedSources`
	#[must_use]
	pub fn new() -> Self {
		RefreshedSources::default()
	}

	/// Refreshes the source asynchronously if it has not already been refreshed.
	/// Will prevent more refreshes of the same source.
	pub async fn refresh(
		&self,
		source: &PackageSources,
		options: &RefreshOptions,
	) -> Result<(), source::errors::RefreshError> {
		let mut hasher = std::hash::DefaultHasher::new();
		source.hash(&mut hasher);
		let hash = hasher.finish();

		let mut refreshed_sources = self.0.lock().await;

		if refreshed_sources.insert(hash) {
			source.refresh(options).await
		} else {
			Ok(())
		}
	}
}

async fn deser_manifest(path: &Path) -> Result<Manifest, errors::ManifestReadError> {
	let string = fs::read_to_string(path.join(MANIFEST_FILE_NAME)).await?;
	toml::from_str(&string).map_err(|e| errors::ManifestReadError::Serde(path.into(), e))
}

/// Find the project & workspace directory roots
pub async fn find_roots(
	cwd: PathBuf,
) -> Result<(PathBuf, Option<PathBuf>), errors::FindRootsError> {
	let mut current_path = Some(cwd.clone());
	let mut project_root = None::<PathBuf>;
	let mut workspace_dir = None::<PathBuf>;

	async fn get_workspace_members(
		manifest_file: &mut fs::File,
		path: &Path,
	) -> Result<HashSet<PathBuf>, errors::FindRootsError> {
		let mut manifest = String::new();
		manifest_file
			.read_to_string(&mut manifest)
			.await
			.map_err(errors::ManifestReadError::Io)?;
		let manifest: Manifest = toml::from_str(&manifest)
			.map_err(|e| errors::ManifestReadError::Serde(path.into(), e))?;

		if manifest.workspace_members.is_empty() {
			return Ok(HashSet::new());
		}

		matching_globs(
			path,
			manifest.workspace_members.iter().map(String::as_str),
			false,
			false,
		)
		.await
		.map_err(errors::FindRootsError::Globbing)
	}

	while let Some(path) = current_path {
		current_path = path.parent().map(Path::to_path_buf);

		if workspace_dir.is_some() {
			if let Some(project_root) = project_root {
				return Ok((project_root, workspace_dir));
			}
		}

		let mut manifest = match fs::File::open(path.join(MANIFEST_FILE_NAME)).await {
			Ok(manifest) => manifest,
			Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
			Err(e) => return Err(errors::ManifestReadError::Io(e).into()),
		};

		match (project_root.as_ref(), workspace_dir.as_ref()) {
			(Some(project_root), None) => {
				if get_workspace_members(&mut manifest, &path)
					.await?
					.contains(project_root)
				{
					workspace_dir = Some(path);
				}
			}

			(None, None) => {
				if get_workspace_members(&mut manifest, &path)
					.await?
					.contains(&cwd)
				{
					// initializing a new member of a workspace
					return Ok((cwd, Some(path)));
				}

				project_root = Some(path);
			}

			(_, _) => unreachable!(),
		}
	}

	// we mustn't expect the project root to be found, as that would
	// disable the ability to run pesde in a non-project directory (for example to init it)
	Ok((project_root.unwrap_or(cwd), workspace_dir))
}

/// Returns whether a version matches a version requirement
/// Differs from `VersionReq::matches` in that EVERY version matches `*`
#[must_use]
pub fn version_matches(req: &VersionReq, version: &Version) -> bool {
	*req == VersionReq::STAR || req.matches(version)
}

pub(crate) fn all_packages_dirs() -> HashSet<String> {
	let mut dirs = HashSet::new();
	for target_kind_a in TargetKind::VARIANTS {
		for target_kind_b in TargetKind::VARIANTS {
			dirs.insert(target_kind_a.packages_folder(*target_kind_b));
		}
	}
	dirs
}

/// Errors that can occur when using the pesde library
pub mod errors {
	use std::path::Path;
	use thiserror::Error;

	/// Errors that can occur when reading the manifest file
	#[derive(Debug, Error)]
	#[non_exhaustive]
	pub enum ManifestReadError {
		/// An IO error occurred
		#[error("io error reading manifest file")]
		Io(#[from] std::io::Error),

		/// An error occurred while deserializing the manifest file
		#[error("error deserializing manifest file at {0}")]
		Serde(Box<Path>, #[source] toml::de::Error),
	}

	/// Errors that can occur when reading the lockfile
	#[derive(Debug, Error)]
	#[non_exhaustive]
	pub enum LockfileReadError {
		/// An IO error occurred
		#[error("io error reading lockfile")]
		Io(#[from] std::io::Error),

		/// An error occurred while parsing the lockfile
		#[error("error parsing lockfile")]
		Parse(#[from] crate::lockfile::errors::ParseLockfileError),
	}

	/// Errors that can occur when writing the lockfile
	#[derive(Debug, Error)]
	#[non_exhaustive]
	pub enum LockfileWriteError {
		/// An IO error occurred
		#[error("io error writing lockfile")]
		Io(#[from] std::io::Error),

		/// An error occurred while serializing the lockfile
		#[error("error serializing lockfile")]
		Serde(#[from] toml::ser::Error),
	}

	/// Errors that can occur when finding workspace members
	#[derive(Debug, Error)]
	#[non_exhaustive]
	pub enum WorkspaceMembersError {
		/// An error occurred parsing the manifest file
		#[error("error parsing manifest file")]
		ManifestParse(#[from] ManifestReadError),

		/// An error occurred interacting with the filesystem
		#[error("error interacting with the filesystem")]
		Io(#[from] std::io::Error),

		/// An error occurred while globbing
		#[error("error globbing")]
		Globbing(#[from] MatchingGlobsError),
	}

	/// Errors that can occur when finding matching globs
	#[derive(Debug, Error)]
	#[non_exhaustive]
	pub enum MatchingGlobsError {
		/// An error occurred interacting with the filesystem
		#[error("error interacting with the filesystem")]
		Io(#[from] std::io::Error),

		/// An error occurred while building a glob
		#[error("error building glob")]
		BuildGlob(#[from] wax::BuildError),
	}

	/// Errors that can occur when finding project roots
	#[derive(Debug, Error)]
	#[non_exhaustive]
	pub enum FindRootsError {
		/// Reading the manifest failed
		#[error("error reading manifest")]
		ManifestRead(#[from] ManifestReadError),

		/// Globbing failed
		#[error("error globbing")]
		Globbing(#[from] MatchingGlobsError),
	}
}