update build scripts

This commit is contained in:
Genteure 2018-04-14 07:05:25 +08:00
parent 8f08d35010
commit 0fd1636487
11 changed files with 487 additions and 43 deletions

404
CI/FileCryptography.psm1 Normal file
View File

@ -0,0 +1,404 @@
# https://gallery.technet.microsoft.com/scriptcenter/EncryptDecrypt-files-use-65e7ae5d
# See FileCryptography.psm1.LICENSE.txt
function New-CryptographyKey()
{
<#
.SYNOPSIS
Generates a random cryptography key.
.DESCRIPTION
Generates a random cryptography key based on the desired key size.
.PARAMETER Algorithm
Algorithm to generate key for.
.PARAMETER KeySize
Number of bits the generated key will have.
.PARAMETER AsPlainText
Returns a String instead of SecureString.
.OUTPUTS
System.Security.SecureString. New-CryptographyKey return the key as a SecureString by default.
System.String. New-CryptographyKey will return the key in plain text as a string if the -AsPlainText parameter is specified.
.EXAMPLE
$key = New-CryptographyKey
This example generates a random 256-bit AES key and stores it in the variable $key.
.NOTES
Author: Tyler Siegrist
Date: 9/22/2017
#>
[CmdletBinding()]
[OutputType([System.Security.SecureString])]
[OutputType([String], ParameterSetName='PlainText')]
Param(
[Parameter(Mandatory=$false, Position=1)]
[ValidateSet('AES','DES','RC2','Rijndael','TripleDES')]
[String]$Algorithm='AES',
[Parameter(Mandatory=$false, Position=2)]
[Int]$KeySize,
[Parameter(ParameterSetName='PlainText')]
[Switch]$AsPlainText
)
Process
{
try
{
$Crypto = [System.Security.Cryptography.SymmetricAlgorithm]::Create($Algorithm)
if($PSBoundParameters.ContainsKey('KeySize')){
$Crypto.KeySize = $KeySize
}
$Crypto.GenerateKey()
if($AsPlainText)
{
return [System.Convert]::ToBase64String($Crypto.Key)
}
else
{
return [System.Convert]::ToBase64String($Crypto.Key) | ConvertTo-SecureString -AsPlainText -Force
}
}
catch
{
Write-Error $_
}
}
}
Function Protect-File
{
<#
.SYNOPSIS
Encrypts a file using a symmetrical algorithm.
.DESCRIPTION
Encrypts a file using a symmetrical algorithm.
.PARAMETER FileName
File(s) to be encrypted.
.PARAMETER Key
Cryptography key as a SecureString to be used for encryption.
.PARAMETER KeyAsPlainText
Cryptography key as a String to be used for encryption.
.PARAMETER CipherMode
Specifies the block cipher mode to use for encryption.
.PARAMETER PaddingMode
Specifies the type of padding to apply when the message data block is shorter than the full number of bytes needed for a cryptographic operation.
.PARAMETER Suffix
Suffix of the encrypted file to be removed.
.PARAMETER RemoveSource
Removes the source (decrypted) file after encrypting.
.OUTPUTS
System.IO.FileInfo. Protect-File will return FileInfo with the SourceFile, Algorithm, Key, CipherMode, and PaddingMode as added NoteProperties
.EXAMPLE
Protect-File 'C:\secrets.txt' $key
This example encrypts C:\secrets.txt using the key stored in the variable $key. The encrypted file would have the default extension of '.AES' and the source (decrypted) file would not be removed.
.EXAMPLE
Protect-File 'C:\secrets.txt' -Algorithm DES -Suffix '.Encrypted' -RemoveSource
This example encrypts C:\secrets.txt with a randomly generated DES key. The encrypted file would have an extension of '.Encrypted' and the source (decrypted) file would be removed.
.EXAMPLE
Get-ChildItem 'C:\Files' -Recurse | Protect-File -Algorithm AES -Key $key -RemoveSource
This example encrypts all of the files under the C:\Files directory using the key stored in the variable $key. The encrypted files would have the default extension of '.AES' and the source (decrypted) files would be removed.
.NOTES
Author: Tyler Siegrist
Date: 9/22/2017
#>
[CmdletBinding(DefaultParameterSetName='SecureString')]
[OutputType([System.IO.FileInfo[]])]
Param(
[Parameter(Mandatory=$true, Position=1, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[Alias('PSPath','LiteralPath')]
[string[]]$FileName,
[Parameter(Mandatory=$false, Position=2)]
[ValidateSet('AES','DES','RC2','Rijndael','TripleDES')]
[String]$Algorithm = 'AES',
[Parameter(Mandatory=$false, Position=3, ParameterSetName='SecureString')]
[System.Security.SecureString]$Key = (New-CryptographyKey -Algorithm $Algorithm),
[Parameter(Mandatory=$true, Position=3, ParameterSetName='PlainText')]
[String]$KeyAsPlainText,
[Parameter(Mandatory=$false, Position=4)]
[System.Security.Cryptography.CipherMode]$CipherMode,
[Parameter(Mandatory=$false, Position=5)]
[System.Security.Cryptography.PaddingMode]$PaddingMode,
[Parameter(Mandatory=$false, Position=6)]
[String]$Suffix = ".$Algorithm",
[Parameter()]
[Switch]$RemoveSource
)
Begin
{
#Configure cryptography
try
{
if($PSCmdlet.ParameterSetName -eq 'PlainText')
{
$Key = $KeyAsPlainText | ConvertTo-SecureString -AsPlainText -Force
}
#Decrypt cryptography Key from SecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Key)
$EncryptionKey = [System.Convert]::FromBase64String([System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR))
$Crypto = [System.Security.Cryptography.SymmetricAlgorithm]::Create($Algorithm)
if($PSBoundParameters.ContainsKey('CipherMode')){
$Crypto.Mode = $CipherMode
}
if($PSBoundParameters.ContainsKey('PaddingMode')){
$Crypto.Padding = $PaddingMode
}
$Crypto.KeySize = $EncryptionKey.Length*8
$Crypto.Key = $EncryptionKey
}
Catch
{
Write-Error $_ -ErrorAction Stop
}
}
Process
{
$Files = Get-Item -LiteralPath $FileName
ForEach($File in $Files)
{
$DestinationFile = $File.FullName + $Suffix
Try
{
$FileStreamReader = New-Object System.IO.FileStream($File.FullName, [System.IO.FileMode]::Open)
$FileStreamWriter = New-Object System.IO.FileStream($DestinationFile, [System.IO.FileMode]::Create)
#Write IV (initialization-vector) length & IV to encrypted file
$Crypto.GenerateIV()
$FileStreamWriter.Write([System.BitConverter]::GetBytes($Crypto.IV.Length), 0, 4)
$FileStreamWriter.Write($Crypto.IV, 0, $Crypto.IV.Length)
#Perform encryption
$Transform = $Crypto.CreateEncryptor()
$CryptoStream = New-Object System.Security.Cryptography.CryptoStream($FileStreamWriter, $Transform, [System.Security.Cryptography.CryptoStreamMode]::Write)
$FileStreamReader.CopyTo($CryptoStream)
#Close open files
$CryptoStream.FlushFinalBlock()
$CryptoStream.Close()
$FileStreamReader.Close()
$FileStreamWriter.Close()
#Delete unencrypted file
if($RemoveSource){Remove-Item -LiteralPath $File.FullName}
#Output ecrypted file
$result = Get-Item $DestinationFile
$result | Add-Member MemberType NoteProperty Name SourceFile Value $File.FullName
$result | Add-Member MemberType NoteProperty Name Algorithm Value $Algorithm
$result | Add-Member MemberType NoteProperty Name Key Value $Key
$result | Add-Member MemberType NoteProperty Name CipherMode Value $Crypto.Mode
$result | Add-Member MemberType NoteProperty Name PaddingMode Value $Crypto.Padding
$result
}
Catch
{
Write-Error $_
If($FileStreamWriter)
{
#Remove failed file
$FileStreamWriter.Close()
Remove-Item -LiteralPath $DestinationFile -Force
}
Continue
}
Finally
{
if($CryptoStream){$CryptoStream.Close()}
if($FileStreamReader){$FileStreamReader.Close()}
if($FileStreamWriter){$FileStreamWriter.Close()}
}
}
}
}
Function Unprotect-File
{
<#
.SYNOPSIS
Decrypts a file encrypted with Protect-File.
.DESCRIPTION
Decrypts a file using a provided cryptography key.
.PARAMETER FileName
File(s) to be decrypted.
.PARAMETER Key
Cryptography key as a SecureString be used for decryption.
.PARAMETER KeyAsPlainText
Cryptography key as a String to be used for decryption.
.PARAMETER CipherMode
Specifies the block cipher mode that was used for encryption.
.PARAMETER PaddingMode
Specifies the type of padding that was applied when the message data block was shorter than the full number of bytes needed for a cryptographic operation.
.PARAMETER Suffix
Suffix of the encrypted file to be removed.
.PARAMETER RemoveSource
Removes the source (encrypted) file after decrypting.
.OUTPUTS
System.IO.FileInfo. Unprotect-File will return FileInfo with the SourceFile as an added NoteProperty
.EXAMPLE
Unprotect-File 'C:\secrets.txt.AES' $key
This example decrypts C:\secrets.txt.AES using the key stored in the variable $key. The decrypted file would remove the default extension of '.AES' and the source (encrypted) file would not be removed.
.EXAMPLE
Unprotect-File 'C:\secrets.txt.Encrypted' -Algorithm DES -Key $key -Suffix '.Encrypted' -RemoveSource
This example decrypts C:\secrets.txt.Encrypted using DES and the key stored in the variable $key. The decrypted file would remove the extension of '.Encrypted' and the source (encrypted) file would be removed.
.EXAMPLE
Get-ChildItem 'C:\Files' -Recurse | Unprotect-File -Algorithm AES -Key $key -RemoveSource
This example decrypts all of the files under the C:\Files directory using the key stored in the variable $key. The decrypted files would remove the default extension of '.AES' and the source (encrypted) files would be removed.
.NOTES
Author: Tyler Siegrist
Date: 9/22/2017
#>
[CmdletBinding(DefaultParameterSetName='SecureString')]
[OutputType([System.IO.FileInfo[]])]
Param(
[Parameter(Mandatory=$true, Position=1, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[Alias('PSPath','LiteralPath')]
[string[]]$FileName,
[Parameter(Mandatory=$false, Position=2, ValueFromPipelineByPropertyName=$true)]
[ValidateSet('AES','DES','RC2','Rijndael','TripleDES')]
[String]$Algorithm = 'AES',
[Parameter(Mandatory=$true, Position=3, ValueFromPipelineByPropertyName=$true, ParameterSetName='SecureString')]
[System.Security.SecureString]$Key,
[Parameter(Mandatory=$true, Position=3, ParameterSetName='PlainText')]
[String]$KeyAsPlainText,
[Parameter(Mandatory=$false, Position=4, ValueFromPipelineByPropertyName=$true)]
[System.Security.Cryptography.CipherMode]$CipherMode = 'CBC',
[Parameter(Mandatory=$false, Position=5, ValueFromPipelineByPropertyName=$true)]
[System.Security.Cryptography.PaddingMode]$PaddingMode = 'PKCS7',
[Parameter(Mandatory=$false, Position=6)]
[String]$Suffix, #Assigning default value in code due to it not processing ".$Algorithm" properly when Algorithm is ValueFromPipelineByPropertyName
[Parameter()]
[Switch]$RemoveSource
)
Process
{
#Configure cryptography
try
{
if($PSCmdlet.ParameterSetName -eq 'PlainText')
{
$Key = $KeyAsPlainText | ConvertTo-SecureString -AsPlainText -Force
}
#Decrypt cryptography Key from SecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Key)
$EncryptionKey = [System.Convert]::FromBase64String([System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR))
$Crypto = [System.Security.Cryptography.SymmetricAlgorithm]::Create($Algorithm)
$Crypto.Mode = $CipherMode
$Crypto.Padding = $PaddingMode
$Crypto.KeySize = $EncryptionKey.Length*8
$Crypto.Key = $EncryptionKey
}
Catch
{
Write-Error $_ -ErrorAction Stop
}
if(-not $PSBoundParameters.ContainsKey('Suffix'))
{
$Suffix = ".$Algorithm"
}
#Used to store successfully decrypted file names.
$Files = Get-Item -LiteralPath $FileName
ForEach($File in $Files)
{
#Verify file ends with supplied suffix
If(-not $File.Name.EndsWith($Suffix))
{
Write-Error "$($File.FullName) does not have an extension of '$Suffix'."
Continue
}
$DestinationFile = $File.FullName -replace "$Suffix$"
Try
{
$FileStreamReader = New-Object System.IO.FileStream($File.FullName, [System.IO.FileMode]::Open)
$FileStreamWriter = New-Object System.IO.FileStream($DestinationFile, [System.IO.FileMode]::Create)
#Get IV from file
[Byte[]]$LenIV = New-Object Byte[] 4
$FileStreamReader.Seek(0, [System.IO.SeekOrigin]::Begin) | Out-Null
$FileStreamReader.Read($LenIV, 0, 3) | Out-Null
[Int]$LIV = [System.BitConverter]::ToInt32($LenIV, 0)
[Byte[]]$IV = New-Object Byte[] $LIV
$FileStreamReader.Seek(4, [System.IO.SeekOrigin]::Begin) | Out-Null
$FileStreamReader.Read($IV, 0, $LIV) | Out-Null
$Crypto.IV = $IV
#Peform Decryption
$Transform = $Crypto.CreateDecryptor()
$CryptoStream = New-Object System.Security.Cryptography.CryptoStream($FileStreamWriter, $Transform, [System.Security.Cryptography.CryptoStreamMode]::Write)
$FileStreamReader.CopyTo($CryptoStream)
#Close open files
$CryptoStream.FlushFinalBlock()
$CryptoStream.Close()
$FileStreamReader.Close()
$FileStreamWriter.Close()
#Delete encrypted file
if($RemoveSource){Remove-Item $File.FullName}
#Output decrypted file
Get-Item $DestinationFile | Add-Member MemberType NoteProperty Name SourceFile Value $File.FullName -PassThru
}
Catch
{
Write-Error $_
If($FileStreamWriter)
{
#Remove failed file
$FileStreamWriter.Close()
Remove-Item -LiteralPath $DestinationFile -Force
}
Continue
}
Finally
{
if($CryptoStream){$CryptoStream.Close()}
if($FileStreamReader){$FileStreamReader.Close()}
if($FileStreamWriter){$FileStreamWriter.Close()}
}
}
}
}
Export-ModuleMember -Function New-CryptographyKey
Export-ModuleMember -Function Protect-File
Export-ModuleMember -Function Unprotect-File

View File

@ -0,0 +1,9 @@
The MIT License (MIT)
Copyright (c)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,9 @@
if ($env:BILILIVERECORDER_RELEASE)
{
msbuild /t:Publish /verbosity:minimal /p:Configuration=Release /p:ApplicationVersion="$env:p_version" /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll"
$host.SetShouldExit($LastExitCode)
}
else
{
Write-Host "Not a new release, skipping msbuild /t:Publish"
}

View File

@ -0,0 +1,10 @@
if ($env:BILILIVERECORDER_RELEASE)
{
./CI/do_codesign.ps1
./CI/push_clickonce.ps1
./CI/push_master.ps1
}
else
{
Write-Host "Not a new release!"
}

24
CI/appveyor_init.ps1 Normal file
View File

@ -0,0 +1,24 @@
if($env:APPVEYOR){
git config --global credential.helper store
Add-Content "$env:USERPROFILE\.git-credentials" "https://$($env:github_access_token):x-oauth-basic@github.com`n"
git config --global user.email "appveyor@genteure.com"
git config --global user.name "Appveyor(Genteure)"
git config --global core.autocrlf false
}
$commit_message_version_regex="^Release: (\d+\.\d+\.\d+)$"
if ($env:APPVEYOR_REPO_BRANCH -eq "dev" -and $env:APPVEYOR_REPO_COMMIT_MESSAGE -cmatch $commit_message_version_regex)
{
$env:BILILIVERECORDER_RELEASE=$true
$env:p_version="$($Matches[1])"
Update-AppveyorBuild -Version "$env:p_version"
}
else
{
$env:BILILIVERECORDER_RELEASE=$false
$env:p_version="0.0.0.$env:APPVEYOR_BUILD_NUMBER"
Update-AppveyorBuild -Version "dev-$($env:APPVEYOR_REPO_COMMIT.Substring(0, 7))-$env:APPVEYOR_BUILD_NUMBER"
}
Write-Host "Current build version is $env:p_version"

View File

@ -1,6 +0,0 @@
if ($env:APPVEYOR) {
msbuild "BililiveRecorder.sln" /t:Publish /verbosity:minimal /p:Configuration=Release /p:ApplicationVersion="$env:p_version" /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll"
}else {
msbuild "BililiveRecorder.sln" /t:Publish /verbosity:minimal /p:Configuration=Release /p:ApplicationVersion="$env:p_version"
}
if ($LastExitCode -ne 0) { $host.SetShouldExit($LastExitCode) }

1
CI/do_codesign.ps1 Normal file
View File

@ -0,0 +1 @@
Write-Host "do_codesign.ps1: TODO"

View File

@ -1,3 +1,5 @@
# called by msbuild
$isAppveyor = if ($env:APPVEYOR -eq $null) { "false" } else { $env:APPVEYOR }
$buildversion = if ($env:p_version -eq $null) { "本地编译" } else { $env:p_version }
$githash = git rev-parse --verify HEAD

View File

@ -1,8 +1,7 @@
if (-not (Test-Path env:APPVEYOR)) {
Throw New-Object System.NotSupportedException "Not Running on Appveyor!"
if ($env:APPVEYOR) {
git clone --quiet --depth 1 https://github.com/Bililive/soft.danmuji.org.git C:\projects\site
Get-ChildItem -Path .\BililiveRecorder.WPF\bin\Release\app.publish | Copy-Item -Destination C:\projects\site\BililiveRecorder -Recurse -Container
git --git-dir=C:\projects\site\.git\ --work-tree=C:\projects\site\ add -A
git --git-dir=C:\projects\site\.git\ --work-tree=C:\projects\site\ commit --quiet -m "BililiveRecorder $env:p_version"
git --git-dir=C:\projects\site\.git\ --work-tree=C:\projects\site\ push --quiet
}
git clone --quiet --depth 1 https://github.com/Bililive/soft.danmuji.org.git C:\projects\site
Get-ChildItem -Path .\BililiveRecorder.WPF\bin\Release\app.publish | Copy-Item -Destination C:\projects\site\BililiveRecorder -Recurse -Container
git --git-dir=C:\projects\site\.git\ --work-tree=C:\projects\site\ add -A
git --git-dir=C:\projects\site\.git\ --work-tree=C:\projects\site\ commit --quiet -m "BililiveRecorder $env:p_version"
git --git-dir=C:\projects\site\.git\ --work-tree=C:\projects\site\ push --quiet

3
CI/push_master.ps1 Normal file
View File

@ -0,0 +1,3 @@
git push origin master
git tag "v$env:p_version"
git push origin "v$env:p_version"

View File

@ -2,9 +2,15 @@ image: Visual Studio 2017
version: Oops{build}
branches:
only:
- dev
#
# 在 Appveyor 控制台设置的
#
# Branches to build
# All except branches specified below
# master
#
# [x] Do not build tags
#
platform: Any CPU
@ -12,25 +18,6 @@ configuration:
- Debug
#- Release
init:
- git config --global credential.helper store
- ps: Add-Content "$env:USERPROFILE\.git-credentials" "https://$($env:github_access_token):x-oauth-basic@github.com`n"
- git config --global user.email "appveyor@genteure.com"
- git config --global user.name "Appveyor(Genteure)"
- git config --global core.autocrlf false
- ps: >-
if ($env:APPVEYOR_REPO_TAG -eq "true")
{
$env:p_version="$($env:APPVEYOR_REPO_TAG_NAME.TrimStart("v")).0"
Update-AppveyorBuild -Version "$env:p_version"
}
else
{
$env:p_version="0.0.0.$env:APPVEYOR_BUILD_NUMBER"
Update-AppveyorBuild -Version "dev-$($env:APPVEYOR_REPO_COMMIT.Substring(0, 7))-$env:APPVEYOR_BUILD_NUMBER"
}
- ps: Write-Host "Current build version is $env:p_version"
assembly_info:
patch: true
file: '**\AssemblyInfo.*'
@ -46,9 +33,12 @@ dotnet_csproj:
file_version: $(p_version)
informational_version: $(p_version)
init:
- ps: ./CI/appveyor_init.ps1
before_build:
- nuget restore
- dotnet restore
- nuget restore -Verbosity quiet
- msbuild /t:Clean /verbosity:quiet /nologo
artifacts:
- path: BililiveRecorder.WPF\bin\Debug
@ -65,12 +55,11 @@ build:
verbosity: minimal
after_build:
- ps: >-
if ($env:APPVEYOR_REPO_TAG -eq "true")
{
./CI/build_clickonce.ps1
./CI/push_clickonce.ps1
}
- ps: ./CI/appveyor_after_build.ps1
before_deploy:
- ps: ./CI/appveyor_before_deploy.ps1
# after_deploy: