pub struct Context<'a> {
pub project_root: PathBuf,
pub start_dir: PathBuf,
pub matched: Vec<String>,
pub config_files: Vec<PathBuf>,
pub pins: BTreeMap<String, String>,
pub reason: SelectionReason,
pub score: u32,
/* private fields */
}Expand description
The context the host passes to the plugin. Read-only.
Fields§
§project_root: PathBufProject root: where the command runs unless the answer names a cwd of its own.
start_dir: PathBufThe directory the person ran pmpx from (its -C, or the process’s own directory).
It differs from project_root whenever the root was found by walking up, which makes this
the only way to tell which package of a monorepo the command is for. Not where the
command will run: that is the cwd of the answer, and it defaults to project_root.
Empty when the host had nothing to say about it.
matched: Vec<String>The files this detection matched, relative to project_root.
This is the plugin’s main channel for learning “what the project looks like”. Example: the
yarn plugin distinguishes classic from berry via has_matched(".yarnrc.yml") without
reading a single file.
config_files: Vec<PathBuf>The project config files that were read, nearest first.
pins: BTreeMap<String, String>[plugin] pins from the project config: family → plugin name.
The whole map, not just this plugin’s family: it is how a plugin can notice that the project pins something for its family which is not installed, and say so.
reason: SelectionReasonWhy this plugin was selected.
score: u32The evidence score it won with. 0 when it was pinned or named outright, since neither needed evidence.
Implementations§
Source§impl<'a> Context<'a>
impl<'a> Context<'a>
Sourcepub fn builder() -> ContextBuilder
pub fn builder() -> ContextBuilder
A context for a plugin’s own test: hand-built, with no host behind it.
let ctx = Context::builder()
.project_root("/work")
.matched(["pnpm-lock.yaml"])
.file("package.json", "{\"name\":\"x\"}")
.build();Sourcepub fn has_matched(&self, file: &str) -> bool
pub fn has_matched(&self, file: &str) -> bool
Whether one of the matched files is this one – the standard way for a plugin to branch on shape.
Sourcepub fn was_pinned(&self) -> bool
pub fn was_pinned(&self) -> bool
Whether the project config is why this plugin is the one being asked.
Sourcepub fn pinned_for(&self, family: &str) -> Option<&str>
pub fn pinned_for(&self, family: &str) -> Option<&str>
What the project pins this plugin’s family to, if anything.
Sourcepub fn file(&self, name: &str) -> Option<ContextFile>
pub fn file(&self, name: &str) -> Option<ContextFile>
The contents of one file this plugin declared in its manifest’s [context] files.
The host reads it now if it has not been read yet. None means it could not be handed over –
a missing file, one that cannot be read, or a name the manifest did not declare. Whether that
matters is the plugin’s call: a missing lockfile and a missing optional config are different
things.