Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ant8e committed Jun 13, 2018
0 parents commit bc40bd8
Show file tree
Hide file tree
Showing 14 changed files with 197 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
@@ -0,0 +1,12 @@
*.class
*.log

target
project/project
project/target
.cache
.classpath
.project
.settings
bin
.idea/
5 changes: 5 additions & 0 deletions .scalafmt.conf
@@ -0,0 +1,5 @@
style = defaultWithAlign # For pretty alignment.
align = most
maxColumn = 100
rewrite.rules = [SortImports, RedundantBraces]
project.git = true
27 changes: 27 additions & 0 deletions .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/
24 changes: 24 additions & 0 deletions 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)
19 changes: 19 additions & 0 deletions 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
1 change: 1 addition & 0 deletions project/build.properties
@@ -0,0 +1 @@
sbt.version=1.1.6
3 changes: 3 additions & 0 deletions 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")
67 changes: 67 additions & 0 deletions 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)
)
}
}
2 changes: 2 additions & 0 deletions src/sbt-test/sbt-i18n/simple/build.sbt
@@ -0,0 +1,2 @@
version := "0.1"
scalaVersion := "2.12.6"
7 changes: 7 additions & 0 deletions 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)
}
Empty file.
19 changes: 19 additions & 0 deletions 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)
}
}
6 changes: 6 additions & 0 deletions 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

5 changes: 5 additions & 0 deletions 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
}

0 comments on commit bc40bd8

Please sign in to comment.