Skip to content

Instantly share code, notes, and snippets.

@mike-neck
Created August 26, 2015 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mike-neck/7abaa560d4728b127efb to your computer and use it in GitHub Desktop.
Save mike-neck/7abaa560d4728b127efb to your computer and use it in GitHub Desktop.
Sample build script for archiving unknown files : see https://discuss.gradle.org/t/zip-task-with-lazy-from-property/11355
plugins {
id 'base'
}
ext {
current = new Date().format('yyyy-MM-dd-HH-mm-ss')
}
task randomFiles {
group = 'Create Files'
description = 'Creates files the amount of which is determined by random.'
// returns random char from 'a' to 'z'
def randomChar = {
def range = 0..25
range.metaClass.define {
randomChar = {
int p = delegate.min() + new Random().nextInt(delegate.size())
((('a' as char) as int) + p) as char
}
}
range.randomChar()
}
// returns random string with its length from 3 to 8
def randomString = {
def range = 3..8
range.metaClass.define {
randomString = {
int s = delegate.min() + new Random().nextInt(delegate.size())
def chars = []
s.times {
chars << randomChar()
}
return chars.join()
}
}
range.randomString()
}
// returns random path name with its depth from 0 to 4
def randomTree = {
def range = 0..4
range.metaClass.define {
path = {
int dep = delegate.min() + new Random().nextInt(delegate.size())
def list = []
dep.times {
list << randomString()
}
return list.join('/')
}
}
range.path()
}
// returns random file name with its length from 5 to 20
def randomFileNames = {
def range = 5..20
range.metaClass.define {
randomNumber = {
delegate.min() + new Random().nextInt(delegate.size())
}
}
def list = []
range.randomNumber().times {
list << randomString()
}
return list
}
// define output file destination directory
def taskDestDir = file("${buildDir}/random/files/${current}/${randomString()}")
// task action
doLast {
if (!taskDestDir.exists()) {
taskDestDir.mkdirs()
}
// writing random files
randomFileNames().each {
def tree = randomTree()
def fileDir = file("${taskDestDir}/${tree}")
if (!fileDir.exists()) {
fileDir.mkdirs()
}
def filePath = file("${fileDir}/${it}.txt")
filePath.write(randomString(), 'UTF-8')
}
}
}
// collect files generated by randomFiles task
task collectFiles(dependsOn: ['randomFiles']) {
doLast {
copy {
// copy files excluding files which contains char 'a'
from fileTree("${buildDir}/random/files/${current}") {
exclude '**/*a*/*.txt'
exclude '**/*a*.txt'
}
into file("${buildDir}/collected")
}
}
}
// archiving files into zip file. Files are collected by collectFiles task
task myZip(type: Zip, dependsOn: ['collectFiles']) {
group = 'Zip Files'
description = 'Zip random files.'
from fileTree("${buildDir}/collected")
destinationDir = file("${buildDir}/myZip")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment