Monday, June 21, 2010

Refactor from the Commandline

Here's an example script I used to refactor the names of files in a C project.
It can easily be adapted to refactor variable names, etc in JavaScript, Ruby, Python, PHP (*shudder*), etc.

#!/bin/bash

if [ ! -n "${1}" ]
then
    echo "
Usage: refactor.sh new_basename old_basename

Note: do not include '.c' or '.h', this is added for you.
    "
    exit
fi

NEW=${1}
OLD=${2}

svn mv include/${OLD}.h include/${NEW}.h
svn mv ${OLD}.c ${NEW}.c


find ./ -name '*.c' | xargs sed -i "s/${OLD}\.h/${NEW}\.h/gi"
find ./ -name '*.c' | xargs sed -i "s/${OLD}\.c/${NEW}\.c/gi"

find ./ -name '*.h' | xargs sed -i "s/${OLD}\.h/${NEW}\.h/gi"
find ./ -name '*.h' | xargs sed -i "s/${OLD}\.c/${NEW}\.c/gi"

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home