Skip to content
Closed
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
62 changes: 9 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Whether you're creating simple or robust modules, ModuleTools streamlines the pr
[![ModuleTools@PowerShell Gallery][BadgeIOCount]][PSGalleryLink]
![WorkFlow Status][WorkFlowStatus]



The structure of the ModuleTools module is meticulously designed according to PowerShell best practices for module development. While some design decisions may seem unconventional, they are made to ensure that ModuleTools and the process of building modules remain straightforward and easy to manage.

> [!IMPORTANT]
Expand All @@ -26,7 +28,7 @@ The structure of the ModuleTools module is meticulously designed according to Po
Install-Module -Name ModuleTools
```

> Note: ModuleTolls is still in early development phase and lot of changes are expected. Please read through [ChangeLog](/CHANGELOG.md) for all updates.
> Note: ModuleTolls is still in early devleopment phase and lot of changes are expected. Please read through [ChangeLog](/CHANGELOG.md) for all updates.

## 🧵 Design

Expand Down Expand Up @@ -60,6 +62,8 @@ Generated module is stored in dist folder, you can easily import it or publish i
└──  TestModule.psm1
```



### Project JSON File

The `project.json` file contains all the important details about your module and is used during the module build. It should comply with a specific schema. You can refer to the sample `project-sample.json` file in the `example` directory for guidance.
Expand All @@ -68,43 +72,9 @@ Run `New-MTModule` to generate the scaffolding; this will also create the `proje

### Src Folder

- Place all your functions in the `private` and `public` folders within the `src` directory.
- All functions in the `public` folder are exported during the module build.
- All functions in the `private` folder are accessible internally within the module but are not exposed outside the module.
- Contents of the `src/resources` folder, including any subfolder, will included in the `dist` folder during the module build.

#### resources Folder

The `resources` folder within the `src` directory is intended for including any additional resources required by your module. This can include files such as:

- **Configuration files**: Store any JSON, XML, or other configuration files needed by your module.
- **Script files**: Place any scripts that are used by your functions or modules, but are not directly part of the public or private functions.
- **Documentation files**: Include any supplementary documentation that supports the usage or development of the module.
- **Data files**: Store any data files that are used by your module, such as CSV or JSON files.
- **Subfolder**: Include any additional folders and their content to be included with the module, such as dependant Modules, APIs, DLLs, etc... organized by a subfolder.

When the module is built, the contents of the `src/resources` folder will be copied directly to the `dist` folder. If the `src/resources` folder contains any subfolders, those subfolders and their contents will also be included in the `dist` folder, ensuring that all necessary files are available for the module to function correctly.

How the resources folder gets copied to the "OutputModuleDir" folder will depends on the "ResourceCopyMode" project setting. When missing or set to "Folder", the resources folder will be copied. When "ResourceCopyMode" is set to "Content", then only the content of the resources folder will be copied.

Leave `src\resources` empty if there is no need to include any additional content in the `dist` folder.

An example of the module build where resources were included:

```powershell
dist
└── TestModule
├── TestModule.psd1
├── TestModule.psm1
├── config.json
├── additionalScript.ps1
├── helpDocumentation.md
├── sampleData.csv
└── subfolder
├── subConfig.json
├── subScript.ps1
└── subData.csv
```
- Place all your functions in the `private` and `public` folders within the `src` directory.
- All functions in the `public` folder are exported during the module build.
- All functions in the `private` folder are accessible internally within the module but are not exposed outside the module.

### Tests Folder

Expand All @@ -127,11 +97,6 @@ New-MTModule ~/Work

`ModuleTools` is designed so that you don't need any additional tools like `make` or `psake` to run the build commands. There's no need to maintain complex `build.ps1` files or sample `.psd1` files. Simply follow the structure outlined above, and you can run `Invoke-MTBuild` to build the module. The output will be saved in the `dist` folder, ready for distribution.

The Invoke-MTBuild CmdLet includes a step where the resources folder and/or it's contents are copied to the "OutputModuleDir" folder. This is controlled by the optional "ResourceCopyMode" project setting.

If "ResourceCopyMode" = 'Folder or if it's missing, the entire resources folder gets copied to the "OutputModuleDir" folder.
If "ResourceCopyMode" = 'Content', only the content of the resources folder gets copied to the "OutputModuleDir" folder.

```powershell
# From the Module root
Invoke-MTBuild
Expand Down Expand Up @@ -161,15 +126,7 @@ A simple command to update the module version by modifying the values in `projec

## Advanced - Use it in Github Actions

This is not required for local module builds, if you are running github actions, use the following yaml workflow template to test, build and publish module which helps to automate the process of:

1. Checking out the repository code.
1. Installing the `ModuleTools` module from the PowerShell Gallery.
1. Building the module.
1. Running Pester tests.
1. Publishing the module to a specified repository.

This allows for seamless and automated management of your PowerShell module, ensuring consistency and reliability in your build, test, and release processes.
This is not required for local module builds, if you are running github actions, use below template to test, build and publish module with ease.

```yaml
name: Build, Test and Publish
Expand Down Expand Up @@ -197,7 +154,6 @@ jobs:
- name: Run Pester Tests
run: Invoke-MTTest
shell: pwsh

- name: Publish Package to Github
run: |
Publish-PSResource -Path ./dist/YourModule -Repository SomeRepository -ApiKey $Env:ApiKey
Expand Down
3 changes: 1 addition & 2 deletions project.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"ProjectName": "ModuleTools",
"Description": "ModuleTools is a versatile, standalone PowerShell module builder. Create anything from simple to robust modules with ease. Built for CICD and Automation.",
"Version": "0.0.10",
"ResourceCopyMode": "Folder",
"Version": "0.0.9",
"Manifest": {
"Author": "Manjunath Beli",
"PowerShellHostVersion": "7.4",
Expand Down
21 changes: 5 additions & 16 deletions src/private/CopyProjectResource.ps1
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
function Copy-ProjectResource {
$data = Get-MTProjectInfo
$resFolder = [System.IO.Path]::Join($data.ProjectRoot, 'src', 'resources')

if (Test-Path $resFolder) {
if ($data.ResourceCopyMode -eq 'Content') {
# Copy the resources folder content to the OutputModuleDir
$items = Get-ChildItem -Path $resFolder -ErrorAction SilentlyContinue
if ($items) {
Write-Verbose 'Files found in resource folder, copying resource folder content'
foreach ($item in $items) {
Copy-Item -Path $item.FullName -Destination ($data.OutputModuleDir) -Recurse -Force -ErrorAction Stop
}
}
} else {
# Copy the resources folder to the OutputModuleDir
if (Get-ChildItem $resFolder -ErrorAction SilentlyContinue) {
Write-Verbose 'Files found in resource folder, Copying resource folder'
Copy-Item -Path $resFolder -Destination ($data.OutputModuleDir) -Recurse -Force -ErrorAction Stop
}
if (Get-ChildItem $resFolder -ErrorAction SilentlyContinue) {
Write-Verbose 'Files found in resource folder, Copying resource folder content'
Copy-Item -Path $resFolder -Destination ($data.OutputModuleDir) -Recurse -Force -ErrorAction Stop
}
}
}
}
1 change: 0 additions & 1 deletion src/resources/ProjectTemplate.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"ProjectName": "",
"Description": "",
"Version": "",
"ResourceCopyMode": "",
"Manifest": {
"Author": "",
"PowerShellHostVersion": "",
Expand Down
3 changes: 0 additions & 3 deletions src/resources/Schema-Build.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
"Version": {
"type": "string"
},
"ResourceCopyMode": {
"type": "string"
},
"Manifest": {
"type": "object",
"properties": {
Expand Down
Loading