Created
September 5, 2017 12:06
-
-
Save Konctantin/3e782a655244add6953519c9d0b601e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # todo: check errors | |
| # todo: save log | |
| # todo: remove bat proxy files | |
| $ftpDir = "E:\FTP" | |
| $tempDir = "E:\FTP\TMP" | |
| $directory = "E:\VagTK" | |
| $project = "vagTK_Dll.groupproj" | |
| $outputDir = "Win32\Debug" | |
| $vagTKdproj= "VAG_SOBSTV\VagTK.dproj" | |
| $fileVersionPattern = ";FileVersion=(\d+\.\d+\.\d+\.)(\d+);" | |
| [string[]]$project2Build = @() | |
| Function HasFile($list, $file) { | |
| foreach ($f in $list) { | |
| if ([IO.Path]::GetFullPath($f) -eq [IO.Path]::GetFullPath($file) ) { | |
| return 1 | |
| } | |
| } | |
| } | |
| Function Add-Depend([string[]]$pb, $Dependencies) { | |
| foreach ($depProj in $Dependencies.Split(";")) { | |
| if ($pb -notcontains $depProj) { | |
| $pb += $depProj | |
| } | |
| } | |
| return $pb | |
| } | |
| Function Get-OutputFileName($proj) { | |
| [xml]$projXml = Get-Content $proj | |
| $type = $projXml.Project.PropertyGroup.AppType | |
| $fname= [IO.Path]::GetFileNameWithoutExtension($projXml.Project.PropertyGroup.MainSource) | |
| if ($type -eq "Application") { | |
| return $fname + ".exe" | |
| } elseif ($type -eq "Library") { | |
| return $fname + ".dll" | |
| } elseif ($type -eq "Package") { | |
| return $fname + ".bpl" | |
| } | |
| } | |
| Function Check-Errors($errMsg) { | |
| if ($Error.Count -gt 0) { | |
| Write-Host $errMsg -ForegroundColor red | |
| foreach ($err in $Error) { | |
| Write-Host $err -ForegroundColor red | |
| } | |
| exit | |
| } | |
| } | |
| $Error.Clear(); | |
| Set-Location $directory | |
| Check-Errors "Не удалось перейти в папку '$directory'" | |
| #Check instaled git | |
| gitу --version | |
| Check-Errors "Не найден гит" | |
| #git reset --hard | |
| #git pull origin master | |
| #git submodule update --init --recursive | |
| # Получаем список измененных файлов | |
| # todo: check file exists | |
| $firstHashe = Get-Content "$directory\LAST_BUILD_HASHE.txt" | |
| $latestHashe = git log -n 1 --pretty=format:"%H" | |
| $changedFileList = git diff --name-only $firstHashe..$latestHashe | |
| [xml]$groupProjXml = Get-Content $project | |
| Check-Errors "Не удалось открыть файл проета '$project'" | |
| [string[]]$project2BuildOrdered = $groupProjXml.Project.ItemGroup.Projects.Include | |
| # ищем измененные файлы в проектах | |
| foreach ($projectNode in $groupProjXml.Project.ItemGroup.Projects) { | |
| $projectPath = $projectNode.Include | |
| [xml]$projXml = Get-Content $projectPath | |
| if (HasFile $changedFileList $projectPath ` | |
| -Or HasFile $changedFileList $projectPath.Replace("dproj", "dpr") ` | |
| -Or HasFile $changedFileList $projectPath.Replace("dproj", "dpk")) { | |
| $project2Build = Add-Depend $project2Build $projectNode.Dependencies | |
| if ($project2Build -notcontains $depProj) { | |
| $project2Build += $projectPath | |
| } | |
| Break | |
| } | |
| foreach ($prfile in $projXml.Project.ItemGroup.DCCReference.Include) { | |
| $f = (Split-Path -parent $projectPath) | |
| $f = Join-Path $f $prfile | |
| if (HasFile $changedFileList $f ` | |
| -Or HasFile $changedFileList $f.Replace(".pas", ".dfm")) { | |
| $project2Build = Add-Depend $project2Build $projectNode.Dependencies | |
| if ($project2Build -notcontains $depProj) { | |
| $project2Build += $projectPath | |
| } | |
| Break | |
| } | |
| } | |
| } | |
| # Получаем обратные зависимости | |
| foreach ($projectNode in $groupProjXml.Project.ItemGroup.Projects) { | |
| foreach ($item in $projectNode.Dependencies.Split(";")) { | |
| if ($project2Build -contains $item ` | |
| -And $project2Build -notcontains $projectNode.Include) { | |
| $project2Build += $projectNode.Include | |
| } | |
| } | |
| } | |
| # для основного проекта увеличиваем версию | |
| if ($project2Build -contains $vagTKdproj) { | |
| [string]$content = [IO.File]::ReadAllText("$directory\$vagTKdproj") | |
| $match = [regex]::Match($content, $fileVersionPattern) | |
| if ($match.Success) { | |
| [int]$ver = $match.Groups[2].Value | |
| $VERSION_VAG_TK = $match.Groups[1].Value + ($ver + 1) | |
| $content = [regex]::Replace($content, $fileVersionPattern, ";FileVersion=$VERSION_VAG_TK;") | |
| $content | Out-File "$directory\$vagTKdproj" utf8 -NoNewline | |
| } | |
| } | |
| foreach ($proj in $project2BuildOrdered) { | |
| if ($project2Build -contains $proj) { | |
| # todo: remove bat proxy files | |
| $bat = Join-Path $directory "build_project.bat" | |
| #& $bat $proj | |
| # После компиляции пакуем скомпилированый файл и перемещаем его в папку для обновлений | |
| $name = Get-OutputFileName($proj) | |
| if ($name.EndsWith(".exe") -Or $name.EndsWith(".dll")) { | |
| $bin = "$directory\$outputDir\$name" | |
| if (Test-Path $bin) { | |
| $out = Join-Path $tempDir $name | |
| #& $directory + "\upx\upx.exe" -3 -f "-o$out" $bin | |
| } else { | |
| # set error | |
| } | |
| } | |
| } | |
| } | |
| # Сформируем файл с данными для обновления | |
| $files = Get-ChildItem -Path $tempDir -Exclude *.inf | |
| $ini = "[ListFile]`r`nStr1=" | |
| $i = 2 | |
| foreach ($f in $files) { | |
| # А зачем мы делим на части???? | |
| if ($ini.Length -gt 150) { | |
| $ini += "`r`nStr$i=" | |
| $i += 1 | |
| } | |
| $ini += $f.Name + ";" | |
| } | |
| $ini += "`r`n" | |
| foreach ($f in $files) { | |
| $ini += "["+$f.Name+"]`r`n" | |
| $ini += "Version=" + (Get-Item $f).VersionInfo.FileVersion + "`r`n" | |
| # Если будет старая версия командлета, тогда нужно будет переписать | |
| $ini += "MD5=" + (Get-FileHash -Path $f -Algorithm MD5).Hash + "`r`n" | |
| } | |
| $ini | Out-File "$tempDir\updateVagTK.inf" -NoNewline | |
| $files | |
| #git add . | |
| #git commit -m "Version changed" | |
| #git push origin master | |
| # копируем файлы в выходные папки | |
| Copy-Item "$tempDir\*" "$ftpDir\UPDATE" | |
| if ($VERSION_VAG_TK) { | |
| mkdir "$ftpDir\VERSIONS\$VERSION_VAG_TK" | |
| Copy-Item "$tempDir\*" "$ftpDir\VERSIONS\$VERSION_VAG_TK" | |
| } | |
| # Удалим старые версии, если таковые есть | |
| $files = Get-ChildItem -Path "$ftpDir\VERSIONS" | Sort-Object -Descending | |
| [int]$counter = 0 | |
| foreach ($item in $files) { | |
| $counter += 1 | |
| if ($counter -gt 100) { | |
| Remove-Item $item.FullName -Recurse | |
| } | |
| } | |
| # Запишем последний скомпилированый коммит | |
| $hash = git log -n 1 --pretty=format:"%H" | |
| #$hash | Out-File "$directory\LAST_BUILD_HASHE.txt" -NoNewline |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment