bundle 6.67 KB
Newer Older
1 2 3 4 5 6 7
#!/bin/bash

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

ME="$0"
8 9
BUNDLE_NAME="Gedit"
BUNDLE="$D/$BUNDLE_NAME.app"
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

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"
}

28 29 30 31
function dmg_help_short() {
	echo "Create a dmg from a bundle"
}

32 33 34 35 36 37 38 39
function cmd_dmg() {
	# Copied and modified from Banshee
	if [ ! -d "$BUNDLE" ]; then
		cmd_make
	fi

	VOLUME_NAME="$BUNDLE_NAME"

40 41 42 43 44
	if [ ! -z "$1" ]; then
		DMG_FILE="$1.dmg"
	else
		DMG_FILE="$D/$BUNDLE_NAME.dmg"
	fi
45 46 47 48 49 50 51 52 53 54

	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)
55
	image_size=$((($image_size * 11) / 10000))
56 57 58 59 60

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

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

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

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
	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."
}

93 94 95 96 97
function make_help_usage() {
	echo "[-f]"
}

function make_help_short() {
98
	echo "Make the $BUNDLE_NAME.app bundle"
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
}

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
125
			git checkout -b stable 5994aa557ebb24687ed07e74910e4007521a53b4 || exit 1
126 127 128 129 130
			HOME="$home" make install
		) || exit 1
	) || exit 1
}

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
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/"
}

168 169 170
function cmd_make() {
	build_gtk_mac_bundler

171
	if [ -d "$BUNDLE" ]; then
172 173 174 175 176 177 178 179 180 181
		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"

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

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

187
	mv "$D/data/$BUNDLE_NAME.app" "$BUNDLE"
188 189 190 191

	echo "Removing unneeded files from bundle"

	# Remove pyc and pyo files
192 193 194
	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
195 196
	done

197 198 199 200 201 202 203 204
	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")
205

206
		mkdir -p "$ldir"
207

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

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

215
	if [ "$strip" = "yes" ] || [ -z "$strip" ]; then
216 217 218 219
		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
220
			do_strip "$i"
221
		done
222

223 224 225 226 227 228 229 230 231 232
		# 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
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
}

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 "- "

255 256 257 258
			icmd=${cmd/-/_}

			if [[ $(type -t "${icmd}_help_short") = "function" ]]; then
				"${icmd}_help_short"
259 260 261 262 263 264 265
			else
				echo ""
			fi
		done

		echo ""
	else
266
		cmd="cmd_${1/-/_}"
267 268 269 270 271 272 273 274

		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 "

275 276
		if [[ $(type -t "${1/-/_}_help_usage") = "function" ]]; then
			"${1/-/_}_help_usage"
277 278 279 280 281 282
		else
			echo ""
		fi

		echo ""

283 284
		if [[ $(type -t "${1/-/_}_help_long") != "function" ]]; then
			"${1/-/_}_help_short"
285
		else
286
			"${1/-/_}_help_long"
287 288 289 290 291 292 293 294 295 296 297 298 299 300
		fi
	fi
}

commands=()
commandsmaxlen=0

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

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

301
		commands+=(${cname/_/-})
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

		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

319
cmd="cmd_${1/-/_}"
320 321 322 323 324 325 326 327

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

shift 1
"$cmd" "$@"