Skip to content

Commit

Permalink
feat: 🚀 Support execution script function
Browse files Browse the repository at this point in the history
  • Loading branch information
viarotel committed Jul 12, 2024
1 parent 33b0181 commit 2013413
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 14 deletions.
4 changes: 2 additions & 2 deletions electron/exposes/adbkit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ const push = async (
progress?.(stats)
})

res.on('end', (ret) => {
resolve(ret)
res.on('end', () => {
resolve(savePath)
})

res.on('error', (err) => {
Expand Down
80 changes: 80 additions & 0 deletions src/components/Device/components/ControlBar/Shell/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<template>
<div class="" @click="handleClick(device)">
<slot :loading="loading" />
</div>
</template>

<script setup>
import { ElMessage } from 'element-plus'
const props = defineProps({
device: {
type: Object,
default: () => null,
},
loading: {
type: Boolean,
default: false,
},
})
const invokeTerminal = inject('invokeTerminal')
async function handleClick(device) {
let files = null
try {
files = await window.electron.ipcRenderer.invoke('show-open-dialog', {
properties: ['openFile'],
filters: [
{
name: window.t('device.control.shell.select'),
extensions: ['sh'],
},
],
})
}
catch (error) {
if (error.message) {
const message = error.message?.match(/Error: (.*)/)?.[1]
ElMessage.warning(message || error.message)
}
return false
}
const closeMessage = ElMessage.loading(
window.t('device.control.shell.pushing'),
).close
const filePath = files[0]
let pushFilePath = ''
try {
pushFilePath = await window.adbkit.push(device.id, filePath)
}
catch (error) {
closeMessage()
ElMessage.warning(error.message)
return false
}
if (!pushFilePath) {
closeMessage()
ElMessage.warning('Push script failed')
return false
}
await window.adbkit.deviceShell(device.id, `sh ${pushFilePath}`)
closeMessage()
await ElMessage.success(window.t('device.control.shell.success'))
const command = `adb -s ${device.id} shell sh ${pushFilePath}`
invokeTerminal(command)
}
</script>
<style></style>
8 changes: 8 additions & 0 deletions src/components/Device/components/ControlBar/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import MirrorGroup from './MirrorGroup/index.vue'
import Rotation from './Rotation/index.vue'
import Volume from './Volume/index.vue'
import FileManage from './FileManage/index.vue'
import Shell from './Shell/index.vue'
export default {
components: {
Expand All @@ -82,6 +83,7 @@ export default {
Rotation,
Volume,
FileManage,
Shell,
},
props: {
device: {
Expand Down Expand Up @@ -149,6 +151,12 @@ export default {
svgIcon: 'file-send',
component: 'FileManage',
},
{
label: 'device.control.shell.name',
svgIcon: 'command',
component: 'Shell',
tips: 'device.control.shell.tips',
},
{
label: 'device.control.gnirehtet',
elIcon: 'Link',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
width="80%"
:close-on-click-modal="false"
:close-on-press-escape="true"
destroy-on-close
class="overflow-hidden !rounded-md el-dialog-headless dark:border dark:border-gray-700"
@open="onOpen"
@closed="onClosed"
>
<el-icon
class="cursor-pointer absolute top-3 right-3 w-8 h-8 flex items-center justify-center hover:bg-gray-200 dark:text-gray-200 dark:hover:bg-gray-700 !active:bg-red-600 !active:text-gray-200 rounded-md"
Expand All @@ -15,8 +16,7 @@
</el-icon>

<VueCommand
v-if="renderShell"
:ref="(value) => (vShell = value)"
ref="vShell"
v-model:history="history"
:dispatched-queries="dispatchedQueries"
:commands="commands"
Expand All @@ -38,7 +38,6 @@
</template>

<script>
import { ref, shallowRef } from 'vue'
import VueCommand, {
createQuery,
createStdout,
Expand All @@ -48,6 +47,7 @@ import 'vue-command/dist/vue-command.css'
import { useAdb } from './composables/adb-async.js'
import { useScrcpy } from './composables/scrcpy.js'
import { useGnirehtet } from './composables/gnirehtet.js'
import { sleep } from '$/utils/index.js'
export default {
components: {
Expand All @@ -57,7 +57,6 @@ export default {
const vShell = ref(null)
const history = shallowRef([createQuery()])
const loading = ref(false)
const renderShell = ref(false)
const dispatchedQueries = ref(new Set([]))
const { adb } = useAdb({ vShell, history, loading })
Expand All @@ -84,18 +83,12 @@ export default {
...Object.keys(commands.value),
])
const onOpen = () => {
renderShell.value = true
}
return {
vShell,
loading,
history,
commands,
dispatchedQueries,
onOpen,
renderShell,
}
},
data() {
Expand Down Expand Up @@ -128,6 +121,26 @@ export default {
this.visible = false
},
async invoke(command) {
this.visible = true
if (!this.vShell) {
await this.$nextTick()
await sleep(1000)
}
await this.vShell.setQuery(command)
const vueCommandHistoryEntryComponentRefs
= this.vShell.$refs.vueCommandHistoryEntryComponentRefs
const queryLength = vueCommandHistoryEntryComponentRefs.length
vueCommandHistoryEntryComponentRefs[queryLength - 1].focus()
this.$message.info(this.$t('device.control.shell.enter'))
},
onDispatchedQueriesUpdate(value) {
this.$appStore.set('terminal.dispatchedQueries', Array.from(value))
Expand All @@ -137,6 +150,10 @@ export default {
onCtrlC() {
window.gnirehtet.shell('stop')
},
onClosed() {
this.vShell.dispatch('clear')
},
},
}
</script>
Expand Down
3 changes: 3 additions & 0 deletions src/components/Device/components/TerminalAction/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export default {
handleShow() {
this.$refs.terminalDialog.show()
},
invoke(...args) {
this.$refs.terminalDialog.invoke(...args)
},
},
}
</script>
Expand Down
7 changes: 6 additions & 1 deletion src/components/Device/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{{ $t('device.log.name') }}
</el-button>

<TerminalAction />
<TerminalAction ref="terminalActionRef" />
</div>

<BatchActions
Expand Down Expand Up @@ -148,6 +148,11 @@ export default {
WirelessAction,
BatchActions,
},
provide() {
return {
invokeTerminal: (...args) => this.$refs.terminalActionRef.invoke(...args),
}
},
data() {
return {
loading: false,
Expand Down
6 changes: 6 additions & 0 deletions src/locales/languages/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@
"device.control.file.push.success": "Successfully pushed {totalCount} files to the /sdcard/Download/ directory of {deviceName}, {successCount} succeeded, and {failCount} failed",
"device.control.file.push.success.single": "Files successfully pushed to the /sdcard/Download/ directory of {deviceName}",
"device.control.file.push.error": "Failed to push the file, please check the file and try again",
"device.control.shell.name": "Execute Script",
"device.control.shell.tips": "Perform custom script through the ADB command",
"device.control.shell.select": "Please select the script you want to execute",
"device.control.shell.pushing": "Push script",
"device.control.shell.success": "Push script successfully",
"device.control.shell.enter": "Please enter the Enter key to confirm the execution of the script",
"device.control.capture": "Screenshot",
"device.control.capture.progress": "Capturing screenshot for {deviceName}...",
"device.control.capture.success.message": "Open screenshot location?",
Expand Down
6 changes: 6 additions & 0 deletions src/locales/languages/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@
"device.control.file.push.success": "已成功将 {totalCount} 个文件推送到 {deviceName} 的 /sdcard/Download/ 目录,{successCount} 成功,{failCount} 失败。",
"device.control.file.push.success.single": "文件已成功推送到 {deviceName} 的 /sdcard/Download/ 目录",
"device.control.file.push.error": "推送文件失败,请检查文件后重试",
"device.control.shell.name": "执行脚本",
"device.control.shell.tips": "通过 ADB 命令执行自定义脚本",
"device.control.shell.select": "请选择要执行的脚本",
"device.control.shell.pushing": "推送脚本中",
"device.control.shell.success": "推送脚本成功",
"device.control.shell.enter": "请输入回车键确认执行该脚本",
"device.control.capture": "截取屏幕",
"device.control.capture.progress": "正在截取 {deviceName} 的屏幕快照...",
"device.control.capture.success.message": "是否前往截屏位置进行查看?",
Expand Down
6 changes: 6 additions & 0 deletions src/locales/languages/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@
"device.control.file.push.success": "已成功將 {totalCount} 個檔案推送到 {deviceName} 的 /sdcard/Download/ 目錄,{successCount} 成功,{failCount} 失敗。",
"device.control.file.push.success.single": "檔案已成功推送到 {deviceName} 的 /sdcard/Download/ 目錄",
"device.control.file.push.error": "推送檔案失敗,請檢查檔案後重試",
"device.control.shell.name": "執行腳本",
"device.control.shell.tips": "透過 ADB 命令執行自訂腳本",
"device.control.shell.select": "請選擇要執行的腳本",
"device.control.shell.pushing": "推送腳本中",
"device.control.shell.success": "推送腳本成功",
"device.control.shell.enter": "請輸入回車鍵確認執行該腳本",
"device.control.capture": "擷取螢幕",
"device.control.capture.progress": "正在擷取 {deviceName} 的螢幕快照...",
"device.control.capture.success.message": "是否前往截圖位置進行檢視?",
Expand Down

0 comments on commit 2013413

Please sign in to comment.