Skip to content

Commit c3534dd

Browse files
author
James Brundage
committed
Adding PipeScript Basics Demo (Fixes #420)
1 parent 3bd89b7 commit c3534dd

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

Demos/101-PipeScript-Basics.demo.ps1

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# 1. PipeScript 101
2+
3+
# PipeScript is a transpiled programming language built atop of PowerShell.
4+
5+
# It aims to make scripting more programmable, and programming more scriptable.
6+
7+
# Using PipeScript, we can expand the syntax of PowerShell in all sorts of ways.
8+
9+
# Let's start with a little example.
10+
11+
# Many modules include a bunch of files in a given directory.
12+
13+
# Instead of typing the same few lines over and over again, we can use the 'include' transpiler
14+
15+
Use-PipeScript {
16+
[Include('*-*.ps1')]$PSScriptRoot
17+
}
18+
19+
# This means we could write the .psm1 for almost any module in a single line.
20+
21+
# Let's prove the point by writing a quick module
22+
23+
{[include('*-*.ps1')]$psScriptRoot} | Set-Content .\PipeScriptDemo.ps.psm1
24+
25+
# Now, we can export or "build" the file using Export-PipeScript
26+
27+
Export-PipeScript .\PipeScriptDemo.ps.psm1
28+
29+
# We don't have any functions written yet, so let's make one of those, too.
30+
31+
# We can also use psuedo-attributes to make parameters with less code.
32+
# In this case, we use `vfp` to shorten ValueFromPipeline, and `vbn` to shorten ValueFromPipelineByPropertyName
33+
{
34+
function Send-Event {
35+
param(
36+
[vbn(Mandatory)]
37+
[Alias('EventName')]
38+
$SourceIdentifier,
39+
[vfp()]
40+
$MessageData,
41+
[vbn()]
42+
[switch]
43+
$Passthru
44+
)
45+
46+
process {
47+
$sentEvent = New-Event -SourceIdentifier $SourceIdentifier -MessageData $MessageData
48+
if ($Passthru) {
49+
$sentEvent
50+
}
51+
}
52+
}
53+
} | Set-Content .\Send-Event.ps.ps1
54+
55+
# Why stop at one command?
56+
57+
# Let's use another very powerful transpiler, Inherit.
58+
59+
{
60+
function Receive-Event
61+
{
62+
[Inherit('Get-Event',Abstract)]
63+
param()
64+
65+
process {
66+
$null = $PSBoundParameters.Remove('First')
67+
$events = @(& $BaseCommand @PSBoundParameters)
68+
[Array]::Reverse($events)
69+
$events
70+
71+
}
72+
}
73+
} | Set-Content .\Receive-Event.ps.ps1
74+
75+
# Let's export again, this time using it's alias, bps (Build-PipeScript)
76+
77+
bps
78+
79+
80+
# All set? Let's import our module:
81+
Import-Module .\PipeScriptDemo.psm1 -Force -PassThru

0 commit comments

Comments
 (0)