summaryrefslogtreecommitdiffstats
path: root/osx/bundle/bundle
blob: 379caf221cb2f536d18093d40f69a256e5c96584 (plain)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/bin/bash

pushd $(dirname "$0") > /dev/null
D=$(pwd -P)
popd > /dev/null

ME="$0"
BUNDLE_NAME="Gedit"
BUNDLE="$D/$BUNDLE_NAME.app"

function do_strip {
	tp=$(file -b --mime-type "$1")

	if [ "$tp" != "application/octet-stream" ]; then
		return
	fi

	name=$(mktemp -t bundle)
	st=$(stat -f %p "$1")

	strip -o "$name" -S "$1"
	mv -f "$name" "$1"

	chmod "$st" "$1"
	chmod u+w "$1"
}

function dmg_help_short() {
	echo "Create a dmg from a bundle"
}

function cmd_dmg() {
	# Copied and modified from Banshee
	if [ ! -d "$BUNDLE" ]; then
		cmd_make
	fi

	VOLUME_NAME="$BUNDLE_NAME"

	if [ ! -z "$1" ]; then
		DMG_FILE="$1.dmg"
	else
		DMG_FILE="$D/$BUNDLE_NAME.dmg"
	fi

	DMG_APP="$BUNDLE"
	TMP_MOUNT_POINT="$D/$VOLUME_NAME.mounted"
	FINAL_MOUNT_POINT="/Volumes/$VOLUME_NAME"

	rm -f $DMG_FILE
	rm -f $DMG_FILE.master

	# Compute an approximated image size in MB, and bloat by 30 MB
	image_size=$(du -ck "$DMG_APP" | tail -n1 | cut -f1)
	image_size=$((($image_size * 11) / 10000))

	echo "Creating disk image (${image_size}MB)..."

	TMPNAME="${DMG_FILE%.dmg}_tmp.dmg"

	cp "$D/data/template.dmg.bz2" "$TMPNAME.bz2"
	bunzip2 "$TMPNAME.bz2"

	hdiutil resize -size ${image_size}m "$TMPNAME" || exit 1

	echo "Attaching to disk image..."
	hdiutil attach "$TMPNAME" -readwrite -noautoopen -mountpoint "$TMP_MOUNT_POINT" -quiet || exit 1

	echo "Populating image..."
	rsync -az "$DMG_APP" "$TMP_MOUNT_POINT/" || exit 1

	echo "Ensuring permissions"
	chmod -Rf go-w "$TMP_MOUNT_POINT" 2>/dev/null

	echo "Blessing image..."
	bless --folder "$TMP_MOUNT_POINT" --openfolder "$TMP_MOUNT_POINT" || exit 1

	echo "Detaching from disk image..."
	hdiutil detach "$TMP_MOUNT_POINT" -quiet || exit 1

	echo "Converting to final image..."
	hdiutil convert -quiet -format UDBZ -o "$DMG_FILE" "$TMPNAME" || exit 1

	# Make internet-enable
	hdiutil internet-enable -yes "$DMG_FILE" || exit 1

	rm -f "$TMPNAME"

	n=$(basename "$DMG_FILE")
	echo "Done $n."
}

function make_help_usage() {
	echo "[-f]"
}

function make_help_short() {
	echo "Make the $BUNDLE_NAME.app bundle"
}

function build_gtk_mac_bundler() {
	b="$D/../build/build"
	lbin=$("$b" env local-bin)

	if [ -x "$lbin/gtk-mac-bundler" ]; then
		return
	fi

	echo "Installing gtk-mac-bundler..."

	home=$("$b" env home)
	sbin="$home/source"

	mkdir -p "$sbin"

	(
		cd "$sbin"

		if [ ! -d gtk-mac-bundler ]; then
			git clone https://github.com/jessevdk/gtk-mac-bundler || exit 1
		fi

		(
			cd gtk-mac-bundler
			git checkout -b stable 5994aa557ebb24687ed07e74910e4007521a53b4 || exit 1
			HOME="$home" make install
		) || exit 1
	) || exit 1
}

function make_fake_help_long() {
	echo "Create a fake bundle pointing towards files in the build. This is useful mostly to test bundle support."
}

function make_fake_help_short() {
	echo "Create a fake bundle"
}

function cmd_make_fake() {
	fake="$D/Gedit-fake.app"

	rm -rf "$fake"
	mkdir -p "$fake/Contents"

	b="$D/../build/build"
	inst=$("$b" env inst)
	source=$("$b" env source)

	mkdir -p "$fake/Contents/Resources"

	for d in share lib; do
		ln -s "$inst/$d" "$fake/Contents/Resources/$d"
	done

	mkdir "$fake/Contents/MacOS"

	bin="$fake/Contents/MacOS/Gedit"

	printf "#!/bin/bash\n\nexec $b run \"$source/gedit/gedit/gedit\"\n" > "$bin"
	chmod u+x "$bin"

	echo "APPL????" > "$fake/Contents/PkgInfo"

	cp "$D/data/Info.plist" "$fake/Contents/"
	cp "$D/data/gedit.icns" "$fake/Contents/Resources/"
}

function cmd_make() {
	build_gtk_mac_bundler

	if [ -d "$BUNDLE" ]; then
		rm -rf "$BUNDLE"
	fi

	b="$D/../build/build"

	# No idea why it's not writable, but we need it to be so that install_name_tool
	# can change the link paths
	inst=$("$b" env inst)
	chmod u+w "$inst/lib/libpython3.3m.dylib"

	lbin=$("$b" env local-bin)

	echo "Generating bundle from Gedit.bundle specification..."
	"$b" run "$lbin/gtk-mac-bundler" "$D/data/Gedit.bundle" || exit 1

	mv "$D/data/$BUNDLE_NAME.app" "$BUNDLE"

	echo "Removing unneeded files from bundle"

	# Remove pyc and pyo files
	for d in lib share/gedit/plugins; do
		find "$BUNDLE/Contents/Resources/$d/"* -type f -regex '.*\.py[oc]' -exec 'rm -f {}' 2>/dev/null
		find "$BUNDLE/Contents/Resources/$d/"* -type d -name '__pycache__' -exec 'rmdir {}' 2>/dev/null
	done

	echo "Creating localized names..."
	locales=$(find "$inst/share/locale" -name gedit.mo | sed 's/.*locale\/\([^\/]*\)\/.*/\1/g' | /usr/bin/grep -v @)

	for locale in en $locales; do
		printf "$locale "
		ldir="$BUNDLE/Contents/Resources/$locale.lproj"

		lname=$(LANG=$locale "$b" run gettext gedit "Text Editor")

		mkdir -p "$ldir"

		echo "CFBundleName = \"Gedit\";" > "$ldir/InfoPlist.strings"
		echo "CFBundleDisplayName = \"Gedit $lname\";" >> "$ldir/InfoPlist.strings"
	done

	printf ", done...\n\nDo you want to strip debug symbols? [\033[1myes\033[0m/no] "
	read strip

	if [ "$strip" = "yes" ] || [ -z "$strip" ]; then
		echo "Strip debug symbols from bundle binaries"

		# Strip debug symbols from libraries/modules
		for i in $(find -E "$BUNDLE/Contents/Resources" -type f -regex '.*\.(so|dylib)$' 2>/dev/null); do
			do_strip "$i"
		done

		# Strip debug symbols from binaries
		for i in $(find "$BUNDLE/Contents/Resources/bin" -type f 2>/dev/null); do
			if [ -x "$i" ]; then
				do_strip "$i"
			fi
		done

		# Strip debug symbols from main binary
		do_strip "$BUNDLE/Contents/MacOS/gedit-bin"
	fi
}

function help_help_short() {
	echo "Shows this help message"
}

function cmd_help() {
	if [ -z "$1" ]; then
		echo "Usage: $ME [command]"
		echo ""
		echo "Available commands:"
		echo ""

		for cmd in "${commands[@]}"; do
			printf "  \033[1m$cmd\x1B[0m "

			l=${#cmd}
			let d="$commandsmaxlen - $l + 1"

			printf "%${d}s" ""
			echo -n "- "

			icmd=${cmd/-/_}

			if [[ $(type -t "${icmd}_help_short") = "function" ]]; then
				"${icmd}_help_short"
			else
				echo ""
			fi
		done

		echo ""
	else
		cmd="cmd_${1/-/_}"

		if [[ $(type -t "$cmd") != "function" ]]; then
			printf "Invalid command \033[1m$1\033[0m, available commands are: $cmds\n"
			exit 1
		fi

		printf "Usage: $ME \033[1m$1\033[0m "

		if [[ $(type -t "${1/-/_}_help_usage") = "function" ]]; then
			"${1/-/_}_help_usage"
		else
			echo ""
		fi

		echo ""

		if [[ $(type -t "${1/-/_}_help_long") != "function" ]]; then
			"${1/-/_}_help_short"
		else
			"${1/-/_}_help_long"
		fi
	fi
}

commands=()
commandsmaxlen=0

while read line
do
	cmd=${line#declare -f }

	if [[ "$cmd" = cmd_* ]]; then
		cname=${cmd#cmd_}

		commands+=(${cname/_/-})

		l=${#cname}

		if [[ $l > $commandsmaxlen ]]; then
			commandsmaxlen=$l
		fi
	fi
done < <(declare -F)

cmds=$(printf ", \033[1m%s\033[0m" "${commands[@]}")
cmds=${cmds:2}

if [ -z "$1" ]; then
	cmd_help
	exit 0
fi

cmd="cmd_${1/-/_}"

if [[ $(type -t "$cmd") != "function" ]]; then
	printf "Invalid command $1, available commands are: $cmds\n"
	exit 1
fi

shift 1
"$cmd" "$@"