4 echo "Usage: $0 [-p <package_name>] [-o <crates_file>] [-f] [-v] [-h] [<pkg_name>.spec]"
6 echo "\t-p <package_name>\tforce cargo <package_name> for version override instead of automatic detection"
7 echo "\t-o <crates_file>\tforce output file name instead of automatically determined name"
8 echo "\t-f\t\t\toverwrite creates file if it already exists"
9 echo "\t-v\t\t\tset cargo package version to @@VERSION@@ for easier crates tarball reuse"
10 echo "\t-h\t\t\tprint this help"
12 echo "\t<pkg_name>.spec is optional and defaults to package found in current working directory"
15 for cmd in bsdtar rpm-specdump cargo perl awk; do
16 if ! command -v $cmd > /dev/null 2> /dev/null; then
17 not_installed="$not_installed$cmd "
21 if [ -n "$not_installed" ]; then
22 echo "ERROR: required commands not found: $not_installed" >&2
26 while getopts p:o:fvh OPTNAME; do
29 force_cargo_package="$OPTARG"
45 echo "ERROR: unknown option '-$OPTNAME'" >&2
52 shift $(($OPTIND - 1))
54 rpm_topdir=$(rpm -E %{_topdir})
57 pkg_name=$(basename "$1")
58 pkg_name=${pkg_name%.spec}
59 pkg_dir="$rpm_topdir/$pkg_name"
60 if [ ! -f "$pkg_dir/$pkg_name.spec" ]; then
61 echo "ERROR: no package $pkg_name found" >&2
66 pkg_name=$(basename "$pkg_dir")
67 if [ "$(readlink -f "$rpm_topdir")" != "$(readlink -f "$(dirname "$pkg_dir")")" ] || [ ! -f "$pkg_dir/$pkg_name.spec" ]; then
68 echo "ERROR: failed to determine package name" >&2
73 spec_dump=$(rpm-specdump "$pkg_dir/$pkg_name.spec")
74 pkg_version=$(echo "$spec_dump" | grep PACKAGE_VERSION | cut -f3 -d' ')
75 pkg_src=$(basename $(echo "$spec_dump" | grep SOURCEURL0 | cut -f3- -d' '))
76 if [ -z "$crates_file" ]; then
77 crates_file="$pkg_name-crates-$pkg_version.tar.xz"
80 if [ -e "$pkg_dir/$crates_file" ] && [ -z "$overwrite" ]; then
81 echo "ERROR: crates file $crates_file already exists" >&2
85 if [ ! -f "$pkg_dir/$pkg_src" ]; then
86 echo "ERROR: source file $pkg_src not found" >&2
93 if [ -n "$tmpdir" -a -d "$tmpdir" ]; then
98 trap rm_tmpdir EXIT INT HUP
101 bsdtar xf "$pkg_dir/$pkg_src"
103 if [ $(echo "$src_dir" | wc -l) -ne 1 ]; then
104 echo "ERROR: unexpected source structure:\n$src_dir" >&2
110 if [ $? -ne 0 ]; then
111 echo "ERROR: cargo vendor failed" >&2
115 if [ -n "$version_override" ]; then
116 if [ -n "$force_cargo_package" ]; then
117 cargo_package=$force_cargo_package
119 cargo_package=$(awk '/^[[:space:]]*\[package\]/ { in_package=1; } /^[[:space:]]*name[[:space:]]*=/ { if (in_package) { gsub("^[[:space:]]*name[[:space:]]*=[[:space:]]*","");gsub("(^\"|\"$)","");print; exit; } }' Cargo.toml)
122 if [ -z "$cargo_package" ]; then
123 echo "ERROR: failed to determine cargo package name" >&2
127 # replace cargo package version with @@VERSION@@
128 perl -pi -e 'BEGIN { undef $/;} s/(\[\[package\]\]\nname\s*=\s*"'"$cargo_package"'"\nversion\s*=\s*")[^"]+/$1\@\@VERSION\@\@/m' Cargo.lock
132 tar cJf "$pkg_dir/$crates_file" "$src_dir"/{Cargo.lock,vendor}
133 echo "Created $pkg_dir/$crates_file"
135 # vim: expandtab shiftwidth=2 tabstop=2