-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-design
executable file
·149 lines (123 loc) · 4.04 KB
/
git-design
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
#!/bin/bash
# vim: set syntax=sh :
set +exo pipefail
# Take a list of json files, and compress each before copying into the archive.
# ie, compress-json meta.json user.json
compress-json() {
for file in "$@"; do
local hashsum="$(echo "${file}" | md5sum | awk '{ print $1 }')"
echo " . ${file} (${hashsum})"
cat "${file}" | jq -c . > /tmp/git-design$$/${hashsum}
mv "/tmp/git-design$$/${hashsum}" "${file}"
done
}
# Take a list of json files, and decompress each right after being unpacked from the archive.
# ie, decompress-json meta.json user.json
decompress-json() {
for file in "$@"; do
local hashsum="$(echo ${file} | md5sum | awk '{ print $1 }')"
echo " . ${file} (${hashsum})"
cat "${file}" | jq . > "/tmp/git-design$$/${hashsum}"
mv "/tmp/git-design$$/${hashsum}" "${file}"
done
}
unpack() {
for n in "$@"; do
# If the unpacked versino was passed, map back to the packed one.
if echo "$n" | grep 'unpacked/*$' > /dev/null; then
n="$(echo $n | sed 's/-unpacked\/*$//g')"
fi
echo " => $n"
local filename="$(pwd)/$n"
local unpacked_filename="${filename}-unpacked"
local file_hashsum="$(echo "${filename}" | md5sum | awk '{ print $1 }')"
if ! [ -f "${filename}" ]; then
echo " . file ${filename} doesn't exist"
continue
fi
if [ "$(cat "${unpacked_filename}/.hashsum" 2> /dev/null)" = "${file_hashsum}" ]; then
echo " . already at latest"
continue
fi
case "${filename}" in
*.sketch)
mkdir -p "${unpacked_filename}" >> /dev/null
echo "${file_hashsum}" > "${unpacked_filename}/.hashsum"
pushd "${unpacked_filename}" >> /dev/null || exit 1
unzip -o "${filename}" >> /dev/null
# Expand some sketch-specific json files
mkdir /tmp/git-design$$
decompress-json meta.json user.json pages/*.json
rm -rf /tmp/git-design$$
popd >> /dev/null || exit 1
;;
*.studio)
mkdir -p "${unpacked_filename}" >> /dev/null
echo "${file_hashsum}" > "${unpacked_filename}/.hashsum"
pushd "${unpacked_filename}" >> /dev/null
unzip -o "${filename}" >> /dev/null
# Expand some studio-specific json files
mkdir /tmp/git-design$$
decompress-json metadata.json settings.json snapshot.json
rm -rf /tmp/git-design$$
popd >> /dev/null || exit 1
;;
*)
echo " unsure how to unpack ${filename}"
esac
done
}
pack() {
for n in "$@"; do
# If the unpacked versino was passed, map back to the packed one.
if echo "$n" | grep 'unpacked/*$' > /dev/null; then
n="$(echo $n | sed 's/-unpacked\/*$//g')"
fi
echo " => $n"
local filename="$(pwd)/$n"
local unpacked_filename="${filename}-unpacked"
if ! [ -d "${unpacked_filename}" ]; then
continue
fi
case "${filename}" in
*.sketch)
mkdir -p /tmp/git-design$$
cp -R "${unpacked_filename}" /tmp/git-design$$
pushd "/tmp/git-design$$/$(basename "${unpacked_filename}")" >> /dev/null
# Remove the hashsum file, it's not needed in the export
rm -rf .hashsum
# Expand some sketch-specific json files
compress-json meta.json user.json pages/*.json
zip -r "$(basename "${filename}")" -- * >> /dev/null
mv "$(basename "${filename}")" "${filename}"
popd >> /dev/null || exit 1
rm -rf /tmp/git-design$$
;;
*.studio)
mkdir -p /tmp/git-design$$
cp -R "${unpacked_filename}" "/tmp/git-design$$"
pushd "/tmp/git-design$$/$(basename ${unpacked_filename})" >> /dev/null
rm -rf .hashsum
compress-json metadata.json settings.json snapshot.json
zip -r "$(basename "${filename}")" -- * >> /dev/null
mv "$(basename "${filename}")" "${filename}"
popd >> /dev/null || exit 1
rm -rf /tmp/git-design$$
;;
esac
done
}
case $1 in
pack)
shift 1
pack $@
;;
unpack)
shift 1
unpack $@
;;
*)
echo "git-design is a way to manage binary design assets in git, built by Ryan Gaus <rgaus.net>"
echo "Read documentation at https://github.com/1egoman/git-design"
;;
esac