-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #549 from alvasw/gradle_electrum_macOS_unpack_app_…
…at_build_time macOS: Unpack Electrum.app at build time
- Loading branch information
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...trum-binaries/src/main/kotlin/bisq/gradle/electrum/tasks/ExtractElectrumAppFromDmgFile.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package bisq.gradle.electrum.tasks | ||
|
||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.file.DirectoryProperty | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.InputDirectory | ||
import org.gradle.api.tasks.OutputDirectory | ||
import org.gradle.api.tasks.TaskAction | ||
import java.io.File | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import java.nio.file.Paths | ||
import java.util.concurrent.TimeUnit | ||
|
||
|
||
abstract class ExtractElectrumAppFromDmgFile : DefaultTask() { | ||
|
||
companion object { | ||
private const val MOUNT_DIR = "/Volumes/Electrum" | ||
private const val ELECTRUM_APP = "Electrum.app" | ||
private const val MOUNTED_ELECTRUM_APP_PATH = "$MOUNT_DIR/$ELECTRUM_APP" | ||
} | ||
|
||
|
||
@get:Input | ||
abstract val electrumVersion: Property<String> | ||
|
||
@get:InputDirectory | ||
abstract val inputDirectory: DirectoryProperty | ||
|
||
@get:OutputDirectory | ||
abstract val outputDirectory: DirectoryProperty | ||
|
||
private val electrumAppDestinationFile: File | ||
get() = outputDirectory.get().asFile.resolve(ELECTRUM_APP) | ||
|
||
@TaskAction | ||
fun extract() { | ||
if (electrumAppDestinationFile.exists()) { | ||
return | ||
} | ||
|
||
attachDmgFile() | ||
copyElectrumAppToOutputDirectory() | ||
detachDmgFile() | ||
} | ||
|
||
private fun attachDmgFile() { | ||
val dmgFile = inputDirectory.get().asFile.resolve("electrum-${electrumVersion.get()}.dmg") | ||
val attachDmgFile: Process = ProcessBuilder("hdiutil", "attach", dmgFile.absolutePath).start() | ||
val isSuccess: Boolean = attachDmgFile.waitFor(25, TimeUnit.SECONDS) | ||
if (!isSuccess) { | ||
throw IllegalStateException("Could not attach DMG file.") | ||
} | ||
} | ||
|
||
private fun copyElectrumAppToOutputDirectory() { | ||
val electrumAppFile = Paths.get(MOUNTED_ELECTRUM_APP_PATH) | ||
val destinationDir = electrumAppDestinationFile.absolutePath | ||
|
||
Files.walk(electrumAppFile) | ||
.forEach { source -> | ||
val destination: Path = Paths.get( | ||
destinationDir, source.toString().substring(MOUNTED_ELECTRUM_APP_PATH.length) | ||
) | ||
Files.copy(source, destination) | ||
} | ||
} | ||
|
||
private fun detachDmgFile() { | ||
val attachDmgFile: Process = ProcessBuilder("hdiutil", "detach", MOUNT_DIR).start() | ||
val isSuccess: Boolean = attachDmgFile.waitFor(25, TimeUnit.SECONDS) | ||
if (!isSuccess) { | ||
throw IllegalStateException("Could not detach DMG file.") | ||
} | ||
} | ||
} |