-
Notifications
You must be signed in to change notification settings - Fork 5
/
qmlpp.sh
133 lines (118 loc) · 3.61 KB
/
qmlpp.sh
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
#!/bin/bash
#
# qmlpp - KISS QML and JavaScript preprocessor
#
# Copyright (C) 2013 Andre Beckedorf
# <evilJazz _AT_ katastrophos _DOT_ net>
#
# This library is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version
# 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA
#
# Alternatively, this file is available under the Mozilla Public
# License Version 1.1. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
findCmd=$(which find)
sedCmd=$(which sed)
sedOSCmdExt=
isWindows=$(echo $OS | grep "Windows")
if [ "$isWindows" != "" ]; then
findCmd="/usr/bin/find" # to override Microsoft Windows FIND command and
sedCmd="/usr/bin/sed" # make sure the Cygwin's find and sed are launched...
sedOSCmdExt=-b
fi
sedPingTest=$(echo "ping" | $sedCmd -r "s/ping/pong/" 2> /dev/null)
if [ "$sedPingTest" != "pong" ]; then
sedIsBSD=true
sedCmdExt=-E
else
sedIsBSD=
sedCmdExt=-r
fi
function callSed()
{
if [[ -n "$sedIsBSD" && -n "$processInline" ]]; then
"$sedCmd" $sedOSCmdExt $sedCmdExt -i '' "$@" # Braindead BSD sed syntax, try to escape '' in Bash!
else
"$sedCmd" $sedOSCmdExt $sedCmdExt $additionalSedArgs "$@"
fi
}
function preprocessFile()
{
callSed \
-e "$rewriteParams" \
-e "s/^([ \x9]*)(\/\/){0,1}(.*)\/\/@(.*)/\1\3\/\/@\4/" \
-e "/($defines)/!s/^([ \x9]*)(.*)\/\/(@.*)/\1\/\/\2\/\/\3/" \
"$1"
}
function preprocessDirectory()
{
"$findCmd" "$1" -type f | grep -v .svn | grep -v .git | while read file; do
[[ "${file##*.}" == "qml" || "${file##*.}" == "js" ]] && preprocessFile "$file"
done
}
function usage()
{
cat << EOF
usage: $0 [options] <filename JS or QML or directoryname>
OPTIONS:
-h Show this message.
-q <major.minor> The version of QtQuick to rewrite to. Example: -q 2.1
-d <defines> The defines to set, separated by |. Example: -d "@QtQuick1|@Meego|@Debug"
-i Modify file in-place instead of dumping to stdout.
EOF
}
processInline=
additionalSedArgs=
defines=@NODEFINESET
while getopts ":hiq:d:" OPTION; do
case $OPTION in
q)
rewriteQtQuickVersion="$OPTARG"
;;
d)
defines="$OPTARG"
;;
i)
processInline=true
additionalSedArgs="-i"
;;
h)
usage
exit 1
;;
?)
echo "Invalid option: -$OPTARG" >&2
usage
exit 1
;;
esac
done
rewriteParams=""
[ "$rewriteQtQuickVersion" != "" ] && rewriteParams="/\/\/!noRewrite/!s/(import QtQuick)[ \x9]*[0-9].[0-9]/\1 $rewriteQtQuickVersion/"
input="${@: -1}"
if [[ -f "$input" || "$input" == "-" ]]; then
preprocessFile "$input"
elif [ -d "$input" ]; then
if [ "$processInline" != "true" ]; then
echo "Please specify -i when trying to preprocess a whole directory recursively."
usage
exit 1
fi
preprocessDirectory "$input"
else
echo "Please specify a valid file or directory."
usage
exit 1
fi
exit 0