T145
T145

Reputation: 1523

Porting from bash to Gradle

I have a Bash script for a project that looks something like this:

#!/usr/bin/env bash

curl -LOk (url)/project.zip
mkdir project
unzip project.zip -d project
cp -rf project/assets assets
rm -rf project && rm -rf project.zip
gradle test

So all it does is download the archive I want from a different project, copy its assets to the current project's workspace, delete said archive and the extracted stuff I don't want, and then run a test suite to be sure everything's working. Since I use Gradle to run the test suite, I'd like to migrate everything to be 100% Gradle so that the build process can be 1) multi-platform & 2) easily managed since my build script is constantly maintained. How should I go about doing this? After some research I ran into gradle-download-task; would that help handle the download segment, or is there a better alternative?

Upvotes: 1

Views: 793

Answers (1)

Opal
Opal

Reputation: 84854

There are 3 ways of doing it:

First (not a cross platform approach)

Run your script directly from gradle (without running tests in it - it can be defined via task dependency in build.gradle itself):

task download1(type: Exec) {
   executable 'sh'
   args 'download.sh'
}

Second

Use groovy + gradle magic:

task download2 << {
   def file = project.file('sample2.zip')   
   def out = new BufferedOutputStream(new FileOutputStream(file))
   out << new URL(zipUrl).openStream()
   out.close()
   project.copy {
      from zipTree(file)
      into 'project2'
   }
   project.copy {
      from project.file('project2/assets')
      into 'assets2'
   }
   project.file('project2').deleteDir()
   project.file('sample2.zip').delete()
}

Third

Use the plugin you found:

task download3(type: Download) {
    def destination = new File(rootDir, 'sample3.zip')
    src zipUrl
    dest destination
    doLast {
      project.copy {
         from zipTree(destination)
         into 'project3'
      }
      project.copy {
         from project.file('project3/assets')
         into 'assets3'
      }
      project.file('project3').deleteDir()
      project.file('sample3.zip').delete()
    }
}

Complete example can be found here.

Upvotes: 2

Related Questions