Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #98 from Thilas/streams
Browse files Browse the repository at this point in the history
Streams support
  • Loading branch information
majkinetor authored Oct 29, 2017
2 parents 9dd0b93 + d9db7dc commit 40cbcc5
Show file tree
Hide file tree
Showing 18 changed files with 926 additions and 87 deletions.
23 changes: 23 additions & 0 deletions AU/Private/AUPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class AUPackage {
[bool] $Ignored
[string] $IgnoreMessage

[string] $StreamsPath
[pscustomobject] $Streams

AUPackage([string] $Path ){
if ([String]::IsNullOrWhiteSpace( $Path )) { throw 'Package path can not be empty' }

Expand All @@ -23,6 +26,9 @@ class AUPackage {

$this.NuspecXml = [AUPackage]::LoadNuspecFile( $this.NuspecPath )
$this.NuspecVersion = $this.NuspecXml.package.metadata.version

$this.StreamsPath = '{0}\{1}.json' -f $this.Path, $this.Name
$this.Streams = [AUPackage]::LoadStreams( $this.StreamsPath )
}

static [xml] LoadNuspecFile( $NuspecPath ) {
Expand All @@ -37,6 +43,23 @@ class AUPackage {
[System.IO.File]::WriteAllText($this.NuspecPath, $this.NuspecXml.InnerXml, $Utf8NoBomEncoding)
}

static [pscustomobject] LoadStreams( $StreamsPath ) {
if (!(Test-Path $StreamsPath)) { return $null }
return Get-Content $StreamsPath | ConvertFrom-Json
}

UpdateStream( $stream, $version ){
if (!$this.Streams) { $this.Streams = [pscustomobject] @{} }
$s = $stream.ToString()
$v = $version.ToString()
if ($this.Streams | Get-Member $s) {
if ($this.Streams.$s -ne 'ignore') { $this.Streams.$s = $v }
} else {
$this.Streams | Add-Member $s $v
}
$this.Streams | ConvertTo-Json | Set-Content $this.StreamsPath -Encoding UTF8
}

Backup() {
$d = "$Env:TEMP\au\" + $this.Name

Expand Down
102 changes: 102 additions & 0 deletions AU/Private/AUVersion.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
class AUVersion : System.IComparable {
[version] $Version
[string] $Prerelease
[string] $BuildMetadata

AUVersion([version] $version, [string] $prerelease, [string] $buildMetadata) {
if (!$version) { throw 'Version cannot be null.' }
$this.Version = $version
$this.Prerelease = $prerelease
$this.BuildMetadata = $buildMetadata
}

static [AUVersion] Parse([string] $input) { return [AUVersion]::Parse($input, $true) }

static [AUVersion] Parse([string] $input, [bool] $strict) {
if (!$input) { throw 'Version cannot be null.' }
$reference = [ref] $null
if (![AUVersion]::TryParse($input, $reference, $strict)) { throw "Invalid version: $input." }
return $reference.Value
}

static [bool] TryParse([string] $input, [ref] $result) { return [AUVersion]::TryParse($input, $result, $true) }

static [bool] TryParse([string] $input, [ref] $result, [bool] $strict) {
$result.Value = [AUVersion] $null
if (!$input) { return $false }
$pattern = [AUVersion]::GetPattern($strict)
if ($input -notmatch $pattern) { return $false }
$reference = [ref] $null
if (![version]::TryParse($Matches['version'], $reference)) { return $false }
$result.Value = [AUVersion]::new($reference.Value, $Matches['prerelease'], $Matches['buildMetadata'])
return $true
}

hidden static [string] GetPattern([bool] $strict) {
$versionPattern = '(?<version>\d+(?:\.\d+){0,3})'
# for now, chocolatey does only support SemVer v1 (no dot separated identifiers in pre-release):
$identifierPattern = "[0-9A-Za-z-]+"
# here is the SemVer v2 equivalent:
#$identifierPattern = "[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*"
if ($strict) {
return "^$versionPattern(?:-(?<prerelease>$identifierPattern))?(?:\+(?<buildMetadata>$identifierPattern))?`$"
} else {
return "$versionPattern(?:-?(?<prerelease>$identifierPattern))?(?:\+(?<buildMetadata>$identifierPattern))?"
}
}

[int] CompareTo($obj) {
if ($obj -eq $null) { return 1 }
if ($obj -isnot [AUVersion]) { throw "AUVersion expected: $($obj.GetType())" }
$t = $this.GetParts()
$o = $obj.GetParts()
for ($i = 0; $i -lt $t.Length -and $i -lt $o.Length; $i++) {
if ($t[$i].GetType() -ne $o[$i].GetType()) {
$t[$i] = [string] $t[$i]
$o[$i] = [string] $o[$i]
}
if ($t[$i] -gt $o[$i]) { return 1 }
if ($t[$i] -lt $o[$i]) { return -1 }
}
if ($t.Length -eq 1 -and $o.Length -gt 1) { return 1 }
if ($o.Length -eq 1 -and $t.Length -gt 1) { return -1 }
if ($t.Length -gt $o.Length) { return 1 }
if ($t.Length -lt $o.Length) { return -1 }
return 0
}

[bool] Equals($obj) { return $this.CompareTo($obj) -eq 0 }

[int] GetHashCode() { return $this.GetParts().GetHashCode() }

[string] ToString() {
$result = $this.Version.ToString()
if ($this.Prerelease) { $result += "-$($this.Prerelease)" }
if ($this.BuildMetadata) { $result += "+$($this.BuildMetadata)" }
return $result
}

[string] ToString([int] $fieldCount) {
if ($fieldCount -eq -1) { return $this.Version.ToString() }
return $this.Version.ToString($fieldCount)
}

hidden [object[]] GetParts() {
$result = @($this.Version)
if ($this.Prerelease) {
$this.Prerelease -split '\.' | % {
# if identifier is exclusively numeric, cast it to an int
if ($_ -match '^[0-9]+$') {
$result += [int] $_
} else {
$result += $_
}
}
}
return $result
}
}

function ConvertTo-AUVersion($Version) {
return [AUVersion] $Version
}
6 changes: 1 addition & 5 deletions AU/Private/is_version.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
# Returns [bool]
function is_version( [string] $Version ) {
$re = '^(\d{1,16})\.(\d{1,16})\.*(\d{1,16})*\.*(\d{1,16})*(-[^.-]+)*$'
if ($Version -notmatch $re) { return $false }

$v = $Version -replace '-.+'
return [version]::TryParse($v, [ref]($__))
return [AUVersion]::TryParse($Version, [ref]($__))
}
24 changes: 24 additions & 0 deletions AU/Public/Get-Version.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Author: Thomas Démoulins <[email protected]>

<#
.SYNOPSIS
Get a semver-like object from a given version string.
.DESCRIPTION
This function parses a string containing a semver-like version
and returns an object that represents both the version (with up to 4 parts)
and optionally a pre-release and a build metadata.
The parsing is quite flexible:
- the string can starts with a 'v'
- there can be no hyphen between the version and the pre-release
- extra spaces (between any parts of the semver-like version) are ignored
#>
function Get-Version {
[CmdletBinding()]
param(
# Version string to parse.
[string] $Version
)
return [AUVersion]::Parse($Version, $false)
}
14 changes: 9 additions & 5 deletions AU/Public/Push-Package.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@

<#
.SYNOPSIS
Push latest created package to the Chocolatey community repository.
Push latest (or all) created package(s) to the Chocolatey community repository.
.DESCRIPTION
The function uses they API key from the file api_key in current or parent directory, environment variable
or cached nuget API key.
#>
function Push-Package() {
param(
[switch] $All
)
$api_key = if (Test-Path api_key) { gc api_key }
elseif (Test-Path ..\api_key) { gc ..\api_key }
elseif ($Env:api_key) { $Env:api_key }

$package = ls *.nupkg | sort -Property CreationTime -Descending | select -First 1
if (!$package) { throw 'There is no nupkg file in the directory'}
$packages = ls *.nupkg | sort -Property CreationTime -Descending
if (!$All) { $packages = $packages | select -First 1 }
if (!$packages) { throw 'There is no nupkg file in the directory'}
if ($api_key) {
cpush $package.Name --api-key $api_key --source https://push.chocolatey.org
$packages | % { cpush $_.Name --api-key $api_key --source https://push.chocolatey.org }
} else {
cpush $package.Name --source https://push.chocolatey.org
$packages | % { cpush $_.Name --source https://push.chocolatey.org }
}
}
3 changes: 2 additions & 1 deletion AU/Public/Update-AUPackages.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function Update-AUPackages {
UpdateTimeout - Timeout for background job in seconds, by default 1200 (20 minutes).
Force - Force package update even if no new version is found.
Push - Set to true to push updated packages to Chocolatey community repository.
PushAll - Set to true to push all updated packages and not only the most recent one per folder.
WhatIf - Set to true to set WhatIf option for all packages.
PluginPath - Additional path to look for user plugins. If not set only module integrated plugins will work
Expand Down Expand Up @@ -214,7 +215,7 @@ function Update-AUPackages {
if ( "$type" -ne 'AUPackage') { throw "'$using:package_name' update script didn't return AUPackage but: $type" }

if ($pkg.Updated -and $Options.Push) {
$pkg.Result += $r = Push-Package
$pkg.Result += $r = Push-Package -All:$Options.PushAll
if ($LastExitCode -eq 0) {
$pkg.Pushed = $true
} else {
Expand Down
Loading

0 comments on commit 40cbcc5

Please sign in to comment.