functions/get-axautorunxml.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
129
130

<#
    .SYNOPSIS
        Get a valid Autorun XML file for AX 2012
         
    .DESCRIPTION
        Generate a valid Autorun XML file based on the selected parameters and have it saved on the disk
         
    .PARAMETER OutputPath
        The path where you want to store to autorun xml file
         
        The path must be a full path to a file
         
    .PARAMETER LogFile
        The path where you want the autorun process to save its log file
         
        The path must be a full path to a file
         
    .PARAMETER ExitWhenDone
        Instruct the autorun process wether you want it to exit the AX 2012 client when done or not
         
    .PARAMETER SynchronizeDB
        Instruct the autorun to do a synchronization of the database
         
    .PARAMETER CompileCilFull
        Instruct the autorun to do a full CIL compile
         
    .PARAMETER CompileCilIncremental
        Instruct the autorun to do a incremental CIL compile
         
    .PARAMETER CompileXpp
        Instruct the autorun to do a full X++ (Xpp) compile
         
    .PARAMETER CompileXppAndXRefUpdate
        Instruct the autorun to do a full X++ (Xpp) compile and do a full Cross Reference (XRef) update
         
    .EXAMPLE
        PS C:\> Get-AxAutoRunXml -ExitWhenDone -SynchronizeDB
         
        This will generate a valid Autorun xml file for synchronizing the database.
        It will use the default path "c:\temp\ax2012.tools\Autorun.xml" where the file will be stored.
        It will use the default path "c:\temp\ax2012.tools\Autorun_Output.txt" to instruct the Autorun where to save its log file.
        It will instruct the Autorun to exit when done.
        It will instruct the Autorun to do a synchronization of the database.
         
    .NOTES
        Tags:
        Author: Mötz Jensen (@Splaxi)
#>


function Get-AxAutoRunXml {
    [CmdletBinding()]
    param (

        [String] $OutputPath = (Join-Path $Script:DefaultTempPath "Autorun.xml"),

        [String] $LogFile = (Join-Path $Script:DefaultTempPath "Autorun_Output.txt"),

        [Switch] $ExitWhenDone,

        [Switch] $SynchronizeDB,

        [Switch] $CompileCilFull,

        [Switch] $CompileCilIncremental,
        
        [Switch] $CompileXpp,

        [Switch] $CompileXppAndXRefUpdate
        
    )

    if ([System.IO.Path]::HasExtension($OutputPath)) {
        $parentPath = Split-Path -Path $OutputPath -Parent

        if (-not (Test-PathExists -Path $parentPath -Type Container -Create)) { return }

        if (Test-PSFFunctionInterrupt) { return }
    }

    $actions = New-Object System.Collections.Generic.List[string]
    
    if ($SynchronizeDB) {
        Write-PSFMessage -Level Verbose -Message "SynchronizeDB was added to the action list." -Target $SynchronizeDB
        $actions.Add("SynchronizeDB")
    }
    
    if ($CompileCilFull -or $CompileCilIncremental) {
        
        if ($CompileCilFull) {
            Write-PSFMessage -Level Verbose -Message "CompileCilFull was added to the action list." -Target $CompileCilFull
            $actions.Add("CompileCilFull")
        }
        else {
            Write-PSFMessage -Level Verbose -Message "CompileCilIncremental was added to the action list." -Target $CompileCilIncremental
            $actions.Add("CompileCilIncremental")
        }
    }

    if ($CompileXppAndXRefUpdate -or $CompileXpp) {
        
        if ($CompileXppAndXRefUpdate) {
            Write-PSFMessage -Level Verbose -Message "CompileXppAndXRefUpdate was added to the action list." -Target $CompileXppAndXRefUpdate
            $actions.Add("CompileXppAndXRef")
        }
        else {
            Write-PSFMessage -Level Verbose -Message "CompileXpp was added to the action list." -Target $CompileXpp
            $actions.Add("CompileXpp")
        }
    }

    $autoRunXml = New-Object System.Collections.Generic.List[string]

    foreach ($item in $actions.ToArray()) {
        $autoRunXml.Add($(Get-AutoRunXML -Action $item))
    }

    $resXml = (Get-Content "$script:ModuleRoot\internal\xml\autotemplate.xml") -join [Environment]::NewLine

    $resXml = $resXml.Replace("{{ExitWhenDone}}", $ExitWhenDone.ToBool().ToString())
    $resXml = $resXml.Replace("{{LogPath}}", $LogFile)

    $resXml = $resXml.Replace("{{Action}}", $($autoRunXml.ToArray() -join [Environment]::NewLine))

    $resXml | Out-File -FilePath $OutputPath -Encoding utf8 -Force

    [PSCustomObject]@{
        Path = $OutputPath
    }
}