Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ytube dl #4

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
choco.wsb
*.nupkg
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "DownloadInfo"]
path = DownloadInfo
url = https://github.com/sangafabrice/download-info.git
branch = pwsh-module
1 change: 1 addition & 0 deletions DownloadInfo
Submodule DownloadInfo added at 8f8a24
32 changes: 0 additions & 32 deletions build.psm1

This file was deleted.

21 changes: 0 additions & 21 deletions config.wsb

This file was deleted.

120 changes: 120 additions & 0 deletions dist.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
Import-Module "$PSScriptRoot\DownloadInfo" -Force

Class Distribution {
Static [hashtable] $PropertyList = @{
RepositoryId = 'ytdl-org/youtube-dl'
AssetPattern = 'youtube-dl.exe$|SHA2-512SUMS$'
}

Static $UpdateInfo32 = $(Get-DownloadInfo -PropertyList ([Distribution]::PropertyList))

Static [string] $PackageName = ((Get-Item "$(& { $PSScriptRoot })\*.nuspec").Name -replace '\.nuspec$')

Static [string] NewNugetPackage($Version, [string] $Properties) {
Push-Location $PSScriptRoot
$Result = $(& {
Try {
If ($Version -is [scriptblock]) { $Version = & $Version }
If ($Version -notmatch '^[0-9]+(\.[0-9]+){2}$') { Throw }
Invoke-Expression "choco pack --version=$Version $Properties" > $Null 2>&1
If ($LASTEXITCODE -eq 1) { Throw }
Return "$([Distribution]::PackageName).$Version.nupkg"
}
Catch { }
})
Pop-Location
Return $Result
}

Static [string] UpdateVersion() {
Push-Location $PSScriptRoot
$GetXmlContent = { [xml] (Get-Content .\pkg.xml -Raw) }
$PkgXml = & $GetXmlContent
@{
Current = [version] $PkgXml.package.version
Download = [version] ([Distribution]::UpdateInfo32).Version
} | ForEach-Object {
If ($_.Current -le $_.Download) {
$PkgXml.package.version = "$($_.Download)"
$PkgXml.OuterXml | Out-File .\pkg.xml
'.\tools\helpers.ps1' |
ForEach-Object {
$Dui = [Distribution]::UpdateInfo()
$Count = (Get-Content $_ |
Select-Object -Skip ($Dui -split "`n").Count).Count
Set-Content $_ -Value ($Dui + "`n" + ((Get-Content $_ -Tail $Count) -join "`n"))
}
}
}
$Result = (& $GetXmlContent).package.version
Pop-Location
Return $Result
}

Static [string] SelectLink($Link) { Return $Link.Where({$_.Url -like '*.exe'}).Url }

Static [string] SelectChecksum($Link) {
Return (
(((Invoke-WebRequest "$($Link.Where({$_.Url -like '*512SUMS'}).Url)").Content |
ForEach-Object { [char] $_ }) -join '' -split "`n" |
ConvertFrom-String).Where({$_.P2 -ieq 'youtube-dl.exe'}).P1
)
}

Static [string] UpdateInfo() {
$UI32 = [Distribution]::UpdateInfo32
Return @"
`$UpdateInfo = [PSCustomObject] @{
Version = '$($UI32.Version)'
Link = '$([Distribution]::SelectLink($UI32.Link))'
Checksum = '$([Distribution]::SelectChecksum($UI32.Link))'
}
"@
}
}

Filter New-Package {
<#
.SYNOPSIS
Create a nuget package
#>

[Distribution]::UpdateVersion() |
ForEach-Object { [Distribution]::NewNugetPackage($_, "year=$((Get-Date).Year) version=$_") }
}

Filter Publish-Package {
<#
.SYNOPSIS
Publish chocolatey package to Community repository
.NOTES
Precondition:
1. The CHOCO_API_KEY environment variable is set.
#>

Param([Parameter(Mandatory=$true)] $NugetPackage)

Try {
If ($null -eq $Env:CHOCO_API_KEY) { Throw 'CHOCO_API_KEY_IsNull' }
choco apikey --key $Env:CHOCO_API_KEY --source https://push.chocolatey.org/
If ($LASTEXITCODE -eq 1) { Throw }
choco push $NugetPackage --source https://push.chocolatey.org/
If ($LASTEXITCODE -eq 1) { Throw }
}
Catch { "ERROR: $($_.Exception.Message)" }
}

Filter Deploy-Package {
<#
.SYNOPSIS
Run deployment tasks
#>

Push-Location $PSScriptRoot
New-Package |
ForEach-Object {
Publish-Package $_
Remove-Item $_ -Force
}
Pop-Location
}
4 changes: 4 additions & 0 deletions pkg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!--
The version in the NuSpec is the build version of the package.
The package version mentionned here is generated at deployment.
--><package id="ytube-dl" version="2021.12.17" />
9 changes: 0 additions & 9 deletions test/startup.bat

This file was deleted.

16 changes: 0 additions & 16 deletions test/test.psm1

This file was deleted.

22 changes: 22 additions & 0 deletions tools/chocolateyinstall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
$ErrorActionPreference = 'Stop'
$Private:ToolDir = Split-Path $MyInvocation.MyCommand.Definition
. "$ToolDir\helpers.ps1"
@{
PackageName = $Env:ChocolateyPackageName
FileFullPath = $Executable
} + (
Get-UpdateInfo |
ForEach-Object {
@{
Url = $_.Link
Checksum = $_.Checksum
ChecksumType = 'SHA512'
Version = $_.Version
}
}
) | ForEach-Object {
If (Test-IsOutdated ([version] $_.Version)) {
$_.Remove('Version')
Get-ChocolateyWebFile @_
}
}
7 changes: 7 additions & 0 deletions tools/chocolateyuninstall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
$ErrorActionPreference = 'Stop'
. "$(Split-Path $MyInvocation.MyCommand.Definition)\helpers.ps1"
Try {
Remove-Item $Executable -Force
Set-PowerShellExitCode -ExitCode 0
}
Catch { Write-Warning 'Youtube-DL is already uninstalled.' }
44 changes: 44 additions & 0 deletions tools/helpers.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
$UpdateInfo = [PSCustomObject] @{
Version = '2021.12.17'
Link = 'https://github.com/ytdl-org/youtube-dl/releases/download/2021.12.17/youtube-dl.exe'
Checksum = '24cc5ad86c35f40ff8f864f7098ebf50a0a57375216732b4e27a3fffa5de7dbe0f40bd41005e53fe1b2f0713df3f00182b8b552a785ccc41ee968144fe03075c'
}

## Current Install Helper Functions and Variables

$Executable = "${Env:ChocolateyInstall}\bin\youtube-dl.exe"

Filter Get-Executable { (Get-Item $Executable -ErrorAction SilentlyContinue).FullName }

Filter Get-Version {
[version] $(Switch (Get-Executable) {
{ ![string]::IsNullOrEmpty($_) } { (Get-ItemProperty ($_)).VersionInfo.ProductVersion }
Default { '0.0.0.0' }
})
}

Filter Test-IsOutdated {
Param([version] $OpVersion)
(Get-Version) -lt $OpVersion
}

Function Get-UpdateInfo {
Try {
Switch (
Get-DownloadInfo -PropertyList @{
RepositoryId = 'ytdl-org/youtube-dl'
AssetPattern = 'youtube-dl.exe$|SHA2-512SUMS$'
}
) { { $Null -notin @($_.Version,$_.Link) } {
Return [PSCustomObject] @{
Version = $_.Version
Link = $_.Link.Where({$_.Url -like '*.exe'}).Url
Checksum = (((Invoke-WebRequest "$($_.Link.Where({$_.Url -like '*512SUMS'}).Url)").Content |
ForEach-Object { [char] $_ }) -join '' -split "`n" |
ConvertFrom-String).Where({$_.P2 -ieq 'youtube-dl.exe'}).P1
}
} }
Throw
}
Catch { $UpdateInfo }
}
28 changes: 28 additions & 0 deletions ytube-dl.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
<metadata>
<id>ytube-dl</id>
<version>1.0.0</version>
<packageSourceUrl>https://github.com/sangafabrice/choco-packages/tree/ytube-dl</packageSourceUrl>
<owners>sangafabrice</owners>
<title>YouTube-DL</title>
<authors>YT-DL ORG</authors>
<projectUrl>https://yt-dl.org/</projectUrl>
<iconUrl>https://rawcdn.githack.com/sangafabrice/choco-packages/4a3f99c43c1c2529ebb27f5a6b68b4c2efa5d4eb/icon.png</iconUrl>
<copyright>© $year$ YT-DL ORG.</copyright>
<licenseUrl>https://rawcdn.githack.com/ytdl-org/youtube-dl/8a158a936c8b002ef536e9e2b778ded02c09c0fa/LICENSE</licenseUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<tags>youtube-dl youtube cli download video</tags>
<summary>Youtube-dl is a small command-line program to download videos from YouTube.com and few more sites.</summary>
<description>Youtube-dl is a small command-line program to download videos from YouTube.com and few more sites.</description>
<releaseNotes>https://github.com/ytdl-org/youtube-dl/releases/tag/$version$</releaseNotes>
<dependencies>
<dependency id="chocolatey-core.extension" version="1.1.0" />
<dependency id="download-info.extension" version="3.1.6" />
<dependency id="vcredist2010" version="10.0.40219.32503" />
</dependencies>
</metadata>
<files>
<file src="tools\**" target="tools" />
</files>
</package>