Skip to content
Browse files

Changes to make it easier to write simple generators using only a sin…

…gle "generate" script that takes parameters. Adding MIT license.
  • Loading branch information...
1 parent 2c805ae commit 408ac864c1245876e5d34fe9a54d99c304060658 @briancavalier briancavalier committed Feb 24, 2011
View
24 LICENSE.txt
@@ -0,0 +1,24 @@
+Open Source Initiative OSI - The MIT License
+
+http://www.opensource.org/licenses/mit-license.php
+
+Copyright (c) 2011 Brian Cavalier
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
6 README.txt
@@ -46,3 +46,9 @@ You can list a subset of the components you'd like to generate. For
example, when generating a view, if you don't need a strings file, you
could do: canhaz my/view/MyView js scss css html test (i.e. leaving
out strings)
+
+------------------------------------------------------------------------
+License
+------------------------------------------------------------------------
+
+Canhaz is available under the MIT license. See LICENSE.txt for details.
View
27 bin/gen-object
@@ -27,26 +27,37 @@ function relpath {
# $2 - generator, e.g. "html"
#
function generate {
- local GEN_EXE="$1/$2"
+ local GEN_EXE="$1/gen-$2"
- execute "$GEN_EXE" "Don't know how to generate $2 for $1"
+ # Try to execute the specific gen type, but if that doesn't exist or fails,
+ # try to execute the generic generator for this object type and pass it
+ # the aspect to be generated.
+ execute "$GEN_EXE"
+ if [ $? -ne 0 ]; then
+ execute "$1/generate" "$2"
+ if [ $? -ne 0 ]; then
+ err "Don't know how to generate $2 for $1"
+ fi
+ fi
}
#
# Execute the specified generator script, or print an error if the script
# doesn't exist
#
function execute {
+ local RET=1
local EXE="${GEN_BASE_DIR}/$1"
- # echo "Executing $EXE"
+ shift
# If the requested file exists, execute it
- # If it doesn't, and error message was supplied, print it
+ # If it doesn't return an error
if [[ -f "$EXE" ]]; then
- sh "$EXE"
- elif [[ "$2" != "" ]]; then
- err "$2"
+ sh "$EXE" "$@"
+ RET=0
fi
+
+ return $RET
}
export -f template relpath
@@ -73,7 +84,7 @@ fi
# Run all generators
for GEN in $GENERATORS
do
- generate "$OBJECT_TYPE" "gen-$GEN"
+ generate "$OBJECT_TYPE" "$GEN"
done
# Run type-specific teardown if present
View
3 bin/generate
@@ -146,7 +146,6 @@ do
done
BASE_DIR=`pwd`
-TEST_DIR=${BASE_DIR/\/src\/main/\/src\/test}
# We want at least one non-option argument.
# Remove this block if you don't need it.
@@ -187,7 +186,7 @@ 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
+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"
View
6 config
@@ -1,3 +1,6 @@
+# Copyright text used in any/all generators.
+COPYRIGHT="Copyright (c) 2011, Your Company, Inc. All rights reserved."
+
# The default type of thing to generate
DEFAULT_TYPE="canhaz:help"
@@ -28,3 +31,6 @@ GEN_BASE_DIR="$MY_DIR/generators"
# PHP executable
PHP="/usr/bin/env php"
+
+# perl executable
+PERL="/usr/bin/env perl"
View
8 generators/canhaz/config/gen-config
@@ -1,4 +1,8 @@
#!/bin/sh
-msg " ${MODULE_PATH:-.}/.canhaz/config"
-cp "$CANHAZ_DIR/config" config
+DIR="${MODULE_PATH:-.}/.canhaz"
+FILE="$DIR/config"
+msg " $FILE"
+
+mkdir -p "$DIR"
+template "$MY_DIR/templates/config" > "$FILE"
View
42 generators/canhaz/config/templates/config
@@ -0,0 +1,42 @@
+# This is a generated canhaz config file. Anything you set in
+# this file will override the base configuration.
+
+# Copyright text used in any/all generators.
+#COPYRIGHT="Copyright (c) 2011, Your Company, Inc. All rights reserved."
+
+# The default type of thing to generate
+# You can override this to set which generator should be run
+# when one is not specified
+#DEFAULT_TYPE="canhaz:help"
+
+# Override output dirs to point to whatever you want. By default
+# canhaz will generate files starting in the current dir. So only
+# set these if you want to override that behavior.
+#
+# Example 1
+# If you want to explicitly specify an output dir and test output
+# dir.
+# BASE_DIR="/path/to/output/src"
+# TEST_DIR="/path/to/output/test"
+#
+# Example 2
+# If you have a custom canhaz config file in your base project dir,
+# you can tell canhaz to default to generating output in the parent
+# dir of your config. For example, if your project dir is
+# /Users/brian/Projects/thinger
+# and you have a custom config at
+# /Users/brian/Projects/thinger/.canhaz/config
+# using this setting will cause canhaz to generate files to
+# /Users/brian/Projects/thinger
+# BASE_DIR="$CONFIG_PARENT_DIR"
+# TEST_DIR="$BASEDIR/../test"
+
+# Where to find generators
+# If you want to keep your generators outside of the canhaz install
+# dir, you can point this wherever you want to keep them
+#GEN_BASE_DIR="$MY_DIR/generators"
+
+# PHP executable
+# This should work in most cases, but if you want to use a particular
+# PHP, point to it here.
+#PHP="/usr/bin/env php"
View
4 generators/jquery/widget/gen-css
@@ -1,4 +0,0 @@
-#!/bin/sh
-
-msg " $MODULE_BASE/$MODULE.css"
-template "$MY_DIR/templates/widget.css" > "$MODULE.css"
View
4 generators/jquery/widget/gen-js
@@ -1,4 +0,0 @@
-#!/bin/sh
-
-msg " $MODULE_BASE/$MODULE.js"
-template "$MY_DIR/templates/widget.js" > "$MODULE.js"
View
4 generators/jquery/widget/generate
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+msg " $MODULE_BASE/$MODULE.$1"
+template "$MY_DIR/templates/widget.$1" > "$MODULE.$1"
View
11 generators/setup
@@ -1,13 +1,10 @@
-# Copyright text used in any/all generators.
-COPYRIGHT=`template "${TEMPLATE_DIR}/copyright.txt"`
-
# Collect all gen-* scripts for the object. These can be overridden in
# the object's setup script, but this is a reasonable default.
-export GENERATORS=`ls -1 "$GEN_BASE_DIR/$OBJECT_TYPE/gen-"* | sed -E 's/.*\/gen-(.*)$/\1/g'`
+export GENERATORS=`ls -1 "$GEN_BASE_DIR/$OBJECT_TYPE" | grep -E 'gen-.+$' | sed -E 's/.*\/gen-(.*)$/\1/g'`
# Create explicit capitalized and non-capitalized versions of the name
# instead of forcing the user to specify one or the other
-NAME=`echo "$MODULE" | perl -wp -e '$_ = ucfirst'`
-LNAME=`echo "$MODULE" | perl -wp -e '$_ = lcfirst'`
+NAME=`echo "$MODULE" | $PERL -wp -e '$_ = ucfirst'`
+LNAME=`echo "$MODULE" | $PERL -wp -e '$_ = lcfirst'`
-export COPYRIGHT NAME LNAME
+export NAME LNAME

0 comments on commit 408ac86

Please sign in to comment.
Something went wrong with that request. Please try again.