-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ps1
More file actions
112 lines (95 loc) · 4.27 KB
/
main.ps1
File metadata and controls
112 lines (95 loc) · 4.27 KB
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
Clear-Host
$toolsPath = "tools"
function Get-NugetExe {
param ($toolsPath)
$nugetDir = "$toolsPath\nuget"
if (-not(Test-Path $nugetDir)) {
New-Item -ItemType Directory -Path $nugetDir | Out-Null
}
$targetNugetExe = "$nugetDir\nuget.exe"
if (-not(Test-Path $targetNugetExe)) {
Write-Host "Downloading nuget.exe" -ForegroundColor Green
$sourceNugetExe = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
Invoke-WebRequest $sourceNugetExe -OutFile $targetNugetExe | Out-Null
}
$targetNugetExe
}
if (-not(Get-Alias nuget -ErrorAction SilentlyContinue)) {
$targetNugetExe = Get-NugetExe $toolsPath
Set-Alias nuget $targetNugetExe -Scope Script
}
$toolName = "7-Zip.CommandLine"
nuget install $toolName -OutputDirectory "$toolsPath\$toolName"
$7zip = Get-Item -Path "$toolsPath\$toolName\*\tools\7za.exe"
Set-Alias 7zip $7zip.FullName -Scope Script
if (-not(Test-Path "$PSScriptRoot\configuration.json")) {
Write-Host "Cannot find configuration file '.\configuration.json' because it does not exist." -ForegroundColor Red
return
}
$config = Get-Content .\configuration.json | ConvertFrom-Json
Write-Host "Preparing directory" -ForegroundColor Green
if (Test-Path -Path "temp") {
Write-Host "Removing temporary directories" -ForegroundColor Green
Remove-Item -Path .\temp -Force -Recurse
}
New-Item -ItemType Directory -Path "temp" | Out-Null
Write-Host "Looking for scwdp package" -ForegroundColor Green
$wdpPackage = Get-ChildItem -Path . -Filter "*single.scwdp.zip" | Select-Object -First 1
if ($wdpPackage) {
Write-Host "scwdp found" -ForegroundColor Green
Move-Item -Path $wdpPackage.FullName -Destination ".\temp"
$wdpPackage = Get-Item -Path "temp\$($wdpPackage.Name)"
if (-not($wdpPackage)) {
Write-Error "Couldn't find archive."
exit
}
$folderName = $wdpPackage.Name.Replace(" (OnPrem)_single.scwdp", "").Replace(".zip", "")
Write-Host "Extracting 'dacpac' files" -ForegroundColor Green
7zip e "$($wdpPackage.FullName)" -otemp\dacpac *.dacpac
Write-Host "scwdop archive cleanup" -ForegroundColor Green
7zip d "$($wdpPackage.FullName)" *.*
Write-Host "Converting databases" -ForegroundColor Green
$dbLocation = ".\temp\dacpac"
Get-ChildItem -Path $dbLocation | ? { $_.Extension -eq ".dacpac" } | % {
.\create-db-from-dacpac.ps1 $config.sqlUser $config.sqlPassword $config.serverName $_.FullName "$dbLocation\mdf"
}
mkdir "temp\Data\packages"
Write-Host "Packaging databases" -ForegroundColor Green
$path = $wdpPackage.FullName
Write-Host "Adding databases to archive" -ForegroundColor Green
7zip a $path "temp\dacpac\mdf"
7zip rn $path "temp\dacpac\mdf" "$folderName\Databases"
Write-Host "Adding Data folder to archive" -ForegroundColor Green
7zip a $path "temp\Data"
7zip rn $path "temp\Data" "$folderName\Data"
7zip rn $path "Content" "$folderName"
if (Test-Path "$PSScriptRoot\$($wdpPackage.Name)") {
Write-Host "File '$PSScriptRoot\$($wdpPackage.Name)' already exists" -ForegroundColor Yellow
$confirmation = Read-Host "Do you want to overwrite"
if ($confirmation -eq 'y') {
Remove-Item -Path $PSScriptRoot\$($wdpPackage.Name)
}
else {
Write-Host "Script aborted"
return
}
}
Write-Host "Moving processed wdp to outp folder" -ForegroundColor Green
Move-Item -Path $wdpPackage.FullName -Destination $PSScriptRoot
$wdp = Get-ChildItem -Path "." -Filter "*single.scwdp.zip" | Select-Object -First 1
Write-Host "Renaming package" -ForegroundColor Green
Write-Host "$PSScriptRoot\$folderName.1click" -ForegroundColor Green
$fileName = "$folderName.1click"
if (Test-Path "$PSScriptRoot\$folderName.1click") {
Write-Host "File with that name already exists" -ForegroundColor Green
$postfix = (New-Guid).ToString()
$fileName = "$folderName-$postfix.1click"
Write-Host "New file name: '$fileName'" -ForegroundColor Green
}
Rename-Item -Path $wdp.FullName -NewName $fileName
}else {
Write-Host "Couldn't find any scwdp package" -ForegroundColor Magenta
}
Write-Host "Removing temp folder" -ForegroundColor Green
$temp = Get-Item -Path .\temp
$temp | Remove-Item -Force -Recurse