Last active
December 22, 2019 23:31
-
-
Save samuelorji/58681595768e5ad37597479462ad4d6d to your computer and use it in GitHub Desktop.
build with config file
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
lazy val fetchConfFile = taskKey[File]("fetch Config file") | |
fetchConfFile := { | |
//simulate fetching a remote file | |
sLog.value.info("fetching file ...... ") | |
Thread.sleep(1000) | |
new java.io.File("/tmp/application.conf").createNewFile() | |
val configFile : File = new java.io.File("/tmp/application.conf") | |
sLog.value.info("done fetching file .... ") | |
configFile | |
} | |
lazy val zip = taskKey[Unit]("zip files") | |
zip := { | |
val logger = sLog.value | |
val fromZip: File = srcFile.value | |
val toZip: File = baseDirectory.value / "build_and_config.zip" | |
// we expect this to be run before the `fetchConfFile task | |
// but this is actually run after | |
logger.info(s"zipping files from ${fromZip.absolutePath}") | |
//1.) calling .value actually executes this task | |
//2.) sbt figures that the `zip task depends on the `fetchConfFile task | |
// so sbt actually runs the `fetchConfFile task before running the actual `zip task | |
val configFile: File = fetchConfFile.value | |
val filesToZip = List(configFile, fromZip) | |
val fos = new FileOutputStream(toZip) | |
// zip function | |
filesToZip.foldLeft(new ZipOutputStream(fos)){ | |
case (zs, file) => | |
val fis = new FileInputStream(file) | |
val zipEntry = new ZipEntry(file.getName) | |
zs.putNextEntry(zipEntry) | |
@scala.annotation.tailrec | |
def readAgain(c: Int): Int = { | |
if (c < 0) { | |
-1 | |
} else { | |
zs.write(c) | |
readAgain(fis.read()) | |
} | |
} | |
readAgain(fis.read()) | |
fis.close() | |
zs | |
}.close() | |
logger.info(s"zipped files to ${toZip.absolutePath}") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment