From 196275970809cfe0a9b6a950c322b94295cd627b Mon Sep 17 00:00:00 2001 From: Markus Fleschutz Date: Mon, 12 May 2025 15:48:41 +0200 Subject: [PATCH] Added new-markdown-file.ps1 --- data/templates/Markdown.md | 7 +++++ .../PowerShell.ps1} | 0 scripts/new-markdown-file.ps1 | 31 +++++++++++++++++++ scripts/new-script.ps1 | 23 +++++++------- 4 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 data/templates/Markdown.md rename data/{template.ps1 => templates/PowerShell.ps1} (100%) create mode 100644 scripts/new-markdown-file.ps1 diff --git a/data/templates/Markdown.md b/data/templates/Markdown.md new file mode 100644 index 00000000..e3acd8a8 --- /dev/null +++ b/data/templates/Markdown.md @@ -0,0 +1,7 @@ +Your Headline +============ + +Some introductory words... + +- Point 1 +- Point 2 diff --git a/data/template.ps1 b/data/templates/PowerShell.ps1 similarity index 100% rename from data/template.ps1 rename to data/templates/PowerShell.ps1 diff --git a/scripts/new-markdown-file.ps1 b/scripts/new-markdown-file.ps1 new file mode 100644 index 00000000..5e744aaf --- /dev/null +++ b/scripts/new-markdown-file.ps1 @@ -0,0 +1,31 @@ +<# +.SYNOPSIS + Creates a new Markdown file +.DESCRIPTION + This PowerShell script creates a new Markdown file from template file at: ../data/templates/Markdown.md. +.PARAMETER path + Specifies the path and new filename +.EXAMPLE + PS> ./new-markdown-file.ps1 letter.md + ✅ New Markdown file 'letter.md' created from template 'Markdown.md'. +.LINK + https://github.com/fleschutz/PowerShell +.NOTES + Author: Markus Fleschutz | License: CC0 +#> + +param([string]$path = "") + +try { + if ($path -eq "" ) { $path = Read-Host "Enter the new filename" } + + $pathToTemplate = Resolve-Path "$PSScriptRoot/../data/templates/Markdown.md" + Copy-Item $pathToTemplate "$path" + if ($lastExitCode -ne 0) { throw "Can't copy template to: $path" } + + "✅ New Markdown file '$path' created from template 'Markdown.md'." + exit 0 # success +} catch { + "⚠️ Error: $($Error[0])" + exit 1 +} diff --git a/scripts/new-script.ps1 b/scripts/new-script.ps1 index 0df107b2..53925d97 100755 --- a/scripts/new-script.ps1 +++ b/scripts/new-script.ps1 @@ -2,29 +2,30 @@ .SYNOPSIS Creates a new PowerShell script .DESCRIPTION - This PowerShell script creates a new PowerShell script file by using the template file ../data/template.ps1. -.PARAMETER filename - Specifies the path and filename to the new script + This PowerShell script creates a new PowerShell script file by using the template file at: ../data/templates/PowerShell.ps1. +.PARAMETER path + Specifies the path and new filename .EXAMPLE - PS> ./new-script myscript.ps1 - ✅ New PowerShell script 'myscript.ps1' created from: C:\PowerShell\data\template.ps1 + PS> ./new-script.ps1 bot.ps1 + ✅ New PowerShell script 'bot.ps1' created from template 'PowerShell.ps1'. .LINK https://github.com/fleschutz/PowerShell .NOTES Author: Markus Fleschutz | License: CC0 #> -param([string]$filename = "") +param([string]$path = "") try { - if ($filename -eq "" ) { $filename = Read-Host "Enter the new filename" } + if ($path -eq "" ) { $path = Read-Host "Enter the new filename" } - $pathToTemplate = Resolve-Path "$PSScriptRoot/../data/template.ps1" - Copy-Item $pathToTemplate "$filename" + $pathToTemplate = Resolve-Path "$PSScriptRoot/../data/templates/PowerShell.ps1" + Copy-Item $pathToTemplate "$path" + if ($lastExitCode -ne 0) { throw "Can't copy to: $path" } - "✅ New PowerShell script '$filename' created from: $pathToTemplate" + "✅ New PowerShell script '$path' created from template 'PowerShell.ps1'." exit 0 # success } catch { - "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + "⚠️ Error: $($Error[0])" exit 1 }