File size: 4,400 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<#
.SYNOPSIS
Move metadata JSON and package-level overview markdown files for deprecated packages to the legacy folder.

.DESCRIPTION
Move onboarding information to the "legacy" moniker for whose support is "deprecated" in the Metadata CSV.
Only one version of a package can be documented in the "legacy" moniker. If multiple versions are available,
the "latest" version will be used and the "preview" version will be deleted.

.PARAMETER DocRepoLocation
The location of the target docs repository.
#>

param(
    [Parameter(Mandatory = $true)]
    [string] $DocRepoLocation
)

. (Join-Path $PSScriptRoot common.ps1)

Set-StrictMode -Version 3

function getPackageMetadata($moniker) { 
    $jsonFiles = Get-ChildItem -Path (Join-Path $DocRepoLocation "metadata/$moniker") -Filter *.json
    $metadata = @{}

    foreach ($jsonFile in $jsonFiles) {
        $packageMetadata = Get-Content $jsonFile -Raw | ConvertFrom-Json -AsHashtable
        $packageIdentity = $packageMetadata.Name
        if (Test-Path "Function:$GetPackageIdentity") {
            $packageIdentity = &$GetPackageIdentity $packageMetadata
        }

        $metadata[$packageIdentity] = @{ File = $jsonFile; Metadata = $packageMetadata }
    }
    
    return $metadata
}

function getPackageInfoFromLookup($packageIdentity, $version, $lookupTable) { 
    if ($lookupTable.ContainsKey($packageIdentity)) {
        if ($lookupTable[$packageIdentity]['Metadata'].Version -eq $version) {
            # Only return if the version matches
            return $lookupTable[$packageIdentity]
        }
    }

    return $null 
}

function moveToLegacy($packageInfo) { 
    $docsMsMetadata = &$GetDocsMsMetadataForPackageFn -PackageInfo $packageInfo['Metadata']

    Write-Host "Move to legacy: $($packageInfo['Metadata'].Name)"
    $packageInfoPath = $packageInfo['File']
    Move-Item "$($packageInfoPath.Directory)/$($packageInfoPath.BaseName).*" "$DocRepoLocation/metadata/legacy/" -Force

    $readmePath = "$DocRepoLocation/$($docsMsMetadata.PreviewReadMeLocation)/$($docsMsMetadata.DocsMsReadMeName)-readme.md"
    if (Test-Path $readmePath) {
        Move-Item `
            $readmePath `
            "$DocRepoLocation/$($docsMsMetadata.LegacyReadMeLocation)/" `
            -Force
    }
}

function deletePackageInfo($packageInfo) { 
    $docsMsMetadata = &$GetDocsMsMetadataForPackageFn -PackageInfo $packageInfo['Metadata']

    Write-Host "Delete superseded package: $($packageInfo['Metadata'].Name)"
    $packageInfoPath = $packageInfo['File']
    Remove-Item "$($packageInfoPath.Directory)/$($packageInfoPath.BaseName).*" -Force

    $readmePath = "$DocRepoLocation/$($docsMsMetadata.PreviewReadMeLocation)/$($docsMsMetadata.DocsMsReadMeName)-readme.md"
    if (Test-Path $readmePath) {
        Remove-Item $readmePath -Force
    }
}

$metadataLookup = @{ 
    'latest'  = getPackageMetadata 'latest'
    'preview' = getPackageMetadata 'preview'
}
$deprecatedPackages = (Get-CSVMetadata).Where({ $_.Support -eq 'deprecated' })

foreach ($package in $deprecatedPackages) {
    $packageIdentity = $package.Package
    if (Test-Path "Function:$GetPackageIdentityFromCsvMetadata") {
        $packageIdentity = &$GetPackageIdentityFromCsvMetadata $package
    }

    $packageInfoPreview = $packageInfoLatest = $null
    if ($package.VersionPreview) {
        $packageInfoPreview = getPackageInfoFromLookup `
            -packageIdentity $packageIdentity `
            -version $package.VersionPreview `
            -lookupTable $metadataLookup['preview']
    }

    if ($package.VersionGA) { 
        $packageInfoLatest = getPackageInfoFromLookup `
            -packageIdentity $packageIdentity `
            -version $package.VersionGA `
            -lookupTable $metadataLookup['latest']
    }

    if (!$packageInfoPreview -and !$packageInfoLatest) {
        # Nothing to move or delete
        continue
    }

    if ($packageInfoPreview -and $packageInfoLatest) {
        # Delete metadata JSON and package-level overview markdown files for
        # the preview version instead of moving both. This mitigates situations
        # where the "latest" verison doesn't have a package-level overview 
        # markdown file and the "preview" version does.
        deletePackageInfo $packageInfoPreview
        moveToLegacy $packageInfoLatest
    } else {
        moveToLegacy ($packageInfoPreview ?? $packageInfoLatest)
    }
}