diff --git a/.gitignore b/.gitignore index ff5b00c..28428da 100644 --- a/.gitignore +++ b/.gitignore @@ -261,4 +261,9 @@ paket-files/ # Python Tools for Visual Studio (PTVS) __pycache__/ -*.pyc \ No newline at end of file +*.pyc + + +# Project specific exclusions - ZIP files (so Teams apps aren't inadvertently included, and Temp folder). +Temp/ +*.zip diff --git a/Scripts/2.0/Add-ValidDomainToTeamsApp.ps1 b/Scripts/2.0/Add-ValidDomainToTeamsApp.ps1 new file mode 100644 index 0000000..60986e7 --- /dev/null +++ b/Scripts/2.0/Add-ValidDomainToTeamsApp.ps1 @@ -0,0 +1,83 @@ +<# + .SYNOPSIS + Add-ValidDomainToTeamsApp.ps1 + + .DESCRIPTION + Adds a valid domain to a custom Microsoft Teams app manifest file + + .PARAMETER AppManifestPath + Path to the app manifest file to add the valid domain to + + .PARAMETER ValidDomain + The domain to add as a ValidDomain to the Microsoft Teams app manifest file + + .PARAMETER NoBackup + (switch) Don't take a backup of the app manifest file + + .PARAMETER KeepTempFolder + (switch) Keep the temp folder where the script updates the manifest + +#> + +param( + [Parameter(Mandatory = $false)] + [string]$AppManifestPath = ".\Valo.Connect.app.zip", + + [Parameter(Mandatory = $true)] + [string]$ValidDomain, + + [Parameter(Mandatory = $false)] + [switch]$NoBackup, + + [Parameter(Mandatory = $false)] + [switch]$KeepTempFolder + +) + +if ($true -ne $AppManifestPath.ToLower().EndsWith(".zip")) { + Write-Error "AppManifestPath doesn't have a .zip extension" + exit +} + +$AppManifestFile = Get-Item $AppManifestPath -ErrorAction SilentlyContinue + +if ($null -eq $AppManifestFile) { + Write-Error "Couldn't find file from AppManifestPath paramater: $AppManifestPath" + exit +} + +$DestinationFolderName = $AppManifestFile.Name.Replace($AppManifestFile.Extension, "") +$DestinationPath = Join-Path -Path $AppManifestFile.Directory.FullName -ChildPath "Temp" -AdditionalChildPath $DestinationFolderName + +Expand-Archive $AppManifestFile.FullName -DestinationPath $DestinationPath -Force + +$ManifestPath = Join-Path -Path $DestinationPath -ChildPath "manifest.json" +if ($true -ne (Test-Path $ManifestPath)) { + Write-Error "Unable to find manifest.json in app manifest. Is $($AppManifestFile.Name) a valid Teams app?" + exit +} + +$ManifestContentsJson = ConvertFrom-Json (Get-Content $ManifestPath -Raw) +if ($true -eq $ManifestContentsJson.validDomains.Contains($ValidDomain)) { + Write-Warning "Manifest file in $($AppManifestFile.Name) already contains validDomain $ValidDomain, no change required" + exit +} + +$ManifestContentsJson.validDomains += $ValidDomain +New-Item -Path $DestinationPath -Name "manifest.json" -ItemType File -Value (ConvertTo-Json -InputObject $ManifestContentsJson -Depth 99) -Force | Out-Null + +$BackupPath = Join-Path -Path $AppManifestFile.Directory.FullName -ChildPath "Temp" -AdditionalChildPath "Backup" +New-Item -Path $BackupPath -ItemType Directory -Force | Out-Null + +if ($true -ne $NoBackup) { + $BackupFileDestination = Join-Path -Path $BackupPath -ChildPath $AppManifestFile.Name + Write-Output "Backing up $($AppManifestFile.Name) to $($BackupFileDestination.Replace($AppManifestFile.Directory.FullName, "."))" + Move-Item $AppManifestFile.FullName -Destination $BackupFileDestination -Force +} + +Write-Output "Updating $($AppManifestFile.Name) with additional ValidDomain $ValidDomain" +Compress-Archive -DestinationPath $AppManifestFile.FullName -LiteralPath (Get-ChildItem $DestinationPath -Depth 1) -Force + +if ($true -ne $KeepTempFolder) { + Remove-Item -Path $BackupPath -Recurse +}