com um clique
create-dsc-resource
// Create a complete, accurate DSC resource in this repository following the provided guidelines and design patterns.
// Create a complete, accurate DSC resource in this repository following the provided guidelines and design patterns.
| name | create-dsc-resource |
| description | Create a complete, accurate DSC resource in this repository following the provided guidelines and design patterns. |
You are a code agent. Your task is to create a complete, accurate DSC resource in this repository.
A DSC (Desired State Configuration) resource is an executable that performs configuration tasks for a specific domain. Example domains include Windows features, package manager, services, files, registry, and more. Each resource has a specific schema and set of properties that define its behavior. Management tasks or operations are specific to the resource type, but may include: get, set, test, and export.
.project.data.json file in the root of the project folder defines properties of the project and non-code files to include during buildrust-i18n for internationalization. For script-based resources (such as PowerShell), follow the existing localization and string-handling patterns used by those scripts or any repository-specific localization guidance.// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
For PowerShell test files, use # comment syntax instead of //.cargo new --bin <resource_name> to create a new Rust binary project for the resource under the resources directory<resource_name>.dsc.resource.json in the same directory using ./resources/windows_service/windows_service.dsc.resource.json as an example.project.data.json file in the root of the resource project directorylocales/en-us.toml file for localized stringsThis file defines the project metadata used by the build system. Structure:
{
"Name": "<resource_name>",
"Kind": "Resource",
"IsRust": true,
"SupportedPlatformOS": "Windows",
"Binaries": [
"<resource_name>"
],
"CopyFiles": {
"Windows": [
"<resource_name>.dsc.resource.json"
]
}
}
SupportedPlatformOS: Use "Windows" for Windows-only resources. Omit this field for cross-platform resources.CopyFiles: Use "Windows" for Windows-only files, "All" for cross-platform files.[package]
name = "<resource_name>"
version = "0.1.0"
edition = "2024"
[[bin]]
name = "<resource_name>"
path = "src/main.rs"
[package.metadata.i18n]
available-locales = ["en-us"]
default-locale = "en-us"
load-path = "locales"
[dependencies]
rust-i18n = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
[target.'cfg(windows)'.dependencies]
windows = { workspace = true }
{ workspace = true } to use workspace-level version pinning.[target.'cfg(windows)'.dependencies] for Windows-only crates like windows.[package.metadata.i18n] section configures the rust-i18n crate.<resource_name>.dsc.resource.json)Key fields:
{
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
"type": "Microsoft.<Namespace>/<Resource>",
"description": "Short description of the resource",
"tags": ["Windows"],
"version": "0.1.0",
"get": {
"executable": "<resource_name>",
"args": ["get", { "jsonInputArg": "--input", "mandatory": true }]
},
"set": {
"executable": "<resource_name>",
"args": ["set", { "jsonInputArg": "--input", "mandatory": true }],
"implementsPretest": false,
"return": "state",
"requireSecurityContext": "elevated"
},
"export": {
"executable": "<resource_name>",
"args": ["export", { "jsonInputArg": "--input", "mandatory": false }]
},
"exitCodes": {
"0": "Success",
"1": "Invalid arguments",
"2": "Invalid input",
"3": "Resource-specific error description"
},
"schema": {
"embedded": { ... }
}
}
implementsPretest: Set to false if set does not internally perform a test first. DSC will call test before set automatically.return: Use "state" if the set operation returns the final state of the resource.requireSecurityContext: Use "elevated" for operations that require administrator privileges.schema.embedded: Define the JSON schema inline with "additionalProperties": false. Schema property names use camelCase. Use "readOnly": true for properties that are output-only (e.g., _exist).rust-i18n with rust_i18n::i18n!("locales", fallback = "en-us");const EXIT_SUCCESS: i32 = 0;
const EXIT_INVALID_ARGS: i32 = 1;
const EXIT_INVALID_INPUT: i32 = 2;
const EXIT_RESOURCE_ERROR: i32 = 3;
write_error(message) — writes {"error": "<message>"} to stderrrequire_input(input_json) — deserializes the JSON input or exits with an errorprint_json(value) — serializes and prints to stdout, or exits with an errorparse_input_arg(args) — parses --input <json> from command-line argumentst!("key") macro from rust-i18n for all user-facing strings (error messages, etc.)#[cfg(not(windows))]
fn main() {
write_error(&t!("main.windowsOnly"));
exit(EXIT_RESOURCE_ERROR);
}
#[cfg(windows)]
fn main() {
// ... actual implementation
}
get, set, export), with --input <json> as the input argument#[derive(Debug, Default, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct MyResource {
#[serde(skip_serializing_if = "Option::is_none")]
pub some_field: Option<String>,
#[serde(rename = "_exist", skip_serializing_if = "Option::is_none")]
pub exist: Option<bool>,
}
Option<T> for all fields so that partial input is accepted (only specified fields are acted upon)Serialize, Deserialize, PartialEq, Clone derivesstd::fmt::Display for enums that need string representationstd::error::Error and std::fmt::Display implementations, plus a From<String> conversionlocales/en-us.toml)All user-facing strings must be defined in a TOML locale file and referenced via the t!() macro. Structure:
_version = 1
[main]
missingOperation = "Missing operation. Usage: <resource_name> get --input <json> | set --input <json>"
unknownOperation = "Unknown operation: '%{operation}'. Expected: get, set, or export"
missingInput = "Missing --input argument"
invalidJson = "Invalid JSON input: %{error}"
[get]
someError = "Failed to do something: %{error}"
[set]
someError = "Failed to do something: %{error}"
%{variable} syntax for interpolated valuest!("section.key", variable = value)service.rs) for interacting with native OS APIs that contains unsafe code and FFI bindings#[cfg(windows)] if it uses Windows-specific APIsDrop (e.g., CloseServiceHandle, CloseHandle)to_wide, pwstr_to_string)Result type for error handling in operation handlers{"error": "Error message here"}{"warn": "Warning message here"}EXIT_INVALID_ARGS, EXIT_INVALID_INPUT) and document each in the resource manifest exitCodes section.tests folder in the resource project directory<resource_name>_get.tests.ps1, <resource_name>_set.tests.ps1, etc.)dsc resource command instead of calling the executable directly-f - flag: $json | dsc resource get -r $resourceType -f -2>$testdrive/error.log-Because with $LASTEXITCODE assertions to include stderr output for diagnostics:
$out = $json | dsc resource get -r $resourceType -f - 2>$testdrive/error.log
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Raw $testdrive/error.log)
$output.actualState:
$output = $out | ConvertFrom-Json
$result = $output.actualState
-Skip on the top-level Describe block:
Describe 'Resource tests' -Skip:(!$IsWindows) { ... }
-ForEach or -TestCases for data-driven tests (e.g., validating enum property values)build.ps1 -project <resource_name> from the root of the repository, which will handle building the Rust code and ensure it is found in PATH for testing