chiark / gitweb /
source Cargo.nail-env to allow setting RUSTFLAGS etc.
[nailing-cargo.git] / nailing-cargo
1 #!/bin/bash
2
3 #    nailing-cargo: wrapper to use unpublished local crates
4 #
5 #    Copyright (C) 2019 Ian Jackson
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 set -e
21
22 # example usages:
23 #   ../nailing-cargo/nailing-cargo make
24 #   ../nailing-cargo/nailing-cargo cargo build
25 #   CARGO='../nailing-cargo/nailing-cargo cargo' make
26
27
28 # Why do we need this ?
29 #
30 #  https://github.com/rust-lang/cargo/issues/6713
31 #  https://stackoverflow.com/questions/33025887/how-to-use-a-local-unpublished-crate
32 #  https://github.com/rust-lang/cargo/issues/1481
33
34
35 lock=${PWD%/*}/.nail.lock
36 if [ "x$NAILING_CARGO" != "x$lock" ]; then
37         NAILING_CARGO=$lock \
38         exec with-lock-ex -w "$lock" "$0" "$@"
39 fi
40
41 exec 203<../Cargo.nail
42 f=Cargo.toml
43
44 sed='
45 /^ *\[dependencies\]/,/^ \[/{
46 '
47
48 if test -e ../Cargo.nail-env; then
49         . ../Cargo.nail-env
50 fi
51
52 exec 204<../Cargo.nail
53 while read <&204 what where; do
54         if [ "x$what" = x- ]; then continue; fi
55         if [ "x$what" = 'x#' ]; then continue; fi
56         qwhere="${where//\//\\/}"
57         sed+='
58                 s/^'$what' *= *\(\".*\"\) *$/'$what' = { version = \1 }/;
59                 s/^'$what' *= *{/'$what' = { path = "..\/'"${qwhere}"'", /;
60                 /^'$what' *=/ s/version *= *\"[^"]*\"//;
61                 /^'$what' *=/ s/, *\([,}]\)/\1/;
62         '
63 done
64 sed+='}
65 '
66
67 exec 204<../Cargo.nail
68 while read <&204 what where; do
69         if [ "x$what" = 'x#' ]; then continue; fi
70         wf=../$where/$f
71         rm -f $wf.nailing~
72         sed <$wf >$wf.nailing~ "$sed"
73 done
74
75 exec 204<../Cargo.nail
76 while read <&204 what where; do
77         if [ "x$what" = 'x#' ]; then continue; fi
78         wf=../$where/$f
79         if ! test -e $wf.unnailed~; then
80                 ln $wf $wf.unnailed~
81         fi
82 done
83
84 trap '
85         set +e
86         while read <&203 what where; do
87                 if [ "x$what" = "x#" ]; then continue; fi
88                 wf=../$where/$f
89                 if test -e $wf.unnailed~; then
90                         rm -f $wf.nailed~
91                         ln $wf $wf.nailed~
92                         mv -f $wf.unnailed~ $wf
93                 fi
94         done
95         echo >&2 'Unnailed'
96 ' EXIT
97
98 exec 204<../Cargo.nail
99 printf >&2 'Nailing'
100 while read <&204 what where; do
101         if [ "x$what" = 'x#' ]; then continue; fi
102         wf=../$where/$f
103         printf >&2 ' %s' "$what"
104         if cmp -s $wf.nailed~ $wf.nailing~; then
105                 mv -f $wf.nailed~ $wf
106                 rm -f $wf.nailing
107         else
108                 mv -f $wf.nailing~ $wf
109                 rm -f $wf.nailed
110         fi
111 done
112 echo >&2
113
114 "$@"