Src/Build.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
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
param ( [switch]$clean )
if ( $clean )
{
    remove-item SMLets.Module.dll -ea silentlycontinue
    remove-item SMLets.Module.dll-Help.xml -ea silentlycontinue
    remove-item SMLets.Module.pdb -ea silentlycontinue
    exit
}

# this function creates the Get-SMLetsVersion cmdlet file
function New-SMLetsVersionFile
{
    $rs = ""; 
    $rs += " -replace '_SMCOREVERSION_','" + $COREDLLVERSION.FileVersion + "'"
    $foundMatches = $null
    $pstring = " -replace '_PRIVATE_','true'"
    $cstring = " -replace '_CHANGES_',''"
    if ( get-command -ea silentlycontinue svn )
    {
        $changedFiles = (svn stat) -match "^[AM]"
        if ( $changedFiles )
        { 
            $pstring = " -replace '_PRIVATE_','true'"
            $cstring = " -replace '_CHANGES_','" + ($changedFiles -join '","') + "'"
        }
        else
        {
            $pstring = " -replace '_PRIVATE_','false'"
        }
        $l1 = "Working Copy Root Path: (?<_WORKINGCOPYROOTPATH_>.*)"
        $l2 = "URL: (?<_URL_>.*)"
        $l3 = "Repository Root: (?<_REPOSITORYROOT_>.*)"
        $l4 = "Repository UUID: (?<_REPOSITORYUUID_>.*)"
        $l5 = "Revision: (?<_REVISION_>.*)"
        $l6 = "Node Kind: (?<j1_>.*)"
        $l7 = "Schedule: (?<j2_>.*)"
        $l8 = "Last Changed Author: (?<_LASTCHANGEDAUTHOR_>.*)"
        $l9 = "Last Changed Rev: (?<_LASTCHANGEDREV_>.*)"
        $la = "Last Changed Date: (?<_LASTCHANGEDDATE_>.*)"
        $pattern = "$l1 $l2 $l3 $l4 $l5 $l6 $l7 $l8 $l9 $la" 
        if ( ([string](svn info) -match $pattern))
        {
            $foundMatches = $matches
        }
    }
    if ( $foundMatches -eq $null )
    {
        $foundMatches = @{ 
        _WORKINGCOPYROOTPATH_ = "$PWD"
        _URL_ = "https://smlets.svn.codeplex.com/svn/Main/Source/SMLets/SMLets"
        _REPOSITORYROOT_ = "https://smlets.svn.codeplex.com/svn"
        _REPOSITORYUUID_ = "e17a0e51-4ae3-4d35-97c3-1a29b211df97"
        _REVISION_ = "unknown"
        _LASTCHANGEDAUTHOR_ = $env:username
        _LASTCHANGEDREV_ = "unknown"
        _LASTCHANGEDDATE_ = get-date
        }
    }
    $foundMatches['_TARGETPRODUCT_'] = (get-itemproperty 'hklm:/software/microsoft/system center/2010/Service Manager/Setup' product).product

    foreach($k in $foundMatches.keys)
    {
        if ( $k -match "^_" )
        {
            $rs += " -replace '$k','" + $foundMatches.$k + "'"
        }
    }

    $rs += " $pstring"
    $rs += " $cstring"
    $rs += " -replace '\\','/'"
    $sv = '${' + "$pwd\SMLETSVERSION.cs}"
    $executionContext.InvokeCommand.NewScriptBlock("$sv $rs").Invoke()
}

# Find the compiler - pick the latest
$INSTALLDIR = (gp 'hklm:/software/microsoft/system center\2010\Service Manager\Setup').InstallDirectory
$COREDLL = "${INSTALLDIR}\SDK Binaries\Microsoft.EnterpriseManagement.Core.dll"
$COREDLLVERSION = [system.diagnostics.fileversioninfo]::GetVersionInfo($COREDLL)
$SMDLL = "${INSTALLDIR}\SDK Binaries\Microsoft.EnterpriseManagement.ServiceManager.dll"
$PKGDLL = "${INSTALLDIR}\SDK Binaries\Microsoft.EnterpriseManagement.Packaging.dll"
$SMADLL = ([appdomain]::CurrentDomain.getassemblies()|?{$_.location -match "System.Management.Automation.dll"}).location
foreach ( $version in  "v3.5","v3.0","v2.0.50727" )
{
    $fmwk64 = "${env:windir}\Microsoft.Net\Framework64\$version"
    $fmwk32 = "${env:windir}\Microsoft.Net\Framework\$version"
    if ( test-path $fmwk64 )
    {
        set-alias csc "$fmwk64\csc.exe"
        break
    }
    elseif ( test-path $fmwk32 )
    {
        set-alias csc "$fmwk32\csc.exe"
        break
    }
}

$files = "Identifiers.cs", "Incident.cs", "Helper.cs", "Session.cs", "EntityTypes.cs", "DataWarehouse.cs", "EntityObjects.cs", "Subscription.cs", "Announcement.cs", "ManagementPack.cs", "Security.cs", "Templates.cs", "Presentation.cs", "Monitoring.cs", "Categories.cs", "Resources.cs", "Offering.cs"

$CurrentVersionFile = "SMLETSCURRENTVERSION.CS"
New-SMLetsVersionFile > $CurrentVersionFile
$files += $CurrentVersionFile

$output = "SMLets.Module.dll"
$reference = "/r:$SMADLL","/r:$COREDLL","/r:$PKGDLL","/r:$SMDLL"
$R2Version = new-object System.Version 7.5.0.0
if ( $COREDLLVERSION.ProductVersion -ge $R2Version )
{
    $CFLAGS = "/d:_SERVICEMANAGER_R2_"
}
else
{
    $CFLAGS = ""
}
csc /target:library /debug $CFLAGS /out:$output $files $reference

# we've named the Module assembly something different
# so we'll copy that help to the appropriate name for the Module assembly
$SNAPINHELP = "SMLets.dll-Help.xml"
$MODULEHELP = "SMLets.Module.dll-Help.xml"
Copy-Item $SNAPINHELP $MODULEHELP

# remove the SMLETSCURRENTVERSION file
if ( test-path $CurrentVersionFile ) { remove-item  $CurrentVersionFile }