Nupkg.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
# ipmo process
# ipmo assemblymeta -Global
# ipmo semver -Global
# ipmo newtonsoft.json
# ipmo pathutils

$root = "."
if (![string]::IsNullOrEmpty($PSScriptRoot)) {
    $root = $PSScriptRoot
}
#if ($MyInvocation.MyCommand.Definition -ne $null) {
# $root = $MyInvocation.MyCommand.Definition
#}
$helpersPath = $root
@("choco-utils.ps1", "internal.ps1", "nuget-helpers.ps1") |
    % { 
        $p = get-item "$root/functions/$_"
        . $p.fullname
    }


function Expand-ZIPFile
{
[CmdletBinding()]
param($file, $destination)
    if (!(test-path $file)) { throw "zip file '$file' not found in '$((get-item .).FullName)'" }
    $shell = new-object -com shell.application
    $zip = $shell.NameSpace((get-item $file).FullName)
    if ($zip -eq $null) { throw "failed to open zipfile '$file'" }
    try {
        foreach($item in $zip.items())
        {
            write-verbose "extracting $($item.Name) to $destination"
            if ($item.name -eq "lib") {                
                $shell.Namespace((get-item $destination).fullname).copyhere($item, 0x14)
            } else {
                $shell.Namespace((get-item $destination).fullname).copyhere($item, 0x14)
            }
        }
    } catch {
        throw (New-Object System.Exception "Failed to extract zip '$file' to '$destination'", $_.Exception)
    }
}

function new-nuspec($projectPath = ".") {
    pushd
    try {
        if (!(test-path $projectpath)) { throw "file '$projectPath' not found"}
        if ((get-item $projectpath).psiscontainer) {
            $dir = $projectpath
            $projects = @(get-childitem $dir -filter "*.csproj")
            if ($projects.Length -gt 1) {
                throw "more than one csproj file found in dir '$projectPath'"
            } 
            if ($projects.Length -lt 1) {
                throw "no csproj file found in dir '$projectPath'"
            }
            $projectpath = $projects[0].fullname
        }
        $dir = split-path $projectpath -parent
        $csproj = split-path $projectpath -leaf
        cd $dir
        nuget spec $csproj
    } finally {
        popd
    }
}

function Get-InstalledNugets($packagesdir) {
    $subdirs = get-childitem $packagesdir -Directory
    $result = @()
    foreach($s in $subdirs) {
        $name = get-packageName $s
        $version = get-packageversion $s
        
        if ($name -ne $null -and $version -ne $null) {
            $result += new-object -type pscustomobject -Property @{ 
                Name = $name; Version = $version 
            }
        }
    }
    
    return $result
}

function Get-AvailableNugets ($source) {
    $l = invoke nuget list -source $source -passthru -silent
    $l = $l | % {
        $s = $_.split(" ")
         new-object -type pscustomobject -Property @{ 
                Name = $s[0]; Version = $s[1] 
            }
    }
    return $l
}


function invoke-nugetpush {
    [CmdletBinding(SupportsShouldProcess=$true)]
    param(
    [Parameter(ValueFromPipeline=$true,Position=0)] $file = $null, 
    [Parameter(Mandatory=$false)] $source,
    [Parameter(Mandatory=$false)] $apikey,
    [switch][bool] $Symbols,
    [switch][bool] $Build,
    [switch][bool] $ForceDll,
    [switch][bool] $Stable,
    [switch][bool] $useDotnet,
    [Alias("newVersion")]
    [switch][bool] $incrementVersion,
    [switch][bool] $keepVersion,
    $suffix = $null,
    $branch = $null,
    $buildProperties = @{}) 
process {
    if ($stable -and $incrementVersion) {
        throw "-Stable cannot be used with -incrementVersion"
    }

    $bound = $PSBoundParameters
    if ($bound.stable -eq $null) {
        $Symbols = !$stable
    }
    if ($bound.build -eq $null) {
        # build dev packages by default
        if (!$stable) { $build = $true }
        # build stable packages by default
        if ($stable) { $build = $true }
    }
    if ($file -eq $null -and !$build) {
        $files = @(get-childitem -filter "*.nupkg" | sort LastWriteTime -Descending)
        if ($Symbols) {
            $files = $files | ? { $_ -match "\.symbols\." }
        }
        if ($files.length -gt 0) {
            $file = $files[0].name
        } else {
           # will build
        }
        
    }
    if ($file -eq $null -or !($file.EndsWith(".nupkg"))){
        $nupkg = invoke-nugetpack $file -Build:$build -symbols:$symbols -stable:$stable -forceDll:$forceDll -buildProperties:$buildProperties -usedotnet:$usedotnet -keepVersion:$keepVersion -suffix:$suffix -incrementVersion:$incrementVersion -branch:$branch
    } else {
        $nupkg = $file
    }
    #in case multiple nupkgs are created
    if ($symbols) {
        $symbolpkg = @($nupkg) | ? { $_ -match "\.symbols\." } | select -last 1
        if ($symbolpkg -ne $null) {
            $nupkg = $symbolpkg
            $nosymbolspkg = $nupkg -replace "\.symbols\.","."
            if (test-path $nosymbolspkg) {
                write-verbose "copying '$nosymbolspkg' to '$($nupkg -replace "\.symbols\.",".nosymbols.")'"
                copy-item $nosymbolspkg "$($nupkg -replace "\.symbols\.",".nosymbols.")" 
            }
            write-host "push Symbols package: replacing $nosymbolspkg with $symbolpkg"
            copy-item $symbolpkg $nosymbolspkg -Force
            $nupkg = $nosymbolspkg
        } else {
            write-warning "did not find a matching .symbols.nupkg package"
            $nupkg = @($nupkg)| ? { $_ -notmatch "\.symbols\." }  | select -last 1
        }
    } else {
        $nupkg = @($nupkg)| ? { $_ -notmatch "\.symbols\." }  | select -last 1

    }

    if ($nupkg -eq $null) {
        throw "no nupkg produced"
    }

    $nupkg = (get-item $nupkg).FullName

    if ($source -eq $null) {
        $source = $env:NUGET_PUSH_SOURCE
        if ($source -ne $null) {
            write-warning "pushing to default source from `$env:NUGET_PUSH_SOURCE=$env:NUGET_PUSH_SOURCE"
        } else {
            Write-Warning "If you want to push to nuget, set `$env:NUGET_PUSH_SOURCE variable to target feed"
            return
        }
    }

    $sources = @($source)
    foreach($source in $sources) {
        if ($source -eq $null) {
            write-error "no source specified. skipping push."
            continue
        }
        Write-Host "pushing package '$nupkg' to '$source'"
        $p = @(
            $nupkg
            "-NonInteractive"
        )

        # TODO: handle rolling updates for dotnet/dnx/nuget3 model
        if ($source.startsWith("rolling")) {
            
            pushd
            try {
                $versionhint = $null

                if ($source -match "rolling:(.*):v(.*)") {
                    cd $matches[1]
                    $versionhint = $matches[2]
                }
                elseif ($source -match "rolling:v(.*)") {
                    $versionhint = $matches[1]
                }
                elseif ($source -match "rolling:(.*)") {
                    cd $matches[1]
                }
            
                
                $packagesDirs = find-packagesdir -all
                if ($packagesDirs -eq $null) { throw "packages dir not found" }
                write-host "rolling source specified. Will extract to $packagesDirs"    
                $packagename = (split-packagename (split-path -leaf $nupkg)).Name
                
                foreach($packagesDir in $packagesDirs) {        
                    $nuget = find-nugetPath $packagename -packagesRelPath $packagesDir -versionhint $versionhint
                    $packagedir = $nuget.PackageDir
                    if ($packagedir -eq $null) {
                        write-warning "package dir for package '$packagename' (version=$versionhint) not found in '$packagesdir'"
                        continue
                    }
                    $zip = "$nupkg.zip"
                    copy-item $nupkg $zip

                    
                    Expand-ZIPFile -file $zip -destination $packageDir -verbose
                    copy-item $nupkg $packageDir -Verbose
                    $lib = get-childitem $packageDir -Filter "lib"
                    if($lib -ne $null) {
                        $frameworks = get-childitem ($lib.FullName)
                        foreach($f in $frameworks) {
                            if ($f.name.contains("%")) {
                                [Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null
                                $decoded = $([System.Web.HttpUtility]::UrlDecode($f.fullname))
                                if ($decoded -ne $f.fullname) {
                                    if (test-path $decoded) { rmdir $decoded -Force -Recurse }
                                    Rename-Item -path $($f.fullname) -NewName $decoded -Verbose -Force
                                }
                            }
                        }
                    }

                }
            } finally {
                popd
            }
        }
        else {
            if ($source -ne $null -and $apikey -eq $null) {
                # try to get apikey from cache
                if ($null -eq (gmo cache)) {
                    ipmo cache -erroraction ignore -MinimumVersion 1.1.0
                }
                if ($null -eq $apikey -and $null -ne (gmo cache)) {
                    $apikey = get-passwordcached $source
                    if ($apikey -ne $null) { write-verbose "found cached api key for source $source" }
                }                

                #try to get api key from global settings
                if ($null -eq (gmo cache)) {
                    ipmo cache -erroraction ignore -MinimumVersion 1.1.0
                }
                if ($null -eq $apikey -and $null -ne (gmo cache)) {
                    $settings = cache\import-settings
                    if ($settings -ne $null) {
                        $apikey = $settings["$source.apikey"]
                        if ($apikey -ne $null) {
                            $cred = new-object "system.management.automation.pscredential" "dummy",$apikey
                            $apikey = $cred.GetNetworkCredential().Password
                            if ($apikey -ne $null) { write-verbose "found api key in globalsettings for source $source" }
                        }
                    }
                }
            }

            if ($source -ne $null) {
                $p += "-source",$source
            }
            if ($apikey -ne $null) {
                write-verbose "using apikey $apikey"
                $p += "-apikey",$apikey
            } else {
                write-verbose "no apikey found"
            }
        
            if ($PSCmdlet.ShouldProcess("pushing package '$p'")) {
                write-verbose "nuget push $p"
                $o = invoke nuget push @p -passthru -verbose -nothrow -WhatIf:($WhatIfPreference)
                if ($lastexitcode -ne 0) {
                    $summary = $o | select -last 1
                    if ($summary -is [System.Management.Automation.ErrorRecord]) { $summary = $summary.Exception.Message }
                    throw "nuget command failed! `r`n$summary"
                }                
            }
        }
    }

    write-output $nupkg
}
}

function invoke-nugetpack {
    [CmdletBinding()]
    param(
    [Parameter(ValueFromPipeline=$true,Position=0)]
    $nuspecOrCsproj = $null,
    [switch][bool] $Build,
    [switch][bool] $Symbols,
    [switch][bool] $NoProjectReferences,
    [switch][bool] $Stable,
    [switch][bool] $ForceDll,
    [switch][bool] $useDotnet,
    [Alias("newVersion")]
    [switch][bool] $incrementVersion,
    $suffix = $null,
    $branch = $null,
    [switch][bool] $keepVersion,
    $buildProperties = @{}) 
process {    
    pushd
    try {
        if ($nuspecorcsproj -ne $null -and (test-path $nuspecOrCsproj) -and (get-item $nuspecorcsproj).PsIsContainer) {
            cd $nuspecorcsproj
            $nuspecorcsproj = $null
        } 
        if ($nuspecorcsproj -eq $null) {
            $csprojs = @(gci . -filter "*.csproj") 
            if ($csprojs.Length -eq 0) {
                $csprojs += @(gci . -filter "project.json")  
            }
            if ($csprojs.length -eq 1) {
                $nuspecorcsproj = $csprojs[0].Name
            } else {
                throw "found multiple csproj/project.json files in '$((gi .).FullName)'. please choose one."
            }
        }
        if ($nuspecorcsproj -eq $null) {
            $csprojs = @(gci . -filter "*.nuspec")
            if ($csprojs.length -eq 1) {
                $nuspecorcsproj = $csprojs[0].Name
            } else {
                throw "found multiple nuspec files in '$((gi .).FullName)'. please choose one."
            }
        }

        if ($incrementVersion -and !$build) {
            throw "-incrementVersion has no meaning when used without -build"
        }
        if ($nuspecorcsproj.endswith("project.json")) {
            $useDotnet = $true
            $dotnet = get-dotnetcommand
            if ($dotnet -eq $null) { $dotnet = "dotnet" }
            if ($useDotnet) { $dotnet = "dotnet" }
        }
        
        if ($nuspecOrCsproj.endswith("csproj")) {
            $xml = [xml](get-content $nuspecOrCsproj)
            if ($xml.Project.sdk -ne $null) {
                $useDotnet = $true
                $dotnet = get-dotnetcommand
                if ($dotnet -eq $null) { $dotnet = "dotnet" }
                if ($useDotnet) { $dotnet = "dotnet" }
                write-verbose "using 'dotnet' for this csproj (SDK attribute is set in xml)"
            } else {
                write-verbose "using standard 'nuget' for this csproj (SDK attribute is NOT set in xml)"
            }
        }

        $dir = split-path -parent $nuspecorcsproj
        if ([string]::isnullorempty($dir)) { $dir = "." }
        $nuspecorcsproj = split-path -Leaf $nuspecorcsproj
        write-verbose "packing nuget for $(split-path -leaf $nuspecorcsproj) in $((get-item $dir).fullname)"
            
        cd $dir

        if ($Build) {
            if ($suffix -ne $null) {
                $newver = update-buildversion -component Suffix -value $suffix
            }
            else {
                if ($incrementVersion) { $newver = update-buildversion -component patch } 
                elseif (!$keepversion) { $newver = update-buildversion } 
                if ($branch -ne $null) {
                    $newver = update-buildversion -component SuffixBranch -value $branch
                }
                if ($stable) {
                    $newver = update-buildversion -stable:$stable
                }
            }
            if ($useDotnet) {
                    # don't restore - if user has just built the project, it is already restored
                    # if not, dotnet will detect out-of-date project.lock.json and build will fail
                    #$o = invoke $dotnet restore -verbose:$($verbosePreference="Continue") -passthru
                    $o = invoke $dotnet build -verbose:$($verbosePreference="Continue") -passthru
            }
            else {
                $a = @()
                if ($forceDll) {
                    $a += @("-p:OutputType=Library")
                }
                if ($buildProperties -ne $null) {
                    $buildProperties.GetEnumerator() | % { $a += @("-p:$($_.Key)=$($_.Value)") }
                }
                write-host "building project: msbuild $nuspecorcsproj $a "
                $o = invoke msbuild $nuspecorcsproj $a -passthru                
            }
        }
    
        if ($useDotnet) {
            $a = @() 
            
            $o = invoke $dotnet pack @a -verbose -passthru
            $success = $o | % {
                    if ($_ -match "(?<project>.*) -> (?<nupkg>.*\.nupkg)") {
                        return $matches["nupkg"]
                    }
                    if ($_ -match "Successfully created package '(?<nupkg>.*\.nupkg)'") {
                        return $matches["nupkg"]
                    }
            }
            
            # for some reason, dotnet pack results are duplicated
            <#
            write-host "success:"
            foreach($l in $success) { write-host ": $l" }
            write-host "output:"
            foreach($l in $o) { write-host ": $l" }
            #>

            return $success | select -unique
        }
        else {
            $a = @() 
            if ($forcedll) {
                $tmpproj = "$nuspecorcsproj.tmp.csproj" 
                copy-item $nuspecOrCsproj $tmpproj -Force
                $c = get-content $tmpproj
                $c = $c | % { $_ -replace "<OutputType>Exe</OutputType>","<OutputType>Library</OutputType>" } 
                $c | out-string | out-file $tmpproj -Encoding utf8
                
                $a += @(
                    "$tmpproj"
                )        
            } else {    
                $a += @(
                    "$nuspecorcsproj"
                )     
            }
            if (!$noprojectreferences) {
                $a += "-IncludeReferencedProjects"
            }
            if ($symbols) {
                $a += "-Symbols"
            }
            
            if ($buildProperties -ne $null) {
                    $properties = ""
                    $buildProperties.GetEnumerator() | % { $properties += "$($_.Key)=$($_.Value);" }
                    if (![string]::IsNullOrEmpty($properties)) {
                        $a += @("-Properties","$properties")
                    }
                }
            
            write-host "packing nuget"
            
            $o = invoke nuget pack @a -passthru -verbose
            if (($tmpproj -ne $null) -and (test-path $tmpproj)) { 
                remove-item $tmpproj
            }
            if ($lastexitcode -ne 0) {
                throw "nuget command failed! `r`n$($o | out-string)"
            } else {
                $success = $o | % {
                    if ($_ -match "Successfully created package ['\`"](.*)['\`"]") {
                        return $matches[1]
                    }
                    if ($_ -match "utworzono pakiet ['\`"](.*)['\`"]") {
                        return $matches[1]
                    }
                }
             
                return $success | select -unique
            }
        }
    } finally {
        popd
    }
}
}


function update-nugetmeta {
    [CmdletBinding(SupportsShouldProcess=$true)]
    param($path = ".", $description = $null, [Alias("company")]$author = $null, $version = $null)
    
    $verb = $psBoundParameters["Verbose"] -eq $true
    write-verbose "generating nuget meta"
    $v = get-assemblymeta "Description" $path
    if ([string]::isnullorempty($v) -or $description -ne $null) {
        if ($description -eq $null) { $description =  "no description" }
        set-assemblymeta "Description" $description $path
    } else {
        write-verbose "found Description: $v"
    }
    
    $v = get-assemblymeta "Company" $path   
    if ([string]::isnullorempty($v) -or $company -ne $null) {
        if ($company -eq $null) { $company =  "MyCompany" }
        set-assemblymeta "Company" $company $path
    } else {
        write-verbose "found Company: $v"
    }
   
   Update-AssemblyVersion $version $path -Verbose:$verb
}



function get-vcsname($path = ".") {
    $reporoot = $null
     $path = (get-item $path).FullName
        if (!(get-item $path).PsIsContainer) {
            $dir = split-path -Parent $path
        }
        else {
            $dir = $path
        }
        while(![string]::IsNullOrEmpty($dir)) {
            if ((test-path "$dir/.hg") -or (Test-Path "$dir/.git")) {
                $reporoot = $dir
                break;
            }
            $dir = split-path -Parent $dir
        }
     if ($reporoot -ne $null) {         
        if (test-path "$reporoot/.hg") { return "hg" }
        if (test-path "$reporoot/.git") { return "git" }
     }   
     
     return $null
    
}

function get-vcsbranch() {
    $vcs = get-vcsname   
    $branch = $null
    if ($vcs -eq "hg") { $branch = hg branch }
    elseif ($vcs -eq "git") { $branch = git rev-parse --abbrev-ref HEAD }
    
    return $branch
}
function get-vcsrev() {
    $id = $null
    $vcs = get-vcsname   
     if ($vcs -eq "hg") {
        $id = (hg id -i)
    } 
    elseif ($vcs -eq "git") {
        $id = (git rev-parse --short HEAD)
    }
    
    return $id
}

function Update-BuildVersion {
    [CmdletBinding(SupportsShouldProcess=$true)]
    param(
        [Parameter(ValueFromPipeline=$true, Position = 0)]
        $path = ".",
        [Parameter(Position = 1)]
        $version = $null,
        [VersionComponent]$component = [VersionComponent]::SuffixBuild,
        [switch][bool] $stable,
        $value = $null
        
    ) 
process {
    $verb = $psBoundParameters["Verbose"] -eq $true
    write-verbose "verbosity switch: $verb"
    pushd
    try {
        if ($path -ne $null) {
            $i = gi $path
            if (!$i.PsIsContainer) {
                $path = split-path -parent $path
            } 
            cd $path 
        }
        if ($version -eq $null) {
            
        }
        if ($version -eq $null -and $path -ne $null) {
            $ver = Get-AssemblyMeta InformationalVersion -verbose:$verb
            if ($ver -eq $null) { $ver = Get-AssemblyMeta Version -verbose:$verb }
        }
        else {
            $ver = $version
        }
        
        $newver = $ver
        if ($newver -eq $null) {
            # maybe the version is linked from some other file?
            # do nothing
            return $newver
        }
        if ($newver -eq "1.0.0.0") {
            $newver = "1.0.0"
            $newver = Update-Version $newver Patch -nuget -verbose:$verb
        }

        if ($newver -match "\.\*") {
            $newver = $newver.trim(".*")
            $splits = $newver.Split(".")
            $c= $splits.Length - 1
            if ($component -eq $null -or ($component -gt $c -and $value -eq $null)) {
                $newver = Update-Version $newver $c -nuget -verbose:$verb    
            }
        }        

        if ($newver.split(".").length -lt 3) {
            1..(3-$newver.split(".").Length) | % {
                $newver += ".0"
            }
        }

        $branch = get-vcsbranch -verbose:$verb
        if ($branch -ne $null) {
            $branchname = $branch 
            if ($branchname.StartsWith("release")) {
                $branchname = $branchname -replace "/","-" -replace "_","-" -replace "[0-9]","" -replace "-","" -replace "release","rc-"
                $branchname = $branchname.Trim("-")
            }
            write-verbose "found branch '$branch' => '$branchname'"            
            $newver = Update-Version $newver SuffixBranch -value $branchname -verbose:$verb
        }
       
       
       
        

        if ($component -ne $null) {
            $newver = Update-Version $newver $component -nuget -verbose:$verb -value $value  
        } else {
            $newver = Update-Version $newver SuffixBuild -nuget -verbose:$verb
        }
        
        if ($component -ne [VersionComponent]::Suffix -or $value -eq $null) {
            #Write-Verbose "updating version $ver to $newver"
            try {
                write-verbose "getting source control revision id"
                $id = get-vcsrev -verbose:$verb
                write-verbose "rev id='$id'"
            } catch {
                write-warning "failed to get vcs rev"
            }
            if ($id -ne $null) {
                $id = $id.substring(0,5)
                $newver = Update-Version $newver SuffixRevision -value $id -nuget -verbose:$verb
            } else {
                write-warning "vcs rev returned null"
            }
        }
        
        if ($stable) {
            $newver = update-version $newver Suffix -value "" -nuget -verbose:$verb
        }

        
        Write-host "updating version $ver to $newver"
        if ($path -ne $null -and $PSCmdlet.ShouldProcess("update version $ver to $newver")) {
            $assemblyinfos = Get-AssemblyMetaFile -ErrorAction Ignore
            if ($assemblyinfos -ne $null) { update-nugetmeta -version $newver -verbose:$verb }
        } else {

        }
        return $newver
    } 
    catch {
        throw $_
    }
    finally {
        popd
    }
}
}


function find-globaljson($path = ".") {
    ipmo pathutils
    return find-upwards "global.json" -path $path    
}


function get-dotnetcommand {
    [CmdletBinding()]
    param()
    $global = find-globaljson
    if ($global -eq $null) { write-verbose "dotnet: no global.json found for dir $((get-item .).FullName)"; return $null }
    $json = get-content $global | out-string | ConvertFrom-JsonNewtonsoft
    $default = "dotnet"
    if ($json.sdk -eq $null) { write-verbose "dotnet: no sdk version specified in '$global'"; return $default }
    if ($json.sdk.version -eq $null) { write-verbose "dotnet: no sdk version specified in '$global'"; return $default }
    if ($json.sdk.version.startswith("1.0.0-beta") -or $json.sdk.version.startswith("1.0.0-rc2")) {
        write-verbose "dotnet: sdk version='$($json.sdk.version)'. using dnu"
        return "dnu"
    } 
    else {
        write-verbose "dotnet: sdk version='$($json.sdk.version)'. using dotnet"
        return "dotnet"
    }
}

new-alias generate-nugetmeta update-nugetmeta -Force
new-alias push-nuget invoke-nugetpush -Force
new-alias pack-nuget invoke-nugetpack -Force
new-alias generate-nuspec new-nuspec -Force

export-modulemember -function * -alias *