Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create java file for constants interface for GWT from property file
#!/bin/sh
allargs=$@
extendsType=com.google.gwt.i18n.client.Constants
if [[ $allargs == *"--extends="* ]]; then
echo " --extends=constants-class-name Constantsの拡張クラスを指定できます。指定がない場合はConstantsになります。"
extendsType=`echo $allargs | sed -e "s/.*--extends=\\([a-zA-Z0-9_.]*\\).*/\\1/g"`
allargs=`echo $allargs | sed -e "s/\\(.*\\)\\(--extends=[a-zA-Z0-9_.]*\\)\\(.*\\)/\\1\\3/g"`
fi
instanceDef=0
if [[ $allargs == *"--instance-def "* ]]; then
echo " --instance-def INSTANCEという名前でインスタンスを内部に定義します。"
allargs=${allargs//--instance-def /}
instanceDef=1
fi
allargs=${allargs## }
allargs=${allargs%% }
propertyFile=$allargs
function usage() {
echo "mvn構成のmain/resources/ や test/resources/ 内の指定されたプロパティファイルから値をjavadocコメントにしてメソッド定義したgwt.i18n.client.Constantsの実装ファイルを作成します。"
echo ""main/java/ や test/java/ 配下の同じパス内に上書き作成します。
echo ""
echo "Usage...: ${0##*/} [--extends=constants-class-name] [--instance-def] property-file-name"
echo " --extends=constants-class-name Constantsの拡張クラスを指定できます。指定がない場合はConstantsになります。"
echo " --instance-def INSTANCEという名前で作成したインターフェースの中に定義します。"
echo ""
echo "ex)"
echo " ${0##*/} --extends=com.google.gwt.i18n.client.ConstantsWithLookup"
}
if [ "$propertyFile" == "" ] || [ ! -e $propertyFile ]; then
usage
exit 1
fi
propertyFile=$(cd $(dirname $propertyFile) && pwd)/$(basename $propertyFile)
if [ "java" == "${propertyFile##*.}" ]; then
javaFile=$propertyFile
javaFile=${javaFile%.java}
propertyFile=${propertyFile//\/java\//\/resources\/}
propertyFile=${propertyFile%.java}
test_propertyFile=${propertyFile}_ja.properties
if [ ! -e $test_propertyFile ]; then
test_propertyFile=${propertyFile}_en.properties
if [ ! -e $test_propertyFile ]; then
test_propertyFile=${propertyFile}.properties
if [ ! -e $test_propertyFile ]; then
echo "File not found - ${propertyFile}.properties"
usage
exit 1
fi
fi
fi
propertyFile=$test_propertyFile
if [ "$propertyFile" == "" ] || [ ! -e $propertyFile ]; then
usage
exit 1
fi
else
javaFile=${propertyFile//\/resources\//\/java\/}
javaFile=${javaFile%_ja.properties}
javaFile=${javaFile%_en.properties}
fi
className=${javaFile##*/}
package=${javaFile%/*}
package=${package#*/java/}
package=${package//\//\.}
javaFile=${javaFile}.java
extendsPackage=${extendsType%.*}
extendsType=${extendsType##*.}
echo $propertyFileを、
echo $javaFileに変換して上書きします。
echo "package $package;">$javaFile
echo "">>$javaFile
if [[ $instanceDef == 1 ]]; then
echo "import com.google.gwt.core.client.GWT;">>$javaFile
fi
if [[ $package != $extendsPackage ]]; then
echo "import $extendsPackage.$extendsType;">>$javaFile
fi
echo "">>$javaFile
echo "public interface $className extends $extendsType {">>$javaFile
if [[ $instanceDef == 1 ]]; then
echo "">>$javaFile
echo "\t$className INSTANCE = GWT.create($className.class);">>$javaFile
fi
name=
value=
eol_esc=0
CT=BLANK
native2ascii -reverse $propertyFile | while read -r line
do
if [[ $eol_esc == 0 ]]; then
if [[ $line =~ \\$ ]]; then
eol_esc=1
else
eol_esc=0
fi
#コメントや空行は無視
if [[ $line =~ \s*# ]]; then
echo "">>$javaFile
if [[ $eol_esc == 0 ]]; then
echo "\t/*"$line "*/">>$javaFile
else
echo "\t/*">>$javaFile
echo "\t * ${line%\\}">>$javaFile
CT=COMMENT
fi
elif [[ $line =~ ^\s*$ ]]; then
if [[ $eol_esc == 0 ]]; then
echo "">>$javaFile
else
CT=BLANK
fi
else
name=${line%%=*}
value=${line#*=}
value=${value%\\}
value=${value%\\n}
if [[ $eol_esc == 0 ]]; then
echo "">>$javaFile
if [[ $value =~ \\n ]]; then
echo "\t/**">>$javaFile
echo "\t * ${value//\\n/\\n\\t * }">>$javaFile
echo "\t */">>$javaFile
else
echo "\t/** ${value} */">>$javaFile
fi
echo "\tString "$name"();">>$javaFile
else
echo "">>$javaFile
echo "\t/**">>$javaFile
echo "\t * ${value//\\n/\\n\\t * }">>$javaFile
CT=NV
fi
fi
else
if [[ $line =~ \\$ ]]; then
eol_esc=1
else
eol_esc=0
fi
if [[ $CT == COMMENT ]]; then
echo "\t * ${line%\\}">>$javaFile
if [[ $eol_esc == 0 ]]; then
echo "\t */">>$javaFile
CT=
fi
elif [[ $CT == BLANK ]]; then
echo "">>$javaFile
if [[ $eol_esc == 0 ]]; then
CT=
fi
elif [[ $CT == NV ]]; then
value=${line%\\}
value=${value%\\n}
echo "\t * ${value//\\n/\\n\\t * }">>$javaFile
if [[ $eol_esc == 0 ]]; then
echo "\t */">>$javaFile
echo "\tString "$name"();">>$javaFile
CT=
fi
fi
fi
done
echo "">>$javaFile
echo "}">>$javaFile
echo "作成しました。"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.