Skip to content

Instantly share code, notes, and snippets.

@mmacedo
Created August 1, 2013 04:25
Show Gist options
  • Save mmacedo/6128401 to your computer and use it in GitHub Desktop.
Save mmacedo/6128401 to your computer and use it in GitHub Desktop.
Rename file names and contents
#!/usr/bin/env fish
for tool in ack rename perl
if not which $tool >/dev/null
echo You need $tool to run this tool >&2
exit 1
end
end
if [ (count $argv) != 3 ]
echo USAGE: translate FROM TO FILE_OR_DIRECTORY >&2
exit 1
end
set -l from $argv[1]
set -l to $argv[2]
set -l target $argv[3]
function tolower
echo $argv | tr [A-Z] [a-z]
end
function toupper
echo $argv | tr [a-z] [A-Z]
end
function capitalize
tolower $argv | sed 's/^./\U&/; s/[^a-zA-Z0-9]./\U&/g'
end
function parameterize
set -l c ""
if [ (count $argv) = 1 ]
set c $argv[1]
end
sed 's/[^a-zA-Z0-9]/'$c'/g'
end
function pascalize
capitalize $argv | parameterize
end
function camelize
pascalize $argv | sed 's/^./\L&/'
end
function wikipedify
echo $argv | parameterize _
end
function hyphenate
echo $argv | parameterize -
end
set -l lowercase 's/\Q'(tolower $from)'\E/\Q'(tolower $to)'\E/g'
set -l uppercase 's/\Q'(toupper $from)'\E/\Q'(toupper $to)'\E/g'
set -l capitalized 's/\Q'(capitalize $from)'\E/\Q'(capitalize $to)'\E/g'
set -l replace "$lowercase ; $uppercase ; $capitalized"
# more than one word
if echo $from | grep [^a-zA-Z0-9]
set -l pascalcase 's/\Q'(pascalize $from)'\E/\Q'(pascalize $to)'\E/g'
set -l camelcase 's/\Q'(camelize $from)'\E/\Q'(camelize $to)'\E/g'
set -l underline_lowercase 's/\Q'(wikipedify $lowercase[1])'\E/\Q'(wikipedify $lowercase[2])'\E/g'
set -l underline_uppercase 's/\Q'(wikipedify $uppercase[1])'\E/\Q'(wikipedify $uppercase[2])'\E/g'
set -l hyphenated_lowercase 's/\Q'(hyphenate $lowercase[1])'\E/\Q'(hyphenate $lowercase[2])'\E/g'
set -l hyphenated_uppercase 's/\Q'(hyphenate $uppercase[1])'\E/\Q'(hyphenate $uppercase[2])'\E/g'
set replace "$replace ; $pascalcase ; $camelcase ; $underline_lowercase ; $underline_uppercase ; $hyphenated_lowercase ; $hyphenated_uppercase"
end
if [ -d $target ]
pushd $target
rename "$replace" **
ack -f --nopager | xargs perl -p -e "$replace" -i
popd
else
rename "$replace" $target
perl -p -e "$replace" -i $target
end
@mmacedo
Copy link
Author

mmacedo commented Aug 1, 2013

Using it to compare differences between rbenv and pyenv:

git clone https://github.com/sstephenson/rbenv
git clone https://github.com/yyuu/pyenv
./translate.fish rbenv pyenv rbenv
./translate.fish ruby pythong rbenv
./translate.fish sstephenson yyuu rbenv
meld rbenv pyenv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment