Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/AppInstallerCLICore/AppInstallerCLICore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@
<ClInclude Include="Commands\DscCommandBase.h" />
<ClInclude Include="Commands\DscComposableObject.h" />
<ClInclude Include="Commands\DscTestFileResource.h" />
<ClInclude Include="Commands\DscTestJsonResource.h" />
<ClInclude Include="Commands\ErrorCommand.h" />
<ClInclude Include="Commands\ExperimentalCommand.h" />
<ClInclude Include="Commands\ExportCommand.h" />
Expand Down Expand Up @@ -391,6 +392,7 @@
<ClCompile Include="Commands\DscCommandBase.cpp" />
<ClCompile Include="Commands\DscComposableObject.cpp" />
<ClCompile Include="Commands\DscTestFileResource.cpp" />
<ClCompile Include="Commands\DscTestJsonResource.cpp" />
<ClCompile Include="Commands\ErrorCommand.cpp" />
<ClCompile Include="Commands\FontCommand.cpp" />
<ClCompile Include="Commands\ImportCommand.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions src/AppInstallerCLICore/AppInstallerCLICore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@
<ClInclude Include="Commands\DscComposableObject.h">
<Filter>Commands\Configuration</Filter>
</ClInclude>
<ClInclude Include="Commands\DscTestJsonResource.h">
<Filter>Commands\Configuration</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="pch.cpp">
Expand Down Expand Up @@ -529,6 +532,9 @@
<ClCompile Include="Commands\DscComposableObject.cpp">
<Filter>Commands\Configuration</Filter>
</ClCompile>
<ClCompile Include="Commands\DscTestJsonResource.cpp">
<Filter>Commands\Configuration</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="PropertySheet.props" />
Expand Down
2 changes: 2 additions & 0 deletions src/AppInstallerCLICore/Commands/DscCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#ifndef AICLI_DISABLE_TEST_HOOKS
#include "DscTestFileResource.h"
#include "DscTestJsonResource.h"
#endif

namespace AppInstaller::CLI
Expand All @@ -14,6 +15,7 @@ namespace AppInstaller::CLI
return InitializeFromMoveOnly<std::vector<std::unique_ptr<Command>>>({
#ifndef AICLI_DISABLE_TEST_HOOKS
std::make_unique<DscTestFileResource>(FullName()),
std::make_unique<DscTestJsonResource>(FullName()),
#endif
});
}
Expand Down
22 changes: 21 additions & 1 deletion src/AppInstallerCLICore/Commands/DscCommandBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ namespace AppInstaller::CLI
return false;
}

std::optional<std::string> GetReturnType(DscFunctionModifiers modifiers)
{
if (WI_IsFlagSet(modifiers, DscFunctionModifiers::ReturnsStateAndDiff))
{
return "stateAndDiff";
}

if (WI_IsFlagSet(modifiers, DscFunctionModifiers::ReturnsState))
{
return "state";
}

return std::nullopt;
}

Json::Value CreateJsonDefinitionFor(std::string_view name, DscFunctions function, DscFunctionModifiers modifiers)
{
THROW_HR_IF(E_INVALIDARG, !WI_IsSingleFlagSet(function));
Expand Down Expand Up @@ -131,7 +146,12 @@ namespace AppInstaller::CLI

if (FunctionSpecifiesReturn(function))
{
result["return"] = "stateAndDiff";
std::optional<std::string> returnType = GetReturnType(modifiers);

if (returnType)
{
result["return"] = returnType.value();
}
}

if (function == DscFunctions::Schema)
Expand Down
4 changes: 4 additions & 0 deletions src/AppInstallerCLICore/Commands/DscCommandBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ namespace AppInstaller::CLI
// The resource will act on the `_exist` property during Set (and WhatIf).
// If not provided, the resource should implement Delete.
HandlesExist = 0x02,
// Functions that may return state information (set, what-if, test) return only the state.
ReturnsState = 0x04,
// Functions that may return state information (set, what-if, test) return the state and property difference.
ReturnsStateAndDiff = 0x08,
};

DEFINE_ENUM_FLAG_OPERATORS(DscFunctionModifiers);
Expand Down
6 changes: 5 additions & 1 deletion src/AppInstallerCLICore/Commands/DscComposableObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ namespace AppInstaller::CLI

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

property["type"] = std::string{ type };
if (!type.empty())
{
property["type"] = std::string{ type };
}

property["description"] = std::string{ description };

propertiesObject[nameString] = std::move(property);
Expand Down
18 changes: 17 additions & 1 deletion src/AppInstallerCLICore/Commands/DscComposableObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#pragma once
#include <AppInstallerErrors.h>
#include <AppInstallerLanguageUtilities.h>
#include <AppInstallerLogging.h>
#include <json/json.h>
#include <optional>

Expand Down Expand Up @@ -69,6 +70,21 @@ namespace AppInstaller::CLI
}
};

template <>
struct GetJsonTypeValue<Json::Value>
{
static Json::Value Get(const Json::Value& value)
{
return value;
}

static std::string_view SchemaTypeName()
{
// Indicates that the schema should not set a type
return {};
}
};

// Template useful for composing objects for DSC resources.
// Properties should be of the shape:
//
Expand Down Expand Up @@ -140,7 +156,7 @@ namespace AppInstaller::CLI
{
if constexpr (WI_IsFlagSet(PropertyFlags, DscComposablePropertyFlag::Required))
{
THROW_HR(WINGET_CONFIG_ERROR_MISSING_FIELD);
THROW_HR_MSG(WINGET_CONFIG_ERROR_MISSING_FIELD, "Required property `%hs` not provided.", Derived::Name().data());
}
else
{
Expand Down
14 changes: 7 additions & 7 deletions src/AppInstallerCLICore/Commands/DscTestFileResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ namespace AppInstaller::CLI

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

struct FunctionData
struct TestFileFunctionData
{
FunctionData(const std::optional<Json::Value>& json) : Input(json), Output(Input.CopyForOutput())
TestFileFunctionData(const std::optional<Json::Value>& json) : Input(json), Output(Input.CopyForOutput())
{
Path = Utility::ConvertToUTF16(Input.Path().value());
THROW_HR_IF(E_INVALIDARG, !Path.is_absolute());
Expand Down Expand Up @@ -104,7 +104,7 @@ namespace AppInstaller::CLI
DscTestFileResource::DscTestFileResource(std::string_view parent) :
DscCommandBase(parent, "test-file", DscResourceKind::Resource,
DscFunctions::Get | DscFunctions::Set | DscFunctions::Test | DscFunctions::Export | DscFunctions::Schema,
DscFunctionModifiers::ImplementsPretest | DscFunctionModifiers::HandlesExist)
DscFunctionModifiers::ImplementsPretest | DscFunctionModifiers::HandlesExist | DscFunctionModifiers::ReturnsStateAndDiff)
{
}

Expand All @@ -127,7 +127,7 @@ namespace AppInstaller::CLI
{
if (auto json = GetJsonFromInput(context))
{
anon::FunctionData data{ json };
anon::TestFileFunctionData data{ json };

data.Get();

Expand All @@ -139,7 +139,7 @@ namespace AppInstaller::CLI
{
if (auto json = GetJsonFromInput(context))
{
anon::FunctionData data{ json };
anon::TestFileFunctionData data{ json };

data.Get();

Expand Down Expand Up @@ -186,7 +186,7 @@ namespace AppInstaller::CLI
{
if (auto json = GetJsonFromInput(context))
{
anon::FunctionData data{ json };
anon::TestFileFunctionData data{ json };

data.Get();
data.Output.InDesiredState(data.Test());
Expand All @@ -200,7 +200,7 @@ namespace AppInstaller::CLI
{
if (auto json = GetJsonFromInput(context))
{
anon::FunctionData data{ json };
anon::TestFileFunctionData data{ json };

if (std::filesystem::exists(data.Path))
{
Expand Down
Loading
Loading