fun extractDmgApp()

in ide-launcher/src/main/kotlin/com/intellij/remoterobot/launcher/utils.kt [52:109]


fun extractDmgApp(dmg: Path, to: Path): Path {
    check(Files.exists(dmg)) { "dmg is not exists" }
    check(Files.exists(to)) { "target directory is not exists" }
    check(ProcessBuilder().command("which", "hdiutil").start().waitFor() == 0)
    check(
        ProcessBuilder()
            .command("hdiutil", "verify", dmg.toAbsolutePath().toString())
            .start()
            .waitFor() == 0
    ) { "${dmg.toAbsolutePath()} is not valid disk image" }

    val mountPoint: Path = Paths.get(to.toAbsolutePath().toString(), "mnt")
    if (!Files.exists(mountPoint)) Files.createDirectory(mountPoint)
    check(
        ProcessBuilder()
            .command(
                "hdiutil",
                "attach", dmg.toAbsolutePath().toString(),
                "-mountpoint", mountPoint.toString(),
                "-noautoopen",
                "-nobrowse"
            )
            .inheritIO()
            .start()
            .waitFor() == 0
    ) { "failed to mount $dmg to $mountPoint" }

    val mounted: Path =
        Files.list(mountPoint).filter { it.fileName.toString().endsWith(".app") }.findFirst().orElseThrow {
            IllegalStateException("ide app is not found")
        }
    val app: Path = Paths.get(to.toAbsolutePath().toString(), mounted.fileName.toString())

    check(
        ProcessBuilder().command(
            "cp",
            "-R",
            mounted.toAbsolutePath().toString(),
            app.toAbsolutePath().toString()
        ).inheritIO().start().waitFor() == 0
    ) { "failed to copy ide app from mounted .dmg" }

    check(
        ProcessBuilder()
            .command(
                "hdiutil",
                "detach", mountPoint.toString(),
                "-force"
            )
            .inheritIO()
            .start()
            .waitFor() == 0
    ) { "failed to unmount $mountPoint" }

    Files.delete(mountPoint)

    return app
}