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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
//! Subcommands for building.
//!
//! Currently has the following functions:
//!
//! - [`build`]: Builds general cargo projects (i.e. zellij components) with `cargo build`
use crate::{flags, metadata, WorkspaceMember};
use anyhow::Context;
use std::path::{Path, PathBuf};
use xshell::{cmd, Shell};
/// Build members of the zellij workspace.
///
/// Build behavior is controlled by the [`flags`](flags::Build). Calls some variation of `cargo
/// build` under the hood.
pub fn build(sh: &Shell, flags: flags::Build) -> anyhow::Result<()> {
let _pd = sh.push_dir(crate::project_root());
let cargo = crate::cargo()?;
if flags.no_plugins && flags.plugins_only {
eprintln!("Cannot use both '--no-plugins' and '--plugins-only'");
std::process::exit(1);
}
// zellij-utils requires protobuf definition files to be present. Usually these are
// auto-generated with `build.rs`-files, but this is currently broken for us.
// See [this PR][1] for details.
//
// [1]: https://github.com/zellij-org/zellij/pull/2711#issuecomment-1695015818
run_proto_codegen(sh, false);
// Build all plugins in a single invocation so Cargo can unify transitive dependency
// features across all of them and compile shared crates (e.g. zellij-utils) only once.
let build_plugins =
!flags.no_plugins && (flags.release || plugins_force() || plugin_sources_changed());
if build_plugins {
let plugin_members: Vec<&WorkspaceMember> = crate::workspace_members()
.iter()
.filter(|m| m.build && m.crate_name.contains("plugins"))
.collect();
if !plugin_members.is_empty() {
eprintln!();
let msg = ">> Building plugins";
crate::status(msg);
eprintln!("{}", msg);
if flags.release {
build_plugins_release_into_assets(sh, &plugin_members)?;
} else {
let mut base_cmd = cmd!(sh, "{cargo} build --target wasm32-wasip1");
for member in &plugin_members {
base_cmd = base_cmd.args(["-p", plugin_name_of(member)?]);
}
base_cmd.run().context("failed to build plugins")?;
write_plugin_stamp(sh);
}
}
} else if !flags.no_plugins {
let msg = ">> Plugins unchanged since last build, skipping (set ZELLIJ_FORCE_PLUGINS=1 to rebuild)";
crate::status(msg);
eprintln!("{}", msg);
}
// Build non-plugin crates (native target).
if !flags.plugins_only {
for WorkspaceMember { crate_name, .. } in crate::workspace_members()
.iter()
.filter(|member| member.build && !member.crate_name.contains("plugins"))
{
let err_context = || format!("failed to build '{crate_name}'");
let _pd = sh.push_dir(Path::new(crate_name));
println!();
let msg = format!(">> Building '{crate_name}'");
crate::status(&msg);
println!("{}", msg);
let mut base_cmd = cmd!(sh, "{cargo} build");
if flags.release {
base_cmd = base_cmd.arg("--release");
} else {
base_cmd = base_cmd.args(["--profile", "dev-opt"]);
}
if flags.no_web {
// Check if this crate has web features that need modification
match metadata::get_no_web_features(sh, crate_name)
.context("Failed to check web features")?
{
Some(features) => {
base_cmd = base_cmd.arg("--no-default-features");
if !features.is_empty() {
base_cmd = base_cmd.arg("--features");
base_cmd = base_cmd.arg(features);
}
},
None => {
// Crate doesn't have web features, build normally
},
}
}
base_cmd = base_cmd.args(&flags.args);
base_cmd.run().with_context(err_context)?;
}
}
Ok(())
}
fn plugins_force() -> bool {
std::env::var_os("ZELLIJ_FORCE_PLUGINS").is_some()
}
fn plugin_asset_path(plugin_name: &str) -> PathBuf {
crate::asset_dir()
.join("plugins")
.join(plugin_name)
.with_extension("wasm")
}
fn newest_plugin_source_time() -> Option<std::time::SystemTime> {
let root = crate::project_root();
["default-plugins", "zellij-tile", "zellij-tile-utils"]
.iter()
.filter_map(|dir| newest_file_time(&root.join(dir)))
.max()
}
fn newest_file_time(dir: &Path) -> Option<std::time::SystemTime> {
let entries = std::fs::read_dir(dir).ok()?;
let mut newest: Option<std::time::SystemTime> = None;
for entry in entries.flatten() {
let path = entry.path();
let file_type = match entry.file_type() {
Ok(file_type) => file_type,
Err(_) => continue,
};
if file_type.is_dir() {
if path
.file_name()
.map(|name| name == "target")
.unwrap_or(false)
{
continue;
}
if let Some(child) = newest_file_time(&path) {
newest = Some(newest.map_or(child, |current| current.max(child)));
}
} else if let Ok(modified) = entry.metadata().and_then(|m| m.modified()) {
newest = Some(newest.map_or(modified, |current| current.max(modified)));
}
}
newest
}
pub fn ensure_plugin_assets(sh: &Shell) -> anyhow::Result<()> {
let plugin_members: Vec<&WorkspaceMember> = crate::workspace_members()
.iter()
.filter(|m| m.build && m.crate_name.contains("plugins"))
.collect();
if plugin_members.is_empty() {
return Ok(());
}
let newest_source = newest_plugin_source_time();
let stale = plugins_force()
|| plugin_members.iter().any(|member| {
let plugin_name = match member.crate_name.rsplit_once('/') {
Some((_, name)) => name,
None => return true,
};
let asset_time = std::fs::metadata(plugin_asset_path(plugin_name))
.and_then(|m| m.modified())
.ok();
match (asset_time, newest_source) {
(Some(asset_time), Some(newest_source)) => asset_time < newest_source,
_ => true,
}
});
if !stale {
let msg = ">> Plugin assets up to date, skipping plugin build";
crate::status(msg);
eprintln!("{}", msg);
return Ok(());
}
let msg = ">> Building plugin assets (release)";
crate::status(msg);
eprintln!("{}", msg);
build_plugins_release_into_assets(sh, &plugin_members)
}
fn build_plugins_release_into_assets(
sh: &Shell,
plugin_members: &[&WorkspaceMember],
) -> anyhow::Result<()> {
let cargo = crate::cargo()?;
let mut base_cmd = cmd!(sh, "{cargo} build --target wasm32-wasip1 --release");
for member in plugin_members {
let plugin_name = plugin_name_of(member)?;
base_cmd = base_cmd.args(["-p", plugin_name]);
}
base_cmd.run().context("failed to build plugin assets")?;
for member in plugin_members {
move_plugin_to_assets(sh, plugin_name_of(member)?)?;
}
Ok(())
}
fn plugin_name_of(member: &WorkspaceMember) -> anyhow::Result<&'static str> {
Ok(member
.crate_name
.rsplit_once('/')
.context("Cannot determine plugin name from crate path")?
.1)
}
fn plugin_stamp_path() -> PathBuf {
crate::target_dir().join(".xtask-plugins-stamp")
}
fn write_plugin_stamp(sh: &Shell) {
let _ = sh.write_file(plugin_stamp_path(), b"");
}
fn plugin_sources_changed() -> bool {
let stamp_time = match std::fs::metadata(plugin_stamp_path()).and_then(|m| m.modified()) {
Ok(stamp_time) => stamp_time,
Err(_) => return true,
};
match newest_plugin_source_time() {
Some(newest_source) => newest_source > stamp_time,
None => true,
}
}
pub fn proto(sh: &Shell) -> anyhow::Result<()> {
let msg = ">> Generating protobuffer code";
crate::status(msg);
println!("{}", msg);
run_proto_codegen(sh, true);
Ok(())
}
fn run_proto_codegen(sh: &Shell, force: bool) {
let zellij_utils_basedir = crate::project_root().join("zellij-utils");
let _pd = sh.push_dir(&zellij_utils_basedir);
let specs: &[(&str, &str, &str)] = &[
("assets/prost", "src/plugin_api", "generated_plugin_api.rs"),
(
"assets/prost_ipc",
"src/client_server_contract",
"generated_client_server_api.rs",
),
(
"assets/prost_web_server",
"src/web_server_contract",
"generated_web_server_api.rs",
),
(
"assets/prost_nested_session",
"src/nested_session_contract",
"generated_nested_session_api.rs",
),
];
for (out_subdir, src_subdir, include_file) in specs {
let out_dir = sh.current_dir().join(out_subdir);
let src_dir = sh.current_dir().join(src_subdir);
std::fs::create_dir_all(&out_dir).unwrap();
let last_generated = out_dir
.join(include_file)
.metadata()
.and_then(|m| m.modified());
let mut proto_files = vec![];
let mut needs_regeneration = force;
for entry in std::fs::read_dir(&src_dir).unwrap() {
let entry_path = entry.unwrap().path();
if entry_path.is_file()
&& entry_path
.extension()
.map(|e| e == "proto")
.unwrap_or(false)
{
let modified = entry_path.metadata().and_then(|m| m.modified());
needs_regeneration |= match (&last_generated, modified) {
(Ok(last_generated), Ok(modified)) => modified > *last_generated,
// Couldn't read some metadata, assume needs update
_ => true,
};
proto_files.push(entry_path.display().to_string());
}
}
proto_files.sort();
if needs_regeneration {
let mut prost = prost_build::Config::new();
prost.out_dir(&out_dir);
prost.include_file(include_file);
prost.compile_protos(&proto_files, &[src_dir]).unwrap();
}
}
}
fn move_plugin_to_assets(sh: &Shell, plugin_name: &str) -> anyhow::Result<()> {
let err_context = || format!("failed to move plugin '{plugin_name}' to assets folder");
// Get asset path
let asset_name = crate::asset_dir()
.join("plugins")
.join(plugin_name)
.with_extension("wasm");
// Get plugin path
let plugin = crate::target_dir()
.join("wasm32-wasip1")
.join("release")
.join(plugin_name)
.with_extension("wasm");
if !plugin.is_file() {
return Err(anyhow::anyhow!("No plugin found at '{}'", plugin.display()))
.with_context(err_context);
}
// This is a plugin we want to move
let from = plugin.as_path();
let to = asset_name.as_path();
sh.copy_file(from, to).with_context(err_context)
}
|