Skip to content

Commit 4397fb6

Browse files
Merge pull request #6 from PowershellFrameworkCollective/dotnet-magic
Added dotnet templates magic
2 parents 9447acb + f689f37 commit 4397fb6

File tree

11 files changed

+243
-5
lines changed

11 files changed

+243
-5
lines changed

PSModuleDevelopment/PSModuleDevelopment.psd1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
RootModule = 'PSModuleDevelopment.psm1'
55

66
# Version number of this module.
7-
ModuleVersion = '2.0.0.0'
7+
ModuleVersion = '2.1.0.1'
88

99
# ID used to uniquely identify this module
1010
GUID = '37dd5fce-e7b5-4d57-ac37-832055ce49d6'
@@ -41,7 +41,7 @@
4141

4242
# Modules that must be imported into the global environment prior to importing
4343
# this module
44-
RequiredModules = @(@{ ModuleName='PSFramework'; ModuleVersion='0.9.4.9' })
44+
RequiredModules = @(@{ ModuleName='PSFramework'; ModuleVersion= '0.9.8.17' })
4545

4646
# Assemblies that must be loaded prior to importing this module
4747
RequiredAssemblies = @('bin\PSModuleDevelopment.dll')
@@ -71,6 +71,7 @@
7171
'Get-PSMDModuleDebug',
7272
'Import-PSMDModuleDebug',
7373
'Measure-PSMDCommandEx',
74+
'New-PSMDDotNetProject',
7475
'New-PSMDHeader',
7576
'New-PSMDFormatTableDefinition',
7677
'New-PssModuleProject',
@@ -92,6 +93,7 @@
9293

9394
# Aliases to export from this module
9495
AliasesToExport = @(
96+
'dotnetnew',
9597
'find',
9698
'hex',
9799
'Restart-Shell',

PSModuleDevelopment/PSModuleDevelopment.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
$script:PSModuleRoot = $PSScriptRoot
2-
$script:PSModuleVersion = "2.0.0.0"
2+
$script:PSModuleVersion = "2.1.0.1"
33

44
$script:doDotSource = $false
55
if (Get-PSFConfigValue -Name PSModuleDevelopment.Import.DoDotSource) { $script:doDotSource = $true }

PSModuleDevelopment/PSModuleDevelopment.psproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
<Folder>tests\functions</Folder>
2424
<Folder>tests\general</Folder>
2525
<Folder>internal\functions</Folder>
26+
<Folder>functions\templating</Folder>
27+
<Folder>internal\tabcompletion</Folder>
28+
<Folder>internal\tabcompletion\scriptblocks</Folder>
29+
<Folder>internal\maintenance</Folder>
2630
</Folders>
2731
<Files>
2832
<File Build="2">PSModuleDevelopment.psd1</File>
@@ -70,6 +74,12 @@
7074
<File Build="2">tests\readme.md</File>
7175
<File Build="2">xml\PSModuleDevelopment.Format.ps1xml</File>
7276
<File Build="2">tests\functions\readme.md</File>
77+
<File Build="2" Shared="True" ReferenceFunction="Invoke-New-PSMDDotNetProject_ps1">functions\templating\New-PSMDDotNetProject.ps1</File>
78+
<File Build="2" Shared="True" ReferenceFunction="Invoke-assignment_ps1">internal\tabcompletion\assignment.ps1</File>
79+
<File Build="2" Shared="True" ReferenceFunction="Invoke-dotNetTemplates_ps1">internal\tabcompletion\scriptblocks\dotNetTemplates.ps1</File>
80+
<File Build="2" Shared="True" ReferenceFunction="Invoke-dotNetTemplatesUninstall_ps1">internal\tabcompletion\scriptblocks\dotNetTemplatesUninstall.ps1</File>
81+
<File Build="2" Shared="True" ReferenceFunction="Invoke-dotNetTemplateCache_ps1">internal\maintenance\dotNetTemplateCache.ps1</File>
82+
<File Build="2" Shared="True" ReferenceFunction="Invoke-dotNetTemplatesInstall_ps1">internal\tabcompletion\scriptblocks\dotNetTemplatesInstall.ps1</File>
7383
</Files>
7484
<StartupScript>F:\Synchronized Data\Scripte\Powershell Studio\Projects\PSModuleDevelopment\Test-Module.ps1</StartupScript>
7585
</Project>

PSModuleDevelopment/en-us/about_psmoduledevelopment.help.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,11 @@ LONG DESCRIPTION
165165
# Changelog #
166166
#-------------------------------------------------------------------------#
167167

168-
2.1.0.1 (???)
168+
2.1.0.1 (January 24th, 2018)
169169
- new: Included suite of tests, in order to provide a more reliable user
170170
experience.
171+
- new: Command New-PSMDDotNetProject
172+
A wrapper around dotnet.exe
171173

172174
2.0.0.0 (December 18th, 2017)
173175
- Breaking change: Renamed all commands to include the PSMD prefix
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
function New-PSMDDotNetProject
2+
{
3+
<#
4+
.SYNOPSIS
5+
Wrapper function around 'dotnet new'
6+
7+
.DESCRIPTION
8+
This function is a wrapper around the dotnet.exe application with the parameter 'new'.
9+
It can be used to create projects from templates, as well as to administrate templates.
10+
11+
.PARAMETER TemplateName
12+
The name of the template to create
13+
14+
.PARAMETER List
15+
List the existing templates.
16+
17+
.PARAMETER Help
18+
Ask for help / documentation.
19+
Particularly useful when dealing with project types that have a lot of options.
20+
21+
.PARAMETER Force
22+
Overwrite existing files.
23+
24+
.PARAMETER Name
25+
The name of the project to create
26+
27+
.PARAMETER Output
28+
The folder in which to create it.
29+
Note: This folder will automatically be te root folder of the project.
30+
If this folder doesn't exist yet, it will be created.
31+
When used with -Force, it will automatically purge all contents.
32+
33+
.PARAMETER Install
34+
Install the specified template from the VS marketplace.
35+
36+
.PARAMETER Uninstall
37+
Uninstall an installed template.
38+
39+
.PARAMETER Arguments
40+
Additional arguments to pass to the application.
41+
Generally used for parameters when creating a project from a template.
42+
43+
.PARAMETER Confirm
44+
If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.
45+
46+
.PARAMETER WhatIf
47+
If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.
48+
49+
.EXAMPLE
50+
PS C:\> dotnetnew -l
51+
52+
Lists all installed templates.
53+
54+
.EXAMPLE
55+
PS C:\> dotnetnew mvc foo F:\temp\projects\foo -au Windows --no-restore
56+
57+
Creates a new MVC project named "foo" in folder "F:\Temp\projects\foo"
58+
- It will set authentication to windows
59+
- It will skip the automatic restore of the project on create
60+
#>
61+
[CmdletBinding(SupportsShouldProcess = $true, DefaultParameterSetName = 'Create')]
62+
Param (
63+
[Parameter(Position = 0, Mandatory = $true, ParameterSetName = 'Create')]
64+
[Parameter(Position = 0, ParameterSetName = 'List')]
65+
[string]
66+
$TemplateName,
67+
68+
[Parameter(ParameterSetName = 'List')]
69+
[Alias('l')]
70+
[switch]
71+
$List,
72+
73+
[Alias('h')]
74+
[switch]
75+
$Help,
76+
77+
[switch]
78+
$Force,
79+
80+
[Parameter(Position = 1, ParameterSetName = 'Create')]
81+
[Alias('n')]
82+
[string]
83+
$Name,
84+
85+
[Parameter(Position = 2, ParameterSetName = 'Create')]
86+
[Alias('o')]
87+
[string]
88+
$Output,
89+
90+
[Parameter(Mandatory = $true, ParameterSetName = 'Install')]
91+
[Alias('i')]
92+
[string]
93+
$Install,
94+
95+
[Parameter(Mandatory = $true, ParameterSetName = 'Uninstall')]
96+
[Alias('u')]
97+
[string]
98+
$Uninstall,
99+
100+
[Parameter(ValueFromRemainingArguments = $true)]
101+
[Alias('a')]
102+
[string[]]
103+
$Arguments
104+
)
105+
106+
begin
107+
{
108+
$parset = $PSCmdlet.ParameterSetName
109+
Write-PSFMessage -Level InternalComment -Message "Active parameterset: $parset" -Tag 'start'
110+
111+
if (-not (Get-Command dotnet.exe))
112+
{
113+
throw "Could not find dotnet.exe! This should automatically be available on machines with Visual Studio installed."
114+
}
115+
116+
$dotNetArgs = @()
117+
switch ($parset)
118+
{
119+
'Create'
120+
{
121+
if (Test-PSFParameterBinding -ParameterName TemplateName) { $dotNetArgs += $TemplateName }
122+
if ($Help) { $dotNetArgs += "-h" }
123+
if (Test-PSFParameterBinding -ParameterName Name)
124+
{
125+
$dotNetArgs += "-n"
126+
$dotNetArgs += $Name
127+
}
128+
if (Test-PSFParameterBinding -ParameterName Output)
129+
{
130+
$dotNetArgs += "-o"
131+
$dotNetArgs += $Output
132+
}
133+
if ($Force) { $dotNetArgs += "--Force" }
134+
}
135+
'List'
136+
{
137+
if (Test-PSFParameterBinding -ParameterName TemplateName) { $dotNetArgs += $TemplateName }
138+
$dotNetArgs += '-l'
139+
if ($Help) { $dotNetArgs += "-h" }
140+
}
141+
'Install'
142+
{
143+
$dotNetArgs += '-i'
144+
$dotNetArgs += $Install
145+
if ($Help) { $dotNetArgs += '-h'}
146+
}
147+
'Uninstall'
148+
{
149+
$dotNetArgs += '-u'
150+
$dotNetArgs += $Uninstall
151+
if ($Help) { $dotNetArgs += '-h' }
152+
}
153+
}
154+
155+
foreach ($item in $Arguments)
156+
{
157+
$dotNetArgs += $item
158+
}
159+
Write-PSFMessage -Level Verbose -Message "Resolved arguments: $($dotNetArgs -join " ")" -Tag 'argument','start'
160+
}
161+
process
162+
{
163+
if ($PSCmdlet.ShouldProcess("dotnet", "Perform action: $parset"))
164+
{
165+
if ($parset -eq 'Create')
166+
{
167+
if ($Output)
168+
{
169+
if ((Test-Path $Output) -and $Force) { $null = New-Item $Output -ItemType Directory -Force -ErrorAction Stop }
170+
if (-not (Test-Path $Output)) { $null = New-Item $Output -ItemType Directory -Force -ErrorAction Stop }
171+
}
172+
}
173+
Write-PSFMessage -Level Verbose -Message "Executing with arguments: $($dotNetArgs -join " ")" -Tag 'argument', 'start'
174+
& dotnet.exe new $dotNetArgs
175+
}
176+
}
177+
end
178+
{
179+
180+
}
181+
}
182+
183+
New-Alias -Name dotnetnew -Value New-PSMDDotNetProject -Option AllScope -Scope Global -ErrorAction Ignore
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
$scriptBlock = {
2+
$webclient = New-Object System.Net.WebClient
3+
$string = $webclient.DownloadString("http://dotnetnew.azurewebsites.net/")
4+
$templates = $string -split "`n" | Select-String '<a href="/template/(.*?)/.*?">.*?</a>' | ForEach-Object { $_.Matches.Groups[1].Value } | Select-Object -Unique | Sort-Object
5+
6+
Set-PSFTaskEngineCache -Module PSModuleDevelopment -Name "dotNetTemplates" -Value $templates
7+
}
8+
Register-PSFTaskEngineTask -Name "psmd_dotNetTemplateCache" -ScriptBlock $scriptBlock -Priority Low -Once -Description "Builds up the cache of installable templates for dotnet"

PSModuleDevelopment/internal/scripts/postload.ps1

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,17 @@ foreach ($__module in $__modules)
1010
}
1111

1212
# Import License
13-
. Import-PSMDFile -Path "$PSModuleRoot\internal\scripts\license.ps1"
13+
. Import-PSMDFile -Path "$PSModuleRoot\internal\scripts\license.ps1"
14+
15+
# Import maintenance tasks
16+
foreach ($file in (Get-ChildItem -Path "$PSModuleRoot\internal\maintenance"))
17+
{
18+
. Import-PSMDFile -Path $file.FullName
19+
}
20+
21+
# Import tab completion
22+
foreach ($file in (Get-ChildItem -Path "$PSModuleRoot\internal\tabcompletion\scriptblocks"))
23+
{
24+
. Import-PSMDFile -Path $file.FullName
25+
}
26+
Import-PSMDFile -Path "$PSModuleRoot\internal\tabcompletion\assignment.ps1"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# New-PSMDDotNetProject
2+
Register-PSFTeppArgumentCompleter -Name PSMD_dotNetTemplates -Command New-PSMDDotNetProject -Parameter TemplateName
3+
Register-PSFTeppArgumentCompleter -Name PSMD_dotNetTemplatesUninstall -Command New-PSMDDotNetProject -Parameter Uninstall
4+
Register-PSFTeppArgumentCompleter -Name PSMD_dotNetTemplatesInstall -Command New-PSMDDotNetProject -Parameter Install
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Register-PSFTeppScriptblock -Name PSMD_dotNetTemplates -ScriptBlock {
2+
if (-not (Test-Path "$env:USERPROFILE\.templateengine\dotnetcli")) { return }
3+
4+
$folder = (Get-ChildItem "$env:USERPROFILE\.templateengine\dotnetcli" | Sort-Object Name | Select-Object -Last 1).FullName
5+
Get-Content -Path "$folder\templatecache.json" | ConvertFrom-Json | Select-Object -ExpandProperty TemplateInfo | Select-Object -ExpandProperty ShortName -Unique
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Register-PSFTeppScriptblock -Name PSMD_dotNetTemplatesInstall -ScriptBlock {
2+
Get-PSFTaskEngineCache -Module PSModuleDevelopment -Name "dotNetTemplates"
3+
}

0 commit comments

Comments
 (0)