]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - rust-crates.sh
use cargo-vendor-filterer if available
[packages/rpm-build-tools.git] / rust-crates.sh
1 #!/bin/sh
2
3 usage() {
4   echo "Usage: $0 [-p <package_name>] [-o <crates_file>] [-f] [-v] [-h] [<pkg_name>.spec]"
5   echo
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-d <src subdirectory>\tsubdirectory within source directory where rust sources are located"
9   echo "\t-f\t\t\toverwrite creates file if it already exists"
10   echo "\t-v\t\t\tset cargo package version to @@VERSION@@ for easier crates tarball reuse"
11   echo "\t-h\t\t\tprint this help"
12   echo
13   echo "\t<pkg_name>.spec is optional and defaults to package found in current working directory"
14 }
15
16 for cmd in bsdtar rpm-specdump cargo perl awk; do
17   if ! command -v $cmd > /dev/null 2> /dev/null; then
18     not_installed="$not_installed$cmd "
19   fi
20 done
21
22 if [ -n "$not_installed" ]; then
23   echo "ERROR: required commands not found: $not_installed" >&2
24   exit 1
25 fi
26
27 while getopts :p:o:d:fvh OPTNAME; do
28   case $OPTNAME in
29     p)
30       force_cargo_package="$OPTARG"
31       ;;
32     f)
33       overwrite=1
34       ;;
35     o)
36       crates_file="$OPTARG"
37       ;;
38     d)
39       subdir="$OPTARG"
40       ;;
41     v)
42       version_override=1
43       ;;
44     h)
45       usage
46       exit 0
47       ;;
48     ?)
49       echo "ERROR: unknown option '-$OPTARG'" >&2
50       usage
51       exit 1
52       ;;
53   esac
54 done
55
56 shift $(($OPTIND - 1))
57
58 rpm_topdir=$(rpm -E %{_topdir})
59
60 if [ -n "$1" ]; then
61   pkg_name=$(basename "$1")
62   pkg_name=${pkg_name%.spec}
63   pkg_dir="$rpm_topdir/$pkg_name"
64   if [ ! -f "$pkg_dir/$pkg_name.spec" ]; then
65     echo "ERROR: no package $pkg_name found" >&2
66     exit 1
67   fi
68 else
69   pkg_dir="$(pwd)"
70   pkg_name=$(basename "$pkg_dir")
71   if [ "$(readlink -f "$rpm_topdir")" != "$(readlink -f "$(dirname "$pkg_dir")")" ] || [ ! -f "$pkg_dir/$pkg_name.spec" ]; then
72     echo "ERROR: failed to determine package name" >&2
73     exit 1
74   fi
75 fi
76
77 spec_dump=$(rpm-specdump "$pkg_dir/$pkg_name.spec")
78 pkg_version=$(echo "$spec_dump" | grep PACKAGE_VERSION | cut -f3 -d' ')
79 pkg_src=$(basename $(echo "$spec_dump" | grep SOURCEURL0 | cut -f3- -d' '))
80 if [ -z "$crates_file" ]; then
81   crates_file="$pkg_name-crates-$pkg_version.tar.xz"
82 fi
83
84 if [ -e "$pkg_dir/$crates_file" ] && [ -z "$overwrite" ]; then
85   echo "ERROR: crates file $crates_file already exists" >&2
86   exit 1
87 fi
88
89 if [ ! -f "$pkg_dir/$pkg_src" ]; then
90   echo "ERROR: source file $pkg_src not found" >&2
91   exit 1
92 fi
93
94 tmpdir=$(mktemp -d)
95
96 rm_tmpdir() {
97   if [ -n "$tmpdir" -a -d "$tmpdir" ]; then
98     rm -rf "$tmpdir"
99   fi
100 }
101
102 trap rm_tmpdir EXIT INT HUP
103
104 cd "$tmpdir"
105 bsdtar xf "$pkg_dir/$pkg_src"
106 src_dir=$(ls)
107 if [ $(echo "$src_dir" | wc -l) -ne 1 ]; then
108   echo "ERROR: unexpected source structure:\n$src_dir" >&2
109   exit 1
110 fi
111
112 cd "$src_dir${subdir:+/$subdir}"
113 if command -v cargo-vendor-filterer > /dev/null 2> /dev/null; then
114   cargo vendor-filterer --platform='*-unknown-linux-*' --tier=2
115 else
116   cargo vendor
117 fi
118 if [ $? -ne 0 ]; then
119   echo "ERROR: cargo vendor failed" >&2
120   exit 1
121 fi
122
123 if [ -n "$version_override" ]; then
124   if [ -n "$force_cargo_package" ]; then
125     cargo_package=$force_cargo_package
126   else
127     cargo_package=$(awk '/^[[:space:]]*\[package\]/ { in_package=1; } /^[[:space:]]*name[[:space:]]*=/ { if (in_package) { gsub("^[[:space:]]*name[[:space:]]*=[[:space:]]*","");gsub("(^\"|\"$)","");print; exit; } }' Cargo.toml)
128   fi
129
130   if [ -z "$cargo_package" ]; then
131     echo "ERROR: failed to determine cargo package name" >&2
132     exit 1
133   fi
134
135   # replace cargo package version with @@VERSION@@
136   perl -pi -e 'BEGIN { undef $/;} s/(\[\[package\]\]\nname\s*=\s*"'"$cargo_package"'"\nversion\s*=\s*")[^"]+/$1\@\@VERSION\@\@/m' Cargo.lock
137 fi
138
139 cd "$tmpdir"
140 tar cJf "$pkg_dir/$crates_file" "$src_dir${subdir:+/$subdir}"/{Cargo.lock,vendor}
141 echo "Created $pkg_dir/$crates_file"
142
143 # vim: expandtab shiftwidth=2 tabstop=2
This page took 0.059368 seconds and 3 git commands to generate.