Skip to content

Commit

Permalink
Handle multiple streams as per majkinetor#74
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Démoulins authored and majkinetor committed Oct 29, 2017
1 parent 696bf40 commit 7201c2b
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 85 deletions.
25 changes: 25 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,25 @@ class AUPackage {
[System.IO.File]::WriteAllText($this.NuspecPath, $this.NuspecXml.InnerXml, $Utf8NoBomEncoding)
}

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

UpdateStreams( [hashtable] $streams ){
$streams.Keys | % {
$stream = $_.ToString()
$version = $streams[$_].Version.ToString()
if($this.Streams | Get-Member $stream) {
$this.Streams.$stream = $version
} else {
$this.Streams | Add-Member $stream $version
}
}
$this.Streams | ConvertTo-Json -Compress | Set-Content $this.StreamsPath -Encoding UTF8
}

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

Expand Down
70 changes: 70 additions & 0 deletions AU/Private/AUVersion.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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})'
$identifierPattern = "[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*"
if ($strict) {
return "^$versionPattern(?:-(?<prerelease>$identifierPattern))?(?:\+(?<buildMetadata>$identifierPattern))?`$"
} else {
return "^v?\s*$versionPattern(?:-?(?<prerelease>$identifierPattern))?(?:\+(?<buildMetadata>$identifierPattern))?\s*`$"
}
}

[int] CompareTo($obj) {
if ($obj -eq $null) { return 1 }
if ($obj -isnot [AUVersion]) { throw "AUVersion expected: $($obj.GetType())" }
if ($obj.Version -ne $this.Version) { return $this.Version.CompareTo($obj.Version) }
if ($obj.Prerelease -and $this.Prerelease) { return $this.Prerelease.CompareTo($obj.Prerelease) }
if (!$obj.Prerelease -and !$this.Prerelease) { return 0 }
if ($obj.Prerelease) { return 1 }
return -1
}

[bool] Equals($obj) { return $obj -is [AUVersion] -and $obj -and $this.ToString().Equals($obj.ToString()) }

[int] GetHashCode() { return $this.ToString().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)
}
}
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 7201c2b

Please sign in to comment.