FancyZones CLI: migrate to System.CommandLine and centralize data I/O (#44344)

<!-- Enter a brief description/summary of your PR here. What does it
fix/what does it change/how was it tested (even manually, if necessary)?
-->
## Summary of the Pull Request
This PR refactors FancyZones CLI to use System.CommandLine and
consolidates all FancyZones JSON config read/write through
FancyZonesEditorCommon data models and FancyZonesDataIO, so CLI and
Editor share the same serialization and file paths.

For detailed command definitions, see PR #44078
<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist

- [ ] Closes: #xxx
<!-- - [ ] Closes: #yyy (add separate lines for additional resolved
issues) -->
- [ ] **Communication:** I've discussed this with core contributors
already. If the work hasn't been agreed, this work might be rejected
- [ ] **Tests:** Added/updated and all pass
- [ ] **Localization:** All end-user-facing strings can be localized
- [ ] **Dev docs:** Added/updated
- [ ] **New binaries:** Added on the required places
- [ ] [JSON for
signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json)
for new binaries
- [ ] [WXS for
installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs)
for new binaries and localization folder
- [ ] [YML for CI
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml)
for new test projects
- [ ] [YML for signed
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml)
- [ ] **Documentation updated:** If checked, please file a pull request
on [our docs
repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys)
and link it here: #xxx

<!-- Provide a more detailed description of the PR, other things fixed,
or any additional comments/features here -->
## Detailed Description of the Pull Request / Additional comments

<!-- Describe how you validated the behavior. Add automated tests
wherever possible, but list manual validation steps taken as well -->
## Validation Steps Performed
This commit is contained in:
leileizhang
2025-12-19 11:53:43 +08:00
committed by GitHub
parent 7cd201d355
commit dd138fb94b
37 changed files with 1513 additions and 899 deletions

View File

@@ -12,7 +12,7 @@ namespace FancyZonesEditorCommon.Data
{
get
{
return GetDataFolder() + "\\Microsoft\\PowerToys\\FancyZones\\applied-layouts.json";
return FancyZonesPaths.AppliedLayouts;
}
}

View File

@@ -15,7 +15,7 @@ namespace FancyZonesEditorCommon.Data
{
get
{
return GetDataFolder() + "\\Microsoft\\PowerToys\\FancyZones\\custom-layouts.json";
return FancyZonesPaths.CustomLayouts;
}
}

View File

@@ -14,7 +14,7 @@ namespace FancyZonesEditorCommon.Data
{
get
{
return GetDataFolder() + "\\Microsoft\\PowerToys\\FancyZones\\default-layouts.json";
return FancyZonesPaths.DefaultLayouts;
}
}

View File

@@ -14,7 +14,7 @@ namespace FancyZonesEditorCommon.Data
{
get
{
return GetDataFolder() + "\\Microsoft\\PowerToys\\FancyZones\\editor-parameters.json";
return FancyZonesPaths.EditorParameters;
}
}

View File

@@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
namespace FancyZonesEditorCommon.Data;
/// <summary>
/// Provides paths to FancyZones configuration files.
/// </summary>
public static class FancyZonesPaths
{
private static readonly string DataPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"Microsoft",
"PowerToys",
"FancyZones");
public static string AppliedLayouts => Path.Combine(DataPath, "applied-layouts.json");
public static string CustomLayouts => Path.Combine(DataPath, "custom-layouts.json");
public static string LayoutTemplates => Path.Combine(DataPath, "layout-templates.json");
public static string LayoutHotkeys => Path.Combine(DataPath, "layout-hotkeys.json");
public static string EditorParameters => Path.Combine(DataPath, "editor-parameters.json");
public static string DefaultLayouts => Path.Combine(DataPath, "default-layouts.json");
}

View File

@@ -14,7 +14,7 @@ namespace FancyZonesEditorCommon.Data
{
get
{
return GetDataFolder() + "\\Microsoft\\PowerToys\\FancyZones\\layout-hotkeys.json";
return FancyZonesPaths.LayoutHotkeys;
}
}

View File

@@ -14,7 +14,7 @@ namespace FancyZonesEditorCommon.Data
{
get
{
return GetDataFolder() + "\\Microsoft\\PowerToys\\FancyZones\\layout-templates.json";
return FancyZonesPaths.LayoutTemplates;
}
}

View File

@@ -0,0 +1,154 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
using FancyZonesEditorCommon.Data;
namespace FancyZonesEditorCommon.Utils
{
/// <summary>
/// Unified helper for all FancyZones data file I/O operations.
/// Centralizes reading and writing of all JSON configuration files.
/// </summary>
public static class FancyZonesDataIO
{
private static TWrapper ReadData<TData, TWrapper>(
Func<TData> createInstance,
Func<TData, string> fileSelector,
Func<TData, string, TWrapper> readFunc)
{
var instance = createInstance();
string filePath = fileSelector(instance);
if (!File.Exists(filePath))
{
throw new FileNotFoundException($"File not found: {Path.GetFileName(filePath)}", filePath);
}
return readFunc(instance, filePath);
}
private static void WriteData<TData, TWrapper>(
Func<TData> createInstance,
Func<TData, string> fileSelector,
Func<TData, TWrapper, string> serializeFunc,
TWrapper data)
{
var instance = createInstance();
var filePath = fileSelector(instance);
IOUtils ioUtils = new IOUtils();
ioUtils.WriteFile(filePath, serializeFunc(instance, data));
}
// AppliedLayouts operations
public static AppliedLayouts.AppliedLayoutsListWrapper ReadAppliedLayouts()
{
return ReadData(
() => new AppliedLayouts(),
instance => instance.File,
(instance, file) => instance.Read(file));
}
public static void WriteAppliedLayouts(AppliedLayouts.AppliedLayoutsListWrapper data)
{
WriteData(
() => new AppliedLayouts(),
instance => instance.File,
(instance, wrapper) => instance.Serialize(wrapper),
data);
}
// CustomLayouts operations
public static CustomLayouts.CustomLayoutListWrapper ReadCustomLayouts()
{
return ReadData(
() => new CustomLayouts(),
instance => instance.File,
(instance, file) => instance.Read(file));
}
public static void WriteCustomLayouts(CustomLayouts.CustomLayoutListWrapper data)
{
WriteData(
() => new CustomLayouts(),
instance => instance.File,
(instance, wrapper) => instance.Serialize(wrapper),
data);
}
// LayoutTemplates operations
public static LayoutTemplates.TemplateLayoutsListWrapper ReadLayoutTemplates()
{
return ReadData(
() => new LayoutTemplates(),
instance => instance.File,
(instance, file) => instance.Read(file));
}
public static void WriteLayoutTemplates(LayoutTemplates.TemplateLayoutsListWrapper data)
{
WriteData(
() => new LayoutTemplates(),
instance => instance.File,
(instance, wrapper) => instance.Serialize(wrapper),
data);
}
// LayoutHotkeys operations
public static LayoutHotkeys.LayoutHotkeysWrapper ReadLayoutHotkeys()
{
return ReadData(
() => new LayoutHotkeys(),
instance => instance.File,
(instance, file) => instance.Read(file));
}
public static void WriteLayoutHotkeys(LayoutHotkeys.LayoutHotkeysWrapper data)
{
WriteData(
() => new LayoutHotkeys(),
instance => instance.File,
(instance, wrapper) => instance.Serialize(wrapper),
data);
}
// EditorParameters operations
public static EditorParameters.ParamsWrapper ReadEditorParameters()
{
return ReadData(
() => new EditorParameters(),
instance => instance.File,
(instance, file) => instance.Read(file));
}
public static void WriteEditorParameters(EditorParameters.ParamsWrapper data)
{
WriteData(
() => new EditorParameters(),
instance => instance.File,
(instance, wrapper) => instance.Serialize(wrapper),
data);
}
// DefaultLayouts operations
public static DefaultLayouts.DefaultLayoutsListWrapper ReadDefaultLayouts()
{
return ReadData(
() => new DefaultLayouts(),
instance => instance.File,
(instance, file) => instance.Read(file));
}
public static void WriteDefaultLayouts(DefaultLayouts.DefaultLayoutsListWrapper data)
{
WriteData(
() => new DefaultLayouts(),
instance => instance.File,
(instance, wrapper) => instance.Serialize(wrapper),
data);
}
}
}