Skip to content

Commit 05cf4f7

Browse files
authored
chore: update submodule reference and refactor package information handling (#930)
* chore: update submodule reference and refactor package information handling * fix: refine codes * fix: refine codes * fix: refine codes * fix: refine codes * fix: refine codes * fix: refine codes * fix: refine codes * fix: refine codes * fix: refine codes * fix: refine codes * fix: refine codes * fix: refine codes * fix: refine codes * fix: refine codes * fix: refine codes * fix: refine codes * fix: refine codes * fix: refine codes
1 parent 6514ee1 commit 05cf4f7

35 files changed

+777
-621
lines changed

core/src/ten_manager/src/cmd/cmd_check/cmd_check_graph.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ use clap::{Arg, ArgMatches, Command};
1111
use console::Emoji;
1212

1313
use ten_rust::{
14+
base_dir_pkg_info::BaseDirPkgInfo,
1415
graph::Graph,
1516
pkg_info::{
1617
get_app_installed_pkgs, localhost, property::parse_property_in_folder,
17-
PkgInfo,
1818
},
1919
};
2020

@@ -103,8 +103,8 @@ fn validate_cmd_args(command: &CheckGraphCommand) -> Result<()> {
103103

104104
fn get_app_installed_pkgs_with_cmd_data(
105105
command: &CheckGraphCommand,
106-
) -> Result<HashMap<String, Vec<PkgInfo>>> {
107-
let mut pkgs_info: HashMap<String, Vec<PkgInfo>> = HashMap::new();
106+
) -> Result<HashMap<String, BaseDirPkgInfo>> {
107+
let mut pkgs_info: HashMap<String, BaseDirPkgInfo> = HashMap::new();
108108

109109
let single_app = command.app_dir.len() == 1;
110110

@@ -133,6 +133,7 @@ fn get_app_installed_pkgs_with_cmd_data(
133133
} else {
134134
app_uri.clone()
135135
};
136+
136137
let present_pkg = pkgs_info.insert(key, app_installed_pkgs);
137138
if present_pkg.is_some() {
138139
return Err(anyhow::anyhow!(

core/src/ten_manager/src/cmd/cmd_designer.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,7 @@ pub async fn execute_cmd(
136136
}
137137

138138
if let Some(actual_base_dir) = actual_base_dir_opt.as_ref() {
139-
get_all_pkgs(
140-
tman_config,
141-
&mut state.write().unwrap().pkgs_cache,
142-
actual_base_dir,
143-
&out,
144-
)?;
139+
get_all_pkgs(&mut state.write().unwrap().pkgs_cache, actual_base_dir)?;
145140
}
146141

147142
let server = HttpServer::new(move || {

core/src/ten_manager/src/designer/apps/addons.rs

+57-13
Original file line numberDiff line numberDiff line change
@@ -161,19 +161,63 @@ pub async fn get_app_addons_endpoint(
161161
return Ok(HttpResponse::NotFound().json(error_response));
162162
}
163163

164-
let mut all_addons: Vec<GetAppAddonsSingleResponseData> = state_read
165-
.pkgs_cache
166-
.get(&request_payload.base_dir)
167-
.unwrap()
168-
.iter()
169-
.map(convert_pkg_info_to_addon)
170-
.collect();
171-
172-
all_addons.retain(|addon| addon.addon_type != PkgType::App.to_string());
173-
174-
// Filter by addon_type if provided.
175-
if let Some(addon_type) = &request_payload.addon_type {
176-
all_addons.retain(|addon| &addon.addon_type == addon_type);
164+
let mut all_addons: Vec<GetAppAddonsSingleResponseData> = Vec::new();
165+
166+
// Get the BaseDirPkgInfo and extract only the requested packages from it.
167+
if let Some(base_dir_pkg_info) =
168+
state_read.pkgs_cache.get(&request_payload.base_dir)
169+
{
170+
let addon_type_filter = request_payload.addon_type.as_deref();
171+
172+
// Only process these packages if no addon_type filter is specified or
173+
// if it matches "extension"
174+
if addon_type_filter.is_none() || addon_type_filter == Some("extension")
175+
{
176+
// Extract extension packages if they exist.
177+
if let Some(extensions) = &base_dir_pkg_info.extension_pkg_info {
178+
for ext in extensions {
179+
all_addons.push(convert_pkg_info_to_addon(ext));
180+
}
181+
}
182+
}
183+
184+
// Only process these packages if no addon_type filter is specified or
185+
// if it matches "protocol"
186+
if addon_type_filter.is_none() || addon_type_filter == Some("protocol")
187+
{
188+
// Extract protocol packages if they exist.
189+
if let Some(protocols) = &base_dir_pkg_info.protocol_pkg_info {
190+
for protocol in protocols {
191+
all_addons.push(convert_pkg_info_to_addon(protocol));
192+
}
193+
}
194+
}
195+
196+
// Only process these packages if no addon_type filter is specified or
197+
// if it matches "addon_loader".
198+
if addon_type_filter.is_none()
199+
|| addon_type_filter == Some("addon_loader")
200+
{
201+
// Extract addon loader packages if they exist.
202+
if let Some(addon_loaders) =
203+
&base_dir_pkg_info.addon_loader_pkg_info
204+
{
205+
for loader in addon_loaders {
206+
all_addons.push(convert_pkg_info_to_addon(loader));
207+
}
208+
}
209+
}
210+
211+
// Only process these packages if no addon_type filter is specified or
212+
// if it matches "system".
213+
if addon_type_filter.is_none() || addon_type_filter == Some("system") {
214+
// Extract system packages if they exist.
215+
if let Some(systems) = &base_dir_pkg_info.system_pkg_info {
216+
for system in systems {
217+
all_addons.push(convert_pkg_info_to_addon(system));
218+
}
219+
}
220+
}
177221
}
178222

179223
// Filter by addon_name if provided.

core/src/ten_manager/src/designer/apps/create.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,10 @@ pub async fn create_app_endpoint(
104104

105105
// Re-acquire the lock for updating the cache.
106106
let mut state_write = state.write().unwrap();
107-
let DesignerState {
108-
tman_config,
109-
pkgs_cache,
110-
out,
111-
} = &mut *state_write;
107+
let pkgs_cache = &mut state_write.pkgs_cache;
112108

113109
// Try to load the newly created app into the cache.
114-
if let Err(err) = get_all_pkgs(
115-
tman_config.clone(),
116-
pkgs_cache,
117-
&app_path_str,
118-
out,
119-
) {
110+
if let Err(err) = get_all_pkgs(pkgs_cache, &app_path_str) {
120111
// Don't delete the app directory on cache update failure.
121112
let error_response = ErrorResponse::from_error(
122113
&err,

core/src/ten_manager/src/designer/apps/get.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::sync::{Arc, RwLock};
99
use actix_web::{web, HttpResponse, Responder};
1010
use anyhow::Result;
1111
use serde::{Deserialize, Serialize};
12-
use ten_rust::pkg_info::pkg_type::PkgType;
1312

1413
use crate::designer::{
1514
response::{ApiResponse, Status},
@@ -41,16 +40,15 @@ pub async fn get_apps_endpoint(
4140
state_read
4241
.pkgs_cache
4342
.iter()
44-
.map(|(base_dir, pkg_infos)| {
45-
// Find the App package in pkg_infos.
46-
let app_uri = pkg_infos
47-
.iter()
48-
.find(|pkg_info| {
49-
pkg_info.manifest.as_ref().is_some_and(|m| {
50-
m.type_and_name.pkg_type == PkgType::App
51-
})
43+
.map(|(base_dir, base_dir_pkg_info)| {
44+
// Get the App package info directly from
45+
// BaseDirPkgInfo.
46+
let app_uri = base_dir_pkg_info
47+
.app_pkg_info
48+
.as_ref()
49+
.and_then(|app_pkg_info| {
50+
app_pkg_info.property.as_ref()
5251
})
53-
.and_then(|pkg_info| pkg_info.property.as_ref())
5452
.and_then(|property| property._ten.as_ref())
5553
.and_then(|ten| ten.uri.as_ref())
5654
.map(|uri| uri.to_string());
@@ -73,6 +71,7 @@ mod tests {
7371
use std::collections::HashMap;
7472

7573
use actix_web::{test, App};
74+
use ten_rust::base_dir_pkg_info::BaseDirPkgInfo;
7675

7776
use super::*;
7877
use crate::{
@@ -99,11 +98,14 @@ mod tests {
9998
)
10099
.await;
101100

101+
// Create an empty BaseDirPkgInfo.
102+
let empty_pkg_info = BaseDirPkgInfo::default();
103+
102104
designer_state
103105
.write()
104106
.unwrap()
105107
.pkgs_cache
106-
.insert(TEST_DIR.to_string(), vec![]);
108+
.insert(TEST_DIR.to_string(), empty_pkg_info);
107109

108110
let req = test::TestRequest::get()
109111
.uri("/api/designer/v1/apps")

core/src/ten_manager/src/designer/apps/load.rs

+25-36
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,18 @@
44
// Licensed under the Apache License, Version 2.0, with certain conditions.
55
// Refer to the "LICENSE" file in the root directory for more information.
66
//
7-
use std::{
8-
collections::HashMap,
9-
path::Path,
10-
sync::{Arc, RwLock},
11-
};
7+
use std::collections::HashMap;
8+
use std::path::Path;
9+
use std::sync::{Arc, RwLock};
1210

1311
use actix_web::{web, HttpResponse, Responder};
1412
use anyhow::Result;
1513
use serde::{Deserialize, Serialize};
16-
use ten_rust::pkg_info::{pkg_type::PkgType, PkgInfo};
14+
use ten_rust::base_dir_pkg_info::BaseDirPkgInfo;
1715

1816
use crate::{
19-
designer::{
20-
response::{ApiResponse, ErrorResponse, Status},
21-
DesignerState,
22-
},
17+
designer::response::{ApiResponse, ErrorResponse, Status},
18+
designer::DesignerState,
2319
fs::check_is_app_folder,
2420
pkg_info::get_all_pkgs::get_all_pkgs,
2521
};
@@ -55,18 +51,11 @@ pub async fn load_app_endpoint(
5551

5652
match check_is_app_folder(Path::new(&request_payload.base_dir)) {
5753
Ok(_) => {
58-
let DesignerState {
59-
tman_config,
60-
pkgs_cache,
61-
out,
62-
} = &mut *state_write;
63-
64-
if let Err(err) = get_all_pkgs(
65-
tman_config.clone(),
66-
pkgs_cache,
67-
&request_payload.base_dir,
68-
out,
69-
) {
54+
let pkgs_cache = &mut state_write.pkgs_cache;
55+
56+
if let Err(err) =
57+
get_all_pkgs(pkgs_cache, &request_payload.base_dir)
58+
{
7059
let error_response =
7160
ErrorResponse::from_error(&err, "Error fetching packages:");
7261
return Ok(HttpResponse::NotFound().json(error_response));
@@ -92,22 +81,22 @@ pub async fn load_app_endpoint(
9281
}
9382

9483
fn extract_app_uri(
95-
pkgs_cache: &HashMap<String, Vec<PkgInfo>>,
84+
pkgs_cache: &HashMap<String, BaseDirPkgInfo>,
9685
base_dir: &str,
9786
) -> Option<String> {
98-
pkgs_cache.get(base_dir).and_then(|pkg_infos| {
99-
pkg_infos
100-
.iter()
101-
.find(|pkg_info| {
102-
pkg_info
103-
.manifest
104-
.as_ref()
105-
.is_some_and(|m| m.type_and_name.pkg_type == PkgType::App)
106-
})
107-
.and_then(|pkg_info| pkg_info.property.as_ref())
108-
.and_then(|property| property._ten.as_ref())
109-
.and_then(|ten| ten.uri.as_ref())
110-
.map(|uri| uri.to_string())
87+
pkgs_cache.get(base_dir).and_then(|base_dir_pkg_info| {
88+
// Check the app package first.
89+
if let Some(app_pkg) = &base_dir_pkg_info.app_pkg_info {
90+
if let Some(property) = &app_pkg.property {
91+
if let Some(ten) = &property._ten {
92+
if let Some(uri) = &ten.uri {
93+
return Some(uri.to_string());
94+
}
95+
}
96+
}
97+
}
98+
99+
None
111100
})
112101
}
113102

core/src/ten_manager/src/designer/apps/reload.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ pub async fn reload_app_endpoint(
2828
state: web::Data<Arc<RwLock<DesignerState>>>,
2929
) -> Result<impl Responder, actix_web::Error> {
3030
let mut state_write = state.write().unwrap();
31-
let DesignerState {
32-
tman_config,
33-
pkgs_cache,
34-
out,
35-
} = &mut *state_write;
31+
let pkgs_cache = &mut state_write.pkgs_cache;
3632

3733
if let Some(base_dir) = &request_payload.base_dir {
3834
// Case 1: base_dir is specified in the request payload.
@@ -54,9 +50,7 @@ pub async fn reload_app_endpoint(
5450
pkgs_cache.remove(base_dir);
5551

5652
// Reload packages for this base_dir.
57-
if let Err(err) =
58-
get_all_pkgs(tman_config.clone(), pkgs_cache, base_dir, out)
59-
{
53+
if let Err(err) = get_all_pkgs(pkgs_cache, base_dir) {
6054
return Ok(HttpResponse::InternalServerError().json(
6155
ErrorResponse::from_error(&err, "Failed to reload packages:"),
6256
));
@@ -81,9 +75,7 @@ pub async fn reload_app_endpoint(
8175
pkgs_cache.remove(&base_dir);
8276

8377
// Reload packages for this base_dir.
84-
if let Err(err) =
85-
get_all_pkgs(tman_config.clone(), pkgs_cache, &base_dir, out)
86-
{
78+
if let Err(err) = get_all_pkgs(pkgs_cache, &base_dir) {
8779
return Ok(HttpResponse::InternalServerError().json(
8880
ErrorResponse::from_error(
8981
&err,
@@ -106,6 +98,7 @@ mod tests {
10698
use std::collections::HashMap;
10799

108100
use actix_web::{http::StatusCode, test, App};
101+
use ten_rust::base_dir_pkg_info::BaseDirPkgInfo;
109102

110103
use super::*;
111104
use crate::{
@@ -200,10 +193,13 @@ mod tests {
200193
// This is a bit of a hack but should work for testing purposes.
201194
let invalid_path = "/definitely/invalid/path/that/doesnt/exist";
202195

196+
// Create an empty BaseDirPkgInfo.
197+
let empty_pkg_info = BaseDirPkgInfo::default();
198+
203199
// Inject an entry with the invalid path.
204200
designer_state
205201
.pkgs_cache
206-
.insert(invalid_path.to_string(), Vec::new());
202+
.insert(invalid_path.to_string(), empty_pkg_info);
207203

208204
let designer_state = Arc::new(RwLock::new(designer_state));
209205

core/src/ten_manager/src/designer/apps/scripts.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::sync::{Arc, RwLock};
99
use actix_web::{web, HttpResponse, Responder};
1010
use anyhow::Result;
1111
use serde::{Deserialize, Serialize};
12-
use ten_rust::pkg_info::pkg_type::PkgType;
1312

1413
use crate::designer::{
1514
response::{ApiResponse, ErrorResponse, Status},
@@ -33,13 +32,11 @@ pub async fn get_app_scripts_endpoint(
3332
) -> Result<impl Responder, actix_web::Error> {
3433
let state_read = state.read().unwrap();
3534

36-
if let Some(pkgs) = &state_read.pkgs_cache.get(&request_payload.base_dir) {
37-
if let Some(pkg) = pkgs.iter().find(|pkg| {
38-
pkg.manifest
39-
.as_ref()
40-
.is_some_and(|m| m.type_and_name.pkg_type == PkgType::App)
41-
}) {
42-
let scripts = pkg
35+
if let Some(base_dir_pkg_info) =
36+
&state_read.pkgs_cache.get(&request_payload.base_dir)
37+
{
38+
if let Some(app_pkg) = &base_dir_pkg_info.app_pkg_info {
39+
let scripts = app_pkg
4340
.manifest
4441
.as_ref()
4542
.and_then(|m| m.scripts.as_ref())

0 commit comments

Comments
 (0)