/
rt.rs
238 lines (222 loc) · 8.15 KB
/
rt.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
use std::collections::HashMap;
use std::net::{IpAddr, Ipv4Addr};
use std::path::PathBuf;
use std::sync::Arc;
use anyhow::{anyhow, Context, Result};
use axum::http::Uri;
use crate::config::{
ConfigOptsBuild, ConfigOptsClean, ConfigOptsHook, ConfigOptsProxy, ConfigOptsServe,
ConfigOptsTools, ConfigOptsWatch,
};
/// Runtime config for the build system.
#[derive(Clone, Debug)]
pub struct RtcBuild {
/// The index HTML file to drive the bundling process.
pub target: PathBuf,
/// The parent directory of the target index HTML file.
pub target_parent: PathBuf,
/// Build in release mode.
pub release: bool,
/// The public URL from which assets are to be served.
pub public_url: String,
pub filehash: bool,
/// The directory where final build artifacts are placed after a successful build.
pub final_dist: PathBuf,
/// The directory used to stage build artifacts during an active build.
pub staging_dist: PathBuf,
/// Configuration for automatic application download.
pub tools: ConfigOptsTools,
/// Build process hooks.
pub hooks: Vec<ConfigOptsHook>,
/// A bool indicating if the output HTML should have the WebSocket autoloader injected.
///
/// This value is configured via the server config only. If the server is not being used, then
/// the autoloader will not be injected.
pub inject_autoloader: bool,
/// Optional pattern for the app loader script.
pub pattern_script: Option<String>,
/// Optional pattern for the app preload element.
pub pattern_preload: Option<String>,
/// Optional replacement parameters corresponding to the patterns provided in
/// `pattern_script` and `pattern_preload`.
pub pattern_params: Option<HashMap<String, String>>,
}
impl RtcBuild {
/// Construct a new instance.
pub(super) fn new(
opts: ConfigOptsBuild,
tools: ConfigOptsTools,
hooks: Vec<ConfigOptsHook>,
inject_autoloader: bool,
) -> Result<Self> {
// Get the canonical path to the target HTML file.
let pre_target = opts.target.clone().unwrap_or_else(|| "index.html".into());
let target = pre_target.canonicalize().with_context(|| {
format!(
"error getting canonical path to source HTML file {:?}",
&pre_target
)
})?;
// Get the target HTML's parent dir, falling back to OS specific root, as that is the only
// time where no parent could be determined.
let target_parent = target
.parent()
.map(|path| path.to_owned())
.unwrap_or_else(|| PathBuf::from(std::path::MAIN_SEPARATOR.to_string()));
// Ensure the final dist dir exists and that we have a canonical path to the dir. Normally
// we would want to avoid such an action at this layer, however to ensure that other layers
// have a reliable FS path to work with, we make an exception here.
let final_dist = opts
.dist
.unwrap_or_else(|| target_parent.join(super::DIST_DIR));
if !final_dist.exists() {
std::fs::create_dir(&final_dist).with_context(|| {
format!("error creating final dist directory {:?}", &final_dist)
})?;
}
let final_dist = final_dist
.canonicalize()
.context("error taking canonical path to dist dir")?;
let staging_dist = final_dist.join(super::STAGE_DIR);
Ok(Self {
target,
target_parent,
release: opts.release,
public_url: opts.public_url.unwrap_or_else(|| "/".into()),
filehash: opts.filehash.unwrap_or(true),
staging_dist,
final_dist,
tools,
hooks,
inject_autoloader,
pattern_script: opts.pattern_script,
pattern_preload: opts.pattern_preload,
pattern_params: opts.pattern_params,
})
}
}
/// Runtime config for the watch system.
#[derive(Clone, Debug)]
pub struct RtcWatch {
/// Runtime config for the build system.
pub build: Arc<RtcBuild>,
/// Paths to watch, defaults to the build target parent directory.
pub paths: Vec<PathBuf>,
/// Paths to ignore.
pub ignored_paths: Vec<PathBuf>,
}
impl RtcWatch {
pub(super) fn new(
build_opts: ConfigOptsBuild,
opts: ConfigOptsWatch,
tools: ConfigOptsTools,
hooks: Vec<ConfigOptsHook>,
inject_autoloader: bool,
) -> Result<Self> {
let build = Arc::new(RtcBuild::new(build_opts, tools, hooks, inject_autoloader)?);
// Take the canonical path of each of the specified watch targets.
let mut paths = vec![];
for path in opts.watch.unwrap_or_default() {
let canon_path = path
.canonicalize()
.map_err(|_| anyhow!("invalid watch path provided: {:?}", path))?;
paths.push(canon_path);
}
// If no watch paths were provied, then we default to the target HTML's parent dir.
if paths.is_empty() {
paths.push(build.target_parent.clone());
}
// Take the canonical path of each of the specified ignore targets.
let mut ignored_paths = match opts.ignore {
None => vec![],
Some(paths) => {
paths
.into_iter()
.try_fold(vec![], |mut acc, path| -> Result<Vec<PathBuf>> {
let canon_path = path
.canonicalize()
.map_err(|_| anyhow!("invalid ignore path provided: {:?}", path))?;
acc.push(canon_path);
Ok(acc)
})?
}
};
// Ensure the final dist dir is always ignored.
ignored_paths.push(build.final_dist.clone());
Ok(Self {
build,
paths,
ignored_paths,
})
}
}
/// Runtime config for the serve system.
#[derive(Clone, Debug)]
pub struct RtcServe {
/// Runtime config for the watch system.
pub watch: Arc<RtcWatch>,
/// The IP address to serve on.
pub address: IpAddr,
/// The port to serve on.
pub port: u16,
/// Open a browser tab once the initial build is complete.
pub open: bool,
/// A URL to which requests will be proxied.
pub proxy_backend: Option<Uri>,
/// The URI on which to accept requests which are to be rewritten and proxied to backend.
pub proxy_rewrite: Option<String>,
/// Configure the proxy for handling WebSockets.
pub proxy_ws: bool,
/// Configure the proxy to accept insecure connections.
pub proxy_insecure: bool,
/// Any proxies configured to run along with the server.
pub proxies: Option<Vec<ConfigOptsProxy>>,
/// Whether to disable auto-reload of the web page when a build completes.
pub no_autoreload: bool,
}
impl RtcServe {
pub(super) fn new(
build_opts: ConfigOptsBuild,
watch_opts: ConfigOptsWatch,
opts: ConfigOptsServe,
tools: ConfigOptsTools,
hooks: Vec<ConfigOptsHook>,
proxies: Option<Vec<ConfigOptsProxy>>,
) -> Result<Self> {
let watch = Arc::new(RtcWatch::new(
build_opts,
watch_opts,
tools,
hooks,
!opts.no_autoreload,
)?);
Ok(Self {
watch,
address: opts.address.unwrap_or(IpAddr::V4(Ipv4Addr::LOCALHOST)),
port: opts.port.unwrap_or(8080),
open: opts.open,
proxy_backend: opts.proxy_backend,
proxy_rewrite: opts.proxy_rewrite,
proxy_insecure: opts.proxy_insecure,
proxy_ws: opts.proxy_ws,
proxies,
no_autoreload: opts.no_autoreload,
})
}
}
/// Runtime config for the clean system.
#[derive(Clone, Debug)]
pub struct RtcClean {
/// The output dir for all final assets.
pub dist: PathBuf,
/// Optionally perform a cargo clean.
pub cargo: bool,
}
impl RtcClean {
pub(super) fn new(opts: ConfigOptsClean) -> Self {
Self {
dist: opts.dist.unwrap_or_else(|| super::DIST_DIR.into()),
cargo: opts.cargo,
}
}
}