forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchive.sh
More file actions
executable file
·145 lines (129 loc) · 3.63 KB
/
Copy patharchive.sh
File metadata and controls
executable file
·145 lines (129 loc) · 3.63 KB
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
#!/usr/bin/env bash
# This script creates an archive containing the given binary renamed to
# `coder(.exe)?`, as well as the README.md and LICENSE files from the repo root.
#
# Usage: ./archive.sh --format tar.gz --os linux/darwin/windows [--output path/to/output.tar.gz] [--sign-darwin] [--agpl] path/to/binary
#
# The --format parameter must be set, and must either be "zip" or "tar.gz".
#
# If the --output parameter is not set, the default output path is the binary
# path (minus any .exe suffix) plus the format extension ".zip" or ".tar.gz".
#
# If --sign-darwin is specified, the zip file is signed with the `codesign`
# utility and then notarized using the `gon` utility, which may take a while.
# $AC_APPLICATION_IDENTITY must be set and the signing certificate must be
# imported for this to work. Also, the input binary must already be signed with
# the `codesign` tool.
#
# If the --agpl parameter is specified, only the AGPL license is included in the
# outputted archive.
#
# The absolute output path is printed on success.
set -euo pipefail
# shellcheck source=scripts/lib.sh
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
format=""
output_path=""
sign_darwin="${CODER_SIGN_DARWIN:-0}"
os=""
agpl="${CODER_BUILD_AGPL:-0}"
args="$(getopt -o "" -l format:,output:,sign-darwin,os:,agpl -- "$@")"
eval set -- "$args"
while true; do
case "$1" in
--format)
format="${2#.}"
if [[ "$format" != "zip" ]] && [[ "$format" != "tar.gz" ]]; then
error "Invalid --format parameter '$format', must be 'zip' or 'tar.gz'"
fi
shift 2
;;
--output)
# realpath fails if the dir doesn't exist.
mkdir -p "$(dirname "$2")"
output_path="$(realpath "$2")"
shift 2
;;
--os)
os="$2"
shift 2
;;
--sign-darwin)
sign_darwin=1
shift
;;
--agpl)
agpl=1
shift
;;
--)
shift
break
;;
*)
error "Unrecognized option: $1"
;;
esac
done
if [[ "$format" == "" ]]; then
error "--format is a required parameter"
fi
if [[ "$#" != 1 ]]; then
error "Exactly one argument must be provided to this script, $# were supplied"
fi
if [[ ! -f "$1" ]]; then
error "File '$1' does not exist or is not a regular file"
fi
input_file="$(realpath "$1")"
sign_darwin="$([[ "$sign_darwin" == 1 ]] && [[ "$os" == "darwin" ]] && echo 1 || echo 0)"
if [[ "$sign_darwin" == 1 ]] && [[ "${AC_APPLICATION_IDENTITY:-}" == "" ]]; then
error "AC_APPLICATION_IDENTITY must be set when --sign-darwin or CODER_SIGN_DARWIN=1 is supplied"
fi
# Check dependencies
if [[ "$format" == "zip" ]]; then
dependencies zip
fi
if [[ "$format" == "tar.gz" ]]; then
dependencies tar
fi
if [[ "$sign_darwin" == 1 ]]; then
dependencies jq codesign gon
fi
# Determine default output path.
if [[ "$output_path" == "" ]]; then
output_path="${input_file%.exe}"
output_path+=".$format"
fi
# Determine the filename of the binary inside the archive.
output_file="coder"
if [[ "$input_file" == *".exe" ]]; then
output_file+=".exe"
fi
# Make temporary dir where all source files intended to be in the archive will
# be symlinked from.
cdroot
temp_dir="$(mktemp -d)"
ln -s "$input_file" "$temp_dir/$output_file"
ln -s "$(realpath README.md)" "$temp_dir/"
ln -s "$(realpath LICENSE)" "$temp_dir/"
if [[ "$agpl" == 0 ]]; then
ln -s "$(realpath LICENSE.enterprise)" "$temp_dir/"
fi
# Ensure parent output dir and non-existent output file.
mkdir -p "$(dirname "$output_path")"
if [[ -e "$output_path" ]]; then
rm "$output_path"
fi
cd "$temp_dir"
if [[ "$format" == "zip" ]]; then
zip "$output_path" ./* 1>&2
else
tar --dereference -czvf "$output_path" ./* 1>&2
fi
cdroot
rm -rf "$temp_dir"
if [[ "$sign_darwin" == 1 ]]; then
log "Notarizing archive..."
execrelative ./sign_darwin.sh "$output_path"
fi
echo "$output_path"