Skip to content

Commit 1f9cf83

Browse files
author
Steve Lee (POWERSHELL HE/HIM) (from Dev Box)
committed
add support for set
1 parent 8f76f97 commit 1f9cf83

4 files changed

Lines changed: 79 additions & 6 deletions

File tree

resources/registry/locales/en-us.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ _version = 1
33
[adapter]
44
getProcessingKey = "Processing key: %{key}"
55
getNoAdaptedRegistryValueFound = "No adapted registry value found for key: %{key}"
6+
setProcessingKey = "Setting key: %{key}"
7+
setNoAdaptedRegistryValueFound = "No adapted registry value found for key: %{key}"
68
registryValueNotFound = "Registry value not found for key path: %{key_path} and value name: %{value_name}"
79
couldNotConvertRegistryValue = "Could not convert registry value for key path '%{key_path}' and value name '%{value_name}' to JSON type: %{json_type}"
810
unsupportedConversionToJsonType = "Unsupported conversion from %{registry_value_data} to JSON type: %{json_type}"

resources/registry/src/adapter.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,15 +298,37 @@ fn get_registry_value_data(value_name: &str, value: &Value, map: &Map<String, Va
298298
Ok(registry_value_data)
299299
}
300300

301-
pub fn adapter_set(input: &str, adapted_resource: &str) -> Result<String, RegistryResourceError> {
302-
trace!("Adapter Set with input: {input}");
301+
pub fn adapter_set(input: &str, adapted_resource: &str) -> Result<(), RegistryResourceError> {
303302
let adapted_resource: AdaptedRegistryResource = serde_json::from_str(adapted_resource)
304303
.map_err(|e| RegistryResourceError::AdaptedResource(e.to_string()))?;
305-
304+
let mut resource_map = HashMap::new();
305+
306306
for (key, value) in adapted_resource.properties.iter() {
307-
trace!("Property: {key} = {value}");
307+
let adapted_registry_value: AdaptedRegistryValue = serde_json::from_value(value.clone())
308+
.map_err(|e| RegistryResourceError::AdaptedResource(e.to_string()))?;
309+
resource_map.insert(key.clone(), adapted_registry_value);
308310
}
309-
Ok("{}".to_string())
311+
312+
let input_map: Map<String, Value> = serde_json::from_str(input)
313+
.map_err(|e| RegistryResourceError::AdaptedResource(e.to_string()))?;
314+
for (key, value) in input_map.iter() {
315+
if let Some(adapted_registry_value) = resource_map.get(key) {
316+
debug!("{}", t!("adapter.setProcessingKey", key = key));
317+
if let Some(json_map) = adapted_registry_value.map_json_to_registry.as_object() {
318+
let registry_data = get_registry_value_data(&adapted_registry_value.value_name, value, json_map, &adapted_registry_value.value_type)?;
319+
let registry_helper = RegistryHelper::new(&adapted_registry_value.key_path, Some(adapted_registry_value.value_name.clone()), Some(registry_data))?;
320+
if let Err(e) = registry_helper.set() {
321+
return Err(RegistryResourceError::RegistryError(e));
322+
}
323+
} else {
324+
warn!("No mapping found for key {}", key);
325+
}
326+
} else {
327+
debug!("{}", t!("adapter.setNoAdaptedRegistryValueFound", key = key));
328+
}
329+
}
330+
331+
Ok(())
310332
}
311333

312334
pub fn adapter_export(input: &str, adapted_resource: &str) -> Result<String, RegistryResourceError> {

resources/registry/src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ fn main() {
4343
adapter_get(&input, &adapted_resource)
4444
},
4545
AdapterSubCommand::Set { input, adapted_resource } => {
46-
adapter_set(&input, &adapted_resource)
46+
if let Err(e) = adapter_set(&input, &adapted_resource) {
47+
error!("{e}");
48+
exit(EXIT_REGISTRY_ERROR);
49+
}
50+
exit(EXIT_SUCCESS);
4751
},
4852
AdapterSubCommand::Export { input, adapted_resource } => {
4953
adapter_export(&input, &adapted_resource)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
Describe 'Personalization resource set tests' -Skip:(!$IsWindows) {
5+
BeforeAll {
6+
$currentSettings = dsc resource export -r Microsoft.Windows/Personalization 2>$TestDrive/error.log
7+
$LASTEXITCODE | Should -Be 0 -Because "Failed to export current personalization settings with exit code $LASTEXITCODE. Error log: $(Get-Content $TestDrive/error.log -Raw)"
8+
}
9+
10+
AfterAll {
11+
dsc resource set -r Microsoft.Windows/Personalization -i $currentSettings 2>$TestDrive/error.log
12+
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.log -Raw)
13+
}
14+
15+
It 'Can set personalization settings' {
16+
$newSettings = @{
17+
appsUseLightTheme = $true
18+
systemUsesLightTheme = $true
19+
autoColorization = $true
20+
colorPrevalence = $true
21+
transparencyEffects = $true
22+
startMenuVisiblePlaces = @('Documents', 'Downloads','Music', 'Pictures', 'Videos', 'Network', 'UserProfile', 'Explorer', 'Settings')
23+
startMenuShowRecentList = $true
24+
showRecommendedApps = $true
25+
taskbarShowBadges = $true
26+
desktopTaskbarShowBadges = $true
27+
multimonitorTaskbarGroupingMode = 'CombineWhenTaskbarIsFull'
28+
multimonitorTaskbar = $true
29+
multimonitorDesktopTaskbar = $true
30+
multimonitorTaskbarMode = 'PrimaryAndMonitorWindowIsOn'
31+
multimonitorDesktopTaskbarMode = 'MonitorWindowIsOn'
32+
}
33+
34+
dsc resource set -r Microsoft.Windows/Personalization -i ($newSettings | ConvertTo-Json) 2>$TestDrive/error.log
35+
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.log -Raw)
36+
37+
$out = dsc resource export -r Microsoft.Windows/Personalization 2>$TestDrive/error.log | ConvertFrom-Json
38+
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.log -Raw)
39+
foreach ($key in $newSettings.Keys) {
40+
$keyValue = $out.resources[0].properties.$key
41+
$expectedValue = $newSettings.$key
42+
$keyValue | Should -Be $expectedValue -Because "Property $key has value $keyValue which is not equal to expected value $expectedValue"
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)