Skip to content

Commit 511d4f9

Browse files
authored
DSC v3 Export (#5319)
## Change Adds a more generic `GetAllUnits(Async)` export function to the `ConfigurationProcessor`. This produces full configuration units, which may then differ from the given unit's type. This models the DSC v3 `Exporter` resource type. The implementation of this function prefers the full implementation from the processor, but if not provided it can fall back to the previous `GetAllSettings` function and synthesize results. Implements both the "all settings" and "all units" functionality for the DSC v3 processor. This in turn is done through the only DSC v3 option, `export`.
1 parent bf264aa commit 511d4f9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1336
-37
lines changed

src/AppInstallerCLICore/AppInstallerCLICore.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@
306306
<ClInclude Include="Commands\DscCommandBase.h" />
307307
<ClInclude Include="Commands\DscComposableObject.h" />
308308
<ClInclude Include="Commands\DscTestFileResource.h" />
309+
<ClInclude Include="Commands\DscTestJsonResource.h" />
309310
<ClInclude Include="Commands\ErrorCommand.h" />
310311
<ClInclude Include="Commands\ExperimentalCommand.h" />
311312
<ClInclude Include="Commands\ExportCommand.h" />
@@ -391,6 +392,7 @@
391392
<ClCompile Include="Commands\DscCommandBase.cpp" />
392393
<ClCompile Include="Commands\DscComposableObject.cpp" />
393394
<ClCompile Include="Commands\DscTestFileResource.cpp" />
395+
<ClCompile Include="Commands\DscTestJsonResource.cpp" />
394396
<ClCompile Include="Commands\ErrorCommand.cpp" />
395397
<ClCompile Include="Commands\FontCommand.cpp" />
396398
<ClCompile Include="Commands\ImportCommand.cpp" />

src/AppInstallerCLICore/AppInstallerCLICore.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@
281281
<ClInclude Include="Commands\DscComposableObject.h">
282282
<Filter>Commands\Configuration</Filter>
283283
</ClInclude>
284+
<ClInclude Include="Commands\DscTestJsonResource.h">
285+
<Filter>Commands\Configuration</Filter>
286+
</ClInclude>
284287
</ItemGroup>
285288
<ItemGroup>
286289
<ClCompile Include="pch.cpp">
@@ -529,6 +532,9 @@
529532
<ClCompile Include="Commands\DscComposableObject.cpp">
530533
<Filter>Commands\Configuration</Filter>
531534
</ClCompile>
535+
<ClCompile Include="Commands\DscTestJsonResource.cpp">
536+
<Filter>Commands\Configuration</Filter>
537+
</ClCompile>
532538
</ItemGroup>
533539
<ItemGroup>
534540
<None Include="PropertySheet.props" />

src/AppInstallerCLICore/Commands/DscCommand.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#ifndef AICLI_DISABLE_TEST_HOOKS
77
#include "DscTestFileResource.h"
8+
#include "DscTestJsonResource.h"
89
#endif
910

1011
namespace AppInstaller::CLI
@@ -14,6 +15,7 @@ namespace AppInstaller::CLI
1415
return InitializeFromMoveOnly<std::vector<std::unique_ptr<Command>>>({
1516
#ifndef AICLI_DISABLE_TEST_HOOKS
1617
std::make_unique<DscTestFileResource>(FullName()),
18+
std::make_unique<DscTestJsonResource>(FullName()),
1719
#endif
1820
});
1921
}

src/AppInstallerCLICore/Commands/DscCommandBase.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ namespace AppInstaller::CLI
9292
return false;
9393
}
9494

95+
std::optional<std::string> GetReturnType(DscFunctionModifiers modifiers)
96+
{
97+
if (WI_IsFlagSet(modifiers, DscFunctionModifiers::ReturnsStateAndDiff))
98+
{
99+
return "stateAndDiff";
100+
}
101+
102+
if (WI_IsFlagSet(modifiers, DscFunctionModifiers::ReturnsState))
103+
{
104+
return "state";
105+
}
106+
107+
return std::nullopt;
108+
}
109+
95110
Json::Value CreateJsonDefinitionFor(std::string_view name, DscFunctions function, DscFunctionModifiers modifiers)
96111
{
97112
THROW_HR_IF(E_INVALIDARG, !WI_IsSingleFlagSet(function));
@@ -131,7 +146,12 @@ namespace AppInstaller::CLI
131146

132147
if (FunctionSpecifiesReturn(function))
133148
{
134-
result["return"] = "stateAndDiff";
149+
std::optional<std::string> returnType = GetReturnType(modifiers);
150+
151+
if (returnType)
152+
{
153+
result["return"] = returnType.value();
154+
}
135155
}
136156

137157
if (function == DscFunctions::Schema)

src/AppInstallerCLICore/Commands/DscCommandBase.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ namespace AppInstaller::CLI
5858
// The resource will act on the `_exist` property during Set (and WhatIf).
5959
// If not provided, the resource should implement Delete.
6060
HandlesExist = 0x02,
61+
// Functions that may return state information (set, what-if, test) return only the state.
62+
ReturnsState = 0x04,
63+
// Functions that may return state information (set, what-if, test) return the state and property difference.
64+
ReturnsStateAndDiff = 0x08,
6165
};
6266

6367
DEFINE_ENUM_FLAG_OPERATORS(DscFunctionModifiers);

src/AppInstallerCLICore/Commands/DscComposableObject.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ namespace AppInstaller::CLI
4545

4646
Json::Value property{ Json::ValueType::objectValue };
4747

48-
property["type"] = std::string{ type };
48+
if (!type.empty())
49+
{
50+
property["type"] = std::string{ type };
51+
}
52+
4953
property["description"] = std::string{ description };
5054

5155
propertiesObject[nameString] = std::move(property);

src/AppInstallerCLICore/Commands/DscComposableObject.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#pragma once
44
#include <AppInstallerErrors.h>
55
#include <AppInstallerLanguageUtilities.h>
6+
#include <AppInstallerLogging.h>
67
#include <json/json.h>
78
#include <optional>
89

@@ -69,6 +70,21 @@ namespace AppInstaller::CLI
6970
}
7071
};
7172

73+
template <>
74+
struct GetJsonTypeValue<Json::Value>
75+
{
76+
static Json::Value Get(const Json::Value& value)
77+
{
78+
return value;
79+
}
80+
81+
static std::string_view SchemaTypeName()
82+
{
83+
// Indicates that the schema should not set a type
84+
return {};
85+
}
86+
};
87+
7288
// Template useful for composing objects for DSC resources.
7389
// Properties should be of the shape:
7490
//
@@ -140,7 +156,7 @@ namespace AppInstaller::CLI
140156
{
141157
if constexpr (WI_IsFlagSet(PropertyFlags, DscComposablePropertyFlag::Required))
142158
{
143-
THROW_HR(WINGET_CONFIG_ERROR_MISSING_FIELD);
159+
THROW_HR_MSG(WINGET_CONFIG_ERROR_MISSING_FIELD, "Required property `%hs` not provided.", Derived::Name().data());
144160
}
145161
else
146162
{

src/AppInstallerCLICore/Commands/DscTestFileResource.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ namespace AppInstaller::CLI
1515

1616
using TestFileObject = DscComposableObject<StandardExistProperty, StandardInDesiredStateProperty, PathProperty, ContentProperty>;
1717

18-
struct FunctionData
18+
struct TestFileFunctionData
1919
{
20-
FunctionData(const std::optional<Json::Value>& json) : Input(json), Output(Input.CopyForOutput())
20+
TestFileFunctionData(const std::optional<Json::Value>& json) : Input(json), Output(Input.CopyForOutput())
2121
{
2222
Path = Utility::ConvertToUTF16(Input.Path().value());
2323
THROW_HR_IF(E_INVALIDARG, !Path.is_absolute());
@@ -104,7 +104,7 @@ namespace AppInstaller::CLI
104104
DscTestFileResource::DscTestFileResource(std::string_view parent) :
105105
DscCommandBase(parent, "test-file", DscResourceKind::Resource,
106106
DscFunctions::Get | DscFunctions::Set | DscFunctions::Test | DscFunctions::Export | DscFunctions::Schema,
107-
DscFunctionModifiers::ImplementsPretest | DscFunctionModifiers::HandlesExist)
107+
DscFunctionModifiers::ImplementsPretest | DscFunctionModifiers::HandlesExist | DscFunctionModifiers::ReturnsStateAndDiff)
108108
{
109109
}
110110

@@ -127,7 +127,7 @@ namespace AppInstaller::CLI
127127
{
128128
if (auto json = GetJsonFromInput(context))
129129
{
130-
anon::FunctionData data{ json };
130+
anon::TestFileFunctionData data{ json };
131131

132132
data.Get();
133133

@@ -139,7 +139,7 @@ namespace AppInstaller::CLI
139139
{
140140
if (auto json = GetJsonFromInput(context))
141141
{
142-
anon::FunctionData data{ json };
142+
anon::TestFileFunctionData data{ json };
143143

144144
data.Get();
145145

@@ -186,7 +186,7 @@ namespace AppInstaller::CLI
186186
{
187187
if (auto json = GetJsonFromInput(context))
188188
{
189-
anon::FunctionData data{ json };
189+
anon::TestFileFunctionData data{ json };
190190

191191
data.Get();
192192
data.Output.InDesiredState(data.Test());
@@ -200,7 +200,7 @@ namespace AppInstaller::CLI
200200
{
201201
if (auto json = GetJsonFromInput(context))
202202
{
203-
anon::FunctionData data{ json };
203+
anon::TestFileFunctionData data{ json };
204204

205205
if (std::filesystem::exists(data.Path))
206206
{

0 commit comments

Comments
 (0)