fun extract()

in src/main/kotlin/org/jetbrains/intellij/platform/gradle/services/ExtractorService.kt [30:91]


    fun extract(path: Path, targetDirectory: Path) {
        log.info("Extracting archive '$path' to directory '$targetDirectory'.")

        val name = path.nameWithoutExtension.removeSuffix(".tar")
        val extension = path.name.removePrefix("$name.")


        when (ArtifactType.from(extension)) {
            ArtifactType.ZIP, ArtifactType.SIT ->
                fileSystemOperations.copy {
                    includeEmptyDirs = false
                    from(archiveOperations.zipTree(path))
                    into(targetDirectory)
                }

            ArtifactType.TAR_GZ ->
                fileSystemOperations.copy {
                    includeEmptyDirs = false
                    from(archiveOperations.tarTree(path))
                    into(targetDirectory)
                }

            ArtifactType.DMG ->
                providerFactory.of(DmgExtractorValueSource::class) {
                    parameters.path = path.toFile()
                    parameters.target = targetDirectory.toFile()
                }.get()

            else
                -> throw IllegalArgumentException("Unknown type archive type '$extension' for '$path'")
        }

        // Resolve the first directory that contains more than a single directory.
        // This approach helps eliminate `/Application Name.app/Contents/...` macOS directories or nested directory from the `tar.gz` archive.
        log.info("Resolving the content directory in '$targetDirectory'.")
        val platformPath = targetDirectory.resolvePlatformPath()

        // Create .toolbox-ignore marker file next to product-info.json
        runCatching {
            val productInfo = ProductInfoPathResolver(platformPath).resolve()
            productInfo.parent.resolve(Constants.TOOLBOX_IGNORE).createFile()
        }

        log.info("The content directory is '$platformPath'.")

        // Move content from the resolved nested directory.
        if (platformPath != targetDirectory) {
            log.info("Copying the content from '$platformPath' to '$targetDirectory'.")
            platformPath.listDirectoryEntries().forEach { file ->
                val destination = targetDirectory.resolve(platformPath.relativize(file))
                destination.parent.createDirectories()
                file.moveTo(destination)
            }

            // Remove an empty directory.
            generateSequence(platformPath) { it.parent }
                .takeWhile { it != targetDirectory }
                .forEach { it.deleteExisting() }
        }

        log.info("Extracting to '$targetDirectory' completed.")
    }