-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added resource bundle generation, fixed #2
- Loading branch information
1 parent
97d608f
commit 56e87ee
Showing
11 changed files
with
309 additions
and
111 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package scommons.sbtplugin.util | ||
|
||
import play.api.libs.json.Json | ||
import sbt._ | ||
|
||
object BundlesUtils { | ||
|
||
def genFromClasspath(logger: String => Unit, | ||
targetDir: File, | ||
cp: Seq[Attributed[File]], | ||
fileFilter: FileFilter): Unit = { | ||
|
||
for (entry <- cp) { | ||
val cpEntry = entry.data | ||
if (cpEntry.exists && cpEntry.isDirectory) { | ||
var bundles = Map.empty[File, (Seq[File], Long)] | ||
for ((file, relPath) <- Path.selectSubpaths(cpEntry, fileFilter)) { | ||
val targetFile = new File(targetDir, relPath) | ||
val bundleDir = targetFile.getParentFile | ||
val (bundleFiles, lastModified) = bundles.getOrElse(bundleDir, (Nil, 0L)) | ||
bundles = bundles.updated(bundleDir, (bundleFiles :+ file, | ||
if (lastModified < file.lastModified()) file.lastModified() | ||
else lastModified | ||
)) | ||
} | ||
|
||
var generated = Seq.empty[File] | ||
for ((bundleDir, (bundleFiles, lastModified)) <- bundles) { | ||
val bundle = new File(bundleDir, "bundle.json") | ||
if (!bundle.exists() || bundle.lastModified() < lastModified) { | ||
generated = generated :+ bundle | ||
|
||
val content = bundleFiles.map { file => | ||
Json.obj( | ||
"file" -> file.getName, | ||
"content" -> IO.read(file) | ||
) | ||
} | ||
|
||
IO.write(bundle, Json.prettyPrint(Json.toJson(content))) | ||
bundle.setLastModified(lastModified) | ||
} | ||
} | ||
|
||
val total = bundles.size | ||
if (generated.nonEmpty) { | ||
logger(s"Generated ${generated.size} bundle files (out of $total)" + | ||
s"\n\t${generated.mkString("\n\t")}") | ||
} | ||
else if (total > 0) { | ||
logger(s"Nothing to generate, all $total bundle files are up to date" + | ||
s"\n\t${bundles.keys.map(new File(_, "bundle.json")).mkString("\n\t")}") | ||
} | ||
} | ||
} | ||
} | ||
} |
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
2 changes: 2 additions & 0 deletions
2
...scommons-plugin/simple/client/src/main/resources/scommons/sbtplugin/test/nested/test3.sql
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
CREATE TABLE test3; |
2 changes: 2 additions & 0 deletions
2
...est/sbt-scommons-plugin/simple/client/src/main/resources/scommons/sbtplugin/test/test.sql
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
CREATE TABLE test; |
2 changes: 2 additions & 0 deletions
2
...st/sbt-scommons-plugin/simple/client/src/main/resources/scommons/sbtplugin/test/test2.sql
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
CREATE TABLE test2; |
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
107 changes: 107 additions & 0 deletions
107
src/test/scala/scommons/sbtplugin/util/BaseUtilsSpec.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package scommons.sbtplugin.util | ||
|
||
import java.io._ | ||
import java.util.zip.{ZipEntry, ZipOutputStream} | ||
|
||
import org.scalamock.scalatest.MockFactory | ||
import org.scalatest._ | ||
import sbt._ | ||
|
||
abstract class BaseUtilsSpec extends FlatSpec | ||
with Matchers | ||
with BeforeAndAfterEach | ||
with MockFactory { | ||
|
||
protected var tmpSourceDir: Option[File] = None | ||
protected var tmpTargetDir: Option[File] = None | ||
|
||
override protected def beforeEach(): Unit = { | ||
tmpSourceDir = Some(createTmpDir(tmpSourceDir, "scommons.sbtplugin.sourceDir.")) | ||
tmpTargetDir = Some(createTmpDir(tmpTargetDir, "scommons.sbtplugin.targetDir.")) | ||
} | ||
|
||
override protected def afterEach(): Unit = { | ||
tmpSourceDir = { | ||
deleteDirRecursively(tmpSourceDir.get) | ||
tmpSourceDir.get.exists() shouldBe false | ||
None | ||
} | ||
tmpTargetDir = { | ||
deleteDirRecursively(tmpTargetDir.get) | ||
tmpTargetDir.get.exists() shouldBe false | ||
None | ||
} | ||
} | ||
|
||
def assertFile(dir: File, relPathName: String, contents: String, exists: Boolean = true): Assertion = { | ||
val file = new File(s"${dir.getPath}${File.separator}$relPathName") | ||
file.exists() shouldBe exists | ||
|
||
if (exists) { | ||
IO.read(file) shouldBe contents | ||
} | ||
|
||
succeed | ||
} | ||
|
||
def writeFile(dir: File, relPathName: String, contents: String): (String, String) = { | ||
val file = new File(s"${dir.getPath}/$relPathName") | ||
IO.write(file, contents) | ||
(relPathName, contents) | ||
} | ||
|
||
def writeZipFile(file: File, relPathNamesWithContents: List[(String, String)]): List[(String, String)] = { | ||
val stream = new ZipOutputStream(new FileOutputStream(file)) | ||
try { | ||
for ((relPathName, contents) <- relPathNamesWithContents) { | ||
stream.putNextEntry(new ZipEntry(relPathName)) | ||
stream.write(contents.getBytes("UTF-8")) | ||
stream.closeEntry() | ||
} | ||
|
||
stream.finish() | ||
relPathNamesWithContents | ||
} finally { | ||
stream.close() | ||
} | ||
} | ||
|
||
private def createTmpDir(currTmpDir: Option[File], prefix: String): File = { | ||
if (currTmpDir.isEmpty) { | ||
val tmpFile = File.createTempFile(prefix, "") | ||
deleteFile(tmpFile) | ||
if (!tmpFile.mkdirs()) { | ||
throw new IllegalStateException(s"Cannot create directory(s): $tmpFile") | ||
} | ||
|
||
tmpFile | ||
} | ||
else { | ||
throw new IllegalStateException("Temp directory already exists, probably it was not cleaned-up properly") | ||
} | ||
} | ||
|
||
private def deleteFile(file: File): Unit = { | ||
if (!file.delete()) { | ||
throw new IllegalStateException(s"Cannot delete file/directory: $file") | ||
} | ||
} | ||
|
||
private def deleteDirRecursively(dir: File): Unit = { | ||
val filter = new FilenameFilter { | ||
override def accept(dir: File, name: String): Boolean = name != "." && name != ".." | ||
} | ||
|
||
val (dirs, files) = dir.listFiles(filter).partition(_.isDirectory) | ||
|
||
for (dir <- dirs) { | ||
deleteDirRecursively(dir) | ||
} | ||
|
||
for (file <- files) { | ||
deleteFile(file) | ||
} | ||
|
||
deleteFile(dir) | ||
} | ||
} |
Oops, something went wrong.