-
Notifications
You must be signed in to change notification settings - Fork 29
/
extract-sublime.sh
executable file
·93 lines (79 loc) · 2.35 KB
/
extract-sublime.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
#!/usr/bin/env bash
# Exit on first failing command
set -e
# Handle our parameters
url="$1"
if test -z "$url"; then
echo "Expected <url> parameter but it was not provided. Please provide a download URL for a Sublime Text tarball" 1>&2
echo "Usage: ./extract-sublime.sh <url>" 1>&2
exit 1
fi
# Check for the presence of 'cut' or 'gcut' utility
if [[ "$OSTYPE" == "darwin"* ]] && $(which gcut &> /dev/null); then
CUT="gcut"
elif $(which cut &> /dev/null); then
CUT="cut"
else
echo "Missing \`cut\` utility. Please install \`cut\` or \`gcut\`" 1>&2
exit 1
fi
# Create a temporary folder to work in
if test -d "tmp/"; then
rm -r tmp/
fi
mkdir tmp/
# Download our package
cd tmp/
wget "$url"
# Extract the package
filename="$(basename "$url")"
tar xf "$filename"
# Enter our directory
subl_dir="sublime_text"
cd "$subl_dir"
# Find and remove files over 1MB (e.g. `plugin_host`, `sublime_text`)
for file in *; do
# If it's a file (e.g. not a folder/symlink)
if test -f "$file"; then
# Get its size
# `du $file | cut --fields 1`: `4172 plugin_host` -> `4172` (kb)
filesize="$(du "$file" | "$CUT" --fields 1)"
# If it's over 1MB, then remove it
if test "$filesize" -ge 1024; then
echo "Removing $file due to being over 1MB" 1>&2
rm "$file"
fi
fi
done
# For each of the Sublime packages
cd Packages/
for file in *; do
# Extract the packages to its corresponding folder
# e.g. `ActionScript.sublime-package` -> `ActionScript`
pkg_dir="$(echo "$file" | sed -E "s/.sublime-package//")"
unzip -d "$pkg_dir" "$file";
# Remove the original file
rm "$file"
done
cd ../
# Perform a sanity check that no single file is over 1MB
shopt -s globstar
for file in **/*; do
# If it's a file (e.g. not a folder/symlink)
if test -f "$file"; then
# Get its size
# `du $file | cut --fields 1`: `4172 plugin_host` -> `4172` (kb)
filesize="$(du "$file" | "$CUT" --fields 1)"
# If it's over 1MB, then complain about it
if test "$filesize" -ge 1024; then
echo "WARNING: $file is over 1MB large" 1>&2
fi
fi
done
shopt -u globstar
# Transfer our files
# `ls` -> `sublime_text_3 sublime_text_3_build_3083_x64.tar.bz2`
cd ../
cp -R "$subl_dir"/* ../
# Navigate out of our temporary directory and notify the user that we are done
echo "Extraction complete. Please run \`git status\` to see what has changed." 1>&2