Skip to content

Commit ba5e643

Browse files
Merge pull request #13 from PowerShellWeb/Initial-Vector
Vector 0.1
2 parents ec4264a + f8c3652 commit ba5e643

18 files changed

+1383
-1
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: [StartAutomating]

.github/workflows/BuildVector.yml

Lines changed: 501 additions & 0 deletions
Large diffs are not rendered by default.

Build/GitHub/Jobs/BuildVector.psd1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@{
2+
"runs-on" = "ubuntu-latest"
3+
if = '${{ success() }}'
4+
steps = @(
5+
@{
6+
name = 'Check out repository'
7+
uses = 'actions/checkout@main'
8+
},
9+
'RunEZOut' # ,
10+
<#@{
11+
name = 'Run Vector (on branch)'
12+
if = '${{github.ref_name != ''main''}}'
13+
uses = './'
14+
id = 'VectorAction'
15+
}#>
16+
# 'BuildAndPublishContainer'
17+
)
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@{
2+
name = 'PublishTestResults'
3+
uses = 'actions/upload-artifact@main'
4+
with = @{
5+
name = 'PesterResults'
6+
path = '**.TestResults.xml'
7+
}
8+
if = '${{always()}}'
9+
}
10+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#requires -Module PSDevOps
2+
Import-BuildStep -SourcePath (
3+
Join-Path $PSScriptRoot 'GitHub'
4+
) -BuildSystem GitHubWorkflow
5+
6+
Push-Location ($PSScriptRoot | Split-Path)
7+
New-GitHubWorkflow -Name "Build Vector Module" -On Push,
8+
PullRequest,
9+
Demand -Job TestPowerShellOnLinux,
10+
TagReleaseAndPublish, BuildVector -Environment ([Ordered]@{
11+
REGISTRY = 'ghcr.io'
12+
IMAGE_NAME = '${{ github.repository }}'
13+
}) -OutputPath .\.github\workflows\BuildVector.yml
14+
15+
Pop-Location

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## Vector 0.1:
2+
3+
* Initial Release of Vector module
4+
* Commands:
5+
* Get-Vector (#1)
6+
* Get-Vector2 (#2)
7+
* Get-Vector3 (#3)
8+
* Get-Vector4 (#4)
9+
* Vector Workflow (#5)
10+
* Vector Tests (#6)
11+
* Vector Docs
12+
* Demo (#7)
13+
* README (#8)
14+
* FUNDING (#9)
15+
* CODE_OF_CONDUCT (#10)
16+
* CONTRIBUTING (#11)
17+
* SECURITY (#12)

CODE_OF_CONDUCT.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Code of Conduct
2+
3+
We have a simple subjective code of conduct:
4+
5+
1. Be Respectful
6+
2. Be Helpful
7+
3. Do No Harm
8+
9+
Failure to follow the code of conduct may result in blocks or banishment.

CONTRIBUTING.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Contibuting
2+
3+
We welcome suggestions and careful contributions.
4+
5+
To suggest something, please [open an issue](https://github.com/PowerShellWeb/Vector/issues) or start a [discussion](https://github.com/PowerShellWeb/Vector/discussion)
6+
7+
To add a feature, please open an issue and create a pull request.
8+
9+
## Contributing Examples
10+
11+
Examples are more than welcome! To contribute an example, please open an issue describing your example and create a pull request.

Commands/Get-Vector.ps1

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
function Get-Vector
2+
{
3+
<#
4+
.SYNOPSIS
5+
Gets a one dimensional vector
6+
.DESCRIPTION
7+
Gets a one dimensional vector (or, more simply, a list of numbers)
8+
9+
This will convert a variety of types into numbers.
10+
.NOTES
11+
This attempts to convert any type into a number.
12+
13+
Some types are special:
14+
15+
* Primitive types will be casted to float
16+
* `[Numerics.Vector2]`,`[Numerics.Vector3]`,`[Numerics.Vector4]` output each component
17+
* `[string]`s that match a range (`$start..$end`) will output that range
18+
* `[Version]`s will output each numeric component
19+
* `[semver]`s will output each numeric component, followed by the bytes of a release type
20+
* `[DateTime]` and `[DateTimeOffset]` will become a series of 12 numbers
21+
* `year`,`month`,`day`
22+
* `hour`, `minute`, `second`
23+
* `millisecond`, `microsecond`, `nanosecond`
24+
* `offset.hours`, `offset.minutes`, `offset.seconds`
25+
* `[string]s` will return their bytes in the current `$outputEncoding`
26+
* Anything unknown will be stringified and the bytes will be returned
27+
#>
28+
[Alias('Vector','Vector1','V1')]
29+
param()
30+
31+
filter toVector {
32+
$arg = $_
33+
# Return primitive types
34+
if ($arg.GetType -and $arg.GetType().IsPrimitive) {
35+
# casted to float
36+
return ($arg -as [float])
37+
}
38+
# Return vector components
39+
if ($arg -is [ValueType]) {
40+
if ($arg -is [Numerics.Vector2]) {
41+
return $arg.X,$arg.Y
42+
}
43+
elseif ($arg -is [Numerics.Vector3]) {
44+
return $arg.X,$arg.Y,$arg.Z
45+
}
46+
elseif ($arg -is [Numerics.Vector4]) {
47+
return $arg.X,$arg.Y,$arg.Z, $arg.W
48+
}
49+
}
50+
# Look for inline ranges.
51+
if ($arg -is [string]) {
52+
if ($arg -match '^\d..\d') {
53+
$start, $end = $arg -split '\..', 2
54+
$startInt = ($start -as [int])
55+
$endInt = ($end -as [int])
56+
if ($null -ne $startInt -and $null -ne $endInt) {
57+
# If found, return them expanded.
58+
return ($startInt..$endInt)
59+
}
60+
}
61+
if ($arg -as [float]) {
62+
return $arg -as [float]
63+
}
64+
}
65+
66+
67+
# If the arg is a version, get each number of the version
68+
if ($arg -is [version]) {return $arg.Major,$arg.Minor,$arg.Build,$arg.Revision}
69+
70+
# If we support semver and the arg is semver
71+
if (('semver' -as [type]) -and $arg -is [semver]) {
72+
# Return the numeric parts of the semver
73+
$arg.Major,$arg.Minor,$arg.Patch
74+
# and turn any string portions to bytes
75+
if ($arg.PreReleaseLabel) {
76+
# make sure to include a leading dash for pre-releases
77+
$OutputEncoding.GetBytes("-$($arg.PreReleaseLabel)")
78+
}
79+
80+
if ($arg.BuildLabel) {
81+
# make sure to include a leading plus for build labels
82+
$OutputEncoding.GetBytes("+$($arg.BuildLabel)")
83+
}
84+
return
85+
}
86+
87+
# If the arg is a datetime or datetimeoffset
88+
if ($arg -is [DateTime] -or $arg -is [DateTimeOffset]) {
89+
# make it an offset, and then output 12 values
90+
$dateArg = $arg -as [DateTimeOffset]
91+
# * `year` `month` `day`
92+
$dateArg.Year, $dateArg.Month, $dateArg.Day,
93+
# * `hour` `minute` `second`
94+
$dateArg.Hour, $dateArg.Minute, $dateArg.Second,
95+
# * `millisecond`, `microsecond`, `nanosecond`
96+
$dateArg.Millisecond, $dateArg.Microsecond, $dateArg.Nanosecond,
97+
# * `offset hours`, `offset minutes`, `offset seconds`
98+
$dateArg.Offset.Hours,$dateArg.Offset.Minutes,$dateArg.Offset.Seconds
99+
return
100+
}
101+
# If the arg is a string
102+
if ($arg -is [string]) {
103+
# return its bytes
104+
return $OutputEncoding.GetBytes($arg)
105+
}
106+
# any input we have not caught, stringify and turn to bytes
107+
return $OutputEncoding.GetBytes("$arg")
108+
}
109+
110+
111+
# Collect all of our input and arguments
112+
$allIn = @($input) + @(
113+
foreach ($arg in $args) {
114+
$arg
115+
}
116+
)
117+
118+
return $allIn | toVector
119+
}

Commands/Get-Vector2.ps1

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
function Get-Vector2 {
2+
<#
3+
.SYNOPSIS
4+
Gets a Vector2
5+
.DESCRIPTION
6+
Gets any input and arguments as a Vector2
7+
.LINK
8+
https://learn.microsoft.com/en-us/dotnet/api/system.numerics.vector2?wt.mc_id=MVP_321542
9+
.EXAMPLE
10+
# Create a vector out of two numbers
11+
Vector2 1 2
12+
.EXAMPLE
13+
(Vector2 1 2) + (Vector2 2 1)
14+
.EXAMPLE
15+
(Vector2 1 2) - (Vector2 2 1)
16+
.EXAMPLE
17+
# Create a thousand vectors
18+
$vectors = Vector2 1..2kb
19+
.EXAMPLE
20+
# Create a thousand vectors in random order, using the pipeline
21+
$vectors = 1..2kb | Get-Random -Count 2kb | Vector2
22+
.EXAMPLE
23+
# Create a vector from a string
24+
$vector = Vector2 "hi"
25+
#>
26+
[Alias('V2','Vector2')]
27+
param()
28+
# Collect all of our input and arguments
29+
$allIn = @($input) + @(
30+
foreach ($arg in $args) {
31+
$arg
32+
}
33+
)
34+
35+
# and expand them
36+
$expandAllIn = @($allIn | Vector)
37+
38+
For ($n = 0; $n -lt $expandAllIn.Length; $n+=2) {
39+
$argSet = $expandAllIn[$n..($n+1)] -as [float[]]
40+
if ($argSet.Length -eq 1) {
41+
[Numerics.Vector2]::new($argSet[0])
42+
} else {
43+
[Numerics.Vector2]::new($argSet)
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)