Skip to content

Commit

Permalink
Adding initial file set.
Browse files Browse the repository at this point in the history
  • Loading branch information
idavis committed Jul 28, 2012
1 parent 4b8d42b commit 2a89b2b
Show file tree
Hide file tree
Showing 12 changed files with 274 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* -crlf
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[B|b]in
*.nupkg
28 changes: 28 additions & 0 deletions NuGetPackageBuilder.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@echo on

:: Assumes robocopy and nuget.exe both on the path
:: This must be run as the administrator

SET DIR=%~dp0%
SET DESTDIR=%DIR%bin

IF EXIST %DESTDIR% goto deletebin

goto prepare

:deletebin
rmdir /s /q %DESTDIR%
if %ERRORLEVEL% NEQ 0 goto errors

:prepare
robocopy %DIR% %DESTDIR%\tools /E /B /NP /R:0 /W:0 /NJH /NJS /NS /NFL /NDL /XF ".git*" "Nuget*" "*.nupkg" /XD "%DIR%nuget" "%DIR%.git" "%DIR%bin"
robocopy %DIR%nuget %DESTDIR% /E /B /NP /R:0 /W:0 /NJH /NJS /NS /NFL /NDL

:build
nuget pack %DESTDIR%\prototype.nuspec
if %ERRORLEVEL% NEQ 0 goto errors

goto :eof

:errors
EXIT /B %ERRORLEVEL%
25 changes: 25 additions & 0 deletions examples/AddingFunctionsToPrototypes.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function New-SapiVoice {
$prototype = new-prototype @{Message0 = "This is Message 0"}

$prototype | new-function say {
param([string]$message)
$speaker = new-object -com SAPI.SpVoice
($speaker.Speak($message, 1)) | out-null
}

$prototype.SayPreRecordedMessage = { . $prototype.say $Message0 }

# always return the base prototype
$prototype
}

$voice = New-SapiVoice
$voice.say("Hello, World!")
# says 'Hello, World!'

# note that because we added the SayPreRecordedMessage directly to the hash base, we have to . invoke it
. $voice.SayPreRecordedMessage
# says 'This is Message 0'

$voice.SayPreRecordedMessage.Invoke()
# says 'This is Message 0'
44 changes: 44 additions & 0 deletions examples/HashBasedObject.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function New-SapiVoice {
$prototype = new-prototype @{Message0 = "This is Message 0"}

#The following would also work and a new, emtpy hash would be the base object
#$prototype = new-prototype $null
#$prototype = new-prototype

$prototype | new-function say {
param([string]$message)
$speaker = new-object -com SAPI.SpVoice
($speaker.Speak($message, 1)) | out-null
}

# Add a new key/value pair to this prototype
$prototype.Message1 = "This is Message 1"

# Add a new property to this prototype
$prototype | new-autoproperty Message2 "This is Message 2"
$prototype | new-property Message3 {"This is Message 3"}

# Add a proxy property to this prototype
$prototype | new-property Message4 {$this.Message1} {param([String]$value); $this.Message1 = $value}

# always return the base prototype
$prototype
}

$voice = New-SapiVoice
$voice.say($voice.Message1)
# says 'This is Message 1'

$voice.say($voice.Message2)
# says 'This is Message 2'

$voice.say($voice.Message3)
# says 'This is Message 3'

$voice.say($voice.Message4)
# says 'This is Message 1'

$voice.Message4 = "Rewriting Message 1 (via message 4)"
# This doesn't work. A new key, Message4 with the value 'Rewriting Message 1 (via message 4)' is added
# The prototype has a Message4 key and a Message4 value, but the hash access takes precedence
# From now on, all access to Message4 is instead routed to the key Message4 and our proxy is lost.
36 changes: 36 additions & 0 deletions examples/PsObjectBasedObject.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function New-SapiVoice {
$prototype = new-prototype (new-object psobject)
$prototype | new-function say {
param([string]$message)
$speaker = new-object -com SAPI.SpVoice
($speaker.Speak($message, 1)) | out-null
}

# Add a new property to this prototype
$prototype | new-autoproperty Message1 "This is Message 1"
$prototype | new-property Message2 {"This is Message 2"}

# Add a proxy property to this prototype
$prototype | new-property Message3 {$this.Message1} {param([String]$value); $this.Message1 = $value}

# always return the base prototype
$prototype
}

$voice = New-SapiVoice
$voice.say($voice.Message1)
# says 'This is Message 1'

$voice.say($voice.Message2)
# says 'This is Message 2'

$voice.say($voice.Message3)
# says 'This is Message 1'

$voice.Message3 = "Rewriting Message 1 (via message 3)"

$voice.say($voice.Message1)
# says 'Rewriting Message 1 (via message 3)'

$voice.say($voice.Message3)
# 'says 'Rewriting Message 1 (via message 3)'
23 changes: 23 additions & 0 deletions new-autoproperty.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<#
.SYNOPSIS
.DESCRIPTION
.PARAMETER name
.PARAMETER value
.EXAMPLE
.NOTES
#>
filter New-AutoProperty {
param(
[string]$name,
[object]$value
)
$variable = new-object System.Management.Automation.PSVariable $name, $value
$property = new-object System.Management.Automation.PSVariableProperty $variable
$_.psobject.properties.add($property)
}
22 changes: 22 additions & 0 deletions new-function.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<#
.SYNOPSIS
.DESCRIPTION
.PARAMETER name
.PARAMETER value
.EXAMPLE
.NOTES
#>
filter New-Function {
param(
[string]$name,
[scriptblock]$value
)
$method = new-object System.Management.Automation.PSScriptMethod "$name", $value
$_.psobject.members.add($method)
}
23 changes: 23 additions & 0 deletions new-property.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<#
.SYNOPSIS
.DESCRIPTION
.PARAMETER name
.PARAMETER value
.EXAMPLE
.NOTES
#>
filter New-Property {
param(
[string]$name,
[scriptblock]$getter,
[scriptblock]$setter = $null
)
$property = new-object System.Management.Automation.PSScriptProperty "$name", $getter, $setter
$_.psobject.properties.add($property)
}
55 changes: 55 additions & 0 deletions new-prototype.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<#
.SYNOPSIS
Creates a new prototypal object to be used as a base object.
.DESCRIPTION
.PARAMETER baseObject
The object which will be used as the underlying prototype. This is an optional parameter; if it is not included, the base object will be set to an empty hash table.
.EXAMPLE
function new-sapivoice {
$prototype = new-prototype (new-object psobject)
$prototype | new-function say {
param([string]$message)
$speaker = new-object -com SAPI.SpVoice
($speaker.Speak($message, 1)) | out-null
}
}
.NOTES
Changing your base object may cause very different behavior. For example:
function New-HashBasedObject {
$prototype = new-prototype @{Message0 = "This is Message 0"}
$prototype | new-function say {
param([string]$message)
$speaker = new-object -com SAPI.SpVoice
($speaker.Speak($message, 1)) | out-null
}
$prototype | new-autoproperty Message1 "This is Message 1"
# We can reference and rewrite Message1 using Message2 as a proxy
# unless the base is a hash in which setting the value of an existing property
# will instead add a new hash key/value. If your base is a hash,
# and you set Message2, Message1 will remain unchanged, and you will have a new
# hash key Message2 with the value you set. This will also override access to Message2
# by prefering the key to the property.
$prototype | new-property Message2 {$this.Message1} {param([String]$value); $this.Message1 = $value}
$prototype
}
#>

function New-Prototype {
param($baseObject = $null)
$base = $baseObject
if($baseObject -eq $null){ $base = @{} }
$prototype = new-object psobject $base
$prototype
}
15 changes: 15 additions & 0 deletions nuget/prototype.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>prototype.ps</id>
<version>0.1.0</version>
<authors>idavis</authors>
<owners>Ian Davis</owners>
<projectUrl>https://github.com/idavis/prototype.ps</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<summary>prototype is a base for creating functions that behave like object prototypes</summary>
<description>prototype is a base for creating functions that behave like object prototypes</description>
<language>en-US</language>
<tags>powershell</tags>
</metadata>
</package>
Binary file added prototype.psm1
Binary file not shown.

0 comments on commit 2a89b2b

Please sign in to comment.