快速导入蓝湖图片压缩包.ps1

61 阅读1分钟
# 桌面地址
$DesktopPath = [Environment]::GetFolderPath("Desktop")
# 项目地址
$WorkPath = 'C:\...\AndroidProject\...\app\src\main\res'

# 获取最后一个下载下来的zip文件
$DownloadPath = "C:\Users\dever\Downloads"
$Info = Get-ChildItem -Path $DownloadPath *.zip | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1
# 蓝湖图片压缩包路径
$ZipPath = $Info.FullName

# 解压文件到桌面
Expand-Archive $ZipPath -DestinationPath $DesktopPath

# 将mipmap命名为drawable
$MinmapFolder = Get-ChildItem -Path $DesktopPath mipmap-*
foreach ($FolderPath in $MinmapFolder) 
{
    $NewFolderName = $FolderPath -replace "mipmap", "drawable"
    Rename-Item -Path $DesktopPath\$FolderPath -NewName $DesktopPath\$NewFolderName
}

# 获取输入的图片名称
function GetImageName
{
    $InputName = Read-Host '图片名称: '
    $WebpName = $InputName + '.webp'
    return $WebpName
}

# 判断是否有同名文件
$InputKey = $null
$IsExists = $null
function NameIsExists ($ImageName)
{
    $WorkRes = Get-ChildItem -Path $WorkPath\drawable-*
    
    foreach ($DrawablePath in $WorkRes) 
    {
        Set-Variable "IsExists" (Test-Path "$DrawablePath\$ImageName") -Scope 2
        # 存在图片为true  不存在为false
        if ($IsExists) 
        {
            Write-Output "IsExists: $IsExists"
            $TempInput = Read-Host '存在同名文件, 是否覆盖?  Y(覆盖)/N(退出)/R(重命名)'
            Set-Variable "InputKey" $TempInput -Scope 2
            break
        }
        Set-Variable "InputKey" $null
    }
}

function RenameFileAndCopy ($ImageName) 
{
    # 遍历桌面文件依次给图片改名
    Get-ChildItem -Path $DesktopPath\drawable-*\*.webp -Recurse | ForEach { Rename-Item -Path $_ -NewName $ImageName }

    # 复制文件到安卓项目
    Copy-Item -Path $DesktopPath\drawable-* -Recurse -Destination $WorkPath -Force
}

function Check {
    $ImageName = GetImageName
    NameIsExists $ImageName

    Switch ($InputKey)
    {
        'Y' 
        {
            RenameFileAndCopy (Get-Variable "ImageName" -ValueOnly)
        }
        'R' 
        {
            Set-Variable "InputKey" $null
            check
        }
        'N' 
        {
            Remove-Item -Path $DesktopPath\drawable-* -Recurse
            Exit
        }
        '' 
        {
            if (Get-Variable "IsExists" -ValueOnly)
            {
                Set-Variable "InputKey" $null
                check
            }
            else 
            {
                renameFileAndCopy (Get-Variable "ImageName" -ValueOnly)
            }
        }
    }
}

Check

# 删除解压文件
Remove-Item -Path $DesktopPath\drawable-* -Recurse
#Exit