diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..993810b --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +*.class +*.log + +target +project/project +project/target +.cache +.classpath +.project +.settings +bin +.idea/ diff --git a/.scalafmt.conf b/.scalafmt.conf new file mode 100644 index 0000000..cfa23eb --- /dev/null +++ b/.scalafmt.conf @@ -0,0 +1,5 @@ +style = defaultWithAlign # For pretty alignment. +align = most +maxColumn = 100 +rewrite.rules = [SortImports, RedundantBraces] +project.git = true diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8772f01 --- /dev/null +++ b/.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/README.md b/README.md new file mode 100644 index 0000000..097ebb5 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# sbt i18n + +An sbt plugin to generate a scala i18n bundle for various sources + +## 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/build.sbt b/build.sbt new file mode 100644 index 0000000..e80df90 --- /dev/null +++ b/build.sbt @@ -0,0 +1,19 @@ +name := "sbt-i18n" +organization := "tech.ant8e" +version := "0.1-SNAPSHOT" + +sbtPlugin := true + +libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.5" % "test" +libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % "test" + +bintrayPackageLabels := Seq("sbt", "plugin") +bintrayVcsUrl := Some("git@github.com:ant8e/sbt-i18n.git") + +initialCommands in console := """import tech.ant8e.sbt.i18n._""" + +// set up 'scripted; sbt plugin for testing sbt plugins +scriptedLaunchOpts ++= + Seq("-Xmx1024M", "-Dplugin.version=" + version.value) + +scalafmtOnCompile in ThisBuild := true diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 0000000..d6e3507 --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.1.6 diff --git a/project/plugins.sbt b/project/plugins.sbt new file mode 100644 index 0000000..28571c9 --- /dev/null +++ b/project/plugins.sbt @@ -0,0 +1,3 @@ +libraryDependencies += "org.scala-sbt" %% "scripted-plugin" % sbtVersion.value +addSbtPlugin("org.foundweekends" % "sbt-bintray" % "0.5.4") +addSbtPlugin("com.lucidchart" % "sbt-scalafmt" % "1.15") diff --git a/src/main/scala/tech/ant8e/sbt/i18n/SbtI18nPlugin.scala b/src/main/scala/tech/ant8e/sbt/i18n/SbtI18nPlugin.scala new file mode 100644 index 0000000..7a361fc --- /dev/null +++ b/src/main/scala/tech/ant8e/sbt/i18n/SbtI18nPlugin.scala @@ -0,0 +1,67 @@ +package tech.ant8e.sbt.i18n + +import sbt._ +import sbt.Keys._ +import sbt.plugins.JvmPlugin +import sbt.internal.io.Source + +object SbtI18nPlugin extends AutoPlugin { + + override def trigger = allRequirements + override def requires = JvmPlugin + + object autoImport { + val i18nBundlePackageSetting = + settingKey[String]("Package name for the i18n bundle ") + val generateI18NBundleTask = + taskKey[Seq[File]]("A task that is automatically imported to the build") + + } + + import autoImport._ + private val i18nSource = + settingKey[File]("Default directory containing i18n configuration sources.") + + override lazy val projectSettings = + inConfig(Compile)( + watchSourceSettings ++ + Seq( + i18nSource := sourceDirectory.value / "i18n", + generateI18NBundleTask := generateFromTemplates(streams.value, + i18nSource.value, + sourceManaged.value / "sbt-i18n", + i18nBundlePackageSetting.value), + mappings in packageSrc ++= managedSources.value pair (Path + .relativeTo(sourceManaged.value) | Path.flat), + sourceGenerators += generateI18NBundleTask.taskValue + )) ++ Seq(i18nBundlePackageSetting := "org.example.i18n") + + def generateFromTemplates(streams: TaskStreams, + srcDir: sbt.File, + outDir: File, + packageName: String): Seq[File] = { + val inputs = srcDir.allPaths + streams.log.debug(s"Found ${inputs.get.size} template files in $srcDir.") + streams.log.info(s"Generating i18n bundle in package $packageName.") + val bundleFile = outDir / "Bundle.scala" + IO.write(bundleFile, + s"""package $packageName + | + |object Bundle {}""".stripMargin) + + Seq(bundleFile) + } + + override lazy val buildSettings = Seq() + + override lazy val globalSettings = Seq() + + def allPaths(f: File) = f.allPaths + + def watchSourceSettings = Def.settings { + Seq( + watchSources in Defaults.ConfigGlobal += + new Source(i18nSource.value, AllPassFilter, NothingFilter) + ) + } +} diff --git a/src/sbt-test/sbt-i18n/simple/build.sbt b/src/sbt-test/sbt-i18n/simple/build.sbt new file mode 100644 index 0000000..b744c98 --- /dev/null +++ b/src/sbt-test/sbt-i18n/simple/build.sbt @@ -0,0 +1,2 @@ +version := "0.1" +scalaVersion := "2.12.6" diff --git a/src/sbt-test/sbt-i18n/simple/project/plugins.sbt b/src/sbt-test/sbt-i18n/simple/project/plugins.sbt new file mode 100644 index 0000000..910e99f --- /dev/null +++ b/src/sbt-test/sbt-i18n/simple/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("tech.ant8e" % """sbt-i18n""" % pluginVersion) +} diff --git a/src/sbt-test/sbt-i18n/simple/src/main/i18n/i18n.conf b/src/sbt-test/sbt-i18n/simple/src/main/i18n/i18n.conf new file mode 100644 index 0000000..e69de29 diff --git a/src/sbt-test/sbt-i18n/simple/src/main/scala/Main.scala b/src/sbt-test/sbt-i18n/simple/src/main/scala/Main.scala new file mode 100644 index 0000000..363b8d4 --- /dev/null +++ b/src/sbt-test/sbt-i18n/simple/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/src/sbt-test/sbt-i18n/simple/test b/src/sbt-test/sbt-i18n/simple/test new file mode 100644 index 0000000..d4b5909 --- /dev/null +++ b/src/sbt-test/sbt-i18n/simple/test @@ -0,0 +1,6 @@ +> show generateI18NBundleTask + +> compile + +$ exists target/scala-2.12/src_managed/main/sbt-i18n/Bundle.scala + diff --git a/src/test/scala/tech/ant8e/sbt/i18n/SbtI18nSpec.scala b/src/test/scala/tech/ant8e/sbt/i18n/SbtI18nSpec.scala new file mode 100644 index 0000000..a1bb779 --- /dev/null +++ b/src/test/scala/tech/ant8e/sbt/i18n/SbtI18nSpec.scala @@ -0,0 +1,5 @@ +package tech.ant8e.sbt.i18n + +class SbtI18nTest { + // write tests with your preferred framework +}