Last active
May 4, 2017 13:35
-
-
Save isaquealves/336b54441baa4c736c70e9f140b84ef1 to your computer and use it in GitHub Desktop.
Script to ease add python modules to applications.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
YES='y' | |
OPTIND=1 | |
MODULE_NAME='' | |
filename='' | |
dirname='' | |
show_help() { | |
echo "Use this command with caution ;) (or not)" | |
echo "Usage: makemodule -h[r]emc" | |
echo "Options:\ | |
\n\th - Show help content\ | |
\n\tr - Remove module. BEWARE: If specified module is a subtree, it will be completelly wiped out from your system\ | |
\n\te - Create module subtree with only __init__.py\ | |
\n\tm - Create a module subtree containing the specified module file\ | |
\n\tc - Copy existing module file to the new module tree" | |
} | |
readInput() { | |
echo "------------------------------------------------------------------------" | |
echo "${1}" | |
echo "What is the ${2}?" | |
read MODULE_NAME | |
} | |
isRegularFile() { | |
if [[ ${MODULE_NAME} =~ '\w\/*' ]]; then | |
return 1 | |
fi | |
return 0 | |
} | |
parseInput() { | |
cut -d/ -f1 <<< "${1}" | read dirname | |
cut -d/ -f2 <<< "${1}" | read filename | |
} | |
validateModuleName() { | |
if [[ -n ${MODULE_NAME} ]]; then | |
return 0 | |
fi | |
return 127 | |
} | |
removeModuleFile() { | |
if [[ validateModuleName -eq 0 ]]; then | |
if [[ -f $MODULE_NAME.py ]]; then | |
rm $MODULE_NAME.py | |
return 2 | |
fi | |
if [[ -d $MODULE_NAME ]];then | |
rm -rf $MODULE_NAME | |
return 3 | |
fi | |
if [[ $MODULE_NAME =~ '\w\/*' ]]; then | |
parseInput $MODULE_NAME | |
if [[ -d $dirname ]]; then | |
rm $dirname/$filename.py | |
fi | |
return 1 | |
fi | |
fi | |
} | |
createModuleDir() { | |
mkdir -p $MODULE_NAME && touch `pwd`/$MODULE_NAME/__init__.py | |
} | |
copyModuleFile() { | |
createModuleDir; | |
cut -d/ -f2 <<<"${MODULE_NAME}" | read filename | |
cp $MODULE_NAME.py `pwd`/$MODULE_NAME/$filename.py | |
removeModuleFile; | |
} | |
createModule() { | |
if [[ isRegularFile -eq 0 ]]; then | |
touch `pwd`/$MODULE_NAME.py | |
else | |
parseInput $MODULE_NAME | |
mkdir -p $dirname && touch $dirname/{__init__.py,$filename} | |
fi | |
} | |
while getopts "h?remp:" opt; do | |
case $opt in | |
h|\?) | |
show_help | |
;; | |
r) | |
readInput "This will remove the module.\nBEWARE: If module is a subtree, it will be fully removed" "file to be removed" | |
echo "Removing file..." | |
removeModuleFile | |
echo "successfully removed!" | |
;; | |
e) | |
readInput "This will start a new python module" "new module name" | |
createModuleDir | |
;; | |
m) | |
readInput "This will create start a new python module with a single submodule file" "module name e.g: modulename.py or module/filename.py" | |
createModule | |
;; | |
c) | |
if [ -z $MODULE_NAME ] ; | |
then | |
readInput "This will convert the single file in a subtree module" "module name. e.g: modulename" | |
fi | |
echo "Copying file" | |
copyModuleFile | |
echo "successfully copied" | |
;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment