]> git.pld-linux.org Git - packages/rpm-build-tools.git/blame - rust-crates.sh
use cargo-vendor-filterer if available
[packages/rpm-build-tools.git] / rust-crates.sh
CommitLineData
9474615a
JP
1#!/bin/sh
2
3usage() {
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"
be864684 8 echo "\t-d <src subdirectory>\tsubdirectory within source directory where rust sources are located"
9474615a
JP
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
16for 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
20done
21
22if [ -n "$not_installed" ]; then
23 echo "ERROR: required commands not found: $not_installed" >&2
24 exit 1
25fi
26
be864684 27while getopts :p:o:d:fvh OPTNAME; do
9474615a
JP
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 ;;
be864684
JP
38 d)
39 subdir="$OPTARG"
40 ;;
9474615a
JP
41 v)
42 version_override=1
43 ;;
44 h)
45 usage
46 exit 0
47 ;;
48 ?)
20089c92 49 echo "ERROR: unknown option '-$OPTARG'" >&2
9474615a
JP
50 usage
51 exit 1
52 ;;
53 esac
54done
55
56shift $(($OPTIND - 1))
57
58rpm_topdir=$(rpm -E %{_topdir})
59
60if [ -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
68else
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
75fi
76
77spec_dump=$(rpm-specdump "$pkg_dir/$pkg_name.spec")
78pkg_version=$(echo "$spec_dump" | grep PACKAGE_VERSION | cut -f3 -d' ')
79pkg_src=$(basename $(echo "$spec_dump" | grep SOURCEURL0 | cut -f3- -d' '))
80if [ -z "$crates_file" ]; then
81 crates_file="$pkg_name-crates-$pkg_version.tar.xz"
82fi
83
84if [ -e "$pkg_dir/$crates_file" ] && [ -z "$overwrite" ]; then
85 echo "ERROR: crates file $crates_file already exists" >&2
86 exit 1
87fi
88
89if [ ! -f "$pkg_dir/$pkg_src" ]; then
90 echo "ERROR: source file $pkg_src not found" >&2
91 exit 1
92fi
93
94tmpdir=$(mktemp -d)
95
96rm_tmpdir() {
97 if [ -n "$tmpdir" -a -d "$tmpdir" ]; then
98 rm -rf "$tmpdir"
99 fi
100}
101
102trap rm_tmpdir EXIT INT HUP
103
104cd "$tmpdir"
105bsdtar xf "$pkg_dir/$pkg_src"
106src_dir=$(ls)
107if [ $(echo "$src_dir" | wc -l) -ne 1 ]; then
108 echo "ERROR: unexpected source structure:\n$src_dir" >&2
109 exit 1
110fi
111
be864684 112cd "$src_dir${subdir:+/$subdir}"
3b5666c2
JP
113if command -v cargo-vendor-filterer > /dev/null 2> /dev/null; then
114 cargo vendor-filterer --platform='*-unknown-linux-*' --tier=2
115else
116 cargo vendor
117fi
9474615a
JP
118if [ $? -ne 0 ]; then
119 echo "ERROR: cargo vendor failed" >&2
120 exit 1
121fi
122
123if [ -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
137fi
138
be864684
JP
139cd "$tmpdir"
140tar cJf "$pkg_dir/$crates_file" "$src_dir${subdir:+/$subdir}"/{Cargo.lock,vendor}
9474615a
JP
141echo "Created $pkg_dir/$crates_file"
142
143# vim: expandtab shiftwidth=2 tabstop=2
This page took 0.066485 seconds and 4 git commands to generate.