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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//! [](https://crates.io/crates/build-data-writer)
//! [](https://gitlab.com/leonhard-llc/ops/-/raw/main/build-data-writer/LICENSE)
//! [](https://github.com/rust-secure-code/safety-dance/)
//! [](https://gitlab.com/leonhard-llc/ops/-/pipelines)
//!
//! # build-data-writer
//!
//! Functions to to write `build-data.txt` from your
//! [`build.rs`](https://doc.rust-lang.org/cargo/reference/build-scripts.html).
//! Read the file with the
//! [`build-data`](https://crates.io/crates/build-data) crate.
//!
//! ## Features
//! - Saves build-time data:
//! - Timestamp
//! - Date-time string
//! - Hostname
//! - git commit, branch, and dirtiness
//! - rustc version
//! - `forbid(unsafe_code)`
//! - 100% test coverage
//!
//! ## Alternatives
//! - [`build-info`](https://crates.io/crates/build-info)
//! - Mature & popular
//! - Confusing API
//! - Uses procedural macros
//!
//! ## Example
//! See [`build-data`](https://crates.io/crates/build-data) crate docs.
//!
//! ## Cargo Geiger Safety Report
//!
//! ## Changelog
//! - v0.1.2 - Support older versions of git when getting branch name.
//! - v0.1.1 - Initial version
//!
//! ## Happy Contributors 🙂
//! Fixing bugs and adding features is easy and fast.
//! Send us a pull request and we intend to:
//! - Always respond within 24 hours
//! - Provide clear & concrete feedback
//! - Immediately make a new release for your accepted change
// https://doc.rust-lang.org/cargo/reference/build-scripts.html
// https://doc.rust-lang.org/cargo/reference/build-script-examples.html
use TimeZone;
use TryFrom;
use OsStr;
/// Executes `cmd` with `args` as parameters, waits for it to exit, and
/// returns its stdout, trimmed, and escaped with
/// [`escape_ascii`](#method.escape_ascii).
///
/// # Errors
/// Returns a descriptive error string if it fails to execute the command
/// or if the command exits with a non-zero status.
///
/// # Panics
/// Panics if the process writes non-UTF bytes to stdout.
/// Converts a byte slice into a string using
/// [`core::ascii::escape_default`](https://doc.rust-lang.org/core/ascii/fn.escape_default.html)
/// to escape each byte.
///
/// # Example
/// ```
/// use build_data_writer::escape_ascii;
/// assert_eq!("abc", escape_ascii(b"abc"));
/// assert_eq!("abc\\n", escape_ascii(b"abc\n"));
/// assert_eq!(
/// "Euro sign: \\xe2\\x82\\xac",
/// escape_ascii("Euro sign: \u{20AC}".as_bytes())
/// );
/// assert_eq!("\\x01\\x02\\x03", escape_ascii(&[1, 2, 3]));
/// ```
/// Gets the number of seconds since 1970-01-01T00:00:00Z.
/// Formats the time as an ISO-8601 or RFC-3339 string.
/// Example: `2021-04-14T06:25:59+00:00`
/// Gets the name of the current machine.
///
/// # Errors
/// Returns an error if it fails to execute the `hostname` command.
/// Gets the latest git commit of the source code directory.
///
/// Example: `"a5547bfb1edb9712588f0f85d3e2c8ba618ac51f"`
///
/// # Errors
/// Returns an error if it fails to execute the `git` command.
/// Gets the current branch of the source code directory.
///
/// Example: `"release"`
///
/// # Errors
/// Returns an error if it fails to execute the `git` command.
/// Returns `true` if the source directory contains uncommitted changes.
///
/// # Errors
/// Returns an error if it fails to execute the `git` command.
/// Gets the version of the Rust compiler used to build the build script.
///
/// Example: `"rustc 1.53.0-nightly (07e0e2ec2 2021-03-24)"`
///
/// # Errors
/// Returns an error if it fails to execute the `rustc` command.
/// Writes `build-data.txt`.
///
/// Uses `"unknown"` when it fails to get a particular value.
///
/// # Example File
/// ```text
/// GIT_BRANCH:release
/// GIT_COMMIT:a5547bfb1edb9712588f0f85d3e2c8ba618ac51f
/// GIT_DIRTY:false
/// HOSTNAME:builder2
/// RUSTC_VERSION:rustc 1.53.0-nightly (07e0e2ec2 2021-03-24)
/// TIME:2021-04-14T06:25:59+00:00
/// TIME_SECONDS:1618381559
/// ```
///
/// # Example
/// Add a [`build.rs`](https://doc.rust-lang.org/cargo/reference/build-scripts.html)
/// file next to your `Cargo.toml`:
/// ```
/// // build.rs
/// use std::env;
/// use std::path::Path;
///
/// fn main() {
/// # }
/// # fn f() {
/// build_data_writer::write(
/// &Path::new(&env::var_os("OUT_DIR").unwrap()).join("build-data.txt")
/// ).unwrap();
/// }
/// ```
///
/// Include the file in your program and parse it.
/// See [`include_str!`](https://doc.rust-lang.org/core/macro.include_str.html),
/// [`concat!`](https://doc.rust-lang.org/core/macro.concat.html),
/// [`env!`](https://doc.rust-lang.org/core/macro.env.html), and
/// [`build_data::BuildData::new`](https://docs.rs/build-data/latest/build_data/struct.BuildData.html#method.new).
/// ```
/// # macro_rules! include_str (
/// # ($arg:expr) => { "GIT_BRANCH:1\nGIT_COMMIT:abcdef123\nGIT_DIRTY:false\nHOSTNAME:h\nRUSTC_VERSION:2\nTIME:123\nTIME_SECONDS:123" };
/// # );
/// # macro_rules! concat (
/// # ($arg:expr) => { "" };
/// # );
/// # macro_rules! env (
/// # ($arg:expr) => { "" };
/// # );
/// fn main() {
/// let bd = build_data::BuildData::new(
/// include_str!(concat!(env!("OUT_DIR"), "/build-data.txt"))
/// ).unwrap();
/// // ...
/// }
/// ```
///
/// # Errors
/// Returns an error if it fails to write the file.
/// Tells cargo not to rebuild `build.rs` during debug builds when other files
/// change.
/// This speeds up development builds.