override fun obtain()

in src/main/kotlin/org/jetbrains/intellij/platform/gradle/providers/DmgExtractorValueSource.kt [44:112]


    override fun obtain(): Path {
        val path = parameters.path.asPath
        val targetDirectory = parameters.target.asPath.createDirectories()

        if (targetDirectory.listFiles().isNotEmpty()) {
            log.info("Directory '$targetDirectory' already exists.")
            return targetDirectory
        }

        log.info("Extracting DMG archive '$path' to temporary directory.")

        val hdiutilInfo = ByteArrayOutputStream().use { os ->
            execOperations.exec {
                commandLine("hdiutil", "info")
                standardOutput = os
            }
            os.toString()
        }

        val resources = hdiutilInfo
            .split("================================================")
            .drop(1).associate {
                with(it.trim().lines()) {
                    first().split(" : ").last() to last().split("\t").last()
                }
            }

        resources[path.pathString]?.let { volume ->
            execOperations.exec {
                commandLine("hdiutil", "detach", "-force", "-quiet", volume)
            }
        }

        execOperations.exec {
            commandLine(
                "hdiutil",
                "attach",
                "-readonly",
                "-noautoopen",
                "-noautofsck",
                "-noverify",
                "-nobrowse",
                "-mountpoint",
                "-quiet",
                tempDirectory,
                path.safePathString,
            )
        }

        execOperations.exec {
            commandLine(
                "rsync",
                "-av",
                "--quiet",
                "--exclude=Applications",
                "--exclude=/.*",
                "$tempDirectory/",
                targetDirectory,
            )
            isIgnoreExitValue = true
        }

        log.info("Unmounting DMG volume '$tempDirectory'.")
        execOperations.exec {
            commandLine("hdiutil", "detach", "-force", "-quiet", tempDirectory)
        }

        return targetDirectory
    }