File: build.sh

package info (click to toggle)
libjs-jquery-selectize.js 0.12.6%2Bdfsg-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,448 kB
  • sloc: javascript: 11,604; sh: 116; makefile: 44
file content (213 lines) | stat: -rwxr-xr-x 5,693 bytes parent folder | download | duplicates (3)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/bin/bash

# Author: Sergio Durigan Junior <sergiodj@sergiodj.net>

# Disclaimer: This script contains bashisms.  It will probably not
# work very well with other shell interpreters.

# The source directory of selectize.js.  If building the Debian
# package, this is $(CURDIR)/../ (from debian/rules).
SOURCEDIR=

# The build directory, where we'll put the generated selectize.js.
BUILDDIR=

# The version of selectize.js being built.  'dpkg-parsechangelog -S
# Version' should be helpful here.
VERSION=


# Print the usage message on stdout.

usage ()
{
    cat <<EOF
$0 -- build selectize.js without using grant

Usage:

$0 SOURCE_DIR BUILD_DIR VERSION

Where:

  SOURCE_DIR -- The source directory (i.e., where selectize.js source
  is located).  If this script is being called from debian/rules, this
  is "\$(CURDIR)/../".

  BUILD_DIR -- Where the built files will be put.

  VERSION -- The version of selectize.js.
EOF
}

# Sanitize the arguments received via CLI.

sanitize_cli_args ()
{
    # source dir
    local sdir=$1
    # build dir
    local bdir=$2
    # version
    local ver=$3

    # Does the source directory exist?
    if test ! -d $sdir ; then
	echo "E: '$1' is not a valid directory."
	exit 1
    fi

    # Do we have a 'src/' directory inside the source directory?
    if test ! -d $sdir/src/ ; then
	echo "E: '$1/src/' doesn't exist.  Are you sure you provided the source directory?"
	exit 1
    fi

    # Change to the source directory.  That's where we'll make the
    # build.
    cd $sdir

    # Setting the global variables.
    SOURCEDIR=$sdir
    BUILDDIR=$bdir
    VERSION=$ver
}

# Prepare the build directory to receive the generated files.  This
# basically means removing the old directory and creating it again.

prepare_build_dir ()
{
    rm -rf $BUILDDIR
    mkdir -p $BUILDDIR
}

# Build selectize.js.
#
# This is the most important function of this script, of course.  It
# was heavily inspired by the original Gruntfile.js, from upstream.
# Since Debian does not ship Grunt, I had to write this "poor man's
# builder" for selectize.js.

build_js ()
{
    # List of files that will compose the final selectize.js script.
    # The order of the files on this list *matters*!
    local file_list="src/contrib/*.js src/constants.js src/utils.js \
src/selectize.js src/defaults.js src/selectize.jquery.js \
src/plugins/drag_drop/*.js src/plugins/dropdown_header/*.js \
src/plugins/optgroup_columns/*.js src/plugins/remove_button/*.js \
src/plugins/restore_on_backspace/*.js"
    # Temporary directory where we'll do some file manipulations.
    local tmpdir=`mktemp -d -p .`

    for files in $file_list ; do
	for file in `ls $files` ; do
	    # We'll do some modifications to the file, so we copy it
	    # to the temporary directory.
	    local b=`dirname $file`
	    mkdir -p $tmpdir/$b
	    cp -a $file $tmpdir/$file

	    if head -n1 $tmpdir/$file | grep -q '^\/\*\*' ; then
		# If the first line of the file is a comment, then
		# this means the file has a license header and we need
		# to remove it (Grunt calls it a 'banner').  The code
		# below is just a quick and dirty way of getting rid
		# of the banner without resorting to magical sed
		# expressions.
		local SAW_FIRST_COMMENT_BEGIN=0
		local SAW_FIRST_COMMENT_END=0

		while IFS= read -r line || test -n "$line" ; do
		    if test $SAW_FIRST_COMMENT_BEGIN -eq 0 ; then
			if echo "$line" | grep -q '^\/\*\*' ; then
			    SAW_FIRST_COMMENT_BEGIN=1
			    continue
			fi
		    elif test $SAW_FIRST_COMMENT_END -eq 0 ; then
			if echo "$line" | grep -q '^ \*\/$' ; then
			    SAW_FIRST_COMMENT_END=1
			fi
			continue
		    fi
		    echo "$line" >> $tmpdir/$file.1
		done < $tmpdir/$file

		mv $tmpdir/$file.1 $tmpdir/$file
	    fi

	    # Every line must be prepended with a TAB.
	    sed -i -e 's/^/\t/' $tmpdir/$file

	    # Concatenating the file.
	    cat $tmpdir/$file >> $BUILDDIR/selectize.js.1
	    /bin/echo -ne '\n\n\t\n' >> $BUILDDIR/selectize.js.1
	done
    done

    # Taking care of src/.wrapper.js now.
    cp -a src/.wrapper.js $tmpdir/
    sed -i "s/@@version/$VERSION/" $tmpdir/.wrapper.js

    local FOUND_JS=0
    # src/.wrapper.js is really a wrapper...  selectize.js.1 needs to
    # be put "in the middle" of it, and we need to get rid of a
    # special mark (@@js) where selectize.js.1 will be placed.
    while IFS= read -r line || test -n "$line" ; do
	if echo "$line" | grep -q '^\s@@js$' > /dev/null 2>&1 ; then
	    FOUND_JS=1
	    continue
	fi

	if test $FOUND_JS -eq 0 ; then
	    echo "$line" >> $BUILDDIR/selectize.js
	else
	    echo "$line" >> $BUILDDIR/selectize.js.epilogue
	fi
    done < $tmpdir/.wrapper.js

    # Final concatenation.
    cat $BUILDDIR/selectize.js.1 >> $BUILDDIR/selectize.js
    cat $BUILDDIR/selectize.js.epilogue >> $BUILDDIR/selectize.js

    # Cleaning up.
    rm -rf $tmpdir
}

# Check that the generated selectize.js is equivalent to the one that
# comes with the upstream source.  Note: this function will have to be
# rewritten/adjusted/skipped if we ever have to patch the JavaScript
# sources.

check_js ()
{
    if ! diff --unified \
	 --ignore-tab-expansion \
	 --ignore-space-change \
	 --ignore-blank-lines \
	 dist/js/selectize.js \
	 $BUILDDIR/selectize.js ; then
	echo "E: The generated selectize.js is NOT equal to the dist/js/selectize.js."
	exit 1
    fi
}

# Check that we received the exact number of arguments expected.  See
# 'usage' for an explanation of each argument.
if test $# -ne 3 ; then
    usage
    exit 1
fi

# Sanitize the arguments received.
sanitize_cli_args $@

# Prepare the build directory.
prepare_build_dir

# Build and check the generated JavaScript file.
build_js
check_js

exit 0