forked from isislovecruft/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boilerplater
executable file
·131 lines (109 loc) · 2.99 KB
/
boilerplater
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
##############################################################################
#
# boilerplater
# ------------
# A simple utility to copy a file to a new location with a new name, and then
# immediately start editing it. For boilerplates.
#
# You should create a file containing the boilerplate to use, see the examples
# boilerplate-bash and boilerplate-python, and then (probably in your
# .bashrc) do:
#
# alias boilerpy="/path/to/boilerplate.sh boilerplate-python "
#
# and use that alias like this "$ boilerpy foobar.py".
#
# @author Isis Agora Lovecruft, 0x2cdb8b35
# @date 16 May 2012
# @version 0.0.1
##############################################################################
## Local variables:
AUTHOR_NAME="Isis Agora Lovecruft"
AUTHOR_GPG_KEY="0x2cdb8b35"
function usage () {
echo "Usage: $0 <boilerplate_file> <new_file_name>"
echo
echo "You should set the following environment variables:"
echo "\$AUTHOR_NAME The name to be credited"
echo "\$AUTHOR_GPG_KEY The GPG keyID or email address of the author"
echo "\$EDITOR Your preferred editor"
echo
}
if [[ "$#" != "2" ]]; then
usage
else
echo "Please provide a short docstring description of the file "
echo "which you are about to create: "
read FDOCSTR
## If docstring is blank, boilerplate that too:
if [[ "x${FDOCSTR}" == "x" ]] ; then
FDOCSTR="XXX fill me in"
fi
## Wrap docstring lines:
INT=0
while [[ ${#FDOCSTR} > 75 ]] ; do
NEWSTR=FDOCSTR_${INT}
eval ${NEWSTR}="# "${FDOCSTR:0:75}
INT=$(( $INT + 1 ))
FDOCSTR=${FDOCSTR:75:}
done
if [[ x$1 == x*sh ]] ; then
cat >$2 <<-EOF
#!/bin/bash
##############################################################################
#
# $2
# -------------------
EOF
if [[ $INT -gt "0" ]] ; then
for int in `seq 0 $INT`; do
cat >>$2 <<EOF
$($FDOCSTR_$int)
EOF
done
fi
cat >>$2 <<EOF
# $FDOCSTR
#
# @author $AUTHOR_NAME, $AUTHOR_GPG_KEY
# @date `date "+%e %B %Y"`
# @version 0.0.1
##############################################################################
EOF
elif [[ x$1 == x*py ]] || [[ x$1 == x*python ]] ; then
cat >$2 <<-EOF
#!/usr/bin/env python
#-*- coding: utf-8 -*-
##############################################################################
#
# $2
# --------------------
EOF
if [[ $INT > 0 ]] ; then
for int in `seq 0 $INT`; do
CURRENT_STR=FDOCSTR_$int
cat >>$2 <<<$CURRENT_STR
done
fi
cat >>$2 <<-EOF
# $FDOCSTR
#
# @author $AUTHOR_NAME, $AUTHOR_GPG_KEY
# @date `date "+%e %B %Y"`
# @version 0.0.1
##############################################################################
import os
def foo():
"""
"""
pass
if __name__ == "__main":
EOF
else
cp $1 $2
fi
## Make it executable and start the editor
chmod +x $2
$EDITOR $2
fi