// we don't manage examples in a project tree, because each example is designed
// as completely self-sufficient project, allowing isolated build.

task('build')
task('clean')

def subdirs = projectDir.listFiles({ it.isDirectory() } as FileFilter) as List

subdirs = subdirs.findResults { subdir ->
  def f = new File(subdir, 'build.gradle')
  if(f.isFile())
    subdir
  else
    subdir.listFiles({ it.isDirectory() && new File(it, 'build.gradle').isFile() } as FileFilter)
}

subdirs = subdirs.flatten()

subdirs.each { subdir ->
  String taskSuffix = subdir.absolutePath.substring(projectDir.absolutePath.length())
  if(taskSuffix.startsWith(File.separator))
    taskSuffix = taskSuffix.substring(1)
  taskSuffix = taskSuffix.replaceAll($/-|/|\\/$, '_')

  task("build_$taskSuffix", type: GradleBuild) { task ->
    dir = subdir
    tasks = [ 'build' ]
    project.tasks.build.dependsOn task
  }

  task("clean_$taskSuffix", type: GradleBuild) { task ->
    dir = subdir
    tasks = [ 'clean' ]
    project.tasks.clean.dependsOn task
  }
}

