Configuring custom executables

For cases where the tool is a single file to be downloaded i.e. a GO executable, the executable declaration can be used instead of the tool declaration.

Consider for a moment that you want to download a PNPM executable. Depending on which platform you are building, you will need to download the appropriate executable, mark it as executable and then make it available.

Specify the custom executable

build.gradle
grabatool { (1)
    tool('pnpm') { (2)
      // ... configuration to follow
    }
}
1 All configuration happens within the grabatool block
2 An executable specification is done using executable keyword, followed by a descriptive shortname. This then followed by a configuration closure (or an Action in case of a Kotlin DSL).

The next step is to specify how to resolve the download URI. This is done via a Closure or a java.util.Function. In both cases a GrabaToolDescriptor will be passed. The description will contain methods for retrieving the appropriate operating system as well as a requested version.

Add a uri block inside the executable('go') block

build.gradle
uri { cfg -> (1)
    String platform
    if (cfg.os.windows) { (2)
        if (cfg.os.arch == OperatingSystem.Arch.X86_64) {
            platform = 'win-x64.exe'
        } else if (cfg.os.arch == OperatingSystem.Arch.ARM64) {
            platform = 'win-arm64.exe'
        } else {
            cfg.fail("${cfg.os.arch} is not a supported configuration for Windows") (3)
        }
    } else if (cfg.os.linux) {
        if (cfg.os.arch == OperatingSystem.Arch.X86_64) {
            platform = 'linuxstatic-x64'
        } else if (cfg.os.arch == OperatingSystem.Arch.ARM64) {
            platform = 'linuxstatic-arm64'
        } else {
            cfg.fail("${cfg.os.arch} is not a supported configuration for Linux")
        }
    } else if (cfg.os.macOsX) {
        if (cfg.os.arch == OperatingSystem.Arch.X86_64) {
            platform = 'macos-x64'
        } else if (cfg.os.arch == OperatingSystem.Arch.ARM64) {
            platform = 'macos-arm64'
        } else {
            cfg.fail("${cfg.os.arch} is not a supported configuration for Mac OSX")
        }
    } else {
        cfg.fail("${cfg.os} is not supported")
    }

  "https://github.com/pnpm/pnpm/releases/download/vVERSION/pnpm-${platform}".toURI() (4)
}
1 In this example a Closure (or an Action) which takes a GrabaToolDescriptor is used.
2 Use appropriate logic to determine the filename. For instance with Go this is dependent on both the operating system and the architecture.
3 Use the fail statement when specific platforms are not supported.
4 Create a download URI which will be returned. Replace VERSION with the version you need.

Grabatool will use the above URI to download the distribution if it is not already cached. Downloaded distributions are usually shared between Gradle projects.

Accessing cached content

build.gradle
Provider<File> location = grabatools.tools.named('pnpm').flatMap { it.location } (1)
1 Gets the executable.