mirror of
https://github.com/goldenfishs/MRobot.git
synced 2026-03-31 21:07:14 +08:00
修复路径
This commit is contained in:
@@ -267,6 +267,7 @@ class CodeGenerateInterface(QWidget):
|
||||
"""生成所有代码,包括未加载页面"""
|
||||
try:
|
||||
# 先收集所有页面名(从CSV配置文件读取)
|
||||
from app.tools.code_generator import CodeGenerator # 在方法内重新导入确保可用
|
||||
csv_path = os.path.join(CodeGenerator.get_assets_dir("User_code"), "config.csv")
|
||||
all_class_names = []
|
||||
if os.path.exists(csv_path):
|
||||
@@ -344,6 +345,7 @@ class CodeGenerateInterface(QWidget):
|
||||
return "未找到.ioc文件"
|
||||
|
||||
def _load_csv_and_build_tree(self):
|
||||
from app.tools.code_generator import CodeGenerator # 在方法内重新导入确保可用
|
||||
csv_path = os.path.join(CodeGenerator.get_assets_dir("User_code"), "config.csv")
|
||||
print(f"加载CSV路径: {csv_path}")
|
||||
if not os.path.exists(csv_path):
|
||||
|
||||
@@ -1235,6 +1235,9 @@ class bsp(QWidget):
|
||||
@staticmethod
|
||||
def generate_bsp(project_path, pages):
|
||||
"""生成所有BSP代码"""
|
||||
# 在方法开始时导入CodeGenerator以确保可用
|
||||
from app.tools.code_generator import CodeGenerator
|
||||
|
||||
# 自动添加 bsp.h
|
||||
src_bsp_h = os.path.join(CodeGenerator.get_assets_dir("User_code/bsp"), "bsp.h")
|
||||
dst_bsp_h = os.path.join(project_path, "User/bsp/bsp.h")
|
||||
|
||||
@@ -286,6 +286,9 @@ class component(QWidget):
|
||||
@staticmethod
|
||||
def generate_component(project_path, pages):
|
||||
"""生成所有组件代码,处理依赖关系"""
|
||||
# 在方法开始时导入CodeGenerator以确保可用
|
||||
from app.tools.code_generator import CodeGenerator
|
||||
|
||||
# 自动添加 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")
|
||||
@@ -318,7 +321,6 @@ class component(QWidget):
|
||||
components_to_generate.add(dep_name)
|
||||
|
||||
# 为没有对应页面但需要生成的依赖组件创建临时页面
|
||||
from ..tools.code_generator import CodeGenerator
|
||||
user_code_dir = CodeGenerator.get_assets_dir("User_code")
|
||||
for comp_name in components_to_generate:
|
||||
if comp_name not in component_pages:
|
||||
|
||||
@@ -41,6 +41,7 @@ def get_available_bsp_devices(project_path, bsp_type, gpio_type=None):
|
||||
|
||||
def generate_device_header(project_path, enabled_devices):
|
||||
"""生成device.h文件"""
|
||||
from app.tools.code_generator import CodeGenerator
|
||||
device_dir = CodeGenerator.get_assets_dir("User_code/device")
|
||||
template_path = os.path.join(device_dir, "device.h")
|
||||
|
||||
@@ -318,6 +319,7 @@ class DeviceSimple(QWidget):
|
||||
def get_device_page(device_name, project_path):
|
||||
"""根据设备名返回对应的页面类"""
|
||||
# 加载设备配置
|
||||
from app.tools.code_generator import CodeGenerator
|
||||
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)
|
||||
|
||||
@@ -7,6 +7,11 @@ import os
|
||||
class CodeGenerator:
|
||||
"""通用代码生成器"""
|
||||
|
||||
# 添加类级别的缓存
|
||||
_assets_dir_cache = None
|
||||
_assets_dir_initialized = False
|
||||
_template_dir_logged = False
|
||||
|
||||
@staticmethod
|
||||
def load_template(template_path: str) -> str:
|
||||
"""加载代码模板"""
|
||||
@@ -66,8 +71,12 @@ class CodeGenerator:
|
||||
# 使用统一的get_assets_dir方法来获取路径
|
||||
template_dir = CodeGenerator.get_assets_dir("User_code/bsp")
|
||||
|
||||
print(f"模板目录路径: {template_dir}")
|
||||
if not os.path.exists(template_dir):
|
||||
# 只在第一次或出现问题时打印日志
|
||||
if not hasattr(CodeGenerator, '_template_dir_logged'):
|
||||
print(f"模板目录路径: {template_dir}")
|
||||
CodeGenerator._template_dir_logged = True
|
||||
|
||||
if template_dir and not os.path.exists(template_dir):
|
||||
print(f"警告:模板目录不存在: {template_dir}")
|
||||
|
||||
return template_dir
|
||||
@@ -80,31 +89,78 @@ class CodeGenerator:
|
||||
Returns:
|
||||
str: 完整的assets路径
|
||||
"""
|
||||
if getattr(sys, 'frozen', False):
|
||||
# 打包后的环境 - 使用可执行文件所在目录而不是临时目录
|
||||
exe_dir = os.path.dirname(sys.executable)
|
||||
assets_dir = os.path.join(exe_dir, "assets")
|
||||
print(f"打包环境:尝试使用路径: {assets_dir}")
|
||||
# 使用缓存机制,避免重复计算和日志输出
|
||||
if not CodeGenerator._assets_dir_initialized:
|
||||
assets_dir = ""
|
||||
|
||||
# 如果exe_dir/assets不存在,尝试使用sys._MEIPASS作为后备
|
||||
if not os.path.exists(assets_dir) and hasattr(sys, '_MEIPASS'):
|
||||
base_path = getattr(sys, '_MEIPASS')
|
||||
assets_dir = os.path.join(base_path, "assets")
|
||||
print(f"后备路径: {assets_dir}")
|
||||
if getattr(sys, 'frozen', False):
|
||||
# 打包后的环境
|
||||
print("检测到打包环境")
|
||||
|
||||
# 优先使用sys._MEIPASS(PyInstaller的临时解包目录)
|
||||
if hasattr(sys, '_MEIPASS'):
|
||||
base_path = getattr(sys, '_MEIPASS')
|
||||
assets_dir = os.path.join(base_path, "assets")
|
||||
print(f"使用PyInstaller临时目录: {assets_dir}")
|
||||
else:
|
||||
# 后备方案:使用可执行文件所在目录
|
||||
exe_dir = os.path.dirname(sys.executable)
|
||||
assets_dir = os.path.join(exe_dir, "assets")
|
||||
print(f"使用可执行文件目录: {assets_dir}")
|
||||
|
||||
# 如果都不存在,尝试其他可能的位置
|
||||
if not os.path.exists(assets_dir):
|
||||
# 尝试从当前工作目录查找
|
||||
cwd_assets = os.path.join(os.getcwd(), "assets")
|
||||
if os.path.exists(cwd_assets):
|
||||
assets_dir = cwd_assets
|
||||
print(f"从工作目录找到assets: {assets_dir}")
|
||||
else:
|
||||
print(f"警告:无法找到assets目录,使用默认路径: {assets_dir}")
|
||||
else:
|
||||
# 开发环境
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
# 向上查找直到找到MRobot目录或到达根目录
|
||||
while current_dir != os.path.dirname(current_dir): # 防止无限循环
|
||||
if os.path.basename(current_dir) == 'MRobot':
|
||||
break
|
||||
parent = os.path.dirname(current_dir)
|
||||
if parent == current_dir: # 已到达根目录
|
||||
break
|
||||
current_dir = parent
|
||||
|
||||
assets_dir = os.path.join(current_dir, "assets")
|
||||
print(f"开发环境:使用路径: {assets_dir}")
|
||||
|
||||
# 如果找不到,尝试从当前工作目录
|
||||
if not os.path.exists(assets_dir):
|
||||
cwd_assets = os.path.join(os.getcwd(), "assets")
|
||||
if os.path.exists(cwd_assets):
|
||||
assets_dir = cwd_assets
|
||||
print(f"开发环境后备:使用工作目录: {assets_dir}")
|
||||
|
||||
# 缓存基础assets目录
|
||||
CodeGenerator._assets_dir_cache = assets_dir
|
||||
CodeGenerator._assets_dir_initialized = True
|
||||
else:
|
||||
# 开发环境
|
||||
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)
|
||||
assets_dir = os.path.join(current_dir, "assets")
|
||||
print(f"开发环境:使用路径: {assets_dir}")
|
||||
# 使用缓存的路径
|
||||
assets_dir = CodeGenerator._assets_dir_cache or ""
|
||||
|
||||
# 构建完整路径
|
||||
if sub_path:
|
||||
full_path = os.path.join(assets_dir, sub_path)
|
||||
else:
|
||||
full_path = assets_dir
|
||||
|
||||
if not os.path.exists(full_path):
|
||||
# 规范化路径(处理路径分隔符)
|
||||
full_path = os.path.normpath(full_path)
|
||||
|
||||
# 只在第一次访问某个路径时检查并警告
|
||||
safe_sub_path = sub_path.replace('/', '_').replace('\\', '_')
|
||||
warning_key = f"_warned_{safe_sub_path}"
|
||||
if full_path and not os.path.exists(full_path) and not hasattr(CodeGenerator, warning_key):
|
||||
print(f"警告:资源目录不存在: {full_path}")
|
||||
setattr(CodeGenerator, warning_key, True)
|
||||
|
||||
return full_path
|
||||
Reference in New Issue
Block a user