libpfu_fixers/
fish_shell.rs

1//! Checks for fish-shell.
2
3use std::fs;
4
5use anyhow::Result;
6use async_trait::async_trait;
7use libpfu::{
8	Linter, Session, declare_lint, declare_linter,
9	message::{LintMessage, Snippet},
10	walk_build_scripts,
11};
12use log::debug;
13
14declare_linter! {
15	pub FISH_SHELL_LINTER,
16	FishShellLinter,
17	[
18		"fish-shell-use-vendor-compl",
19	]
20}
21
22declare_lint! {
23	pub FISH_SHELL_USE_VENDOR_COMPL_LINT,
24	"fish-shell-use-vendor-compl",
25	Warning,
26	"shell completions for fish should be installed to /usr/share/fish/vendor_completions.d"
27}
28
29#[async_trait]
30impl Linter for FishShellLinter {
31	async fn apply(&self, sess: &Session) -> Result<()> {
32		if sess.package.name() == "fish" {
33			debug!("skipping fish shell linter");
34			return Ok(());
35		}
36		for path in walk_build_scripts(sess) {
37			let script = fs::read_to_string(&path)?;
38			if script.contains("/usr/share/fish/completions") {
39				LintMessage::new(FISH_SHELL_USE_VENDOR_COMPL_LINT)
40					.snippet(Snippet::new_file(&path))
41					.emit(sess);
42				if !sess.dry {
43					let script = script.replace(
44						"/usr/share/fish/completions",
45						"/usr/share/fish/vendor_completions.d",
46					);
47					fs::write(&path, script)?;
48				}
49			}
50		}
51		Ok(())
52	}
53}