Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move generating static assets to a cargo-xtask task #196

Merged
merged 4 commits into from
Jun 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[alias]
xtask = "run --package xtask --"
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ jobs:
if: ${{ matrix.use-cross == true }}
shell: bash
run: |
mkdir .cargo
cat > .cargo/config.toml <<EOF
[target.${{ matrix.target }}]
rustflags = ["--cfg", "sd_cross_compile"]
Expand Down
29 changes: 19 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[workspace]
members = [
".",
"xtask",
]

[package]
name = "sd"
version = "0.7.6"
Expand Down Expand Up @@ -28,11 +34,6 @@ clap = { version = "4.2.7", features = ["derive", "deprecated", "wrap_help"] }
assert_cmd = "1.0.3"
anyhow = "1.0.38"

[build-dependencies]
clap = "4.2.7"
clap_complete = "4.2.2"
man = "0.3.0"

[profile.release]
opt-level = 3
lto = true
Expand Down
46 changes: 46 additions & 0 deletions gen/completions/_sd
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#compdef sd

autoload -U is-at-least

_sd() {
typeset -A opt_args
typeset -a _arguments_options
local ret=1

if is-at-least 5.2; then
_arguments_options=(-s -S -C)
else
_arguments_options=(-s -C)
fi

local context curcontext="$curcontext" state line
_arguments "${_arguments_options[@]}" \
'-n+[Limit the number of replacements]:REPLACEMENTS: ' \
'-f+[Regex flags. May be combined (like \`-f mc\`).]:FLAGS: ' \
'--flags=[Regex flags. May be combined (like \`-f mc\`).]:FLAGS: ' \
'-p[Output result into stdout and do not modify files]' \
'--preview[Output result into stdout and do not modify files]' \
'-F[Treat FIND and REPLACE_WITH args as literal strings]' \
'--fixed-strings[Treat FIND and REPLACE_WITH args as literal strings]' \
'-r[Recursively replace files]' \
'-h[Print help (see more with '\''--help'\'')]' \
'--help[Print help (see more with '\''--help'\'')]' \
'-V[Print version]' \
'--version[Print version]' \
':find -- The regexp or string (if -s) to search for:' \
':replace_with -- What to replace each match with. Unless in string mode, you may use captured values like $1, $2, etc:' \
'*::files -- The path to file(s). This is optional - sd can also read from STDIN. {n}{n}Note\: sd modifies files in-place by default. See documentation for examples:_files' \
&& ret=0
}

(( $+functions[_sd_commands] )) ||
_sd_commands() {
local commands; commands=()
_describe -t commands 'sd commands' commands "$@"
}

if [ "$funcstack[1]" = "_sd" ]; then
_sd "$@"
else
compdef _sd sd
fi
42 changes: 42 additions & 0 deletions gen/completions/_sd.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

using namespace System.Management.Automation
using namespace System.Management.Automation.Language

Register-ArgumentCompleter -Native -CommandName 'sd' -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)

$commandElements = $commandAst.CommandElements
$command = @(
'sd'
for ($i = 1; $i -lt $commandElements.Count; $i++) {
$element = $commandElements[$i]
if ($element -isnot [StringConstantExpressionAst] -or
$element.StringConstantType -ne [StringConstantType]::BareWord -or
$element.Value.StartsWith('-') -or
$element.Value -eq $wordToComplete) {
break
}
$element.Value
}) -join ';'

$completions = @(switch ($command) {
'sd' {
[CompletionResult]::new('-n', 'n', [CompletionResultType]::ParameterName, 'Limit the number of replacements')
[CompletionResult]::new('-f', 'f', [CompletionResultType]::ParameterName, 'Regex flags. May be combined (like `-f mc`).')
[CompletionResult]::new('--flags', 'flags', [CompletionResultType]::ParameterName, 'Regex flags. May be combined (like `-f mc`).')
[CompletionResult]::new('-p', 'p', [CompletionResultType]::ParameterName, 'Output result into stdout and do not modify files')
[CompletionResult]::new('--preview', 'preview', [CompletionResultType]::ParameterName, 'Output result into stdout and do not modify files')
[CompletionResult]::new('-F', 'F', [CompletionResultType]::ParameterName, 'Treat FIND and REPLACE_WITH args as literal strings')
[CompletionResult]::new('--fixed-strings', 'fixed-strings', [CompletionResultType]::ParameterName, 'Treat FIND and REPLACE_WITH args as literal strings')
[CompletionResult]::new('-r', 'r', [CompletionResultType]::ParameterName, 'Recursively replace files')
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Print version')
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version')
break
}
})

$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
Sort-Object -Property ListItemText
}
50 changes: 50 additions & 0 deletions gen/completions/sd.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
_sd() {
local i cur prev opts cmd
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
cmd=""
opts=""

for i in ${COMP_WORDS[@]}
do
case "${cmd},${i}" in
",$1")
cmd="sd"
;;
*)
;;
esac
done

case "${cmd}" in
sd)
opts="-p -F -r -n -f -h -V --preview --fixed-strings --flags --help --version <FIND> <REPLACE_WITH> [FILES]..."
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
-n)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
--flags)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
-f)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
esac
}

complete -F _sd -o bashdefault -o default sd
36 changes: 36 additions & 0 deletions gen/completions/sd.elv
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

use builtin;
use str;

set edit:completion:arg-completer[sd] = {|@words|
fn spaces {|n|
builtin:repeat $n ' ' | str:join ''
}
fn cand {|text desc|
edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc
}
var command = 'sd'
for word $words[1..-1] {
if (str:has-prefix $word '-') {
break
}
set command = $command';'$word
}
var completions = [
&'sd'= {
cand -n 'Limit the number of replacements'
cand -f 'Regex flags. May be combined (like `-f mc`).'
cand --flags 'Regex flags. May be combined (like `-f mc`).'
cand -p 'Output result into stdout and do not modify files'
cand --preview 'Output result into stdout and do not modify files'
cand -F 'Treat FIND and REPLACE_WITH args as literal strings'
cand --fixed-strings 'Treat FIND and REPLACE_WITH args as literal strings'
cand -r 'Recursively replace files'
cand -h 'Print help (see more with ''--help'')'
cand --help 'Print help (see more with ''--help'')'
cand -V 'Print version'
cand --version 'Print version'
}
]
$completions[$command]
}
7 changes: 7 additions & 0 deletions gen/completions/sd.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
complete -c sd -s n -d 'Limit the number of replacements' -r
complete -c sd -s f -l flags -d 'Regex flags. May be combined (like `-f mc`).' -r
complete -c sd -s p -l preview -d 'Output result into stdout and do not modify files'
complete -c sd -s F -l fixed-strings -d 'Treat FIND and REPLACE_WITH args as literal strings'
complete -c sd -s r -d 'Recursively replace files'
complete -c sd -s h -l help -d 'Print help (see more with \'--help\')'
complete -c sd -s V -l version -d 'Print version'
63 changes: 63 additions & 0 deletions gen/sd.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
.TH SD 1
.SH NAME
sd
.SH SYNOPSIS
\fBsd\fR [FLAGS] find replace_with [FILES]
.SH FLAGS
.TP
\fB\-p\fR, \fB\-\-preview\fR
Emit the replacement to STDOUT

.TP
\fB\-s\fR, \fB\-\-string\-mode\fR
Treat expressions as non\-regex strings.

.TP
\fB\-f\fR, \fB\-\-flags\fR
Regex flags. May be combined (like `\-f mc`).

c \- case\-sensitive
i \- case\-insensitive
m \- multi\-line matching
w \- match full words only

.SH EXIT STATUS
.TP
\fB0\fR
Successful program execution.

.TP
\fB1\fR
Unsuccessful program execution.

.TP
\fB101\fR
The program panicked.
.SH EXAMPLES
.TP
String\-literal mode
\fB$ echo 'lots((([]))) of special chars' | sd \-s '((([])))' ''\fR
.br
lots of special chars
.TP
Regex use. Let's trim some trailing whitespace
\fB$ echo 'lorem ipsum 23 ' | sd '\s+$' ''\fR
.br
lorem ipsum 23
.TP
Indexed capture groups
\fB$ echo 'cargo +nightly watch' | sd '(\w+)\s+\+(\w+)\s+(\w+)' 'cmd: $1, channel: $2, subcmd: $3'\fR
.br
cmd: cargo, channel: nightly, subcmd: watch
.TP
Named capture groups
\fB$ echo "123.45" | sd '(?P<dollars>\d+)\.(?P<cents>\d+)' '$dollars dollars and $cents cents'\fR
.br
123 dollars and 45 cents
.TP
Find & replace in file
\fB$ sd 'window.fetch' 'fetch' http.js\fR
.TP
Find & replace from STDIN an emit to STDOUT
\fB$ sd 'window.fetch' 'fetch' < http.js\fR

10 changes: 10 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "xtask"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
clap = "4.3.1"
clap_complete = "4.3.1"
man = "0.3.0"
Loading