This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Copy pathreserve-cratesio-package-name.sh
executable file
·120 lines (108 loc) · 2.79 KB
/
reserve-cratesio-package-name.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
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
#!/usr/bin/env bash
display_help() {
local bin
bin="$(basename --suffix='.sh' "$0")"
cat <<EOF 1>&2
$bin
Reserve a Rust package name on crates.io
USAGE:
$bin [FLAGS] [OPTIONS] <TARGET_TYPE> <PACKAGE_NAME>
FLAGS:
--help Display this help message
--no-solana-prefix Do not require \`solana-\` prefix on PACKAGE_NAME
--publish Upload the reserved package. Without this flag, a
dry-run is performed
OPTIONS:
--token <TOKEN> Token used to authenticate with crates.io
ARGS:
TARGET_TYPE The package target type [possible values: bin, lib]
PACKAGE_NAME The desired package name [see:
https://doc.rust-lang.org/cargo/reference/manifest.html#the-name-field]
EOF
}
require_solana_prefix=true
maybe_publish='--dry-run'
positional=()
while [[ -n "$1" ]]; do
case "$1" in
--)
break
;;
--help)
display_help
exit 0
;;
--no-solana-prefix)
require_solana_prefix=false
;;
--publish)
maybe_publish=''
;;
--token)
maybe_crates_token="--token $2"
shift
;;
--* | -*)
echo "error: unexpected argument \`$1\`" 1>&2
display_help
exit 1
;;
*)
positional+=("$1")
;;
esac
shift
done
while [[ -n "$1" ]]; do
positional+=("$1")
shift
done
target_type="${positional[0]:?'TARGET_TYPE must be declared'}"
package_name="${positional[1]:?'PACKAGE_NAME must be declared'}"
case "${target_type}" in
bin)
src_filename='main.rs'
src_file_body='fn main() {}'
;;
lib)
src_filename='lib.rs'
src_file_body=''
;;
*)
echo "error: unexpected TARGET_TYPE: \`${target_type}\`" 1>&2
display_help
exit 1
;;
esac
if ! [[ "${package_name}" =~ ^[a-zA-Z0-9_-]{1,64} ]]; then
echo "error: illegal PACKAGE_NAME: \`${package_name}\`" 1>&2
display_help
exit 1
fi
if ${require_solana_prefix} && ! [[ "${package_name}" =~ ^solana- ]]; then
# shellcheck disable=SC2016 # backticks are not a command here
echo 'error: PACKAGE_NAME MUST start with `solana-`' 1>&2
display_help
exit 1
fi
tmpdir="$(mktemp -d)"
if pushd "${tmpdir}" &>/dev/null; then
cat <<EOF > "Cargo.toml"
[package]
name = "${package_name}"
version = "0.0.0"
description = "reserved for future use"
authors = ["Solana Labs Maintainers <maintainers@solanalabs.com>"]
repository = "https://github.com/solana-labs/solana"
license = "Apache-2.0"
homepage = "https://solanalabs.com"
documentation = "https://docs.rs/${package_name}"
edition = "2021"
EOF
mkdir -p src
echo "${src_file_body}" > "src/${src_filename}"
# shellcheck disable=SC2086 # do not want to quote optional arg tokens
cargo publish ${maybe_publish} ${maybe_crates_token}
popd &>/dev/null || true
fi
rm -rf "${tmpdir}"