diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..49d3f22 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.vscode +.metals +.idea +.DS_Store diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..e990334 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# Changelog + +## 0.1.0 + +Initial release. diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..578b96e --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,9 @@ +# The MIT License (MIT) + +Copyright 2020 Iurii Malchenko + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..6fcfd19 --- /dev/null +++ b/README.md @@ -0,0 +1,208 @@ +![Maven Central](https://img.shields.io/maven-central/v/com.yurique/embedded-files-macro_sjs1_2.13.svg) + +# embedded-files + +An sbt plugin to create Scala objects containing the contents of the files as `String`s or `Array[Byte]`s. + +And an acompanying macro to more easily access those objects. + +## Installation + +### `plugins.sbt` + +```scala +addSbtPlugin("com.yurique" % "sbt-embedded-files" % "0.1.0") +``` + +### `build.sbt` + +```scala + libraryDependencies += "com.yurique" %%% "embedded-files-macro" % "0.1.0" +``` + +## Example usage + +Put a file into `src/main/resources/docs/test.txt`: + +``` +I'm a test text. +``` + +Add `embedFiles` to the `Compile / sourceGenerators`: + +```scala + project + // ... + .settings( + (Compile / sourceGenerators) += embedFiles + ) +``` + +In the code: + +```scala +import com.yurique.embedded.FileAsString + +val testTxtContents = FileAsString("/docs/test.txt") // "I'm a test text." +``` + +## Configuration + +The sbt plugin has the following configuration keys: + +```scala + project + // ... + .settings( + // default is __embedded_files, which is assumed by the macro + // the generated objects and classes are put into this package (and sub-packages) + embedRootPackage := "custom_root_package", + + // a list of directories to look for files to embed in + // default is (Compile / unmanagedResourceDirectories) + embedDirectories ++= (Compile / unmanagedSourceDirectories).value, + + // a list of globs for text files + // default is Seq("**/*.txt") + embedTextGlobs := Seq("**/*.txt", "**/*.md"), + + // a list of globs for binary files + // default is Seq.empty + embedBinGlobs := Seq("**/*.bin"), + + // whether or not to generate a EmbeddedFilesIndex object containing references to all embedded files + // default is false + embedGenerateIndex := true, + + // the intended usage is to use the output of the embedFiles task as generated sources + (Compile / sourceGenerators) += embedFiles + ) +``` + +## Generated files + +### Interfaces + +The `embedFiles` always generates these two abstract classes: + +```scala +package ${embedRootPackage.value} + +abstract class EmbeddedTextFile { + def path: String + def content: String +} + +``` + +and + +```scala +package ${embedRootPackage.value} + +abstract class EmbeddedBinFile { + def path: String + def content: Array[Byte] +} +``` + +### Text files + +For each file in the `embedDirectories` that matches any of the `embedTextGlobs` a file like the following is generated: + +```scala +package ${embedRootPackage.value}.${subPackage} + +object ${className} extends __embedded_files.EmbeddedTextFile { + + val path: String = """path/to/the/file/filename.txt""" + + val content: String = """the content of the file""" + +} +``` + +### Binary files + +For each file in the `embedDirectories` that matches any of the `embedBinGlobs` a file like the following is generated: + +```scala +package ${embedRootPackage.value}.${subPackage} + +object ${className} extends __embedded_files.EmbeddedBinFile { + + val path: String = """path/to/the/file/filename.txt""" + + val content: Array[Byte] = Array( + // example bytes + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f + ) + +} +``` + +### Package- and class-names + +For each file, its path is taken relative to the one of the `embedDirectories` and converted into the package name and the class name. + +For example, if the file was `/home/user/.../.../project/src/main/resources/some-dir/1 some sub dir/some things & other things.txt`: + +- a relative path is `some-dir/1 some sub dir/some things & other things.txt` (relative to `Compile / unmanagedSourceDirectories` in this example) +- the dirname is `some-dir/1 some sub dir`, it is split by `/`, every part is converted to a valid Scala ID (by replacing non alpha-numerics by `_` and prepending `_` is the path starts with a digit) +- the resulting package name is `some_dir._1_some_sub_dir` +- the class name is derived from the file name: `some_things_other_things` + +## Index file + +if `embedGenerateIndex` is set to `true`, the index file is generated like the following: + +```scala +package ${embedRootPackage.value} + +object EmbeddedFilesIndex { + val textFiles: Seq[(String, EmbeddedTextFile)] = Seq( + "test/test-resource.txt" -> __embedded_files.test.test_resource_txt, + "com/company/test_file_1.txt" -> __embedded_files.com.company.test_file_1_txt + ) + val binFiles: Seq[(String, EmbeddedBinFile)] = Seq( + "test/test-bin-resource.bin" -> __embedded_files.test.test_bin_resource_bin, + "com/company/test_bin_file_1.bin" -> __embedded_files.com.company.test_bin_file_1_bin + ) +} +``` + +## macros + +Assuming the `embedFiles` is used as a source generator with the default root package, you can use the macros provided by the `embedded-files-macro` library to get the string/byte-array content of the files like this: + +```scala +import com.yurique.embedded._ +val s: String = FileAsString("/docs/test.txt") +val b: Array[Byte] = FileAsByteArray("/bins/test.bin") +``` + +If the path passed to the macros starts with a slash, it is used as is. + +If it doesn't start with a slash, the macro does the following: + +`/home/user/.../project/src/main/scala/com/company/MyClass.scala` +```scala +package com.company.MyClass + +object MyClass { + val s: String = FileAsString("dir/data.txt") +} +``` + +Here, the file name doesn't start with a `/` – `dir/data.txt`. + +The calling site is in the `/home/user/.../project/src/main/scala/com/company/` directory. + +Those requested file is appended to this directory, resulting in `/home/user/.../project/src/main/scala/com/company/dir/data.txt`. + +This file is taken relative to the first `scala` directory in the path, resulting in `/com/company/dir/data.txt`. + +## Missing embedded files + +The macros are not currently doing any checks for whether the embedded files exist. If they don't, the scalac will just fail to compile in a normal way. + diff --git a/embedded-files-macro/.gitignore b/embedded-files-macro/.gitignore new file mode 100644 index 0000000..a3c91e9 --- /dev/null +++ b/embedded-files-macro/.gitignore @@ -0,0 +1,4 @@ +target +.idea +.bsp + diff --git a/embedded-files-macro/.scalafmt.conf b/embedded-files-macro/.scalafmt.conf new file mode 100644 index 0000000..b583d54 --- /dev/null +++ b/embedded-files-macro/.scalafmt.conf @@ -0,0 +1,18 @@ +version=2.7.5 +style = defaultWithAlign +align.openParenCallSite = true +align.openParenDefnSite = true +align.arrowEnumeratorGenerator = true +maxColumn = 180 +continuationIndent.defnSite = 2 +assumeStandardLibraryStripMargin = true +danglingParentheses.defnSite = true +danglingParentheses.callSite = true +rewrite.rules = [AvoidInfix, ExpandImportSelectors, RedundantParens, SortModifiers] +docstrings = JavaDoc +newlines.afterCurlyLambda = preserve +docstrings.style = Asterisk +docstrings.oneline = unfold +trailingCommas = "never" +optIn.breaksInsideChains = true +includeCurlyBraceInSelectChains = true diff --git a/embedded-files-macro/build.sbt b/embedded-files-macro/build.sbt new file mode 100644 index 0000000..08bcb79 --- /dev/null +++ b/embedded-files-macro/build.sbt @@ -0,0 +1,52 @@ +ThisBuild / organization := "com.yurique" +ThisBuild / homepage := Some(url("https://github.com/yurique/embedded-files")) +ThisBuild / licenses += ("MIT", url("https://github.com/yurique/embedded-files/blob/main/LICENSE.md")) +ThisBuild / developers := List( + Developer( + id = "yurique", + name = "Iurii Malchenko", + email = "i@yurique.com", + url = url("https://github.com/yurique") + ) +) +ThisBuild / scmInfo := Some( + ScmInfo( + url("https://github.com/yurique/embedded-files"), + "scm:git@github.com/yurique/embedded-files.git" + ) +) +ThisBuild / releasePublishArtifactsAction := PgpKeys.publishSigned.value +ThisBuild / publishTo := sonatypePublishToBundle.value +ThisBuild / pomIncludeRepository := { _ => false } +ThisBuild / sonatypeProfileName := "yurique" +ThisBuild / publishArtifact in Test := false +ThisBuild / publishMavenStyle := true +ThisBuild / releaseCrossBuild := true + +ThisBuild / scalaVersion := "2.13.4" +ThisBuild / crossScalaVersions := Seq("2.12.12", "2.13.4") + +lazy val noPublish = Seq( + publishLocal / skip := true, + publish / skip := true, + publishTo := Some(Resolver.file("Unused transient repository", file("target/unusedrepo"))) +) + +lazy val `embedded-files-macro` = crossProject(JVMPlatform, JSPlatform) + .crossType(CrossType.Pure) + .in(file("macro")) + .enablePlugins(ScalaJSPlugin) + .settings( + libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value + ) + +lazy val root = project + .in(file(".")) + .settings( + name := "embedded-files" + ) + .settings(noPublish) + .aggregate( + `embedded-files-macro`.jvm, + `embedded-files-macro`.js + ) diff --git a/embedded-files-macro/macro/src/main/scala/com/yurique/embedded/FileAsByteArray.scala b/embedded-files-macro/macro/src/main/scala/com/yurique/embedded/FileAsByteArray.scala new file mode 100644 index 0000000..bd35d88 --- /dev/null +++ b/embedded-files-macro/macro/src/main/scala/com/yurique/embedded/FileAsByteArray.scala @@ -0,0 +1,48 @@ +package com.yurique.embedded + +import java.io.File +import scala.reflect.macros.blackbox + +object FileAsByteArray { + + var rootPackage: String = "__embedded_files" + def apply(fileName: String): Array[Byte] = macro FileAsByteArrayImpl.referEmbeddedFile + +} + +class FileAsByteArrayImpl(val c: blackbox.Context) { + import c._ + import universe._ + + def referEmbeddedFile(fileName: c.Expr[String]) = fileName.tree match { + case Literal(Constant(fileNameStr: String)) => + val file = { + if (fileNameStr.startsWith("/")) { + new File(fileNameStr) + } else { + new File(new File(c.enclosingPosition.source.path).getParentFile.getAbsolutePath.split('/').dropWhile(_ != "scala").drop(1).mkString("/"), fileNameStr) + } + }.toPath + + val packageName: String = FileAsByteArray.rootPackage + "." + file.getParent.toString.replace("/", ".").dropWhile(_ == '.') + + val className: String = + s"${file.getFileName.toString.replaceAll("\\W", "_").replaceAll("_+", "_")}" + + val maybeSelectObject = packageName + .split('.').foldLeft[Option[c.Tree]]( + None + ) { (chain, next) => + chain match { + case None => Some(Ident(TermName(next))) + case Some(chain) => Some(Select(chain, TermName(next))) + } + } + + maybeSelectObject + .map { selectObject => + Select(Select(selectObject, TermName(className)), TermName("content")) + }.getOrElse(throw new RuntimeException(s"invalid package and class: ${packageName} ${className} for file ${fileNameStr}")) + } + +} diff --git a/embedded-files-macro/macro/src/main/scala/com/yurique/embedded/FileAsString.scala b/embedded-files-macro/macro/src/main/scala/com/yurique/embedded/FileAsString.scala new file mode 100644 index 0000000..56a9ef9 --- /dev/null +++ b/embedded-files-macro/macro/src/main/scala/com/yurique/embedded/FileAsString.scala @@ -0,0 +1,48 @@ +package com.yurique.embedded + +import java.io.File +import scala.reflect.macros.blackbox + +object FileAsString { + + var rootPackage: String = "__embedded_files" + def apply(fileName: String): String = macro FileAsStringImpl.referEmbeddedFile + +} + +class FileAsStringImpl(val c: blackbox.Context) { + import c._ + import universe._ + + def referEmbeddedFile(fileName: c.Expr[String]) = fileName.tree match { + case Literal(Constant(fileNameStr: String)) => + val file = { + if (fileNameStr.startsWith("/")) { + new File(fileNameStr) + } else { + new File(new File(c.enclosingPosition.source.path).getParentFile.getAbsolutePath.split('/').dropWhile(_ != "scala").drop(1).mkString("/"), fileNameStr) + } + }.toPath + + val packageName: String = FileAsString.rootPackage + "." + file.getParent.toString.replace("/", ".").dropWhile(_ == '.') + + val className: String = + s"${file.getFileName.toString.replaceAll("\\W", "_").replaceAll("_+", "_")}" + + val maybeSelectObject = packageName + .split('.').foldLeft[Option[c.Tree]]( + None + ) { (chain, next) => + chain match { + case None => Some(Ident(TermName(next))) + case Some(chain) => Some(Select(chain, TermName(next))) + } + } + + maybeSelectObject + .map { selectObject => + Select(Select(selectObject, TermName(className)), TermName("content")) + }.getOrElse(throw new RuntimeException(s"invalid package and class: ${packageName} ${className} for file ${fileNameStr}")) + } + +} diff --git a/embedded-files-macro/project/build.properties b/embedded-files-macro/project/build.properties new file mode 100644 index 0000000..d91c272 --- /dev/null +++ b/embedded-files-macro/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.4.6 diff --git a/embedded-files-macro/project/plugins.sbt b/embedded-files-macro/project/plugins.sbt new file mode 100644 index 0000000..dc9f9ba --- /dev/null +++ b/embedded-files-macro/project/plugins.sbt @@ -0,0 +1,15 @@ +logLevel := Level.Warn + +addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.0.1") + +addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.0.0") + +addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.2") + +addSbtPlugin("com.jsuereth" % "sbt-pgp" % "2.0.2") + +addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.5") + +addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.13") + +addSbtPlugin("io.github.davidgregory084" % "sbt-tpolecat" % "0.1.16") diff --git a/embedded-files-macro/version.sbt b/embedded-files-macro/version.sbt new file mode 100644 index 0000000..e765444 --- /dev/null +++ b/embedded-files-macro/version.sbt @@ -0,0 +1 @@ +version in ThisBuild := "0.1.0" diff --git a/sbt-plugin/.gitignore b/sbt-plugin/.gitignore new file mode 100644 index 0000000..76325a0 --- /dev/null +++ b/sbt-plugin/.gitignore @@ -0,0 +1,13 @@ +*.idea +*.bsp +*.class +*.log + +target +project/project +project/target +.cache +.classpath +.project +.settings +bin diff --git a/sbt-plugin/.scalafmt.conf b/sbt-plugin/.scalafmt.conf new file mode 100644 index 0000000..8249c07 --- /dev/null +++ b/sbt-plugin/.scalafmt.conf @@ -0,0 +1,3 @@ +version=2.7.5 +style = default +maxColumn = 140 diff --git a/sbt-plugin/.travis.yml b/sbt-plugin/.travis.yml new file mode 100644 index 0000000..8772f01 --- /dev/null +++ b/sbt-plugin/.travis.yml @@ -0,0 +1,27 @@ +language: scala + +jdk: + - oraclejdk8 + +script: + ## runs both regular tests and sbt-scripted integration tests + - sbt -Dfile.encoding=UTF8 test scripted + +sudo: false + +# https://docs.travis-ci.com/user/languages/java/ +addons: + apt: + packages: + - oracle-java8-installer + +before_cache: + # Tricks to avoid unnecessary cache updates + - find $HOME/.sbt -name "*.lock" | xargs rm + - find $HOME/.ivy2 -name "ivydata-*.properties" | xargs rm + +# These directories are cached to S3 at the end of the build +cache: + directories: + - $HOME/.ivy2/cache + - $HOME/.sbt/boot/ diff --git a/sbt-plugin/README.md b/sbt-plugin/README.md new file mode 100644 index 0000000..9592990 --- /dev/null +++ b/sbt-plugin/README.md @@ -0,0 +1,24 @@ +# sbt-embedded-files + +An sbt plugin that generates scala objects embedding the contents of files. + +## Usage + +This plugin requires sbt 1.0.0+ + +### Testing + +Run `test` for regular unit tests. + +Run `scripted` for [sbt script tests](http://www.scala-sbt.org/1.x/docs/Testing-sbt-plugins.html). + +### Publishing + +1. publish your source to GitHub +2. [create a bintray account](https://bintray.com/signup/index) and [set up bintray credentials](https://github.com/sbt/sbt-bintray#publishing) +3. create a bintray repository `sbt-plugins` +4. update your bintray publishing settings in `build.sbt` +5. `sbt publish` +6. [request inclusion in sbt-plugin-releases](https://bintray.com/sbt/sbt-plugin-releases) +7. [Add your plugin to the community plugins list](https://github.com/sbt/website#attention-plugin-authors) +8. [Claim your project an Scaladex](https://github.com/scalacenter/scaladex-contrib#claim-your-project) diff --git a/sbt-plugin/build.sbt b/sbt-plugin/build.sbt new file mode 100644 index 0000000..1721de7 --- /dev/null +++ b/sbt-plugin/build.sbt @@ -0,0 +1,16 @@ +ThisBuild / version := "0.1.0" +ThisBuild / organization := "com.yurique" +ThisBuild / description := "An sbt plugin that generates scala objects embedding the contents of files." +ThisBuild / bintrayRepository := "sbt-plugins" +ThisBuild / bintrayOrganization in bintray := None +ThisBuild / bintrayPackageLabels := Seq("sbt", "plugin") +ThisBuild / bintrayVcsUrl := Some("""git@github.com:yurique/embedded-files.git""") +ThisBuild / licenses += ("MIT", url("https://github.com/yurique/embedded-files/LICENSE")) + +name := "sbt-embedded-files" +sbtPlugin := true +publishMavenStyle := false +initialCommands in console := """import com.yurique.embedded.sbt._""" +enablePlugins(ScriptedPlugin) +scriptedLaunchOpts ++= + Seq("-Xmx1024M", "-Dplugin.version=" + version.value) diff --git a/sbt-plugin/project/build.properties b/sbt-plugin/project/build.properties new file mode 100644 index 0000000..d91c272 --- /dev/null +++ b/sbt-plugin/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.4.6 diff --git a/sbt-plugin/project/plugins.sbt b/sbt-plugin/project/plugins.sbt new file mode 100644 index 0000000..811fb79 --- /dev/null +++ b/sbt-plugin/project/plugins.sbt @@ -0,0 +1,2 @@ +libraryDependencies += "org.scala-sbt" %% "scripted-plugin" % sbtVersion.value +addSbtPlugin("org.foundweekends" % "sbt-bintray" % "0.6.1") diff --git a/sbt-plugin/src/main/scala/com/yurique/embedded/sbt/EmbeddedFilesPlugin.scala b/sbt-plugin/src/main/scala/com/yurique/embedded/sbt/EmbeddedFilesPlugin.scala new file mode 100644 index 0000000..21b8baa --- /dev/null +++ b/sbt-plugin/src/main/scala/com/yurique/embedded/sbt/EmbeddedFilesPlugin.scala @@ -0,0 +1,313 @@ +package com.yurique.embedded.sbt + +import sbt._ +import sbt.Keys._ +import java.nio.file.Files +import java.nio.file.Path +import sbt.nio.Keys._ + +object EmbeddedFilesPlugin extends AutoPlugin { + + override def trigger = noTrigger + + object autoImport { + val embedFiles = taskKey[Seq[File]]("Creates an ExternalFile object with a content field for each file.") + val embedRootPackage = settingKey[String]("Root package for generated classes (default: __embedded_files)") + val embedDirectories = settingKey[Seq[File]]("Directories to look for files in (default: Compile / unmanagedResourceDirectories)") + val embedTextGlobs = settingKey[Seq[String]]("glob patterns for text files (default: Seq(**/*.txt))") + val embedBinGlobs = settingKey[Seq[String]]("glob patterns for binary files (default: Seq.empty)") + val embedGenerateIndex = settingKey[Boolean]("Whether or not to generate the EmbeddedFilesIndex object (default: false)") + } + + import autoImport._ + + private def toValidId = (s: String) => s.trim.replaceAll("\\W", "_").replaceAll("_+", "_") + + override lazy val projectSettings: Seq[Setting[_]] = Seq( + embedRootPackage := "__embedded_files", + embedDirectories := (Compile / unmanagedResourceDirectories).value, + embedTextGlobs := Seq("**/*.txt"), + embedBinGlobs := Seq.empty, + embedGenerateIndex := false, + embedFiles / fileInputs ++= embedDirectories.value.flatMap(embedDirectory => + embedTextGlobs.value.map(glob => embedDirectory.toGlob / glob) + ), + embedFiles / fileInputs ++= embedDirectories.value.flatMap(embedDirectory => + embedBinGlobs.value.map(glob => embedDirectory.toGlob / glob) + ), + embedFiles := { + val rootPackage = embedRootPackage.value + + val outputDir = (Compile / sourceManaged).value.toPath / "scala" + + def packageName(relative: Path): String = { + if (relative.getParent != null) { + var subPackage = relative.getParent.toString.split('/').map(toValidId).mkString(".") + if (subPackage.headOption.exists(_.isDigit)) { + subPackage = "_" + subPackage + } + if (subPackage.isEmpty) { + s"${rootPackage}" + } else { + s"${rootPackage}.${subPackage}" + } + } else { + s"${rootPackage}" + } + } + + def className(relative: Path): String = { + s"${toValidId(relative.getFileName.toString)}" + } + + def outputPath(packageName: String, className: String): Path = { + outputDir / packageName.replaceAllLiterally(".", "/") / s"${className}.scala" + } + + val sourceMap = + embedFiles.inputFiles.view.flatMap { path => + embedDirectories.value + .flatMap(path.toFile.relativeTo) + .headOption + .map(_.toPath) + .map { relative => + outputPath(packageName(relative), className(relative)) -> path + } + }.toMap + + val existingTargets = + fileTreeView.value + .list(outputDir.toGlob / **) + .flatMap { case (p, _) => + if (p.toFile.isFile && !sourceMap.contains(p)) { + Files.deleteIfExists(p) + None + } else { + Some(p) + } + } + .toSet + + val changes = embedFiles.inputFileChanges + val updatedPaths = (changes.created ++ changes.modified).toSet + val needToEmbed = + updatedPaths ++ sourceMap.filterKeys(!existingTargets(_)).values + needToEmbed.foreach { path => + embedDirectories.value + .flatMap(path.toFile.relativeTo) + .headOption + .map(_.toPath) + .foreach { relative => + val isBinary = + embedDirectories.value + .flatMap(embedDirectory => embedBinGlobs.value.map(glob => embedDirectory.toGlob / glob)) + .exists(_.matches(path)) + + val filePackageName = packageName(relative) + val fileClassName = className(relative) + val fileOutputPath = outputPath(filePackageName, fileClassName) + if (isBinary) { + buildEmbeddedBinFile( + input = path, + relative = relative, + output = fileOutputPath, + rootPackage = rootPackage, + packageName = filePackageName, + className = fileClassName + ) + } else { + buildEmbeddedTextFile( + input = path, + relative = relative, + output = fileOutputPath, + rootPackage = rootPackage, + packageName = filePackageName, + className = fileClassName + ) + } + } + } + if (embedGenerateIndex.value) { + val generatedClasses = needToEmbed.flatMap { path => + embedDirectories.value + .flatMap(path.toFile.relativeTo) + .headOption + .map(_.toPath) + .map { relative => + val isBinary = + embedDirectories.value + .flatMap(embedDirectory => + embedBinGlobs.value + .map(glob => embedDirectory.toGlob / glob) + ) + .exists(_.matches(path)) + + isBinary -> (relative, packageName(relative), className( + relative + )) + } + } + buildIndexFile( + textInputs = generatedClasses.toSeq.collect { case (false, generatedClassInfo) => + generatedClassInfo + }, + binInputs = generatedClasses.toSeq.collect { case (true, generatedClassInfo) => + generatedClassInfo + }, + output = outputPath(rootPackage, "EmbeddedFilesIndex"), + packageName = rootPackage + ) + } + buildTextFileInterface( + output = outputPath(rootPackage, "EmbeddedTextFile"), + packageName = rootPackage + ) + buildBinFileInterface( + output = outputPath(rootPackage, "EmbeddedBinFile"), + packageName = rootPackage + ) + + val generatedFiles = sourceMap.keys.toVector.map(_.toFile) ++ + Vector( + outputPath(rootPackage, "EmbeddedTextFile").toFile, + outputPath(rootPackage, "EmbeddedBinFile").toFile + ) ++ + Vector( + outputPath(rootPackage, "EmbeddedFilesIndex").toFile + ).filter(_ => embedGenerateIndex.value) + + generatedFiles + } + ) + + def buildEmbeddedTextFile( + input: Path, + relative: Path, + output: Path, + rootPackage: String, + packageName: String, + className: String + ): Unit = { + val str = + s"""|package $packageName + | + |object $className extends ${rootPackage}.EmbeddedTextFile { + | + | val path: String = \"\"\"${relative.toString.replaceAllLiterally( + "\"\"\"", + "\\\"\\\"\\\"" + )}\"\"\" + | + | val content: String = \"\"\"${IO + .read(input.toFile) + .replaceAllLiterally("\"\"\"", "\\\"\\\"\\\"")}\"\"\" + | + |} + |""".stripMargin + IO.write( + output.toFile, + str + ) + } + + def buildEmbeddedBinFile( + input: Path, + relative: Path, + output: Path, + rootPackage: String, + packageName: String, + className: String + ): Unit = { + val str = + s"""|package $packageName + | + |object $className extends ${rootPackage}.EmbeddedBinFile { + | + | val path: String = \"\"\"${relative.toString.replaceAllLiterally( + "\"\"\"", + "\\\"\\\"\\\"" + )}\"\"\" + | + | val content: Array[Byte] = Array( + | ${IO + .readBytes(input.toFile) + .grouped(16) + .map( + _.map(_.toHexString.reverse.padTo(2, '0').reverse) + .map("0x" + _) + .mkString(", ") + ) + .mkString("\n ")} + | ) + | + |} + |""".stripMargin + IO.write( + output.toFile, + str + ) + } + + def buildTextFileInterface(output: Path, packageName: String): Unit = { + val str = + s"""|package $packageName + | + |abstract class EmbeddedTextFile { + | def path: String + | def content: String + |} + |""".stripMargin + IO.write( + output.toFile, + str + ) + } + + def buildBinFileInterface(output: Path, packageName: String): Unit = { + val str = + s"""|package $packageName + | + |abstract class EmbeddedBinFile { + | def path: String + | def content: Array[Byte] + |} + |""".stripMargin + IO.write( + output.toFile, + str + ) + } + + def buildIndexFile( + textInputs: Seq[(Path, String, String)], + binInputs: Seq[(Path, String, String)], + output: Path, + packageName: String + ): Unit = { + val str = + s"""|package $packageName + | + |object EmbeddedFilesIndex { + | val textFiles: Seq[(String, EmbeddedTextFile)] = Seq( + | ${textInputs + .map { case (path, packageName, className) => + s""""${path.toString}" -> ${packageName}.${className}""" + } + .mkString(",\n ")} + | ) + | val binFiles: Seq[(String, EmbeddedBinFile)] = Seq( + | ${binInputs + .map { case (path, packageName, className) => + s""""${path.toString}" -> ${packageName}.${className}""" + } + .mkString(",\n ")} + | ) + |} + |""".stripMargin + IO.write( + output.toFile, + str + ) + } + +} diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/build.sbt b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/build.sbt new file mode 100644 index 0000000..de0873a --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/build.sbt @@ -0,0 +1,13 @@ +version := "0.1" +scalaVersion := "2.13.4" + +val root = project + .in(file(".")) + .enablePlugins(EmbeddedFilesPlugin) + .settings( + embedTextGlobs := Seq("**/*.txt"), + embedBinGlobs := Seq("**/*.bin"), + embedGenerateIndex := true, + embedDirectories ++= (Compile / unmanagedSourceDirectories).value, + (Compile / sourceGenerators) += embedFiles + ) diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/expected/__embedded_files/EmbeddedBinFile.scala b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/expected/__embedded_files/EmbeddedBinFile.scala new file mode 100644 index 0000000..aacc6c1 --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/expected/__embedded_files/EmbeddedBinFile.scala @@ -0,0 +1,6 @@ +package __embedded_files + +abstract class EmbeddedBinFile { + def path: String + def content: Array[Byte] +} diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/expected/__embedded_files/EmbeddedFilesIndex.scala b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/expected/__embedded_files/EmbeddedFilesIndex.scala new file mode 100644 index 0000000..11582f4 --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/expected/__embedded_files/EmbeddedFilesIndex.scala @@ -0,0 +1,12 @@ +package __embedded_files + +object EmbeddedFilesIndex { + val textFiles: Seq[(String, EmbeddedTextFile)] = Seq( + "test-resource.txt" -> __embedded_files.test_resource_txt, + "com/yurique/embedded/sbt/txt/test_file_1.txt" -> __embedded_files.com.yurique.embedded.sbt.txt.test_file_1_txt + ) + val binFiles: Seq[(String, EmbeddedBinFile)] = Seq( + "test/test-bin-resource.bin" -> __embedded_files.test.test_bin_resource_bin, + "com/yurique/embedded/sbt/binary/test_bin_file_1.bin" -> __embedded_files.com.yurique.embedded.sbt.binary.test_bin_file_1_bin + ) +} diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/expected/__embedded_files/EmbeddedTextFile.scala b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/expected/__embedded_files/EmbeddedTextFile.scala new file mode 100644 index 0000000..a956b44 --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/expected/__embedded_files/EmbeddedTextFile.scala @@ -0,0 +1,6 @@ +package __embedded_files + +abstract class EmbeddedTextFile { + def path: String + def content: String +} diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/expected/__embedded_files/com/yurique/embedded/sbt/binary/test_bin_file_1_bin.scala b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/expected/__embedded_files/com/yurique/embedded/sbt/binary/test_bin_file_1_bin.scala new file mode 100644 index 0000000..9e8e390 --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/expected/__embedded_files/com/yurique/embedded/sbt/binary/test_bin_file_1_bin.scala @@ -0,0 +1,11 @@ +package __embedded_files.com.yurique.embedded.sbt.binary + +object test_bin_file_1_bin extends __embedded_files.EmbeddedBinFile { + + val path: String = """com/yurique/embedded/sbt/binary/test_bin_file_1.bin""" + + val content: Array[Byte] = Array( + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f + ) + +} diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/expected/__embedded_files/com/yurique/embedded/sbt/txt/test_file_1_txt.scala b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/expected/__embedded_files/com/yurique/embedded/sbt/txt/test_file_1_txt.scala new file mode 100644 index 0000000..73c729f --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/expected/__embedded_files/com/yurique/embedded/sbt/txt/test_file_1_txt.scala @@ -0,0 +1,14 @@ +package __embedded_files.com.yurique.embedded.sbt.txt + +object test_file_1_txt extends __embedded_files.EmbeddedTextFile { + + val path: String = """com/yurique/embedded/sbt/txt/test_file_1.txt""" + + val content: String = """A test file. + +Should be embedded. + +Triple " should be \"\"\"handled\"\"\". +""" + +} diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/expected/__embedded_files/test/test_bin_resource_bin.scala b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/expected/__embedded_files/test/test_bin_resource_bin.scala new file mode 100644 index 0000000..47e61b1 --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/expected/__embedded_files/test/test_bin_resource_bin.scala @@ -0,0 +1,11 @@ +package __embedded_files.test + +object test_bin_resource_bin extends __embedded_files.EmbeddedBinFile { + + val path: String = """test/test-bin-resource.bin""" + + val content: Array[Byte] = Array( + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f + ) + +} diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/expected/__embedded_files/test_resource_txt.scala b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/expected/__embedded_files/test_resource_txt.scala new file mode 100644 index 0000000..5823216 --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/expected/__embedded_files/test_resource_txt.scala @@ -0,0 +1,10 @@ +package __embedded_files + +object test_resource_txt extends __embedded_files.EmbeddedTextFile { + + val path: String = """test-resource.txt""" + + val content: String = """A file from resources. +""" + +} diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/project/plugins.sbt b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/project/plugins.sbt new file mode 100644 index 0000000..d1a4238 --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/project/plugins.sbt @@ -0,0 +1,7 @@ +{ + val pluginVersion = System.getProperty("plugin.version") + if(pluginVersion == null) + throw new RuntimeException("""|The system property 'plugin.version' is not defined. + |Specify this property using the scriptedLaunchOpts -D.""".stripMargin) + else addSbtPlugin("com.yurique" % """sbt-embedded-files""" % pluginVersion) +} diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/src/main/resources/test-resource.txt b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/src/main/resources/test-resource.txt new file mode 100644 index 0000000..bacd923 --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/src/main/resources/test-resource.txt @@ -0,0 +1 @@ +A file from resources. diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/src/main/resources/test/test-bin-resource.bin b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/src/main/resources/test/test-bin-resource.bin new file mode 100644 index 0000000..b66efb8 Binary files /dev/null and b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/src/main/resources/test/test-bin-resource.bin differ diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/src/main/scala/Main.scala b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/src/main/scala/Main.scala new file mode 100644 index 0000000..363b8d4 --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/src/main/scala/Main.scala @@ -0,0 +1,19 @@ +package simple + +/** + * A simple class and objects to write tests against. + */ +class Main { + val default = "the function returned" + def method = default + " " + Main.function +} + +object Main { + + val constant = 1 + def function = 2*constant + + def main(args: Array[String]): Unit = { + println(new Main().default) + } +} diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/src/main/scala/com/yurique/embedded/sbt/binary/test_bin_file_1.bin b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/src/main/scala/com/yurique/embedded/sbt/binary/test_bin_file_1.bin new file mode 100644 index 0000000..b66efb8 Binary files /dev/null and b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/src/main/scala/com/yurique/embedded/sbt/binary/test_bin_file_1.bin differ diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/src/main/scala/com/yurique/embedded/sbt/txt/test_file_1.txt b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/src/main/scala/com/yurique/embedded/sbt/txt/test_file_1.txt new file mode 100644 index 0000000..d90efc8 --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/src/main/scala/com/yurique/embedded/sbt/txt/test_file_1.txt @@ -0,0 +1,5 @@ +A test file. + +Should be embedded. + +Triple " should be """handled""". diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/test b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/test new file mode 100644 index 0000000..5014c66 --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/with-index/test @@ -0,0 +1,9 @@ +# check if the embedded files get created +> compile +$ must-mirror target/scala-2.13/src_managed/main/scala/__embedded_files/com/yurique/embedded/sbt/txt/test_file_1_txt.scala expected/__embedded_files/com/yurique/embedded/sbt/txt/test_file_1_txt.scala +$ must-mirror target/scala-2.13/src_managed/main/scala/__embedded_files/test_resource_txt.scala expected/__embedded_files/test_resource_txt.scala +$ must-mirror target/scala-2.13/src_managed/main/scala/__embedded_files/com/yurique/embedded/sbt/binary/test_bin_file_1_bin.scala expected/__embedded_files/com/yurique/embedded/sbt/binary/test_bin_file_1_bin.scala +$ must-mirror target/scala-2.13/src_managed/main/scala/__embedded_files/test/test_bin_resource_bin.scala expected/__embedded_files/test/test_bin_resource_bin.scala +$ must-mirror target/scala-2.13/src_managed/main/scala/__embedded_files/EmbeddedTextFile.scala expected/__embedded_files/EmbeddedTextFile.scala +$ must-mirror target/scala-2.13/src_managed/main/scala/__embedded_files/EmbeddedBinFile.scala expected/__embedded_files/EmbeddedBinFile.scala +$ must-mirror target/scala-2.13/src_managed/main/scala/__embedded_files/EmbeddedFilesIndex.scala expected/__embedded_files/EmbeddedFilesIndex.scala diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/build.sbt b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/build.sbt new file mode 100644 index 0000000..c75bc8e --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/build.sbt @@ -0,0 +1,12 @@ +version := "0.1" +scalaVersion := "2.13.4" + +val root = project + .in(file(".")) + .enablePlugins(EmbeddedFilesPlugin) + .settings( + embedTextGlobs := Seq("**/*.txt"), + embedBinGlobs := Seq("**/*.bin"), + embedDirectories ++= (Compile / unmanagedSourceDirectories).value, + (Compile / sourceGenerators) += embedFiles + ) diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/expected/__embedded_files/EmbeddedBinFile.scala b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/expected/__embedded_files/EmbeddedBinFile.scala new file mode 100644 index 0000000..aacc6c1 --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/expected/__embedded_files/EmbeddedBinFile.scala @@ -0,0 +1,6 @@ +package __embedded_files + +abstract class EmbeddedBinFile { + def path: String + def content: Array[Byte] +} diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/expected/__embedded_files/EmbeddedTextFile.scala b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/expected/__embedded_files/EmbeddedTextFile.scala new file mode 100644 index 0000000..a956b44 --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/expected/__embedded_files/EmbeddedTextFile.scala @@ -0,0 +1,6 @@ +package __embedded_files + +abstract class EmbeddedTextFile { + def path: String + def content: String +} diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/expected/__embedded_files/com/yurique/embedded/sbt/binary/test_bin_file_1_bin.scala b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/expected/__embedded_files/com/yurique/embedded/sbt/binary/test_bin_file_1_bin.scala new file mode 100644 index 0000000..9e8e390 --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/expected/__embedded_files/com/yurique/embedded/sbt/binary/test_bin_file_1_bin.scala @@ -0,0 +1,11 @@ +package __embedded_files.com.yurique.embedded.sbt.binary + +object test_bin_file_1_bin extends __embedded_files.EmbeddedBinFile { + + val path: String = """com/yurique/embedded/sbt/binary/test_bin_file_1.bin""" + + val content: Array[Byte] = Array( + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f + ) + +} diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/expected/__embedded_files/com/yurique/embedded/sbt/txt/test_file_1_txt.scala b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/expected/__embedded_files/com/yurique/embedded/sbt/txt/test_file_1_txt.scala new file mode 100644 index 0000000..73c729f --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/expected/__embedded_files/com/yurique/embedded/sbt/txt/test_file_1_txt.scala @@ -0,0 +1,14 @@ +package __embedded_files.com.yurique.embedded.sbt.txt + +object test_file_1_txt extends __embedded_files.EmbeddedTextFile { + + val path: String = """com/yurique/embedded/sbt/txt/test_file_1.txt""" + + val content: String = """A test file. + +Should be embedded. + +Triple " should be \"\"\"handled\"\"\". +""" + +} diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/expected/__embedded_files/test/test_bin_resource_bin.scala b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/expected/__embedded_files/test/test_bin_resource_bin.scala new file mode 100644 index 0000000..47e61b1 --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/expected/__embedded_files/test/test_bin_resource_bin.scala @@ -0,0 +1,11 @@ +package __embedded_files.test + +object test_bin_resource_bin extends __embedded_files.EmbeddedBinFile { + + val path: String = """test/test-bin-resource.bin""" + + val content: Array[Byte] = Array( + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f + ) + +} diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/expected/__embedded_files/test_resource_txt.scala b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/expected/__embedded_files/test_resource_txt.scala new file mode 100644 index 0000000..5823216 --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/expected/__embedded_files/test_resource_txt.scala @@ -0,0 +1,10 @@ +package __embedded_files + +object test_resource_txt extends __embedded_files.EmbeddedTextFile { + + val path: String = """test-resource.txt""" + + val content: String = """A file from resources. +""" + +} diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/project/plugins.sbt b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/project/plugins.sbt new file mode 100644 index 0000000..d1a4238 --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/project/plugins.sbt @@ -0,0 +1,7 @@ +{ + val pluginVersion = System.getProperty("plugin.version") + if(pluginVersion == null) + throw new RuntimeException("""|The system property 'plugin.version' is not defined. + |Specify this property using the scriptedLaunchOpts -D.""".stripMargin) + else addSbtPlugin("com.yurique" % """sbt-embedded-files""" % pluginVersion) +} diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/src/main/resources/test-resource.txt b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/src/main/resources/test-resource.txt new file mode 100644 index 0000000..bacd923 --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/src/main/resources/test-resource.txt @@ -0,0 +1 @@ +A file from resources. diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/src/main/resources/test/test-bin-resource.bin b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/src/main/resources/test/test-bin-resource.bin new file mode 100644 index 0000000..b66efb8 Binary files /dev/null and b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/src/main/resources/test/test-bin-resource.bin differ diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/src/main/scala/Main.scala b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/src/main/scala/Main.scala new file mode 100644 index 0000000..363b8d4 --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/src/main/scala/Main.scala @@ -0,0 +1,19 @@ +package simple + +/** + * A simple class and objects to write tests against. + */ +class Main { + val default = "the function returned" + def method = default + " " + Main.function +} + +object Main { + + val constant = 1 + def function = 2*constant + + def main(args: Array[String]): Unit = { + println(new Main().default) + } +} diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/src/main/scala/com/yurique/embedded/sbt/binary/test_bin_file_1.bin b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/src/main/scala/com/yurique/embedded/sbt/binary/test_bin_file_1.bin new file mode 100644 index 0000000..b66efb8 Binary files /dev/null and b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/src/main/scala/com/yurique/embedded/sbt/binary/test_bin_file_1.bin differ diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/src/main/scala/com/yurique/embedded/sbt/txt/test_file_1.txt b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/src/main/scala/com/yurique/embedded/sbt/txt/test_file_1.txt new file mode 100644 index 0000000..d90efc8 --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/src/main/scala/com/yurique/embedded/sbt/txt/test_file_1.txt @@ -0,0 +1,5 @@ +A test file. + +Should be embedded. + +Triple " should be """handled""". diff --git a/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/test b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/test new file mode 100644 index 0000000..16eea93 --- /dev/null +++ b/sbt-plugin/src/sbt-test/sbt-embedded-files/without-index/test @@ -0,0 +1,8 @@ +# check if the embedded files get created +> compile +$ must-mirror target/scala-2.13/src_managed/main/scala/__embedded_files/com/yurique/embedded/sbt/txt/test_file_1_txt.scala expected/__embedded_files/com/yurique/embedded/sbt/txt/test_file_1_txt.scala +$ must-mirror target/scala-2.13/src_managed/main/scala/__embedded_files/test_resource_txt.scala expected/__embedded_files/test_resource_txt.scala +$ must-mirror target/scala-2.13/src_managed/main/scala/__embedded_files/com/yurique/embedded/sbt/binary/test_bin_file_1_bin.scala expected/__embedded_files/com/yurique/embedded/sbt/binary/test_bin_file_1_bin.scala +$ must-mirror target/scala-2.13/src_managed/main/scala/__embedded_files/test/test_bin_resource_bin.scala expected/__embedded_files/test/test_bin_resource_bin.scala +$ must-mirror target/scala-2.13/src_managed/main/scala/__embedded_files/EmbeddedTextFile.scala expected/__embedded_files/EmbeddedTextFile.scala +$ must-mirror target/scala-2.13/src_managed/main/scala/__embedded_files/EmbeddedBinFile.scala expected/__embedded_files/EmbeddedBinFile.scala