/
generate
executable file
·199 lines (165 loc) · 3.77 KB
/
generate
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/bin/sh
#############################################################
#
# canhaz generator driver
#
# This script drives the generation of files by setting up
# the general canhaz environment, paths, etc., loading the
# config, and then executing the setup, generator, and
# teardown scripts
#
#############################################################
ME=`basename $0`
USAGE="Usage: $ME [--force] module_path [generators]"
VERSION="v1.0"
MY_DIR="$HOME/.canhaz"
MY_BIN_DIR="$MY_DIR/bin"
GEN_OBJECT="$MY_BIN_DIR/gen-object"
#
# Print a message. Generator plugins should use this instead of echo
#
function msg {
echo "$@"
}
#
# Print an error to stderr
#
function err {
echo "ERROR: $@" >&2
}
#
# Print an error and exit with the supplied exit value, or 1 if no
# exit value specified
#
function fail {
err "$1"
exit $2||1
}
function load {
local FILE="${GEN_BASE_DIR}/$1"
# echo "Loading $FILE"
# If the requested file exists, execute it
# If it doesn't, and error message was supplied, print it
if [[ -f "$FILE" ]]; then
. "$FILE"
elif [[ "$2" != "" ]]; then
err "$2"
fi
}
#
# Function to generate content from a template
# Params:
# $1 - template file
#
function template {
perl -p -e 's/[\$]\{([A-Za-z_][A-Za-z0-9_]+)\}/defined $ENV{$1} ? $ENV{$1} : $&/eg' "$1"
}
export -f msg err fail load template
#
# Find config file. This will try any locations listed first, then search upward,
# and finally use the default config if no other config has been found.
#
function find_config {
local DEFAULT_CONFIG="$MY_DIR/config"
local c
for c in "$@"
do
if [[ -f "$c" ]]; then
CONFIG="$c"
return 1
fi
done
# Search upward for config
local FILE=".canhaz/config"
CONFIG=`find_ancestor $FILE`
if [[ ! -f "$CONFIG" ]]; then
CONFIG="$DEFAULT_CONFIG"
fi
}
#
# Searches upward in the dir hierarchy to find a file or dir with a particular name
# Params:
# $1 - dir or file name to find
# $2 - (optional) directory in which to start searching. Search will proceed upward
# from here. If not specified, the current working dir will be used.
#
function find_ancestor {
# Search upward for config
local FILE="$1"
local DIR=${2:-$PWD}
while [[ "$DIR" != '/' ]]; do
if [[ -e "$DIR/$FILE" ]]; then
echo "$DIR/$FILE"
return 1
else
DIR=`dirname $DIR`
fi
done
}
# Defaults
FORCE=false
# Find config file.
find_config "./.canhaz/config"
export CONFIG_DIR=`dirname "$CONFIG"`
export CONFIG_PARENT_DIR=`dirname "$CONFIG_DIR"`
# Parse command line options.
for arg in "$@"
do
case "$arg" in
-h|--help)
msg "$USAGE"
exit 0
;;
-v|--version)
msg "$ME $VERSION"
exit 0
;;
-f|--force)
FORCE=true
shift
;;
-c|--config)
shift
CONFIG=$1
shift
;;
esac
done
BASE_DIR=`pwd`
# We want at least one non-option argument.
# Remove this block if you don't need it.
if [ $# -eq 0 ]; then
msg "$USAGE"
exit 1
fi
# Load config
if [[ ! -f "$CONFIG" ]]; then
fail "Can't read config: $CONFIG" 1
fi
. "$MY_DIR/config"
# Figure out what type of thing we're generating
USER_OBJECT_TYPE=${1//:/\/}
if [[ -d "$GEN_BASE_DIR/$USER_OBJECT_TYPE" ]]; then
OBJECT_TYPE=$USER_OBJECT_TYPE
shift
else
OBJECT_TYPE="${DEFAULT_TYPE//:/\/}"
fi
# Get the path/name of the thing
MODULE_PATH="$1"
shift
# Command line generator list takes precedent over config
if [[ "$*" != "" ]]; then
USER_GENERATORS="$*"
fi
MODULE=`basename "$MODULE_PATH"`
MODULE_BASE=`dirname "$MODULE_PATH"`
CANHAZ_DIR="$MY_DIR"
# Export vars that should be available to templates
export CANHAZ_DIR ME VERSION PHP MY_DIR MY_BIN_DIR GEN_BASE_DIR MODULE MODULE_BASE BASE_DIR TEST_DIR TEMPLATE_DIR FORCE COPYRIGHT
# Run the general setup, if it exists
load "setup"
sh "$GEN_OBJECT" "$OBJECT_TYPE" $USER_GENERATORS
# Run the general teardown, if it exists
load "teardown"
msg "Done"