Skip to content

Commit a8efbae

Browse files
committed
Package resource property parity
1 parent f7ec554 commit a8efbae

File tree

4 files changed

+91
-93
lines changed

4 files changed

+91
-93
lines changed

src/AppInstallerCLICore/Commands/DscComposableObject.cpp

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@
55

66
namespace AppInstaller::CLI
77
{
8+
namespace
9+
{
10+
std::string GetTypeString(Json::ValueType type)
11+
{
12+
switch (type)
13+
{
14+
case Json::nullValue:
15+
return "null";
16+
case Json::intValue:
17+
case Json::uintValue:
18+
case Json::realValue:
19+
return "number";
20+
case Json::stringValue:
21+
return "string";
22+
case Json::booleanValue:
23+
return "boolean";
24+
case Json::arrayValue:
25+
return "array";
26+
case Json::objectValue:
27+
return "object";
28+
default:
29+
THROW_HR(E_UNEXPECTED);
30+
}
31+
}
32+
}
33+
834
namespace details
935
{
1036
const Json::Value* GetProperty(const Json::Value& object, std::string_view name)
@@ -32,7 +58,14 @@ namespace AppInstaller::CLI
3258
return result;
3359
}
3460

35-
void AddPropertySchema(Json::Value& object, std::string_view name, DscComposablePropertyFlag flags, std::string_view type, std::string_view description)
61+
void AddPropertySchema(
62+
Json::Value& object,
63+
std::string_view name,
64+
DscComposablePropertyFlag flags,
65+
Json::ValueType type,
66+
std::string_view description,
67+
const std::vector<std::string>& enumValues,
68+
const std::optional<std::string>& defaultValue)
3669
{
3770
Json::Value& propertiesObject = object["properties"];
3871

@@ -45,13 +78,30 @@ namespace AppInstaller::CLI
4578

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

48-
if (!type.empty())
81+
if (type != Json::ValueType::objectValue)
4982
{
50-
property["type"] = std::string{ type };
83+
property["type"] = GetTypeString(type);
5184
}
5285

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

88+
if (!enumValues.empty())
89+
{
90+
Json::Value enumArray{ Json::ValueType::arrayValue };
91+
92+
for (const std::string& enumValue : enumValues)
93+
{
94+
enumArray.append(enumValue);
95+
}
96+
97+
property["enum"] = std::move(enumArray);
98+
}
99+
100+
if (defaultValue)
101+
{
102+
property["default"] = defaultValue.value();
103+
}
104+
55105
propertiesObject[nameString] = std::move(property);
56106

57107
if (WI_IsFlagSet(flags, DscComposablePropertyFlag::Required))

src/AppInstallerCLICore/Commands/DscComposableObject.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace AppInstaller::CLI
3939
Json::Value& object,
4040
std::string_view name,
4141
DscComposablePropertyFlag flags,
42-
std::string_view type,
42+
Json::ValueType type,
4343
std::string_view description,
4444
const std::vector<std::string>& enumValues,
4545
const std::optional<std::string>& defaultValue);
@@ -59,9 +59,9 @@ namespace AppInstaller::CLI
5959
return value.asBool();
6060
}
6161

62-
static std::string_view SchemaTypeName()
62+
static Json::ValueType SchemaType()
6363
{
64-
return "boolean"sv;
64+
return Json::ValueType::booleanValue;
6565
}
6666
};
6767

@@ -73,9 +73,9 @@ namespace AppInstaller::CLI
7373
return value.asString();
7474
}
7575

76-
static std::string_view SchemaTypeName()
76+
static Json::ValueType SchemaType()
7777
{
78-
return "string"sv;
78+
return Json::ValueType::stringValue;
7979
}
8080
};
8181

@@ -87,10 +87,9 @@ namespace AppInstaller::CLI
8787
return value;
8888
}
8989

90-
static std::string_view SchemaTypeName()
90+
static Json::ValueType SchemaType()
9191
{
92-
// Indicates that the schema should not set a type
93-
return {};
92+
return Json::ValueType::objectValue;
9493
}
9594
};
9695

@@ -144,7 +143,7 @@ namespace AppInstaller::CLI
144143
static Json::Value Schema(const std::string& title)
145144
{
146145
Json::Value result = details::GetBaseSchema(title);
147-
(FoldHelper{}, ..., details::AddPropertySchema(result, Property::Name(), Property::Flags, GetJsonTypeValue<typename Property::Type>::SchemaTypeName(), Property::Description(), Property::EnumValues(), Property::Default()));
146+
(FoldHelper{}, ..., details::AddPropertySchema(result, Property::Name(), Property::Flags, GetJsonTypeValue<typename Property::Type>::SchemaType(), Property::Description(), Property::EnumValues(), Property::Default()));
148147
return result;
149148
}
150149
};
@@ -204,8 +203,8 @@ namespace AppInstaller::CLI
204203
{ \
205204
static std::string_view Name() { return _json_name_; } \
206205
static std::string_view Description() { return _description_; } \
207-
static std::vector<Type> EnumValues() { return std::vector<Type> _enum_vals_; } \
208-
static std::optional<Type> Default() { return _default_; } \
206+
static std::vector<std::string> EnumValues() { return std::vector<std::string> _enum_vals_; } \
207+
static std::optional<std::string> Default() { return _default_; } \
209208
std::optional<Type>& _property_name_() { return m_value; } \
210209
const std::optional<Type>& _property_name_() const { return m_value; } \
211210
void _property_name_(std::optional<Type> value) { m_value = std::move(value); } \
@@ -220,6 +219,9 @@ namespace AppInstaller::CLI
220219
#define WINGET_DSC_DEFINE_COMPOSABLE_PROPERTY_FLAGS(_property_type_, _value_type_, _property_name_, _json_name_, _flags_, _description_) \
221220
WINGET_DSC_DEFINE_COMPOSABLE_PROPERTY_IMPL(_property_type_, _value_type_, _property_name_, _json_name_, _flags_, _description_, {}, {})
222221

222+
#define WINGET_DSC_DEFINE_COMPOSABLE_PROPERTY_DEFAULT(_property_type_, _value_type_, _property_name_, _json_name_, _description_, _default_) \
223+
WINGET_DSC_DEFINE_COMPOSABLE_PROPERTY_IMPL(_property_type_, _value_type_, _property_name_, _json_name_, DscComposablePropertyFlag::None, _description_, {}, _default_)
224+
223225
#define WINGET_DSC_DEFINE_COMPOSABLE_PROPERTY_ENUM(_property_type_, _value_type_, _property_name_, _json_name_, _description_, _enum_vals_, _default_) \
224226
WINGET_DSC_DEFINE_COMPOSABLE_PROPERTY_IMPL(_property_type_, _value_type_, _property_name_, _json_name_, DscComposablePropertyFlag::None, _description_, _enum_vals_, _default_)
225227

src/AppInstallerCLICore/Commands/DscPackageResource.cpp

Lines changed: 24 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ namespace AppInstaller::CLI
1515
WINGET_DSC_DEFINE_COMPOSABLE_PROPERTY_FLAGS(SourceProperty, std::string, Source, "source", DscComposablePropertyFlag::CopyToOutput, "The source of the package.");
1616
WINGET_DSC_DEFINE_COMPOSABLE_PROPERTY(VersionProperty, std::string, Version, "version", "The version of the package.");
1717
WINGET_DSC_DEFINE_COMPOSABLE_PROPERTY_ENUM(MatchOptionProperty, std::string, MatchOption, "matchOption", "The method for matching the identifier with a package.", ({ "equals", "equalsCaseInsensitive", "startsWithCaseInsensitive", "containsCaseInsensitive" }), "equalsCaseInsensitive");
18+
WINGET_DSC_DEFINE_COMPOSABLE_PROPERTY_DEFAULT(UseLatestProperty, bool, UseLatest, "useLatest", "Indicate that the latest available version of the package should be installed.", "false");
19+
WINGET_DSC_DEFINE_COMPOSABLE_PROPERTY_ENUM(InstallModeProperty, std::string, InstallMode, "installMode", "The install mode to use if needed.", ({ "default", "silent", "interactive" }), "silent");
1820

19-
using PackageResourceObject = DscComposableObject<StandardExistProperty, StandardInDesiredStateProperty, IdProperty, SourceProperty>;
21+
using PackageResourceObject = DscComposableObject<StandardExistProperty, StandardInDesiredStateProperty, IdProperty, SourceProperty, VersionProperty, MatchOptionProperty, UseLatestProperty, InstallModeProperty>;
2022

2123
struct PackageFunctionData
2224
{
@@ -30,17 +32,7 @@ namespace AppInstaller::CLI
3032
// Fills the Output object with the current state
3133
void Get()
3234
{
33-
if (std::filesystem::exists(Path) && std::filesystem::is_regular_file(Path))
34-
{
35-
Output.Exist(true);
36-
37-
std::ifstream stream{ Path, std::ios::binary };
38-
Output.Content(Utility::ReadEntireStream(stream));
39-
}
40-
else
41-
{
42-
Output.Exist(false);
43-
}
35+
THROW_HR(E_NOTIMPL);
4436
}
4537

4638
// Determines if the current Output values match the Input values state.
@@ -53,7 +45,7 @@ namespace AppInstaller::CLI
5345
{
5446
if (Output.Exist().value())
5547
{
56-
return ContentMatches();
48+
THROW_HR(E_NOTIMPL);
5749
}
5850
else
5951
{
@@ -79,25 +71,11 @@ namespace AppInstaller::CLI
7971
}
8072
else
8173
{
82-
if (!ContentMatches())
83-
{
84-
result.append(std::string{ ContentProperty::Name() });
85-
}
74+
THROW_HR(E_NOTIMPL);
8675
}
8776

8877
return result;
8978
}
90-
91-
private:
92-
bool ContentMatches()
93-
{
94-
bool hasInput = Input.Content().has_value() && !Input.Content().value().empty();
95-
bool hasOutput = Output.Content().has_value() && !Output.Content().value().empty();
96-
97-
return
98-
(hasInput && hasOutput && Input.Content().value() == Output.Content().value()) ||
99-
(!hasInput && !hasOutput);
100-
}
10179
};
10280
}
10381

@@ -127,7 +105,7 @@ namespace AppInstaller::CLI
127105
{
128106
if (auto json = GetJsonFromInput(context))
129107
{
130-
anon::TestFileFunctionData data{ json };
108+
PackageFunctionData data{ json };
131109

132110
data.Get();
133111

@@ -139,33 +117,13 @@ namespace AppInstaller::CLI
139117
{
140118
if (auto json = GetJsonFromInput(context))
141119
{
142-
anon::TestFileFunctionData data{ json };
120+
PackageFunctionData data{ json };
143121

144122
data.Get();
145123

146124
if (!data.Test())
147125
{
148-
bool exists = std::filesystem::exists(data.Path);
149-
if (exists)
150-
{
151-
// Don't delete a directory or other special files in this test resource
152-
THROW_WIN32_IF(ERROR_DIRECTORY_NOT_SUPPORTED, !std::filesystem::is_regular_file(data.Path));
153-
}
154-
155-
if (data.Input.ShouldExist())
156-
{
157-
std::filesystem::create_directories(data.Path.parent_path());
158-
159-
std::ofstream stream{ data.Path, std::ios::binary | std::ios::trunc };
160-
if (data.Input.Content())
161-
{
162-
stream.write(data.Input.Content().value().c_str(), data.Input.Content().value().length());
163-
}
164-
}
165-
else if (exists)
166-
{
167-
std::filesystem::remove(data.Path);
168-
}
126+
THROW_HR(E_NOTIMPL);
169127
}
170128

171129
// Capture the diff before updating the output
@@ -174,19 +132,29 @@ namespace AppInstaller::CLI
174132
data.Output.Exist(data.Input.ShouldExist());
175133
if (data.Output.Exist().value())
176134
{
177-
data.Output.Content(data.Input.Content().value_or(""));
135+
THROW_HR(E_NOTIMPL);
178136
}
179137

180138
WriteJsonOutputLine(context, data.Output.ToJson());
181139
WriteJsonOutputLine(context, diff);
182140
}
183141
}
184142

143+
void DscPackageResource::ResourceFunctionWhatIf(Execution::Context& context) const
144+
{
145+
if (auto json = GetJsonFromInput(context))
146+
{
147+
PackageFunctionData data{ json };
148+
149+
THROW_HR(E_NOTIMPL);
150+
}
151+
}
152+
185153
void DscPackageResource::ResourceFunctionTest(Execution::Context& context) const
186154
{
187155
if (auto json = GetJsonFromInput(context))
188156
{
189-
anon::TestFileFunctionData data{ json };
157+
PackageFunctionData data{ json };
190158

191159
data.Get();
192160
data.Output.InDesiredState(data.Test());
@@ -200,37 +168,14 @@ namespace AppInstaller::CLI
200168
{
201169
if (auto json = GetJsonFromInput(context))
202170
{
203-
anon::TestFileFunctionData data{ json };
171+
PackageFunctionData data{ json };
204172

205-
if (std::filesystem::exists(data.Path))
206-
{
207-
if (std::filesystem::is_regular_file(data.Path))
208-
{
209-
data.Get();
210-
WriteJsonOutputLine(context, data.Output.ToJson());
211-
}
212-
else if (std::filesystem::is_directory(data.Path))
213-
{
214-
for (const auto& file : std::filesystem::directory_iterator{ data.Path })
215-
{
216-
if (std::filesystem::is_regular_file(file))
217-
{
218-
anon::TestFileObject output;
219-
output.Path(file.path().u8string());
220-
221-
std::ifstream stream{ file.path(), std::ios::binary};
222-
output.Content(Utility::ReadEntireStream(stream));
223-
224-
WriteJsonOutputLine(context, output.ToJson());
225-
}
226-
}
227-
}
228-
}
173+
THROW_HR(E_NOTIMPL);
229174
}
230175
}
231176

232177
void DscPackageResource::ResourceFunctionSchema(Execution::Context& context) const
233178
{
234-
WriteJsonOutputLine(context, anon::TestFileObject::Schema(ResourceType()));
179+
WriteJsonOutputLine(context, PackageResourceObject::Schema(ResourceType()));
235180
}
236181
}

src/AppInstallerCLICore/Commands/DscPackageResource.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace AppInstaller::CLI
1818

1919
void ResourceFunctionGet(Execution::Context& context) const override;
2020
void ResourceFunctionSet(Execution::Context& context) const override;
21+
void ResourceFunctionWhatIf(Execution::Context& context) const override;
2122
void ResourceFunctionTest(Execution::Context& context) const override;
2223
void ResourceFunctionExport(Execution::Context& context) const override;
2324
void ResourceFunctionSchema(Execution::Context& context) const override;

0 commit comments

Comments
 (0)