vs-utils.ps1

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
#
# vs_utils.ps1
#
function Clear-CopyLocal
{
    [CmdletBinding(SupportsShouldProcess=$true)]
    Param(
        [Parameter(Mandatory=$true)]
        [string]$Path
    )
    
    $excludedList = @(
    'Sitecore.*',
    'Newtonsoft.*'
    )

    $solutionReferences = @();

    dir $path -Recurse *.csproj | 
    ForEach-Object {
        
        $singleFile = $_.FullName;
        Write-Verbose "Processing '$singleFile' file"

        $xml = New-Object System.Xml.XmlDocument;
        $xml.Load($singleFile);
    
        $references = $xml.Project.ItemGroup.Reference;
        $referencesList = @();
        $updateFile = $false

        foreach($reference in $references)
        {
            # Choose only references to Sitecore libraries
            if( $reference.Include -match "Sitecore.*" -or $reference.Include -match "Newtonsoft.*")
            {
                $referencesList += $reference;

                # Check if <Private/> element exist
                $node = $reference.ChildNodes | Where-Object {$_.Name -eq 'Private'}

        
                if( $node.Count -eq 0 )
                {
                    $private = $xml.CreateElement("Private", $xml.DocumentElement.NamespaceURI);
                    $private.InnerText = "False";
                    $reference.AppendChild($private);
                    $updateFile = $true
                    Write-Verbose "`tAdd False - for $($reference.Include)."  
                }
                elseif( $node.'#text' -eq $true )
                {
                    $node.'#text' = "False"
                    Write-Verbose "`tSet $($node.'#text') - for $($reference.Include)" 
                    $updateFile = $true
                }
                else
                {
                    Write-Verbose "`tWas $($node.'#text') - for $($reference.Include)"
                }
            }
        }
        
        if ($updateFile -eq $true -and $pscmdlet.ShouldProcess($singleFile))
        {
            $xml.Save("$singleFile");
        }
    } 
}