make new VSC port pio script more failsafe (#23439)

* no crash when VSC database is not found
* don't stop when unknown OS is detected
This commit is contained in:
Jason2866 2025-05-18 15:41:10 +02:00 committed by GitHub
parent 2b460aa112
commit 54a9a117fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -21,20 +21,22 @@ if os.environ.get("PLATFORMIO_CALLER") == "vscode":
try: try:
db_path = os.path.expanduser(os.path.expandvars(os_paths[os_name])) db_path = os.path.expanduser(os.path.expandvars(os_paths[os_name]))
except KeyError: except KeyError:
raise RuntimeError("Unknown OS: " + os_name) print("Unknown OS: " + os_name)
conn = sqlite3.connect(db_path) # Only when the database is found we can go on
cursor = conn.cursor() if os.path.exists(db_path):
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
for key in ['pioarduino.pioarduino-ide', 'platformio.platformio-ide']: for key in ['pioarduino.pioarduino-ide', 'platformio.platformio-ide']:
cursor.execute("SELECT value FROM ItemTable WHERE key = ?", (key,)) cursor.execute("SELECT value FROM ItemTable WHERE key = ?", (key,))
row = cursor.fetchone() row = cursor.fetchone()
if row: if row:
data = json.loads(row[0]) data = json.loads(row[0])
projects = data.get("projects", {}) projects = data.get("projects", {})
project = projects.get(project_path) project = projects.get(project_path)
if project and "customPort" in project: if project and "customPort" in project:
print("USB port set in VSC:", project["customPort"]) print("USB port set in VSC:", project["customPort"])
env["UPLOAD_PORT"] = project["customPort"] env["UPLOAD_PORT"] = project["customPort"]
break break
conn.close() conn.close()