forked from pantsbuild/scie-pants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
301 lines (275 loc) · 8.89 KB
/
main.rs
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
297
298
299
300
301
// Copyright 2022 Pants project contributors.
// Licensed under the Apache License, Version 2.0 (see LICENSE).
mod scie_pants;
#[macro_use]
mod test;
mod tools_pex;
#[macro_use]
mod utils;
use std::fmt::{Display, Formatter};
use std::io::Write;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use anyhow::{bail, Context, Result};
use clap::{arg, command, Parser, Subcommand};
use termcolor::{Color, WriteColor};
use crate::scie_pants::build_scie_pants_scie;
use crate::test::run_integration_tests;
use crate::tools_pex::build_tools_pex;
use crate::utils::build::{check_sha256, fetch_skinny_scie_tools, fingerprint, BuildContext};
use crate::utils::fs::{canonicalize, copy, ensure_directory};
const BINARY: &str = "scie-pants";
const PTEX_TAG: &str = "v0.7.0";
const SCIE_JUMP_TAG: &str = "v0.11.0";
#[derive(Clone)]
struct SpecifiedPath(PathBuf);
impl SpecifiedPath {
fn new(path: &str) -> Self {
Self::from(path.to_string())
}
}
impl From<String> for SpecifiedPath {
fn from(path: String) -> Self {
SpecifiedPath(PathBuf::from(path))
}
}
impl Deref for SpecifiedPath {
type Target = PathBuf;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl AsRef<Path> for SpecifiedPath {
fn as_ref(&self) -> &Path {
self.0.as_path()
}
}
impl Display for SpecifiedPath {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0.display())
}
}
#[derive(Subcommand)]
enum Commands {
/// Builds the `tools.pex` used by the scie-pants scie to perform Pants installs.
Tools,
/// Builds the `scie-pants` scie.
Scie {
#[arg(
long,
help = "The location of the pre-built tools.pex to use. By default, the tools.pex is \
built fresh."
)]
tools_pex: Option<PathBuf>,
},
/// Builds the `scie-pants` scie and runs it through a series of integration tests.
Test {
#[arg(
long,
help = "The location of the pre-built tools.pex to use. By default, the tools.pex is \
built fresh."
)]
tools_pex: Option<PathBuf>,
#[arg(
long,
help = "The location of the pre-built scie-pants scie to use. By default, the \
scie-pants scie is built fresh."
)]
scie_pants: Option<PathBuf>,
#[arg(
long,
help = "Only check formatting and lints and fail the tests if these checks fail \
instead of re-formatting.",
default_value_t = false
)]
check: bool,
#[arg(
long,
help = "Only warn if the Pants built tools.pex doesn't match ours instead of failing \
the tests.",
default_value_t = false
)]
tools_pex_mismatch_warn: bool,
},
}
#[derive(Parser)]
#[command(about, version)]
struct Args {
#[arg(long, help = "Override the default --target for this platform.")]
target: Option<String>,
#[arg(
long,
help = format!(
"Instead of using the released {PTEX_TAG} ptex, package ptex from the ptex project \
repo at this directory."
)
)]
ptex: Option<PathBuf>,
#[arg(
long,
help = format!(
"Instead of using the released {SCIE_JUMP_TAG} scie-jump, package the scie-jump from \
the scie-jump project repo at this directory."
)
)]
scie_jump: Option<PathBuf>,
#[arg(
long,
help = "Refresh the tools lock before building the tools.pex",
default_value_t = false
)]
update_lock: bool,
#[arg(
long,
help = "The destination directory for the chosen binary and its checksum file.",
default_value_t = SpecifiedPath::new("dist")
)]
dest_dir: SpecifiedPath,
#[command(subcommand)]
command: Commands,
}
fn maybe_build(args: &Args, build_context: &BuildContext) -> Result<Option<PathBuf>> {
match &args.command {
Commands::Test {
tools_pex: Some(tools_pex),
scie_pants: Some(scie_pants),
check,
tools_pex_mismatch_warn,
} => {
run_integration_tests(
&build_context.workspace_root,
&canonicalize(tools_pex)?,
&canonicalize(scie_pants)?,
*check,
*tools_pex_mismatch_warn,
)?;
Ok(None)
}
Commands::Test {
tools_pex: None,
scie_pants: Some(scie_pants),
check,
tools_pex_mismatch_warn,
} => {
let skinny_scie_tools = fetch_skinny_scie_tools(build_context)?;
let tools_pex = build_tools_pex(build_context, &skinny_scie_tools, args.update_lock)?;
run_integration_tests(
&build_context.workspace_root,
&tools_pex,
&canonicalize(scie_pants)?,
*check,
*tools_pex_mismatch_warn,
)?;
Ok(None)
}
Commands::Test {
tools_pex: Some(tools_pex),
scie_pants: None,
check,
tools_pex_mismatch_warn,
} => {
let skinny_scie_tools = fetch_skinny_scie_tools(build_context)?;
let scie_pants = build_scie_pants_scie(build_context, &skinny_scie_tools, tools_pex)?;
run_integration_tests(
&build_context.workspace_root,
&canonicalize(tools_pex)?,
&scie_pants,
*check,
*tools_pex_mismatch_warn,
)?;
Ok(Some(scie_pants))
}
Commands::Test {
tools_pex: None,
scie_pants: None,
check,
tools_pex_mismatch_warn,
} => {
let skinny_scie_tools = fetch_skinny_scie_tools(build_context)?;
let tools_pex = build_tools_pex(build_context, &skinny_scie_tools, args.update_lock)?;
let scie_pants = build_scie_pants_scie(build_context, &skinny_scie_tools, &tools_pex)?;
run_integration_tests(
&build_context.workspace_root,
&tools_pex,
&scie_pants,
*check,
*tools_pex_mismatch_warn,
)?;
Ok(Some(scie_pants))
}
Commands::Scie { tools_pex: None } => {
let skinny_scie_tools = fetch_skinny_scie_tools(build_context)?;
let tools_pex = build_tools_pex(build_context, &skinny_scie_tools, args.update_lock)?;
Ok(Some(build_scie_pants_scie(
build_context,
&skinny_scie_tools,
&tools_pex,
)?))
}
Commands::Scie {
tools_pex: Some(tools_pex),
} => {
let skinny_scie_tools = fetch_skinny_scie_tools(build_context)?;
Ok(Some(build_scie_pants_scie(
build_context,
&skinny_scie_tools,
tools_pex,
)?))
}
Commands::Tools => {
let skinny_scie_tools = fetch_skinny_scie_tools(build_context)?;
Ok(Some(build_tools_pex(
build_context,
&skinny_scie_tools,
args.update_lock,
)?))
}
}
}
fn main() -> Result<()> {
pretty_env_logger::init();
let args = Args::parse();
let dest_dir = &args.dest_dir;
if dest_dir.is_file() {
bail!(
"The specified dest_dir of {dest_dir} is a file. Not overwriting",
dest_dir = dest_dir.display()
);
}
let build_context = BuildContext::new(
args.target.as_deref(),
args.ptex.as_deref(),
args.scie_jump.as_deref(),
)?;
if let Some(output_file) = maybe_build(&args, &build_context)? {
let dest_file_name = output_file
.file_name()
.with_context(|| format!("Failed to determine the basename of {output_file:?}"))?
.to_str()
.with_context(|| {
format!("Failed to interpret the basename of {output_file:?} as a UTF-8 string")
})?;
let dest_file = dest_dir.join(dest_file_name);
ensure_directory(dest_dir, false)?;
copy(&output_file, &dest_file)?;
let fingerprint_file = dest_file.with_file_name(format!("{dest_file_name}.sha256"));
let dest_file_digest = fingerprint(&dest_file)?;
std::fs::write(
&fingerprint_file,
format!("{dest_file_digest} *{dest_file_name}\n"),
)
.with_context(|| {
format!(
"Failed to write fingerprint file {fingerprint_file}",
fingerprint_file = fingerprint_file.display()
)
})?;
check_sha256(&dest_file)?;
log!(
Color::Yellow,
"Wrote {dest_file_name} to {dest_file}",
dest_file = dest_file.display()
);
}
Ok(())
}