修复了exe路径问题

This commit is contained in:
2025-08-18 01:57:03 +08:00
parent c90d0b4d79
commit 898c5dfb2b
5 changed files with 43 additions and 30 deletions

View File

@@ -37,6 +37,8 @@ def save_with_preserve(path, new_code):
with open(path, "r", encoding="utf-8") as f:
old_code = f.read()
new_code = preserve_all_user_regions(new_code, old_code)
# 确保目录存在
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
f.write(new_code)
@@ -47,7 +49,7 @@ class BspSimplePeripheral(QWidget):
self.peripheral_name = peripheral_name
self.template_names = template_names
# 加载描述
describe_path = os.path.join(os.path.dirname(__file__), "../../assets/User_code/bsp/describe.csv")
describe_path = os.path.join(CodeGenerator.get_assets_dir("User_code/bsp"), "describe.csv")
self.descriptions = load_descriptions(describe_path)
self._init_ui()
self._load_config()
@@ -123,7 +125,7 @@ class BspPeripheralBase(QWidget):
self.get_available_func = get_available_func
self.available_list = []
# 新增:加载描述
describe_path = os.path.join(os.path.dirname(__file__), "../../assets/User_code/bsp/describe.csv")
describe_path = os.path.join(CodeGenerator.get_assets_dir("User_code/bsp"), "describe.csv")
self.descriptions = load_descriptions(describe_path)
self._init_ui()
self._load_config()

View File

@@ -105,7 +105,7 @@ class ComponentSimple(QWidget):
self.component_manager = component_manager
# 加载描述和依赖信息
component_dir = os.path.dirname(__file__) + "/../../assets/User_code/component"
component_dir = CodeGenerator.get_assets_dir("User_code/component")
describe_path = os.path.join(component_dir, "describe.csv")
dependencies_path = os.path.join(component_dir, "dependencies.csv")
self.descriptions = load_descriptions(describe_path)
@@ -172,13 +172,7 @@ class ComponentSimple(QWidget):
return True
def _get_component_template_dir(self):
current_dir = os.path.dirname(os.path.abspath(__file__))
while os.path.basename(current_dir) != 'MRobot' and current_dir != '/':
current_dir = os.path.dirname(current_dir)
if os.path.basename(current_dir) == 'MRobot':
return os.path.join(current_dir, "assets/User_code/component")
else:
return os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "assets/User_code/component")
return CodeGenerator.get_assets_dir("User_code/component")
def _save_config(self):
config_path = os.path.join(self.project_path, "User/component/component_config.yaml")
@@ -286,7 +280,7 @@ class component(QWidget):
def generate_component(project_path, pages):
"""生成所有组件代码,处理依赖关系"""
# 自动添加 component.h
src_component_h = os.path.join(os.path.dirname(__file__), "../../assets/User_code/component/component.h")
src_component_h = os.path.join(CodeGenerator.get_assets_dir("User_code/component"), "component.h")
dst_component_h = os.path.join(project_path, "User/component/component.h")
os.makedirs(os.path.dirname(dst_component_h), exist_ok=True)
if os.path.exists(src_component_h):

View File

@@ -37,7 +37,7 @@ def get_available_bsp_devices(project_path, bsp_type, gpio_type=None):
def generate_device_header(project_path, enabled_devices):
"""生成device.h文件"""
device_dir = os.path.join(os.path.dirname(__file__), "../../assets/User_code/device")
device_dir = CodeGenerator.get_assets_dir("User_code/device")
template_path = os.path.join(device_dir, "device.h")
# 读取模板文件
@@ -274,16 +274,7 @@ class DeviceSimple(QWidget):
def _get_device_template_dir(self):
"""获取设备模板目录"""
current_dir = os.path.dirname(os.path.abspath(__file__))
# 向上找到 MRobot 根目录
while os.path.basename(current_dir) != 'MRobot' and current_dir != '/':
current_dir = os.path.dirname(current_dir)
if os.path.basename(current_dir) == 'MRobot':
return os.path.join(current_dir, "assets/User_code/device")
else:
# 如果找不到,使用相对路径作为备选
return os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "assets/User_code/device")
return CodeGenerator.get_assets_dir("User_code/device")
def _save_config(self):
"""保存配置"""
@@ -316,7 +307,7 @@ class DeviceSimple(QWidget):
def get_device_page(device_name, project_path):
"""根据设备名返回对应的页面类"""
# 加载设备配置
device_dir = os.path.join(os.path.dirname(__file__), "../../assets/User_code/device")
device_dir = CodeGenerator.get_assets_dir("User_code/device")
config_path = os.path.join(device_dir, "config.yaml")
device_configs = load_device_config(config_path)