Skip to content

Commit

Permalink
feat(scripts): Add helper script for finding related files
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed Jun 22, 2023
1 parent 55e153c commit 2295a65
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ dist_vim_DATA = citation_audit.vim quotation_audit.vim
dist_data_DATA = functions.zsh

scriptsdir = $(datadir)/scripts
dist_scripts_SCRIPTS = scripts/branch2criticmark.zsh scripts/cover_title.py scripts/diff2marked.zsh scripts/extract_references.js scripts/figure_dash.pl scripts/isbn_format.py scripts/italic_reorder.pl scripts/lazy_quotes.pl scripts/link_verses.js scripts/loadchapters.zsh scripts/msword_escapes.pl scripts/normalize_references.js scripts/ordinal_spaces.pl scripts/renumber_footnotes.pl scripts/reorder_punctuation.pl scripts/series_sort.lua scripts/smart_quotes.pl scripts/split_chapters.zsh scripts/split_mdbook_src.zsh scripts/stats.zsh scripts/toc2breaks.lua scripts/unicode_symbols.pl scripts/worklog.zsh
dist_scripts_SCRIPTS = scripts/branch2criticmark.zsh scripts/cover_title.py scripts/diff2marked.zsh scripts/extract_references.js scripts/figure_dash.pl scripts/isbn_format.py scripts/italic_reorder.pl scripts/lazy_quotes.pl scripts/link_verses.js scripts/list_related_files.zsh scripts/loadchapters.zsh scripts/msword_escapes.pl scripts/normalize_references.js scripts/ordinal_spaces.pl scripts/renumber_footnotes.pl scripts/reorder_punctuation.pl scripts/series_sort.lua scripts/smart_quotes.pl scripts/split_chapters.zsh scripts/split_mdbook_src.zsh scripts/stats.zsh scripts/toc2breaks.lua scripts/unicode_symbols.pl scripts/worklog.zsh

fontsdir = $(datadir)/fonts
dist_fonts_DATA = fonts/Almendra-Bold.otf fonts/Almendra-BoldItalic.otf fonts/Almendra-Italic.otf fonts/Almendra-Regular.otf fonts/AlmendraDisplay-Regular.otf fonts/AlmendraSC-Bold.otf fonts/AlmendraSC-BoldItalic.otf fonts/AlmendraSC-Italic.otf fonts/AlmendraSC-Regular.otf fonts/FeFlow2.otf fonts/NexaRustExtras-Free.otf fonts/NexaRustHandmade-Extended-Free.otf fonts/NexaRustSans-Black-Free.otf fonts/NexaRustScriptL-0-Free.otf fonts/NexaRustSlab-BlackShadow01-Free.otf fonts/Nymphette.ttf fonts/PrintersOrnamentsOne.ttf
Expand Down
12 changes: 12 additions & 0 deletions assets/en-US/cli.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ help-subcommand-make =
help-subcommand-make-target =
Target as defined by rules in CaSILE or project
help-subcommand-script =
Run helper script inside CaSILE environment
help-subcommand-script-name =
Script name as supplied by CaSILE, toolkit, or project
help-subcommand-script-arguments =
Arguments to pass to script
# Currently hard coded, see clap issue #1880
help-subcommand-setup =
Setup a publishing project for use with CaSILE
Expand Down Expand Up @@ -100,6 +109,9 @@ make-error-target =
make-error-unknown =
Make returned unknown error.
script-header =
Running script inside CaSILE environment
setup-header =
Configuring repository for use with CaSILE
Expand Down
12 changes: 12 additions & 0 deletions assets/tr-TR/cli.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@ help-description =
CaSILE avadanlığı için komut satırı arayüzü, SILE ve benzer
sihirbazlığından faydılanan bir yayınlama işi akışı.
help-subcommand-script =
CaSILE ortamı içinde yardımcı komut dosyasını çalıştırın
help-subcommand-script-name =
CaSILE, araç seti veya proje tarafından sağlanan komut dosyası adı
help-subcommand-script-arguments =
Komut dosyasına iletilecek bağımsız değişkenler
welcome =
CaSILE { $version } sürümüne hoş geldiniz!
outro =
CaSILE çalışması tamamlandı
script-header =
CaSILE ortamı içinde komut dosyası çalıştırılıyor
make-header =
‘make’ aracılığıyla hedef(ler) oluşturuluyor
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ AX_SCRIPT([isbn_format.py])
AX_SCRIPT([italic_reorder.pl])
AX_SCRIPT([lazy_quotes.pl])
AX_SCRIPT([link_verses.js])
AX_SCRIPT([list_related_files.zsh])
AX_SCRIPT([loadchapters.zsh])
AX_SCRIPT([msword_escapes.pl])
AX_SCRIPT([normalize_references.js])
Expand Down
25 changes: 25 additions & 0 deletions scripts/list_related_files.zsh.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!@ZSH@
set -e

cmd=$1
base=${2/.md}

value() {
echo $@
exit 0
}

case $cmd in
meta)
value $base.yml
;;
srcid)
$0 meta $2 | read meta
if [[ -f $meta ]] && yq -e 'has("source")' $meta >/dev/null; then
yq -r '[.source[] | select(.type == "bookid")][0].text' $meta | read bookid
value $bookid
fi
;;
esac

exit 1
14 changes: 13 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// use clap::FromArgMatches as _;
use clap::{Args, Subcommand};
use std::path;
use std::{ffi::OsString, path};

// FTL: help-description
/// The command line interface to the CaSILE toolkit,
Expand Down Expand Up @@ -47,6 +47,18 @@ pub enum Commands {
target: Vec<String>,
},

// FTL: help-subcommand-script
/// Run helper script inside CaSILE environment
Script {
// FTL: help-subcommand-script-name
/// Script name as supplied by CaSILE, toolkit, or project
name: String,

// FTL: help-subcommand-script-arguments
/// Arguments to pass to script
arguments: Vec<OsString>,
},

// FTL: help-subcommand-setup
/// Configure a publishing project repository
Setup {},
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod i18n;

// Subcommands
pub mod make;
pub mod script;
pub mod setup;
pub mod status;

Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clap::{Args, Command, FromArgMatches as _};

use casile::cli::{Cli, Commands};
use casile::config::CONF;
use casile::{make, setup, status};
use casile::{make, script, setup, status};
use casile::{Result, VERSION};

fn main() -> Result<()> {
Expand All @@ -18,6 +18,7 @@ fn main() -> Result<()> {
let subcommand = Commands::from_arg_matches(&matches)?;
let ret = match subcommand {
Commands::Make { target } => make::run(target),
Commands::Script { name, arguments } => script::run(name, arguments),
Commands::Setup {} => setup::run(),
Commands::Status {} => status::run(),
};
Expand Down
37 changes: 37 additions & 0 deletions src/script/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::*;

use std::io::prelude::*;
use std::{ffi::OsString, io};
use subprocess::{Exec, Redirection};

// FTL: help-subcommand-script
/// Run helper script inside CaSILE environment
pub fn run(name: String, arguments: Vec<OsString>) -> Result<()> {
setup::is_setup()?;
show_header("script-header");
let cpus = &num_cpus::get().to_string();
let mut process = Exec::cmd(format!("{CONFIGURE_DATADIR}/scripts/{name}")).args(&arguments);
let gitname = status::get_gitname()?;
let git_version = status::get_git_version();
process = process
.env("CASILE_CLI", "true")
.env("CASILE_JOBS", cpus)
.env("CASILEDIR", CONFIGURE_DATADIR)
.env("CONTAINERIZED", status::is_container().to_string())
.env("LANGUAGE", locale_to_language(CONF.get_string("language")?))
.env("PROJECT", gitname)
.env("PROJECTDIR", CONF.get_string("path")?)
.env("PROJECTVERSION", git_version);
let repo = get_repo()?;
let workdir = repo.workdir().unwrap();
process = process.cwd(workdir);
let process = process.stderr(Redirection::Pipe).stdout(Redirection::Pipe);
let mut popen = process.popen()?;
let buf = io::BufReader::new(popen.stdout.as_mut().unwrap());
for line in buf.lines() {
let text: &str =
&line.unwrap_or_else(|_| String::from("INVALID UTF-8 FROM CHILD PROCESS STREAM"));
println!("{text}");
}
Ok(())
}

0 comments on commit 2295a65

Please sign in to comment.