Jelajahi Sumber

修改脚本成powershell

yichael 2 bulan lalu
induk
melakukan
02f79cc24b

+ 7 - 3
bat-tool/adb-connect-test/adb-connect.bat

@@ -1,5 +1,9 @@
 @echo off
-cd /d "%~dp0"
-node adb-connect.js
-
+set "ROOT=%~dp0..\.."
+set "ADB=%ROOT%\lib\scrcpy-adb\adb.exe"
+if not exist "%ADB%" (echo [ERROR] ADB not found: %ADB% & pause & exit /b 1)
+if "%~1"=="" (set "IP=192.168.0.101") else set "IP=%~1"
+if "%~2"=="" (set "PORT=5555") else set "PORT=%~2"
+"%ADB%" connect %IP%:%PORT%
+if errorlevel 1 (echo failed) else (echo success)
 pause

+ 0 - 28
bat-tool/adb-connect-test/adb-connect.js

@@ -1,28 +0,0 @@
-#!/usr/bin/env node
-const { execSync } = require('child_process')
-const path = require('path')
-const fs = require('fs')
-
-// 根目录 = config 文件所在目录的上级(config 在 <根>/configs/config.js)
-const configPath = process.env.STATIC_ROOT
-  ? path.join(path.dirname(process.env.STATIC_ROOT), 'configs', 'config.js')
-  : path.join(__dirname, '..', '..', 'configs', 'config.js')
-const PROJECT_ROOT = path.dirname(path.dirname(path.resolve(configPath)))
-const config = fs.existsSync(configPath) ? require(configPath) : {}
-const adbPath = config.adbPath?.path
-  ? (path.isAbsolute(config.adbPath.path) ? config.adbPath.path : path.resolve(PROJECT_ROOT, config.adbPath.path))
-  : path.join(PROJECT_ROOT, 'lib', 'scrcpy-adb', process.platform === 'win32' ? 'adb.exe' : 'adb')
-
-const deviceIp = '192.168.0.101'
-const devicePort = '5555'
-
-/** Run adb connect and return whether connected. */
-function connect() {
-  const out = execSync(`"${adbPath}" connect ${deviceIp}:${devicePort}`, { encoding: 'utf-8' }).trim()
-  return out.includes('connected') || out.includes('already connected')
-}
-
-const ok = connect()
-console.log(ok ? `${deviceIp}:${devicePort}  Connected` : `${deviceIp}:${devicePort}  Connect failed`)
-process.exit(ok ? 0 : 1)
-

+ 4 - 13
bat-tool/adb-enable-port5555/enable-port5555.bat

@@ -2,16 +2,7 @@
 chcp 65001 >nul
 title Enable ADB Port 5555
 cd /d "%~dp0"
-node enable-port5555.js
-if errorlevel 1 (
-    echo.
-    echo [ERROR] Failed to enable port 5555
-    echo.
-    pause
-    exit /b 1
-) else (
-    echo.
-    echo [OK] Port 5555 enabled successfully
-    echo.
-    pause
-)
+powershell -ExecutionPolicy Bypass -NoProfile -File "%~dp0enable-port5555.ps1"
+if errorlevel 1 (echo. & echo [ERROR] Failed to enable port 5555) else (echo. & echo [OK] Port 5555 enabled successfully)
+echo.
+pause

+ 0 - 77
bat-tool/adb-enable-port5555/enable-port5555.js

@@ -1,77 +0,0 @@
-#!/usr/bin/env node
-const { execSync } = require('child_process')
-const path = require('path')
-
-const fs = require('fs')
-// 根目录 = config 文件所在目录的上级(config 在 <根>/configs/config.js)
-const configPath = process.env.STATIC_ROOT
-  ? path.join(path.dirname(process.env.STATIC_ROOT), 'configs', 'config.js')
-  : path.join(__dirname, '..', '..', 'configs', 'config.js')
-const PROJECT_ROOT = path.dirname(path.dirname(path.resolve(configPath)))
-const TCPIP_PORT = 5555
-
-/** 从配置解析并返回 ADB 可执行文件路径 */
-function getAdbPath() {
-  if (!fs.existsSync(configPath)) return path.join(PROJECT_ROOT, 'lib', 'scrcpy-adb', process.platform === 'win32' ? 'adb.exe' : 'adb')
-  const config = require(configPath)
-  const p = config.adbPath?.path
-  return p ? (path.isAbsolute(p) ? p : path.resolve(PROJECT_ROOT, p)) : path.join(PROJECT_ROOT, 'lib', 'scrcpy-adb', process.platform === 'win32' ? 'adb.exe' : 'adb')
-}
-
-/** 返回当前通过 USB 连接的设备 ID 列表 */
-function getConnectedDeviceIds(adbPath) {
-  const out = execSync(`"${adbPath}" devices`, { encoding: 'utf-8' })
-  return out
-    .split('\n')
-    .filter((line) => line.trim() && !line.startsWith('List'))
-    .map((line) => line.trim().split('\t')[0])
-    .filter((id) => id)
-}
-
-/** 通过 USB 打开系统「无线调试」开关,并读回确认;返回是否成功 */
-function enableWirelessDebuggingSetting(adbPath, deviceId) {
-  execSync(`"${adbPath}" -s ${deviceId} shell settings put global adb_wifi_enabled 1`, { encoding: 'utf-8' })
-  const out = execSync(`"${adbPath}" -s ${deviceId} shell settings get global adb_wifi_enabled`, { encoding: 'utf-8' })
-  return out.trim() === '1'
-}
-
-/** 通过 USB 在该设备上开启 TCPIP 监听,用于后续无线连接 */
-function enableTcpipOnDevice(adbPath, deviceId, port) {
-  return execSync(`"${adbPath}" -s ${deviceId} tcpip ${port}`, { encoding: 'utf-8' }).trim()
-}
-
-/** 获取设备 WLAN IPv4,用于无线连接提示(先 getprop,无则用 ip addr)*/
-function getDeviceWlanIp(adbPath, deviceId) {
-  const prop = execSync(`"${adbPath}" -s ${deviceId} shell getprop dhcp.wlan0.ipaddress`, { encoding: 'utf-8' }).trim()
-  if (prop && /^\d+\.\d+\.\d+\.\d+$/.test(prop)) return prop
-  const out = execSync(`"${adbPath}" -s ${deviceId} shell ip -4 addr show wlan0`, { encoding: 'utf-8' })
-  const m = out.match(/inet\s+(\d+\.\d+\.\d+\.\d+)/)
-  return m ? m[1] : null
-}
-
-const adbPath = getAdbPath()
-const devices = getConnectedDeviceIds(adbPath)
-
-if (devices.length === 0) {
-  process.stderr.write('No devices found. Please connect a device via USB.\n')
-  process.exit(1)
-}
-
-const deviceId = devices[0]
-if (devices.length > 1) {
-  process.stdout.write(`Multiple devices. Using: ${deviceId}\n`)
-}
-
-// 先打开系统「无线调试」、取 WLAN IP(USB 仍连接),再执行 tcpip
-const wirelessOk = enableWirelessDebuggingSetting(adbPath, deviceId)
-if (wirelessOk) {
-  process.stdout.write('无线调试已开启\n')
-} else {
-  process.stdout.write('无线调试开关可能不被本机支持,请到 设置 → 开发者选项 中手动打开\n')
-}
-const wlanIp = getDeviceWlanIp(adbPath, deviceId)
-const tcpipOut = enableTcpipOnDevice(adbPath, deviceId, TCPIP_PORT)
-process.stdout.write(tcpipOut + '\n')
-if (wlanIp) {
-  process.stdout.write(`Wireless: adb connect ${wlanIp}:${TCPIP_PORT}\n`)
-}

+ 22 - 0
bat-tool/adb-enable-port5555/enable-port5555.ps1

@@ -0,0 +1,22 @@
+$ErrorActionPreference = 'SilentlyContinue'
+$TCPIP_PORT = 5555
+$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
+$root = (Get-Item (Join-Path $scriptDir '..\..')).FullName
+$adb = Join-Path $root 'lib\scrcpy-adb\adb.exe'
+if (-not (Test-Path $adb)) { Write-Host '[ERROR] ADB not found'; exit 1 }
+$lines = & $adb devices 2>$null
+$devices = $lines | ForEach-Object { $t = $_.Trim(); if ($t -match '^(\S+)\s+device') { $Matches[1] } } | Where-Object { $_ }
+$dev = $devices | Select-Object -First 1
+if (-not $dev) { Write-Host 'No devices found. Please connect a device via USB.'; exit 1 }
+if ($devices.Count -gt 1) { Write-Host "Multiple devices. Using: $dev" }
+& $adb -s $dev shell 'settings put global adb_wifi_enabled 1' 2>$null | Out-Null
+$wireless = (& $adb -s $dev shell 'settings get global adb_wifi_enabled' 2>$null).Trim()
+if ($wireless -eq '1') { Write-Host 'Wireless debugging enabled.' } else { Write-Host 'Wireless setting may not be supported. Enable it in Settings > Developer options.' }
+$wlanIp = (& $adb -s $dev shell 'getprop dhcp.wlan0.ipaddress' 2>$null).Trim()
+if ($wlanIp -notmatch '^\d+\.\d+\.\d+\.\d+$') {
+  $out = & $adb -s $dev shell 'ip -4 addr show wlan0' 2>$null
+  if ($out -match 'inet\s+(\d+\.\d+\.\d+\.\d+)') { $wlanIp = $Matches[1] }
+}
+$tcpipOut = & $adb -s $dev tcpip $TCPIP_PORT 2>$null
+Write-Host $tcpipOut
+if ($wlanIp) { Write-Host "Wireless: adb connect ${wlanIp}:${TCPIP_PORT}" }

+ 0 - 28
bat-tool/delete-lib-adb.bat

@@ -1,28 +0,0 @@
-@echo off
-echo Attempting to delete lib\adb directory...
-echo.
-
-cd /d "%~dp0\.."
-
-REM 等待一下,确保没有进程在使用
-timeout /t 3 /nobreak >nul
-
-REM 尝试删除目录
-if exist "lib\adb" (
-    echo Deleting lib\adb directory...
-    rd /s /q "lib\adb" 2>nul
-    if exist "lib\adb" (
-        echo.
-        echo ERROR: Failed to delete lib\adb directory.
-        echo Please close any processes using adb.exe and try again.
-        echo.
-        echo You can manually delete the directory after closing all adb processes.
-    ) else (
-        echo.
-        echo SUCCESS: lib\adb directory deleted successfully!
-    )
-) else (
-    echo lib\adb directory not found (already deleted or doesn't exist).
-)
-
-pause

+ 0 - 0
bat-tool/getip.bat


+ 2 - 12
bat-tool/getip/getip.bat

@@ -1,14 +1,4 @@
 @echo off
-chcp 65001 >nul
-title 获取设备 IP
 cd /d "%~dp0"
-node getip.js
-if errorlevel 1 (
-    echo.
-    pause
-    exit /b 1
-) else (
-    echo.
-    pause
-    exit /b 0
-)
+powershell -ExecutionPolicy Bypass -NoProfile -File "%~dp0getip.ps1"
+pause

+ 0 - 91
bat-tool/getip/getip.js

@@ -1,91 +0,0 @@
-#!/usr/bin/env node
-const { execSync } = require('child_process')
-const path = require('path')
-
-const fs = require('fs')
-// 根目录 = config 文件所在目录的上级(config 在 <根>/configs/config.js)
-const configPath = process.env.STATIC_ROOT
-  ? path.join(path.dirname(process.env.STATIC_ROOT), 'configs', 'config.js')
-  : path.join(__dirname, '..', '..', 'configs', 'config.js')
-const PROJECT_ROOT = path.dirname(path.dirname(path.resolve(configPath)))
-
-function getAdbPath() {
-  if (!fs.existsSync(configPath)) return path.join(PROJECT_ROOT, 'lib', 'scrcpy-adb', process.platform === 'win32' ? 'adb.exe' : 'adb')
-  const config = require(configPath)
-  const p = config.adbPath?.path
-  return p ? (path.isAbsolute(p) ? p : path.resolve(PROJECT_ROOT, p)) : path.join(PROJECT_ROOT, 'lib', 'scrcpy-adb', process.platform === 'win32' ? 'adb.exe' : 'adb')
-}
-
-function getConnectedDeviceIds(adbPath) {
-  const out = execSync(`"${adbPath}" devices`, { encoding: 'utf-8' })
-  return out
-    .split('\n')
-    .filter((line) => line.trim() && !line.startsWith('List') && line.includes('\tdevice'))
-    .map((line) => line.trim().split('\t')[0])
-    .filter((id) => id && !id.includes(':'))
-}
-
-function parseInetFromOutput(out, skipLoopback = false) {
-  const re = /inet\s+(\d+\.\d+\.\d+\.\d+)/g
-  const ips = []
-  let m
-  while ((m = re.exec(out || '')) !== null) ips.push(m[1])
-  if (ips.length === 0) return null
-  if (skipLoopback) {
-    const nonLoop = ips.find((ip) => ip !== '127.0.0.1')
-    return nonLoop || null
-  }
-  return ips[0]
-}
-
-function adbShellCmd(adbPath, deviceId, shellCmd) {
-  const id = deviceId.indexOf(' ') >= 0 ? `"${deviceId}"` : deviceId
-  return `"${adbPath}" -s ${id} shell ${shellCmd}`
-}
-
-function getDeviceIp(adbPath, deviceId) {
-  const run = (shellCmd) => {
-    try {
-      return execSync(adbShellCmd(adbPath, deviceId, shellCmd), { encoding: 'utf-8' })
-    } catch (e) {
-      return ''
-    }
-  }
-  let out = run('ip -4 addr show wlan0')
-  let ip = parseInetFromOutput(out)
-  if (ip) return ip
-  out = run('ip -4 addr show eth0')
-  ip = parseInetFromOutput(out)
-  if (ip) return ip
-  out = run('ip route get 1.1.1.1')
-  let m = out.match(/\bsrc\s+(\d+\.\d+\.\d+\.\d+)\b/)
-  if (m) return m[1]
-  m = out.match(/\bfrom\s+(\d+\.\d+\.\d+\.\d+)\b/)
-  if (m) return m[1]
-  for (const prop of ['dhcp.wlan0.ipaddress', 'net.wlan0.ipaddress', 'dhcp.eth0.ipaddress']) {
-    out = run('getprop ' + prop)
-    ip = (out || '').trim()
-    if (/^\d+\.\d+\.\d+\.\d+$/.test(ip)) return ip
-  }
-  out = run('ip -4 addr show')
-  return parseInetFromOutput(out, true)
-}
-
-function run() {
-  const adbPath = getAdbPath()
-  const devices = getConnectedDeviceIds(adbPath)
-  if (devices.length === 0) {
-    process.stderr.write('No devices found. Please connect a device via USB.\n')
-    process.exit(1)
-  }
-  const deviceId = devices[0]
-  const ip = getDeviceIp(adbPath, deviceId)
-  if (ip) {
-    process.stdout.write(ip + '\n')
-    process.exit(0)
-  }
-  process.stderr.write('Could not get device IP. Check WiFi on device.\n')
-  process.exit(1)
-}
-
-run()

+ 28 - 0
bat-tool/getip/getip.ps1

@@ -0,0 +1,28 @@
+$ErrorActionPreference = 'SilentlyContinue'
+$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
+$root = (Get-Item (Join-Path $scriptDir '..\..')).FullName
+$adb = Join-Path $root 'lib\scrcpy-adb\adb.exe'
+if (-not (Test-Path $adb)) { Write-Host '[ERROR] ADB not found'; exit 1 }
+$lines = & $adb devices 2>$null
+$dev = $lines | ForEach-Object { $t = $_.Trim(); if ($t -match '^(\S+)\s+device') { $Matches[1] } } | Where-Object { $_ } | Select-Object -First 1
+if (-not $dev) { Write-Host 'no device'; exit 0 }
+$ip = (& $adb -s $dev shell getprop dhcp.wlan0.ipaddress 2>$null).Trim()
+if ($ip -notmatch '^\d+\.\d+\.\d+\.\d+$') { $ip = (& $adb -s $dev shell getprop dhcp.eth0.ipaddress 2>$null).Trim() }
+if ($ip -notmatch '^\d+\.\d+\.\d+\.\d+$') {
+  $out = & $adb -s $dev shell 'ip route get 1.1.1.1' 2>$null
+  if ($out -match '\bsrc\s+(\d+\.\d+\.\d+\.\d+)\b') { $ip = $Matches[1] }
+}
+if ($ip -notmatch '^\d+\.\d+\.\d+\.\d+$') {
+  $out = & $adb -s $dev shell 'ip -4 addr show wlan0' 2>$null
+  if ($out -match 'inet\s+(\d+\.\d+\.\d+\.\d+)') { $ip = $Matches[1] }
+}
+if ($ip -notmatch '^\d+\.\d+\.\d+\.\d+$') {
+  $out = & $adb -s $dev shell 'ip -4 addr show eth0' 2>$null
+  if ($out -match 'inet\s+(\d+\.\d+\.\d+\.\d+)') { $ip = $Matches[1] }
+}
+if ($ip -notmatch '^\d+\.\d+\.\d+\.\d+$') {
+  $out = & $adb -s $dev shell 'ip -4 addr show' 2>$null
+  $ips = [regex]::Matches($out, 'inet\s+(\d+\.\d+\.\d+\.\d+)') | ForEach-Object { $_.Groups[1].Value }
+  $ip = $ips | Where-Object { $_ -ne '127.0.0.1' } | Select-Object -First 1
+}
+if ($ip) { Write-Host $ip } else { Write-Host 'failed' }

+ 3 - 10
bat-tool/git-push/git-push.bat

@@ -2,13 +2,6 @@
 chcp 65001 >nul
 title Git Push
 cd /d "%~dp0"
-node git-push.js
-if errorlevel 1 (
-    echo.
-    pause
-    exit /b 1
-) else (
-    echo.
-    pause
-    exit /b 0
-)
+powershell -ExecutionPolicy Bypass -NoProfile -File "%~dp0git-push.ps1"
+echo.
+pause

+ 0 - 76
bat-tool/git-push/git-push.js

@@ -1,76 +0,0 @@
-#!/usr/bin/env node
-const { execSync } = require('child_process')
-const path = require('path')
-
-// Get project root (two levels up from this script)
-const projectRoot = path.resolve(__dirname, '../..')
-
-// Change to project root
-process.chdir(projectRoot)
-
-// Configuration: main branch name (leave empty to auto-detect)
-const CONFIG_BRANCH = 'master'
-
-// Detect main branch
-let mainBranch = CONFIG_BRANCH
-
-if (!CONFIG_BRANCH) {
-  // Auto-detect main branch (priority: master > main > current branch)
-  try {
-    execSync('git show-ref --verify --quiet refs/heads/master', { stdio: 'ignore' })
-    mainBranch = 'master'
-  } catch (e) {
-    try {
-      execSync('git show-ref --verify --quiet refs/heads/main', { stdio: 'ignore' })
-      mainBranch = 'main'
-    } catch (e2) {
-      // Use current branch
-      mainBranch = execSync('git rev-parse --abbrev-ref HEAD', { encoding: 'utf-8' }).trim()
-    }
-  }
-}
-
-// Show files to be pushed
-console.log('')
-console.log('Files to be pushed:')
-console.log('========================================')
-try {
-  const diffOutput = execSync(`git diff --name-status origin/${mainBranch}..HEAD`, { encoding: 'utf-8', stdio: ['ignore', 'pipe', 'ignore'] })
-  console.log(diffOutput.trim())
-} catch (e) {
-  // If remote branch doesn't exist, show all files
-  try {
-    const logOutput = execSync('git log --name-status --oneline HEAD', { encoding: 'utf-8', stdio: ['ignore', 'pipe', 'ignore'] })
-    const lines = logOutput.split('\n').filter(line => line.trim() && !/^[a-f0-9]/.test(line))
-    console.log(lines.join('\n'))
-  } catch (e2) {
-    // Ignore errors
-  }
-}
-console.log('========================================')
-console.log('')
-
-// Push to main branch
-console.log('')
-console.log(`Pushing to origin/${mainBranch}...`)
-console.log('========================================')
-try {
-  const pushOutput = execSync(`git push origin ${mainBranch}`, { encoding: 'utf-8', stdio: 'inherit' })
-  console.log('========================================')
-  console.log('')
-  console.log('========================================')
-  console.log('[OK] Push successful')
-  console.log('========================================')
-  console.log('')
-  process.exit(0)
-} catch (error) {
-  console.log('========================================')
-  console.log('')
-  console.log('========================================')
-  console.log(`[ERROR] Push failed (exit code: ${error.status || 1})`)
-  console.log('========================================')
-  console.log('')
-  console.log('Please check the error messages above.')
-  console.log('')
-  process.exit(error.status || 1)
-}

+ 38 - 0
bat-tool/git-push/git-push.ps1

@@ -0,0 +1,38 @@
+$ErrorActionPreference = 'SilentlyContinue'
+$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
+$root = (Get-Item (Join-Path $scriptDir '..\..')).FullName
+Set-Location $root
+$mainBranch = 'master'
+git show-ref --verify --quiet refs/heads/master 2>$null | Out-Null
+if ($LASTEXITCODE -ne 0) {
+  git show-ref --verify --quiet refs/heads/main 2>$null | Out-Null
+  if ($LASTEXITCODE -eq 0) { $mainBranch = 'main' } else { $mainBranch = (git rev-parse --abbrev-ref HEAD 2>$null).Trim() }
+}
+Write-Host ''
+Write-Host 'Files to be pushed:'
+Write-Host '========================================'
+$diff = git diff --name-status "origin/$mainBranch..HEAD" 2>$null
+if ($LASTEXITCODE -eq 0 -and $diff) { Write-Host $diff } else {
+  $log = git log --name-status --oneline HEAD 2>$null
+  $lines = $log -split "`n" | Where-Object { $_.Trim() -and $_ -notmatch '^[a-f0-9]' }
+  if ($lines) { Write-Host ($lines -join "`n") }
+}
+Write-Host '========================================'
+Write-Host ''
+Write-Host "Pushing to origin/$mainBranch..."
+Write-Host '========================================'
+git push origin $mainBranch
+if ($LASTEXITCODE -eq 0) {
+  Write-Host ''
+  Write-Host '========================================'
+  Write-Host '  success (Push successful)'
+  Write-Host '========================================'
+  exit 0
+} else {
+  Write-Host ''
+  Write-Host '========================================'
+  Write-Host '  failed (Push failed)'
+  Write-Host '  Check error messages above.'
+  Write-Host '========================================'
+  exit 1
+}

+ 2 - 1
bat-tool/git-update-pwd/git-credential-config.bat

@@ -1,3 +1,4 @@
 @echo off
-cd /d "%~dp0\.." && powershell.exe -ExecutionPolicy Bypass -NoProfile -File "%~dp0git-credential-config.ps1" %*
+REM 项目根 = bat-tool 的上一级(git 仓库根,便于 git config --local)
+cd /d "%~dp0\..\.." && powershell.exe -ExecutionPolicy Bypass -NoProfile -File "%~dp0git-credential-config.ps1" %*
 pause