Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New module script #121

Merged
merged 10 commits into from
Nov 11, 2024
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
1 change: 1 addition & 0 deletions .github/actions/spelling/expect/generic_terms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ worktree
sortby
msft
automerge
Workaround
31 changes: 24 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,30 @@ Once the team have approved an issue/spec, development can proceed. If no develo

### Fork, Clone, Branch and Create your PR

Once you've discussed your proposed feature/fix/etc. with a team member, and you've agreed an approach or a spec has been written and approved, it's time to start development:
Once you've discussed your proposed feature/fix/etc. with a team member, and you've agreed an approach or a spec has been written and approved, it's time to start development. There are two flows you can follow depending on the proposed feature or fix.

If you're feature (or module) has not yet been created, follow these steps:

1. Fork the repository if you haven't already.
2. Clone your fork locally.
3. Open a PowerShell terminal session and execute: `.\utilities\tools\New-DscResourceModule.ps1 -DscResourceModule '<moduleName>' -Description 'DSC Resource for <description>'`
4. Work on your changes and write tests.
5. Build and test to see if it works.
6. Create & push a feature branch.
7. Create a [Draft Pull Request (PR)](https://github.blog/2019-02-14-introducing-draft-pull-requests/).
8. If you are finished with your changes and you want a review, change the state.

> [!TIP]
> Don't forget to add the `DscResourcesToExport` and `Tags`.

When you are working on a fix or you want to add additional features to an existing module, you can follow the below steps:

1. Fork the repository if you haven't already.
1. Clone your fork locally.
1. Create & push a feature branch.
1. Create a [Draft Pull Request (PR)](https://github.blog/2019-02-14-introducing-draft-pull-requests/).
1. Work on your changes.
1. Build and see if it works.
2. Clone your fork locally.
3. Work on your fix or feature, and _optionally_ write tests
4. Build and test to see if it works.
5. Create & push a feature branch.
6. Create a [Pull Request (PR)](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request) when you are finished with your changes

### Testing

Expand All @@ -126,7 +142,8 @@ Testing is a key component in the development workflow.

When you'd like the team to take a look, (even if the work is not yet fully-complete), mark the Draft PR as 'Ready For Review' so that the team can review your work and provide comments, suggestions, and request changes. It may take several cycles, but the end result will be solid, testable, conformant code that is safe for us to merge.

> ⚠ Remember: **changes you make may affect both the Windows Package Manager and the schema support implemented in our validation pipelines!** Because of this, we will treat community PR's with the same level of scrutiny and rigor as commits submitted to the official Windows source by team members and partners.
> [!CAUTION]
> Remember: **changes you make may affect both the Windows Package Manager and the schema support implemented in our validation pipelines!** Because of this, we will treat community PR's with the same level of scrutiny and rigor as commits submitted to the official Windows source by team members and partners.

### Merge

Expand Down
98 changes: 98 additions & 0 deletions utilities/tools/New-DscResourceModule.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<#
.SYNOPSIS
Creates a new DSC (Desired State Configuration) resource module structure.

.DESCRIPTION
The function New-DscResourceModule function creates a new DSC resource module structure with the specified name and description.
It sets up the necessary directory structure for resources and tests within the given base path.

.PARAMETER DscResourceModule
The name of the DSC resource module to create.

.PARAMETER Description
A description of the DSC resource module.

.PARAMETER BasePath
The base path where the DSC resource module structure will be created. The default value is the parent directory of the script.

.EXAMPLE
PS C:\> New-DscResourceModule -DscResourceModule 'Microsoft.Windows.Language' -Description 'DSC Resource for Windows Language'

This command creates a new DSC resource module named 'Microsoft.Windows.Language' with the specified description in the default base path.
#>
#Requires -Version 7
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPositionalParameters', '', Justification = 'Positional parameters are used for simplicity. Targeting PS 7+')]
param (
[Parameter(Mandatory)]
[string]$DscResourceModule,

[Parameter(Mandatory)]
[string]$Description,

[Parameter()]
[ValidateNotNullOrEmpty()]
[string[]] $DscResourceToExport
)

$basePath = "$PSScriptRoot\..\.."

if (Test-Path $basePath) {
$resourcePath = Join-Path $basePath 'resources' $DscResourceModule
$testsPath = Join-Path $basePath 'tests' $DscResourceModule
} else {
$basePath = $resourcePath = $testsPath = (Resolve-Path '.\').Path
}

# Create directories if they do not exist
if (-not (Test-Path -Path $resourcePath)) {
Write-Verbose -Message "Creating directory: $resourcePath"
New-Item -ItemType Directory -Path $resourcePath -Force | Out-Null
}

if (-not (Test-Path -Path $testsPath)) {
Write-Verbose -Message "Creating test directory: $testsPath"
New-Item -ItemType Directory -Path $testsPath -Force | Out-Null
}

$moduleManifestPath = (Join-Path $basePath 'resources' $DscResourceModule "$DscResourceModule.psd1")

$moduleManifestParams = @{
Path = $moduleManifestPath
RootModule = "$DscResourceModule.psm1"
ModuleVersion = '0.1.0'
Author = 'Microsoft Corporation'
CompanyName = 'Microsoft Corporation'
Copyright = '(c) Microsoft Corporation. All rights reserved.'
Description = $Description
PowerShellVersion = '7.2'
FunctionsToExport = @()
CmdletsToExport = @()
VariablesToExport = @()
AliasesToExport = @()
LicenseUri = 'https://github.com/microsoft/winget-dsc/blob/main/LICENSE'
ProjectUri = 'https://github.com/microsoft/winget-dsc'
Prerelease = 'alpha'
}

if ($DscResourceToExport) {
# New module manifest does not properly handle arrays of strings :(
$moduleManifestParams.Add('DscResourcesToExport', @($DscResourceToExport))
}

if (-not (Test-Path $moduleManifestPath)) {
if ($PSCmdlet.ShouldProcess($moduleManifestPath, 'Create module manifest')) {
Write-Verbose -Message ($moduleManifestParams | ConvertTo-Json -Depth 10 | Out-String)
New-ModuleManifest @moduleManifestParams
}
}

$psm1Path = Join-Path -Path $resourcePath -ChildPath "$DscResourceModule.psm1"
if (-not (Test-Path $psm1Path)) {
New-Item -ItemType File -Path $psm1Path -Force | Out-Null
}

$testsFilePath = Join-Path -Path $testsPath -ChildPath "$DscResourceModule.Tests.ps1"
if (-not (Test-Path $testsFilePath)) {
New-Item -ItemType File -Path $testsFilePath -Force | Out-Null
}

Loading