我最近开始与之合作 石膏 我真的很喜欢这个模块。我介绍了我的第一个og体育 石膏中的冒险 上周博客帖子。我一直在一起举起更多膏药og体育的想法,我觉得一个有趣的乐趣。

I am going to build a Plaster template that builds a Plaster template. I am calling this new template GetPlastered.

这将是一个很好的例子,演示了石膏的TemplateFile功能。

指数

项目计划

My primary goal is to have a Plaster template that will turn an existing folder/project into a Plaster template. Our Plaster template will generate a 石膏 Template.xml for that folder.

This can be confusing because we are also creating a 石膏 Template.xml to make this template that generates a 石膏 Template.xml for a new template. It is like we are writing code that writes the same code that we are writing.

开始一个新og体育

我已经有一个存储库 膏药og体育,所以我需要做的就是创建初始og体育清单。

$templateName = 'GetPlastered'
$manifestProperties = @{
    Path = ".\$templateName\PlasterManifest.xml"
    Title = "Generate Plaster Maifest"
    TemplateName = $templateName
    TemplateVersion = '0.0.1'
    Author = 'Kevin Marquette'
}

New-Item -Path $templateName -ItemType Directory
New-PlasterManifest @manifestProperties

规划问题

Because my intent is that this template will be used instead of the New-PlasterManifest Cmdlet, we need to capture that functionality.

  • og体育名称
  • og体育标题
  • og体育作者

这应该总结我们需要收集的信息。

创建参数

现在我们可以将这些计划的问题转化为参数。这些问题是创建的简单参数。

<parameter name="TemplateName" 
           type="text" 
           prompt="og体育名称" 
           default="${PLASTER_DestinationName}" />

<parameter name="TemplateTitle" 
           type="text" 
           prompt="og体育标题" 
           default="${PLASTER_PARAM_TemplateName" />

<parameter name="TemplateAuthor" 
           type="user-fullname" 
           prompt="Author" />

I added these parameters to the parameters section of the 石膏 Manifest.xml file.

For the TemplateName default value, I use the name of the destination folder that is specified when Invoke-Plaster is invoked.

For the TemplateAuthor, I used user-fullname for the type. That is a special type that pulls the value from the user’s .gitconfig as a default.

templatefile.

Now we need to create a TemplateFile to generate the 石膏 Template.xml file. The first half of the TemplateFile will be basic value substitution.

<?xml version="1.0" encoding="utf-8"?>
<plasterManifest schemaVersion="1.0" 
xmlns="http://www.microsoft.com/schemas/PowerShell/Plaster/v1">
<metadata>
    <name><%= $PLASTER_PARAM_TemplateName %></name>
    <id><%= $PLASTER_GUID1 %></id>
    <version>0.0.1</version>
    <title><%= $PLASTER_PARAM_TemplateTitle %></title>
    <description></description>
    <author><%= $PLASTER_PARAM_TemplateAuthor %></author>
    <tags></tags>
</metadata>
<parameters>
</parameters>
<content>
...

所有的魔法都发生在这个templatefile的下半年。我们浏览了文件夹和文件的目标文件夹以创建内容部分。

...
  <content>
<%
$path = $PLASTER_DestinationPath

$folders = Get-ChildItem -Path $path -Directory -Recurse
$files = Get-ChildItem -Path $path -File -Recurse

$path += '\'  

foreach($node in $folders.fullname)
{
    $destination = $node.replace($path,'')
"    <file source='' destination='$destination'/>" 
}

foreach($node in $files.fullname)
{
    $source = $node.replace($path,'')
"    <file source='$source' destination=''/>" 
}

%>
  </content>
</plasterManifest>

Then we save this into a template file called 石膏 Template.aps1 and add a templateFile entry to our original 石膏 Template.xml content section.

<content>
    <templateFile source=" 石膏 Template.aps1" 
                  destination=" 石膏 Manifest.xml" />
</content>

I can call that template file anything I want and could easily have left the file extension as xml. I am currently using .aps1 as that designation.

采取飞行员的行动

Now we can take any folder and turn that into a Plaster template. The idea is that I would build out the folder first with all the files that should be included. Then instead of running New-PlasterManifest, I would use Invoke-Plaster with this template.

例子

这是一个快速举例,可以看到这个工作。让我们说我有两个常见的测试文件夹,即我丢弃了模块。我首先将所有这些移动到名为MyTests的新文件夹中。

MyTests
├───spec
│       module.feature
│       module.Steps.ps1
│
└───tests
        Export-PSGraph.Tests.ps1
        Feature.Tests.ps1
        Help.Tests.ps1
        Project.Tests.ps1
        Regression.Tests.ps1
        Unit.Tests.ps1

Now we run the GetPlastered template.

PS:> Invoke-Plaster -DestinationPath .\MyTests -TemplatePath .\GetPlastered
____  _           _
|  _ \| | __ _ ___| |_ ___ _ __
| |_) | |/ _` / __| __/ _ \ '__|
|  __/| | (_| \__ \ ||  __/ |
|_|   |_|\__,_|___/\__\___|_|
                                            v1.0.1
==================================================
Template  Name (MyTests):
Template Title (MyTests):
Author (KevinMarquette):
Destination path: .\MyTests
Create PlasterManifest.xml

I just accepted all the defaults and my .\MyTests\PlasterManifest.xml was created. Here is the contents of that file showing every file in the content section.

<?xml version="1.0" encoding="utf-8"?>
<plasterManifest schemaVersion="1.0" 
xmlns="http://www.microsoft.com/schemas/PowerShell/Plaster/v1">
<metadata>
    <name>MyTests</name>
    <id>3c3480d8-c2fa-4777-bb0c-a2453c8147c3</id>
    <version>0.0.1</version>
    <title></title>
    <description></description>
    <author>KevinMarquette</author>
    <tags>GetPlastered</tags>
</metadata>
<parameters>
</parameters>
<content>
    <file source='' destination='spec'/>
    <file source='' destination='tests'/>
    <file source='spec\module.feature' destination=''/>
    <file source='spec\module.Steps.ps1' destination=''/>
    <file source='tests\Feature.Tests.ps1' destination=''/>
    <file source='tests\Help.Tests.ps1' destination=''/>
    <file source='tests\Project.Tests.ps1' destination=''/>
    <file source='tests\Regression.Tests.ps1' destination=''/>
    <file source='tests\Unit.Tests.ps1' destination=''/>
</content>
</plasterManifest>

我们现在可以使用这个新og体育来部署我们的测试。

PS:> Invoke-Plaster -DestinationPath .\TestFolder -TemplatePath .\MyTests
____  _           _
|  _ \| | __ _ ___| |_ ___ _ __
| |_) | |/ _` / __| __/ _ \ '__|
|  __/| | (_| \__ \ ||  __/ |
|_|   |_|\__,_|___/\__\___|_|
                                            v1.0.1
==================================================
Destination path: .\TestFolder
Create spec\
Create tests\
Create spec\module.feature
Create spec\module.Steps.ps1
Create tests\Feature.Tests.ps1
Create tests\Help.Tests.ps1
Create tests\Project.Tests.ps1
Create tests\Regression.Tests.ps1
Create tests\Unit.Tests.ps1

PS:> cd .\TestFolder
PS:> tree /f

TestFolder
├───spec
│       module.feature
│       module.Steps.ps1
│
└───tests
        Feature.Tests.ps1
        Help.Tests.ps1
        Project.Tests.ps1
        Regression.Tests.ps1
        Unit.Tests.ps1

把它包裹起来

除了成立层之外,这真的是一个简单的og体育。这是一个有趣的项目,我已经发表了我的另一个 膏药og体育.

我正在努力将其设置为才能发布到Psgallery。我会在发生这种情况时更新这篇文章。我希望你喜欢它。