Skip to content

Commit 6b97dbf

Browse files
committed
Added 'updateWorkspaceFile.py'.
Updated README_DETAILS.md. Updated SVD and CFG path description.
1 parent 86ed0a9 commit 6b97dbf

File tree

4 files changed

+118
-2
lines changed

4 files changed

+118
-2
lines changed

README_DETAILS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ If any of files/paths are missing or invalid, new ones are created (and backup f
1616

1717
File adds 'print-variable' function to 'Makefile', while creating a backup 'Makefile'. Function is needed for fetching data from 'Makefile' with (example) 'make print-CFLAGS' call.
1818

19+
## updateWorkspaceFile.py
20+
This file adds "cortex-debug" keys to '*.code-workspace' file. It is needed for Cortex-Debug extension and should not be modified by user. Instead, this fields are fetched from 'buildData.json' file.
21+
1922
## updateMakefile.py
2023
This script generate new 'Makefile' from old 'Makefile' and user data. User data specified in 'c_cpp_properties.json' is merged with existing data from 'Makefile' and stored into 'buildData.json'. New 'Makefile' is created by making a copy and appending specific strings (c/asm sources, includes and defines) with proper multi-line escaping ( '\\' ).
2124

ideScripts/update.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import updateBuildData as build
1919
import updateTasks as tasks
2020
import updateLaunchConfig as launch
21+
import updateWorkspaceFile as workspaceFile
2122

2223

2324
__version__ = utils.__version__
@@ -79,3 +80,10 @@
7980
launchData = launch.getLaunchData()
8081
launchData = launch.addAllLaunchConfigurations(launchData)
8182
launch.overwriteLaunchFile(launchData)
83+
84+
# update workspace file with "cortex-debug" specifics
85+
wksFile = workspaceFile.UpdateWorkspaceFile()
86+
wksFile.checkWorkspaceFile()
87+
wksData = wksFile.getWorkspaceFileData()
88+
wksData = wksFile.addBuildDataToWorkspaceFile(wksData, buildData)
89+
wksFile.overwriteWorkspaceFile(wksData)

ideScripts/updatePaths.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ def __init__(self):
2323
(self.bStr.gccExePath, "arm-none-eabi-gcc.exe"),
2424
(self.bStr.buildToolsPath, "make.exe"),
2525
(self.bStr.openOCDPath, "openocd.exe"),
26-
(self.bStr.openOCDTargetPath, "STM target '*.cfg' file (example: stm32f0x.cfg)"),
27-
(self.bStr.stm32svdPath, "STM target '*.svd' file (example: STM32F0x8.svd)")
26+
(self.bStr.openOCDTargetPath, "STM target '*.cfg' file (example: ...scripts/target/stm32f0x.cfg)"),
27+
(self.bStr.stm32svdPath, "STM target '*.svd' file (example: .../Keil*/CMSIS/SVD/STM32F0x8.svd)")
2828
]
2929

3030
def forceUpdatePaths(self, buildData):

ideScripts/updateWorkspaceFile.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
'''
2+
Update existing workspace file with debug paths in "settings":
3+
- "cortex-debug.armToolchainPath"
4+
- "cortex-debug.openocdPath"
5+
'''
6+
import os
7+
import json
8+
import datetime
9+
10+
import utilities as utils
11+
import updatePaths as pth
12+
import updateBuildData as build
13+
14+
__version__ = utils.__version__
15+
16+
17+
class UpdateWorkspaceFile():
18+
def __init__(self):
19+
self.bStr = build.BuildDataStrings()
20+
21+
def checkWorkspaceFile(self):
22+
'''
23+
Check if workspace '*.code-workspace' file exists. If it does, check if it is a valid JSON file.
24+
If it doesn't exist report error and quit.
25+
'''
26+
workspaceFileName = utils.getWorkspaceName()
27+
if utils.fileFolderExists(utils.workspaceFilePath):
28+
# file exists, check if it loads OK
29+
try:
30+
with open(utils.workspaceFilePath, 'r') as workspaceFile:
31+
workspaceFileData = json.load(workspaceFile)
32+
33+
print("Existing " + workspaceFileName + " file found.")
34+
35+
except Exception as err:
36+
errorMsg = "Invalid " + workspaceFileName + " file.\n"
37+
errorMsg += "Possible cause: invalid json format or comments (not supported by this scripts). Error:\n"
38+
errorMsg += str(err)
39+
print(errorMsg)
40+
41+
# else: verified in 'utils.verifyFolderStructure()'
42+
43+
def getWorkspaceFileData(self):
44+
'''
45+
Get data from current '*.code-workspace' file.
46+
File existance is previoulsy checked in 'checkWorkspaceFile()'.
47+
'''
48+
with open(utils.workspaceFilePath, 'r') as workspaceFile:
49+
data = json.load(workspaceFile)
50+
51+
return data
52+
53+
def addBuildDataToWorkspaceFile(self, workspaceData, buildData):
54+
'''
55+
This function ads "cortex-debug.*" items to workspace file, if they don't exist yet.
56+
Returns new data.
57+
'''
58+
armToolchainPath = os.path.dirname(buildData[self.bStr.gccExePath])
59+
armToolchainPath = utils.pathWithForwardSlashes(armToolchainPath)
60+
61+
workspaceData["settings"]["cortex-debug.armToolchainPath"] = armToolchainPath
62+
workspaceData["settings"]["cortex-debug.openocdPath"] = buildData[self.bStr.openOCDPath]
63+
64+
return workspaceData
65+
66+
def overwriteWorkspaceFile(self, data):
67+
'''
68+
Overwrite existing '*.code-workspace' file with new data.
69+
'''
70+
try:
71+
with open(utils.workspaceFilePath, 'r+') as workspaceFile:
72+
workspaceFile.seek(0)
73+
workspaceFile.truncate()
74+
dataToWrite = json.dumps(data, indent=4, sort_keys=False)
75+
workspaceFile.write(dataToWrite)
76+
77+
print("'*.code-workspace' file updated!")
78+
79+
except Exception as err:
80+
errorMsg = "Exception error overwriting '*.code-workspace' file:\n"
81+
errorMsg += str(err)
82+
utils.printAndQuit(errorMsg)
83+
84+
85+
86+
########################################################################################################################
87+
if __name__ == "__main__":
88+
utils.verifyFolderStructure()
89+
90+
paths = pth.UpdatePaths()
91+
bData = build.BuildData()
92+
wksFile = UpdateWorkspaceFile()
93+
94+
# build data (update tools paths if neccessary)
95+
bData.checkBuildDataFile()
96+
buildData = bData.getBuildData()
97+
if not paths.verifyExistingPaths(buildData):
98+
buildData = paths.forceUpdatePaths(buildData)
99+
bData.overwriteBuildDataFile(buildData)
100+
101+
wksFile.checkWorkspaceFile()
102+
wksData = wksFile.getWorkspaceFileData()
103+
wksData = wksFile.addBuildDataToWorkspaceFile(wksData, buildData)
104+
105+
wksFile.overwriteWorkspaceFile(wksData)

0 commit comments

Comments
 (0)