Skip to content

Commit

Permalink
adding xtask for setting up dotfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
sminez committed Feb 20, 2025
1 parent a95eac3 commit 4f21aa4
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 143 deletions.
33 changes: 0 additions & 33 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
test:
cargo nextest run --workspace $(ARGS)


.PHONY: watch-test
watch-test:
cargo nextest run --workspace
Expand Down Expand Up @@ -43,38 +42,6 @@ upgrade-check:
todo:
rg 'TODO|FIXME|todo!' src

.PHONY: ensure-mountpoint
ensure-mountpoint:
mkdir -p $$HOME/.ad/mnt

.PHONY: backup-current-config
backup-current-config: ensure-mountpoint
[ -f $$HOME/.ad/config.toml ] && mv $$HOME/.ad/config.toml $$HOME/.ad/config.toml.bck || true

.PHONY: backup-current-plumbing
backup-current-plumbing: ensure-mountpoint
[ -f $$HOME/.ad/plumbing.rules ] && mv $$HOME/.ad/plumbing.rules $$HOME/.ad/plumbing.rules.bck || true

.PHONY: copy-default-config
copy-default-config: ensure-mountpoint backup-current-config
cp data/config.toml $$HOME/.ad

.PHONY: copy-default-plumbing
copy-default-plumbing: ensure-mountpoint backup-current-plumbing
cp data/plumbing.rules $$HOME/.ad

.PHONY: copy-rust-config
copy-rust-config: ensure-mountpoint backup-current-config
cp data/init-rust.conf $$HOME/.ad/config.toml

.PHONY: copy-bin
copy-bin: ensure-mountpoint
cp -r data/bin $$HOME/.ad
cp -r data/lib $$HOME/.ad

.PHONY: setup-dotfiles
setup-dotfiles: copy-default-config copy-default-plumbing copy-bin

.PHONY: force-unmount
force-unmount:
fusermount -u $$HOME/.ad/mnt
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ in place, so the recommended way to try out `ad` is to clone this repo and compi
$ git clone [email protected]:sminez/ad.git
$ cd ad
$ cargo install --path .
$ make setup-dotfiles
$ cargo xtask setup-dotfiles
```

From there you should be able to open `ad` and run the `:help` command to view the built-in help.
Expand Down
117 changes: 8 additions & 109 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
// https://github.com/matklad/cargo-xtask
use man::prelude::*;
use std::{
env, fs,
env,
path::{Path, PathBuf},
process::exit,
};

const DESCRIPTION: &str = "\
ad is a text editor and command line stream editor. The text editor interface for
ad is inspired by the likes of vim and kakoune, along with the acme and sam editors
from plan9. ad aims to provide an 'integrating development environment' as opposed
to an 'integrated' one: leveraging the surrounding system for the majority of
functionality outisde of the core text editing actions.
";
mod setup;
mod ts;

type DynResult = Result<(), Box<dyn std::error::Error>>;

#[macro_export]
macro_rules! err {
($msg:expr) => {
Err($msg.to_string().into())
Expand All @@ -37,8 +32,9 @@ fn try_main() -> DynResult {
let task = env::args().nth(1);

match task.as_deref() {
Some("lint-ts-queries") => lint_ts_queries()?,
Some("gen-man-page") => generate_manpage()?,
Some("lint-ts-queries") => ts::lint_ts_queries()?,
Some("gen-man-page") => setup::generate_manpage()?,
Some("setup-dotfiles") => setup::setup_dotfiles()?,

_ => list_tasks(),
}
Expand All @@ -49,109 +45,12 @@ fn try_main() -> DynResult {
fn list_tasks() {
eprintln!(
"Available tasks:
* setup-dotfiles copy over the default dotfiles from the /data directory
* lint-ts-queries ensure that the tree-sitter queries in /data are valid
* gen-man-page re-generate the man page"
);
}

fn lint_ts_queries() -> DynResult {
let unsupported_predicates = [
// neovim
"any-contains?",
"any-lua-match?",
"any-vim-match?",
"contains?",
"has-ancestor?",
"has-parent?",
"lua-match?",
"vim-match?",
];
let mut valid = true;
eprintln!(">> Linting tree-sitter queries");

let query_root = project_root().join("data/tree-sitter/queries");
for entry in fs::read_dir(query_root)? {
let path = entry?.path();
if path.is_file() {
eprintln!(
"[x] unexpected file in data/tree-sitter/queries:\n{}",
path.display()
);
valid = false;
}

let lang = path.file_name().unwrap().to_string_lossy();
let highlights = path.join("highlights.scm");
eprintln!("[ ] checking highlights for {lang}...");

if !highlights.exists() {
eprintln!(" [x] no highlights.scm found for {lang}");
valid = false;
continue;
}

let query = fs::read_to_string(highlights)?;
for p in unsupported_predicates {
if query.contains(p) {
eprintln!(" [x] highlights for {lang} contain an unsupported predicate: {p}");
valid = false;
}
}
if query.contains("@spell") {
eprintln!(" [x] highlights for {lang} contain '@spell' which needs to be removed");
valid = false;
}
}

if !valid {
return err!("validation failed: see logs for details");
}

eprintln!("\ntree-sitter queries linted successfully");
Ok(())
}

fn generate_manpage() -> DynResult {
eprintln!(">> Generating man page");
fs::create_dir_all(dist_dir())?;

let content = Manual::new("ad")
.about("An adaptable text editor")
.author(Author::new("Innes Anderson-Morrison"))
.description(DESCRIPTION)
.option(
Opt::new("script")
.short("-e")
.help("Execute an edit script on file(s)"),
)
.option(
Opt::new("script-file")
.short("-f")
.help("Execute an edit script loaded from a script-file on file(s)"),
)
.arg(Arg::new("[file...]"))
.flag(
Flag::new()
.short("-h")
.long("--help")
.help("Print command line help and exit"),
)
.flag(
Flag::new()
.short("-v")
.long("--version")
.help("Print version information and exit"),
)
.render();

let p = dist_dir().join("ad.1");
eprintln!(" [ ] writing manpage to {}", p.display());
fs::write(p, content)?;
eprintln!(" [ ] done");

Ok(())
}

fn project_root() -> PathBuf {
Path::new(&env!("CARGO_MANIFEST_DIR"))
.ancestors()
Expand Down
87 changes: 87 additions & 0 deletions xtask/src/setup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use crate::{dist_dir, project_root, DynResult};
use man::prelude::*;
use std::{fs, path::PathBuf};

const DESCRIPTION: &str = "\
ad is a text editor and command line stream editor. The text editor interface for
ad is inspired by the likes of vim and kakoune, along with the acme and sam editors
from plan9. ad aims to provide an 'integrating development environment' as opposed
to an 'integrated' one: leveraging the surrounding system for the majority of
functionality outisde of the core text editing actions.
";

const DOTFILE_DIR: &str = ".ad";

pub fn generate_manpage() -> DynResult {
eprintln!(">> Generating man page");
let dir = dist_dir();
fs::create_dir_all(&dir)?;

let content = Manual::new("ad")
.about("An adaptable text editor")
.author(Author::new("Innes Anderson-Morrison"))
.description(DESCRIPTION)
.option(
Opt::new("script")
.short("-e")
.help("Execute an edit script on file(s)"),
)
.option(
Opt::new("script-file")
.short("-f")
.help("Execute an edit script loaded from a script-file on file(s)"),
)
.arg(Arg::new("[file...]"))
.flag(
Flag::new()
.short("-h")
.long("--help")
.help("Print command line help and exit"),
)
.flag(
Flag::new()
.short("-v")
.long("--version")
.help("Print version information and exit"),
)
.render();

let p = dir.join("ad.1");
eprintln!(" [ ] writing manpage to {}", p.display());
fs::write(p, content)?;
eprintln!(" [ ] done");

Ok(())
}

pub fn setup_dotfiles() -> DynResult {
eprintln!(">> Setting up dotfile directory");
let data_dir = project_root().join("data");
let dot_dir = PathBuf::from(env!("HOME")).join(DOTFILE_DIR);

eprintln!(" [ ] creating dotfile directory: {}", dot_dir.display());
fs::create_dir_all(dot_dir.join("mnt"))?;

let cp = |path: &str| fs::copy(data_dir.join(path), dot_dir.join(path));

eprintln!(" [ ] copying default config file");
cp("config.toml")?;
eprintln!(" [ ] copying default plumbing rules");
cp("plumbing.rules")?;
eprintln!(" [ ] copying data/bin...");
fs::create_dir_all(dot_dir.join("bin"))?;
for entry in fs::read_dir(data_dir.join("bin"))? {
let path = entry?.path();
let fname = path.file_name().unwrap().to_string_lossy();
cp(&format!("bin/{fname}"))?;
}
eprintln!(" [ ] copying data/lib...");
fs::create_dir_all(dot_dir.join("lib"))?;
for entry in fs::read_dir(data_dir.join("lib"))? {
let path = entry?.path();
let fname = path.file_name().unwrap().to_string_lossy();
cp(&format!("lib/{fname}"))?;
}

Ok(())
}
60 changes: 60 additions & 0 deletions xtask/src/ts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use crate::{err, project_root, DynResult};
use std::fs;

pub fn lint_ts_queries() -> DynResult {
let unsupported_predicates = [
// neovim
"any-contains?",
"any-lua-match?",
"any-vim-match?",
"contains?",
"has-ancestor?",
"has-parent?",
"lua-match?",
"vim-match?",
];
let mut valid = true;
eprintln!(">> Linting tree-sitter queries");

let query_root = project_root().join("data/tree-sitter/queries");
for entry in fs::read_dir(query_root)? {
let path = entry?.path();
if path.is_file() {
eprintln!(
"[x] unexpected file in data/tree-sitter/queries:\n{}",
path.display()
);
valid = false;
}

let lang = path.file_name().unwrap().to_string_lossy();
let highlights = path.join("highlights.scm");
eprintln!("[ ] checking highlights for {lang}...");

if !highlights.exists() {
eprintln!(" [x] no highlights.scm found for {lang}");
valid = false;
continue;
}

let query = fs::read_to_string(highlights)?;
for p in unsupported_predicates {
if query.contains(p) {
eprintln!(" [x] highlights for {lang} contain an unsupported predicate: {p}");
valid = false;
}
}
if query.contains("@spell") {
eprintln!(" [x] highlights for {lang} contain '@spell' which needs to be removed");
valid = false;
}
}

if !valid {
return err!("validation failed: see logs for details");
}

eprintln!("\ntree-sitter queries linted successfully");

Ok(())
}

0 comments on commit 4f21aa4

Please sign in to comment.