-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlib.rs
More file actions
483 lines (414 loc) · 13.9 KB
/
lib.rs
File metadata and controls
483 lines (414 loc) · 13.9 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
use std::{collections::HashMap, sync::Arc};
use anyhow::Context;
use async_trait::async_trait;
use serde::Serialize;
use tokio::sync::Mutex;
use tracing::debug;
use crate::{
canister::{Settings, build, sync},
manifest::{PROJECT_MANIFEST, ProjectRootLocate, project::ProjectManifest},
network::Configuration,
prelude::*,
};
pub mod agent;
pub mod canister;
pub mod context;
pub mod directories;
pub mod fs;
pub mod identity;
pub mod manifest;
pub mod network;
pub mod prelude;
pub mod project;
pub mod store_artifact;
pub mod store_id;
const ICP_BASE: &str = ".icp";
const CACHE_DIR: &str = "cache";
const DATA_DIR: &str = "data";
fn is_glob(s: &str) -> bool {
s.contains('*') || s.contains('?') || s.contains('[') || s.contains('{')
}
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Canister {
pub name: String,
/// Canister settings, such as memory constaints, etc.
pub settings: Settings,
/// The build configuration specifying how to compile the canister's source
/// code into a WebAssembly module, including the adapter to use.
pub build: build::Steps,
/// The configuration specifying how to sync the canister
pub sync: sync::Steps,
}
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Network {
pub name: String,
pub configuration: Configuration,
}
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Environment {
pub name: String,
pub network: Network,
pub canisters: HashMap<String, (PathBuf, Canister)>,
}
impl Environment {
pub fn get_canister_names(&self) -> Vec<String> {
self.canisters.keys().cloned().collect()
}
pub fn contains_canister(&self, canister_name: &str) -> bool {
self.canisters.contains_key(canister_name)
}
pub fn get_canister_info(&self, canister: &str) -> Result<(PathBuf, Canister), String> {
self.canisters
.get(canister)
.ok_or_else(|| {
format!(
"canister '{}' not declared in environment '{}'",
canister, self.name
)
})
.cloned()
}
}
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Project {
pub dir: PathBuf,
pub canisters: HashMap<String, (PathBuf, Canister)>,
pub networks: HashMap<String, Network>,
pub environments: HashMap<String, Environment>,
}
impl Project {
pub fn get_canister(&self, canister_name: &str) -> Option<&(PathBuf, Canister)> {
self.canisters.get(canister_name)
}
}
#[derive(Debug, thiserror::Error)]
pub enum LoadError {
#[error("failed to locate project directory")]
Locate,
#[error("failed to load path")]
Path,
#[error("failed to load the project manifest")]
Manifest,
#[error(transparent)]
Unexpected(#[from] anyhow::Error),
}
#[async_trait]
pub trait Load: Sync + Send {
async fn load(&self) -> Result<Project, LoadError>;
}
#[async_trait]
pub trait LoadPath<M, E>: Sync + Send {
async fn load(&self, path: &Path) -> Result<M, E>;
}
#[async_trait]
pub trait LoadManifest<M, T, E>: Sync + Send {
async fn load(&self, m: &M) -> Result<T, E>;
}
pub struct ProjectLoaders {
pub path: Arc<dyn LoadPath<ProjectManifest, project::LoadPathError>>,
pub manifest: Arc<dyn LoadManifest<ProjectManifest, Project, project::LoadManifestError>>,
}
pub struct Loader {
pub project_root_locate: Arc<dyn ProjectRootLocate>,
pub project: ProjectLoaders,
}
#[async_trait]
impl Load for Loader {
async fn load(&self) -> Result<Project, LoadError> {
debug!("Loading project");
// Locate project root
let pdir = self
.project_root_locate
.locate()
.context(LoadError::Locate)?;
debug!("Located icp project in {pdir}");
// Load project manifest
let m = self
.project
.path
.load(&pdir.join(PROJECT_MANIFEST))
.await
.context(LoadError::Path)?;
debug!("Loaded project manifest: {m:#?}");
// Load project
let p = self
.project
.manifest
.load(&m)
.await
.context(LoadError::Manifest)?;
debug!("Rendered project definition: {p:#?}");
Ok(p)
}
}
pub struct Lazy<T, V>(T, Arc<Mutex<Option<V>>>);
impl<T, V> Lazy<T, V> {
pub fn new(v: T) -> Self {
Self(v, Arc::new(Mutex::new(None)))
}
}
#[async_trait]
impl<T: Load> Load for Lazy<T, Project> {
async fn load(&self) -> Result<Project, LoadError> {
if let Some(v) = self.1.lock().await.as_ref() {
return Ok(v.to_owned());
}
let v = self.0.load().await?;
let mut g = self.1.lock().await;
if g.is_none() {
*g = Some(v.to_owned());
}
Ok(v)
}
}
#[cfg(test)]
/// Mock project loader for testing.
/// Returns a pre-configured `Project` when `load()` is called.
pub struct MockProjectLoader {
project: Project,
}
#[cfg(test)]
impl MockProjectLoader {
/// Creates a new mock project loader with the given project.
pub fn new(project: Project) -> Self {
Self { project }
}
/// Creates a minimal project with one canister, one network, and one environment.
///
/// Structure:
/// - Canister: "backend" (pre-built from local file "backend.wasm")
/// - Network: "local" (managed, localhost:8000)
/// - Environment: "default" (uses local network, includes backend canister)
pub fn minimal() -> Self {
use crate::{
canister::build::{Step as BuildStep, Steps as BuildSteps},
canister::sync::Steps as SyncSteps,
manifest::adapter::prebuilt::{Adapter as PrebuiltAdapter, LocalSource, SourceField},
network::{Configuration, Managed},
};
let backend_canister = Canister {
name: "backend".to_string(),
settings: Settings::default(),
build: BuildSteps {
steps: vec![BuildStep::Prebuilt(PrebuiltAdapter {
source: SourceField::Local(LocalSource {
path: "backend.wasm".into(),
}),
sha256: None,
})],
},
sync: SyncSteps::default(),
};
let local_network = Network {
name: "local".to_string(),
configuration: Configuration::Managed {
managed: Managed::default(),
},
};
let mut canisters = HashMap::new();
canisters.insert(
"backend".to_string(),
("/project".into(), backend_canister.clone()),
);
let mut networks = HashMap::new();
networks.insert("local".to_string(), local_network.clone());
let mut env_canisters = HashMap::new();
env_canisters.insert("backend".to_string(), ("/project".into(), backend_canister));
let default_env = Environment {
name: "default".to_string(),
network: local_network,
canisters: env_canisters,
};
let mut environments = HashMap::new();
environments.insert("default".to_string(), default_env);
let project = Project {
dir: "/project".into(),
canisters,
networks,
environments,
};
Self::new(project)
}
/// Creates a complex project with multiple canisters, networks, and environments.
///
/// Structure:
/// - Canisters:
/// - "backend" (pre-built from local "backend.wasm")
/// - "frontend" (pre-built from local "frontend.wasm")
/// - "database" (pre-built from local "database.wasm")
/// - Networks:
/// - "local" (managed, localhost:8000)
/// - "staging" (managed, localhost:8001)
/// - "ic" (connected to mainnet)
/// - Environments:
/// - "dev" (local network, all three canisters)
/// - "test" (staging network, backend and frontend only)
/// - "prod" (ic network, backend and frontend only)
pub fn complex() -> Self {
use crate::{
canister::build::{Step as BuildStep, Steps as BuildSteps},
canister::sync::Steps as SyncSteps,
manifest::adapter::prebuilt::{Adapter as PrebuiltAdapter, LocalSource, SourceField},
network::{Configuration, Connected, Gateway, Managed, Port},
};
// Create canisters
let backend_canister = Canister {
name: "backend".to_string(),
settings: Settings::default(),
build: BuildSteps {
steps: vec![BuildStep::Prebuilt(PrebuiltAdapter {
source: SourceField::Local(LocalSource {
path: "backend.wasm".into(),
}),
sha256: None,
})],
},
sync: SyncSteps::default(),
};
let frontend_canister = Canister {
name: "frontend".to_string(),
settings: Settings::default(),
build: BuildSteps {
steps: vec![BuildStep::Prebuilt(PrebuiltAdapter {
source: SourceField::Local(LocalSource {
path: "frontend.wasm".into(),
}),
sha256: None,
})],
},
sync: SyncSteps::default(),
};
let database_canister = Canister {
name: "database".to_string(),
settings: Settings::default(),
build: BuildSteps {
steps: vec![BuildStep::Prebuilt(PrebuiltAdapter {
source: SourceField::Local(LocalSource {
path: "database.wasm".into(),
}),
sha256: None,
})],
},
sync: SyncSteps::default(),
};
// Create networks
let local_network = Network {
name: "local".to_string(),
configuration: Configuration::Managed {
managed: Managed {
gateway: Gateway {
host: "localhost".to_string(),
port: Port::Fixed(8000),
},
},
},
};
let staging_network = Network {
name: "staging".to_string(),
configuration: Configuration::Managed {
managed: Managed {
gateway: Gateway {
host: "localhost".to_string(),
port: Port::Fixed(8001),
},
},
},
};
let ic_network = Network {
name: "ic".to_string(),
configuration: Configuration::Connected {
connected: Connected {
url: "https://ic0.app".to_string(),
root_key: None,
},
},
};
// Setup canisters map
let mut canisters = HashMap::new();
canisters.insert(
"backend".to_string(),
("/project/backend".into(), backend_canister.clone()),
);
canisters.insert(
"frontend".to_string(),
("/project/frontend".into(), frontend_canister.clone()),
);
canisters.insert(
"database".to_string(),
("/project/database".into(), database_canister.clone()),
);
// Setup networks map
let mut networks = HashMap::new();
networks.insert("local".to_string(), local_network.clone());
networks.insert("staging".to_string(), staging_network.clone());
networks.insert("ic".to_string(), ic_network.clone());
// Create dev environment (all canisters on local)
let mut dev_canisters = HashMap::new();
dev_canisters.insert(
"backend".to_string(),
("/project/backend".into(), backend_canister.clone()),
);
dev_canisters.insert(
"frontend".to_string(),
("/project/frontend".into(), frontend_canister.clone()),
);
dev_canisters.insert(
"database".to_string(),
("/project/database".into(), database_canister.clone()),
);
let dev_env = Environment {
name: "dev".to_string(),
network: local_network,
canisters: dev_canisters,
};
// Create test environment (backend and frontend on staging)
let mut test_canisters = HashMap::new();
test_canisters.insert(
"backend".to_string(),
("/project/backend".into(), backend_canister.clone()),
);
test_canisters.insert(
"frontend".to_string(),
("/project/frontend".into(), frontend_canister.clone()),
);
let test_env = Environment {
name: "test".to_string(),
network: staging_network,
canisters: test_canisters,
};
// Create prod environment (backend and frontend on ic)
let mut prod_canisters = HashMap::new();
prod_canisters.insert(
"backend".to_string(),
("/project/backend".into(), backend_canister),
);
prod_canisters.insert(
"frontend".to_string(),
("/project/frontend".into(), frontend_canister),
);
let prod_env = Environment {
name: "prod".to_string(),
network: ic_network,
canisters: prod_canisters,
};
// Setup environments map
let mut environments = HashMap::new();
environments.insert("dev".to_string(), dev_env);
environments.insert("test".to_string(), test_env);
environments.insert("prod".to_string(), prod_env);
let project = Project {
dir: "/project".into(),
canisters,
networks,
environments,
};
Self::new(project)
}
}
#[cfg(test)]
#[async_trait]
impl Load for MockProjectLoader {
async fn load(&self) -> Result<Project, LoadError> {
Ok(self.project.clone())
}
}