Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CP-3170] Minor refactor of device-flash #2128

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { useDeviceLockedEffect } from "Core/core/hooks/use-device-locked-effect"
import { useDeviceDetachedEffect } from "Core/core/hooks/use-device-detached-effect"
import { useDeviceConnectFailedEffect } from "Core/core/hooks/use-device-connect-failed-effect"
import { useDiscoveryRedirectEffect } from "Core/core/hooks/use-discovery-redirect-effect"
import { useAbortFlashingOnDeviceDetached } from "Core/core/hooks/use-abort-flashing-on-device-detached"
import { useMscDeviceDetachedEffect } from "Core/core/hooks/use-msc-device-detached-effect"
import { useRouterListener } from "Core/core/hooks"
import {
OutboxWrapper,
Expand Down Expand Up @@ -48,7 +48,7 @@ const BaseApp: FunctionComponent = () => {
useHelp()

// MSC
useAbortFlashingOnDeviceDetached()
useMscDeviceDetachedEffect()

return (
<>
Expand Down
35 changes: 0 additions & 35 deletions libs/core/core/hooks/use-abort-flashing-on-device-detached.ts

This file was deleted.

55 changes: 55 additions & 0 deletions libs/core/core/hooks/use-msc-device-detached-effect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright (c) Mudita sp. z o.o. All rights reserved.
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

import { useCallback, useEffect } from "react"
import { useDispatch, useSelector } from "react-redux"
import { answerMain, useDebouncedEventsHandler } from "shared/utils"
import {
DeviceBaseProperties,
DeviceProtocolMainEvent,
DeviceType,
} from "device-protocol/models"
import {
abortMscFlashing,
FlashingProcessState,
selectIsFlashingInActivePhases,
} from "msc-flash-harmony"
import { Dispatch } from "Core/__deprecated__/renderer/store"

export const useMscDeviceDetachedEffect = () => {
mkurczewski marked this conversation as resolved.
Show resolved Hide resolved
const handleDevicesDetached = useHandleDevicesDetached()

const batchDeviceDetachedEvents =
useDebouncedEventsHandler<DeviceBaseProperties>(handleDevicesDetached)

useEffect(() => {
return answerMain(
DeviceProtocolMainEvent.DeviceDetached,
batchDeviceDetachedEvents
)
}, [batchDeviceDetachedEvents])
}

const useHandleDevicesDetached = () => {
const dispatch = useDispatch<Dispatch>()
const flashingInActivePhases = useSelector(selectIsFlashingInActivePhases)
return useCallback(
(deviceDetachedEvents: DeviceBaseProperties[]) => {
const mscEvents = deviceDetachedEvents.filter(
({ deviceType }) => deviceType === DeviceType.MuditaHarmonyMsc
)

if (mscEvents.length === 0) {
return
}

const reason = flashingInActivePhases
? FlashingProcessState.Failed
: undefined
dispatch(abortMscFlashing({ reason }))
},
[dispatch, flashingInActivePhases]
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

export * from "./select-flashing-state.selector"
export * from "./select-flashing-state"
export * from "./select-flashing-abort-controller"
export * from "./select-flashing-process-state"
export * from "./select-is-flashing-in-active-phases"
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { createSelector } from "@reduxjs/toolkit"
import { flashingState } from "./select-flashing-state.selector"
import { flashingState } from "./select-flashing-state"

export const selectFlashingAbortController = createSelector(
flashingState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { createSelector } from "@reduxjs/toolkit"
import { flashingState } from "./select-flashing-state.selector"
import { flashingState } from "./select-flashing-state"

export const selectFlashingProcessState = createSelector(
flashingState,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

import path from "path"
import { execPromise, execCommandWithSudo } from "shared/utils"
import {
execPromise,
execCommandWithSudo,
splitPathToDirNameAndBaseName,
} from "shared/utils"
import IDeviceFlash from "../device-flash.interface"
import LinuxPartitionParser from "./linux-partition-parser"

Expand Down Expand Up @@ -36,7 +39,7 @@ class LinuxDeviceFlashService implements IDeviceFlash {
const ejectDeviceCommand = await this.getEjectDeviceCommand(device)

const command = `${unmountDeviceCommand} && ${flashImageToDeviceCommand} && ${ejectDeviceCommand}`
await execCommandWithSudo(command, { name: "Mudita Auto Flash" })
await execCommandWithSudo(command)

console.log("Flash process completed successfully")
}
Expand All @@ -55,9 +58,8 @@ class LinuxDeviceFlashService implements IDeviceFlash {
imagePath: string,
scriptPath: string
): Promise<string> {
const [path, scriptBasename] =
this.splitPathToDirnameAndBasename(scriptPath)
const [, imageBasename] = this.splitPathToDirnameAndBasename(imagePath)
const [path, scriptBasename] = splitPathToDirNameAndBaseName(scriptPath)
const [, imageBasename] = splitPathToDirNameAndBaseName(imagePath)
return `chmod +x ${scriptPath} && cd ${path} && ./${scriptBasename} ${imageBasename} /dev/${device}`
}

Expand All @@ -77,12 +79,6 @@ class LinuxDeviceFlashService implements IDeviceFlash {
)
return LinuxPartitionParser.parsePartitions(partitions ?? "")
}

private splitPathToDirnameAndBasename(currentPath: string) {
const dirname = path.dirname(currentPath)
const basename = path.basename(currentPath)
return [dirname, basename]
}
}

export default LinuxDeviceFlashService
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ const getFlashingImageDetails = async (
await dispatch(
getMscFlashingFilesDetails({
signal,
platform,
product: Product.MscHarmony,
environment: RELEASE_SPACE,
platform: platform,
})
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ export const RestartingDeviceModal: FunctionComponent<
const dispatch = useDispatch<Dispatch>()

useEffect(() => {
const timer = setTimeout(() => {
dispatch(setFlashingProcessState(FlashingProcessState.Failed))
}, 2 * 60 * 1000)
let timeoutId: NodeJS.Timeout

return () => clearTimeout(timer)
}, [dispatch])
if (open) {
timeoutId = setTimeout(() => {
dispatch(setFlashingProcessState(FlashingProcessState.Failed))
}, 2 * 60 * 1000)
}

return () => clearTimeout(timeoutId)
}, [dispatch, open])

return (
<LoaderModal
Expand Down
1 change: 1 addition & 0 deletions libs/shared/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from "./lib/prevent-default-shortcuts"
export * from "./lib/register-shortcuts"
export * from "./lib/routes-history.context"
export * from "./lib/settings-service.interface"
export * from "./lib/split-path-to-dir-name-and-base-name"
export * from "./lib/use-debounced-events-handler"
export * from "./lib/exec-command"
export * from "./lib/exec-command-with-sudo"
Expand Down
2 changes: 1 addition & 1 deletion libs/shared/utils/src/lib/exec-command-with-sudo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import sudoPrompt from "@vscode/sudo-prompt"

export const execCommandWithSudo = (
command: string,
options: { name?: string; icns?: string; env?: { [key: string]: string } }
options?: { name?: string; icns?: string; env?: { [key: string]: string } }
): Promise<string | void> => {
return new Promise((resolve, reject) => {
sudoPrompt.exec(command, options, (error, stdout, stderr) => {
Expand Down
14 changes: 14 additions & 0 deletions libs/shared/utils/src/lib/split-path-to-dir-name-and-base-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) Mudita sp. z o.o. All rights reserved.
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

import path from "path"

export const splitPathToDirNameAndBaseName = (
currentPath: string
): [string, string] => {
const dirName = path.dirname(currentPath)
const baseName = path.basename(currentPath)
return [dirName, baseName]
}
Loading