chcp.com 65001 > $null


function Install-VSNativeDesktopComponents {
    param(
        [Parameter(Mandatory = $true)]
        [string]$InstallerPath,

        [switch]$ARM64
    )

    if (!(Test-Path $InstallerPath)) {
        throw "Installer not found: $InstallerPath"
    }

    $args = @(
        "--add", "Microsoft.VisualStudio.Workload.NativeDesktop",
        "--add", "Microsoft.VisualStudio.Component.VC.ATLMFC",
        "--includeRecommended"
    )

    if ($ARM64) {
        $args += @(
            "--add", "Microsoft.VisualStudio.Component.VC.Tools.ARM64",
            "--add", "Microsoft.VisualStudio.Component.VC.MFC.ARM64"
        )
    }

    Write-Host "Running installer: $InstallerPath"
    Write-Host "Arguments:"
    $args | ForEach-Object { Write-Host "  $_" }

    & $InstallerPath @args
    $exitCode = $LASTEXITCODE

    if ($exitCode -ne 0) {
        throw "Visual Studio installer failed with exit code: $exitCode"
    }

    Write-Host "Visual Studio components installed successfully."
}

$script:cache_dir = "D:\wyc\cached_external_library\cef_cache"

function get_cached_file($url, $cache_path, $out_path) {
    $cache_file_path = Join-Path -Path $script:cache_dir -ChildPath $cache_path

    $cache_dir_path = Split-Path -Path $cache_file_path -Parent
    if (!(Test-Path $cache_dir_path)) {
        New-Item -ItemType Directory -Path $cache_dir_path -Force | Out-Null
    }

    if (!(Test-Path $cache_file_path)) {
        Write-Host "下载文件: $url"
        Invoke-WebRequest -Uri $url -OutFile $cache_file_path
    }
    else {
        Write-Host "缓存已存在，跳过下载: $cache_file_path"
    }

    $out_dir = Split-Path -Path $out_path -Parent
    if ($out_dir -and !(Test-Path $out_dir)) {
        New-Item -ItemType Directory -Path $out_dir -Force | Out-Null
    }

    Copy-Item -Path $cache_file_path -Destination $out_path -Force
    Write-Host "已复制到: $out_path"
}

function get_cached_zip($url, $cache_path, $out_path) {
    $zip_path = Join-Path -Path $script:cache_dir -ChildPath $cache_path

    $zip_dir = Split-Path -Path $zip_path -Parent
    if (!(Test-Path $zip_dir)) {
        New-Item -ItemType Directory -Path $zip_dir -Force | Out-Null
    }

    if (!(Test-Path $zip_path)) {
        Write-Host "下载压缩包: $url"
        Invoke-WebRequest -Uri $url -OutFile $zip_path
    }
    else {
        Write-Host "缓存已存在，跳过下载: $zip_path"
    }

    if (Test-Path $out_path) {
        Remove-Item -Path $out_path -Recurse -Force
    }
    New-Item -ItemType Directory -Path $out_path -Force | Out-Null

    Write-Host "解压到: $out_path"
    Expand-Archive -Path $zip_path -DestinationPath $out_path -Force
}

function write_text_file {
    param(
        [string]$file_path,
        [string]$content
    )

    $dir = Split-Path -Path $file_path -Parent
    if ($dir -and !(Test-Path $dir)) {
        New-Item -ItemType Directory -Path $dir -Force | Out-Null
    }

    Set-Content -Path $file_path -Value $content -Encoding UTF8
}



$work_dir
$automate_dir
$depot_tools_dir
$chromium_git_dir
$create_dir
$automate_py
$python3_bat
$depot_tools_url
$automate_url
$ver

function update_tools(){
    get_cached_zip  $depot_tools_url "depot_tools_$ver.zip" $depot_tools_dir
    get_cached_file $automate_url    "automate_$ver.py"     $automate_py


    & (Join-Path $depot_tools_dir "update_depot_tools.bat")
    if ($LASTEXITCODE -ne 0) {
        throw "update_depot_tools.bat 执行失败: $LASTEXITCODE"
    }




    if (($env:PATH -split ';') -notcontains $depot_tools_dir) {
        $env:PATH = "$depot_tools_dir;$env:PATH"
    }

    & $python3_bat $automate_py --help
    if ($LASTEXITCODE -ne 0) {
        throw "automate-git.py --help 执行失败: $LASTEXITCODE"
    }

}


function prepare_env($work_dir, $ver, $chromium_ref) {
    $script:work_dir="$work_dir"
    $script:ver="$ver"
    $script:automate_dir     = Join-Path $work_dir "automate"
    $script:depot_tools_dir  = Join-Path $work_dir "depot_tools"
    $script:chromium_git_dir = Join-Path $work_dir "chromium_git"
    $script:create_dir       = Join-Path $work_dir "chromium_git/chromium/src/cef"
    $script:automate_py      = Join-Path $automate_dir "automate-git.py"
    $script:python3_bat      = Join-Path $depot_tools_dir "python3.bat"
    $script:depot_tools_url = "https://storage.googleapis.com/chrome-infra/depot_tools.zip"
    $script:automate_url    = "https://raw.githubusercontent.com/chromiumembedded/cef/${ver}/tools/automate/automate-git.py"


}




function generate_update_script(){
    $bat= Join-Path $chromium_git_dir "update.bat"
    write_text_file "$bat" @"
@echo off
chcp 65001 >nul
cd /d "$chromium_git_dir"
set "PATH=$depot_tools_dir;%PATH%"
call "$python3_bat" "$automate_py" ^
  --download-dir="$chromium_git_dir" ^
  --depot-tools-dir="$depot_tools_dir" ^
  --no-chromium-history ^
  --no-distrib ^
  --no-build
"@
}


function generate_create_script(){
    $bat = Join-Path $create_dir "create.bat"
    echo "bat $bat"
    write_text_file "$bat" @"
chcp 65001 >nul
cd ${create_dir}
set "PATH=$depot_tools_dir;%PATH%"
set "GN_DEFINES=is_component_build=true"
set "GN_ARGUMENTS=--ide=vs2022 --sln=cef --filters=//cef/*"
call cef_create_projects.bat
"@
}



prepare_env "D:\cef_builds\4_4_1436" "7680" "146.0.7680.184"
#  prepare_env "D:\cef_builds\4_4_851" "6613" "128.0.6613.138"
update_tools
generate_update_script



&  "$chromium_git_dir\update.bat"

generate_create_script
&  "$create_dir\create.bat"


$DepotPyDir = "$work_dir\depot_tools\bootstrap-2@3_11_8_chromium_35_bin\python3\bin"
$env:PATH = "$DepotPyDir;$env:PATH"
where.exe python3
python3 --version

if (($env:PATH -split ';') -notcontains $depot_tools_dir) {
    $env:PATH = "$depot_tools_dir;$env:PATH"
}
Set-Location "$work_dir\chromium_git\chromium\src"

& "C:\Program Files\JetBrains\CLion 2026.1\bin\ninja\win\x64\ninja.exe" `
  -C "out\Release_GN_x64" `
  cef


#Install-VSNativeDesktopComponents -InstallerPath "D:\wyc\Environment\vs_Enterprise2022.exe"

#D:\cef_builds\4_4_851\chromium_git\chromium\src\out\Release_GN_x64
& "$work_dir\chromium_git\chromium\src\out\Release_GN_x64\ceftests.exe"


exit 0