axmol/1k/fetch.ps1

260 lines
7.5 KiB
PowerShell
Raw Normal View History

2024-03-23 00:51:46 +08:00
# fetch pkg by url or manifest.json path
param(
2024-03-24 05:11:41 +08:00
$uri, # the pkg uri
2024-03-23 00:51:46 +08:00
$prefix, # the prefix to store
2024-03-24 05:11:41 +08:00
$manifest_file = $null,
$name = $null,
$version = $null, # version hint
2024-05-17 23:56:54 +08:00
$revision = $null, # revision hint
[switch]$pull_branch
)
# content of _1kiss with yaml format
# ver: 1.0
# branch: 1.x
# commits: 2802
# rev: 29b0b28
Set-Alias println Write-Host
2024-03-24 05:11:41 +08:00
if (!$uri -or !$prefix) {
throw 'fetch.ps1: missing parameters'
}
function download_file($uri, $out) {
if (Test-Path $out -PathType Leaf) { return }
println "Downloading $uri to $out ..."
Invoke-WebRequest -Uri $uri -OutFile $out
}
function mkdirs($path) {
if (!(Test-Path $path -PathType Container)) {
New-Item $path -ItemType Directory 1>$null
}
}
2024-03-24 05:11:41 +08:00
# ensure cachedir
$cache_dir = Join-Path (Resolve-Path $PSScriptRoot/..).Path 'cache'
if (!(Test-Path $cache_dir -PathType Container)) {
mkdirs $cache_dir
}
2024-03-24 05:11:41 +08:00
function fetch_repo($url, $name, $dest, $ext) {
if ($ext -eq '.git') {
git clone --progress $url $dest | Out-Host
}
2024-03-24 05:11:41 +08:00
else {
2024-06-06 00:00:01 +08:00
$out = Join-Path $cache_dir "$Script:url_pkg_name$ext"
2024-03-24 05:11:41 +08:00
download_file $url $out
try {
if ($ext -eq '.zip') {
Expand-Archive -Path $out -DestinationPath $prefix -Force
}
2024-06-06 00:00:01 +08:00
elseif ($Script:url_pkg_name -match '.+\.tar(\..*)?$'){
tar xf "$out" -C $prefix
}
}
2024-03-24 05:11:41 +08:00
catch {
2024-04-02 22:31:34 +08:00
Remove-Item $out -Force
2024-03-24 12:55:10 +08:00
throw "fetch.ps1: extract $out failed, try again"
2024-03-24 05:11:41 +08:00
}
2024-03-24 12:55:10 +08:00
if (!(Test-Path $dest -PathType Container)) {
2024-04-24 22:28:59 +08:00
$original_lib_src = Join-Path $prefix $Script:url_pkg_name
if (Test-Path $original_lib_src -PathType Container) {
Rename-Item $original_lib_src $dest
2024-05-17 23:56:54 +08:00
}
else {
2024-04-24 22:28:59 +08:00
throw "fetch.ps1: the package name mismatch for $out"
}
}
}
2024-03-24 05:11:41 +08:00
}
2024-03-24 05:11:41 +08:00
# parse url from $uri
$uriInfo = [array]$uri.Split('#')
$uri = $uriInfo[0]
if (!$version) {
$version = $uriInfo[1]
}
2024-03-24 05:11:41 +08:00
$url = $null
if ($uri -match '^([a-z]+://|git@)') {
$url = $uri
}
elseif ($uri.StartsWith('gh:')) {
$url = "https://github.com/$($uri.substring(3))"
if (!$url.EndsWith('.git')) { $url += '.git' }
}
elseif ($uri.StartsWith('gl:')) {
$url = "https://gitlab.com/$($uri.substring(3))"
if (!$url.EndsWith('.git')) { $url += '.git' }
}
else {
$name = $uri
}
2024-03-24 05:11:41 +08:00
# simple match url/ssh schema
if (!$url) {
# fetch package from manifest config
$lib_src = Join-Path $prefix $name
$mirror = if (!(Test-Path (Join-Path $PSScriptRoot '.gitee') -PathType Leaf)) { 'github' } else { 'gitee' }
$url_base = @{'github' = 'https://github.com/'; 'gitee' = 'https://gitee.com/' }[$mirror]
2024-03-24 05:11:41 +08:00
$manifest_map = ConvertFrom-Json (Get-Content $manifest_file -raw)
if (!$version) {
$version_map = $manifest_map.versions
$version = $version_map.PSObject.Properties[$name].Value
}
if ($version) {
$url_path = $manifest_map.mirrors.PSObject.Properties[$mirror].Value.PSObject.Properties[$name].Value
if ($url_path) {
$url = "$url_base/$url_path"
if (!$url.EndsWith('.git')) { $url += '.git' }
2023-12-21 23:59:24 +08:00
}
}
2024-03-24 05:11:41 +08:00
}
2023-12-21 23:59:24 +08:00
2024-03-24 05:11:41 +08:00
if (!$url) {
throw "fetch.ps1: can't determine package url of '$name'"
}
2024-03-24 05:11:41 +08:00
$url_pkg_ext = $null
2024-04-24 22:28:59 +08:00
$Script:url_pkg_name = $null
2024-03-24 05:11:41 +08:00
$match_info = [Regex]::Match($url, '(\.git)|(\.zip)|(\.tar\.(gz|bz2|xz))$')
if ($match_info.Success) {
$url_pkg_ext = $match_info.Value
$url_file_name = Split-Path $url -Leaf
2024-04-24 22:28:59 +08:00
$Script:url_pkg_name = $url_file_name.Substring(0, $url_file_name.Length - $url_pkg_ext.Length)
2024-03-24 05:11:41 +08:00
if (!$name) {
2024-04-24 22:28:59 +08:00
$name = $Script:url_pkg_name
2024-03-24 05:11:41 +08:00
}
}
else {
throw "fetch.ps1: invalid url, must be endswith .git, .zip, .tar.xx"
}
2024-03-23 02:06:40 +08:00
2024-04-24 22:28:59 +08:00
$lib_src = Join-Path $prefix $name
2024-03-24 05:11:41 +08:00
$is_git_repo = $url_pkg_ext -eq '.git'
if (!$is_git_repo) {
$match_info = [Regex]::Match($url, '(\d+\.)+(-)?(\*|\d+)')
if ($match_info.Success) {
$version = $match_info.Value
}
}
2024-03-24 05:11:41 +08:00
if (!$version) {
throw "fetch.ps1: can't determine package version of '$name'"
}
2024-03-24 05:11:41 +08:00
Set-Variable -Name "${name}_src" -Value $lib_src -Scope global
2024-03-22 23:37:16 +08:00
2024-03-24 05:11:41 +08:00
$sentry = Join-Path $lib_src '_1kiss'
2024-05-20 23:46:48 +08:00
$is_rev_mod = $false # indicate whether rev already modfied or updated
2024-03-24 05:11:41 +08:00
# if sentry file missing, re-clone
if (!(Test-Path $sentry -PathType Leaf)) {
if (Test-Path $lib_src -PathType Container) {
Remove-Item $lib_src -Recurse -Force
}
2024-03-24 05:11:41 +08:00
fetch_repo -url $url -name $name -dest $lib_src -ext $url_pkg_ext
if (Test-Path $lib_src -PathType Container) {
New-Item $sentry -ItemType File 1>$null
}
else {
throw "fetch.ps1: fetch content from $url failed"
}
2024-05-20 23:46:48 +08:00
$is_rev_mod = $true
2024-03-24 05:11:41 +08:00
}
2024-04-06 08:35:14 +08:00
# re-check does valid local git repo
if (!(Test-Path "$lib_src/.git" -PathType Container)) { $is_git_repo = $false }
2024-03-24 05:11:41 +08:00
# checkout revision for git repo
if (!$revision) {
$ver_pair = [array]$version.Split('-')
$use_hash = $ver_pair.Count -gt 1
$revision = $ver_pair[$use_hash].Trim()
$version = $ver_pair[0]
}
2024-05-17 23:56:54 +08:00
$branch_name = $null
2024-03-24 05:11:41 +08:00
if ($is_git_repo) {
$old_rev_hash = $(git -C $lib_src rev-parse HEAD)
2024-06-02 00:27:51 +08:00
# The 'revision' can be branch name, tag or a commit id
$new_rev_hash = $(git -C $lib_src rev-parse --verify --quiet "$revision^{}")
if (!$new_rev_hash) {
git -C $lib_src fetch
2024-05-20 23:46:48 +08:00
$new_rev_hash = $(git -C $lib_src rev-parse --verify --quiet "$revision^{}")
if (!$new_rev_hash) {
2024-06-02 00:27:51 +08:00
throw "fetch.ps1: Could not found commit hash of $revision"
}
2024-06-02 00:27:51 +08:00
}
2024-05-17 23:56:54 +08:00
2024-06-02 00:27:51 +08:00
if ($old_rev_hash -ne $new_rev_hash) {
git -C $lib_src checkout $revision 1>$null 2>$null
$cur_rev_hash = $(git -C $lib_src rev-parse HEAD)
if ($cur_rev_hash -ne $new_rev_hash) {
println "fetch.ps1: warning: cur_rev_hash($cur_rev_hash) != new_rev_hash($new_rev_hash)"
2024-05-20 23:46:48 +08:00
}
2024-06-02 00:27:51 +08:00
$is_rev_mod = $true
2024-05-20 23:46:48 +08:00
}
2024-06-02 00:27:51 +08:00
$branch_name = $(git -C $lib_src branch --show-current)
if ($branch_name -and $pull_branch) {
2024-05-20 23:46:48 +08:00
git -C $lib_src pull
2024-06-02 00:27:51 +08:00
$new_rev_hash = $(git -C $lib_src rev-parse HEAD)
if ($cur_rev_hash -ne $new_rev_hash) {
$cur_rev_hash = $new_rev_hash
$is_rev_mod = $true
}
2024-03-23 02:06:40 +08:00
}
2024-03-24 05:11:41 +08:00
}
2024-05-20 23:46:48 +08:00
if ($is_rev_mod) {
2024-03-24 05:11:41 +08:00
$sentry_content = "ver: $version"
if ($is_git_repo) {
2024-06-02 00:27:51 +08:00
if ((Test-Path (Join-Path $lib_src '.gitmodules') -PathType Leaf)) {
git -C $lib_src submodule update --recursive --init
}
if ($branch_name) {
2024-05-20 23:46:48 +08:00
# tracking branch
2024-03-24 05:11:41 +08:00
$commits = $(git -C $lib_src rev-list --count HEAD)
$sentry_content += "`nbranch: $branch_name"
$sentry_content += "`ncommits: $commits"
2024-06-02 00:27:51 +08:00
$commit_id = $(git -C $lib_src rev-parse --short=7 HEAD)
$sentry_content += "`nrev: $commit_id"
println "fetch.ps1: HEAD is now at $branch_name@$commit_id"
}
2024-05-20 23:46:48 +08:00
else {
println "fetch.ps1: HEAD is now at $revision@$cur_rev_hash"
}
2024-03-24 05:11:41 +08:00
}
2024-03-24 05:11:41 +08:00
[System.IO.File]::WriteAllText($sentry, $sentry_content)
2024-03-24 12:55:10 +08:00
if ($is_git_repo) { git -C $lib_src add '_1kiss' }
2024-03-24 05:11:41 +08:00
}
# google gclient spec
if (Test-Path (Join-Path $lib_src '.gn') -PathType Leaf) {
# the repo use google gn build system manage deps and build
Push-Location $lib_src
# angle (A GLES native implementation by google)
2024-04-02 22:31:34 +08:00
if (Test-Path 'scripts/bootstrap.py' -PathType Leaf) {
2024-03-24 05:11:41 +08:00
python scripts/bootstrap.py
}
2024-03-24 05:11:41 +08:00
# darwin (A WebGPU native implementation by google)
if (Test-Path 'scripts/standalone.gclient' -PathType Leaf) {
Copy-Item scripts/standalone.gclient .gclient -Force
}
2024-03-24 05:11:41 +08:00
gclient sync -D
Pop-Location
}