win, linux: improve VSCode setting robustness #196
This commit enhances the robustness of setting VSCode configurations,
ensuring consistent and reliable operation even in edge cases, such as
when the settings file is empty. This commit also uniforms behavior of
Linux and Windows modification of VSCode settings.
On Windows:
- Move parameters to on top of scripts to be able to easily test the
scripts using PowerShell without compiling.
- Add a check to exit the script with an error message if the attempt to
parse the JSON content fails.
- Omit the `OutString` cmdlet from the pipeline in the script for
converting JSON file content to a PowerShell object. `Out-String` is
unnecessary in this context because `Get-Content` already outputs the
file content as a string array, which `ConvertFrom-Json` effectively.
Additionally, using `Out-String` could potentially introduce issues by
concatenating file content into a single string, causing
`ConvertFrom-Json` to fail when processing pretty-printed JSON. By
removing `Out-String`, the script is streamlined and potential errors
are avoided.
- Add logic to handle empty settings file. Add an additional check for
empty settings file, if the file is empty, the script writes a default
empty JSON object (`{}`) to the file. The operation is logged to
ensure transparency, notifying the user of the action taken. This
change removes fails due to empty setting files.
- When reverting, do not fail if the setting file is missing because it
means that default settings are already in-place.
- When reverting, show informative message if the key does not exist or
does not have the value set by privacy.sexy and do not take any
further action.
- If the desired value is already set, show a message for it and skip
updating the setting file.
On Linux:
- Handles empty `settings.json` similarly to Windows.
- Add more user friendly error if JSON file cannot be parsed.
This commit is contained in:
@@ -3183,7 +3183,7 @@ functions:
|
||||
parameters:
|
||||
code: |-
|
||||
from pathlib import Path
|
||||
import os, json
|
||||
import os, json, sys
|
||||
property_name = '{{ $setting }}'
|
||||
target = json.loads('{{ $jsonValue }}')
|
||||
home_dir = f'/home/{os.getenv("SUDO_USER", os.getenv("USER"))}'
|
||||
@@ -3200,7 +3200,15 @@ functions:
|
||||
continue
|
||||
print(f'Reading file at "{settings_file}".')
|
||||
file_content = file.read_text()
|
||||
json_object = json.loads(file_content)
|
||||
if not file_content.strip():
|
||||
print('Settings file is empty. Treating it as default empty JSON object.')
|
||||
file_content = '{}'
|
||||
json_object = None
|
||||
try:
|
||||
json_object = json.loads(file_content)
|
||||
except json.JSONDecodeError:
|
||||
print(f'Error, invalid JSON format in the settings file: "{settings_file}".', file=sys.stderr)
|
||||
continue
|
||||
if property_name not in json_object:
|
||||
print(f'Settings "{property_name}" is not configured.')
|
||||
else:
|
||||
@@ -3215,7 +3223,7 @@ functions:
|
||||
print(f'Successfully configured "{property_name}" to {json.dumps(target)}.')
|
||||
revertCode: |-
|
||||
from pathlib import Path
|
||||
import os, json
|
||||
import os, json, sys
|
||||
property_name = '{{ $setting }}'
|
||||
target = json.loads('{{ $jsonValue }}')
|
||||
home_dir = f'/home/{os.getenv("SUDO_USER", os.getenv("USER"))}'
|
||||
@@ -3232,7 +3240,14 @@ functions:
|
||||
continue
|
||||
print(f'Reading file at "{settings_file}".')
|
||||
file_content = file.read_text()
|
||||
json_object = json.loads(file_content)
|
||||
if not file_content.strip():
|
||||
print(f'Skipping, no need to revert because settings file is empty: "{settings_file}".')
|
||||
continue
|
||||
try:
|
||||
json_object = json.loads(file_content)
|
||||
except json.JSONDecodeError:
|
||||
print(f'Error, invalid JSON format in the settings file: "{settings_file}".', file=sys.stderr)
|
||||
continue
|
||||
if property_name not in json_object:
|
||||
print(f'Skipping, "{property_name}" is not configured.')
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user