一連のスカラー形式の設定を自動的に設定するプラグインを構築しようとしています。
私のプラグインの build.sbt:
name := "my-scalariform"
organization := "com.my"
version := "1.0-SNAPSHOT"
sbtPlugin := true
scalacOptions ++= Seq("-feature", "-deprecation", "-unchecked")
addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.3.0")
私の最初のプラグイン設計:
package com.my.plugins
import com.typesafe.sbt.SbtScalariform
import com.typesafe.sbt.SbtScalariform.{
ScalariformKeys,
scalariformSettings
}
import sbt.AutoPlugin
import sbt.{ Compile, Test }
import sbt.Keys.{ compile, compileInputs }
import scalariform.formatter.preferences.{
DoubleIndentClassDeclaration,
FormattingPreferences,
IndentSpaces,
IndentWithTabs,
PreserveDanglingCloseParenthesis
}
object MyScalariformPlugin extends AutoPlugin {
override def trigger = allRequirements
lazy val formattingPreferences = {
import scalariform.formatter.preferences._
Seq(
ScalariformKeys.preferences := FormattingPreferences()
.setPreference(DoubleIndentClassDeclaration, true)
.setPreference(IndentSpaces, 2)
.setPreference(IndentWithTabs, false)
.setPreference(PreserveDanglingCloseParenthesis, true)
)
}
override lazy val projectSettings = scalariformSettings ++ formattingPreferences
}
このプラグインをプロジェクトに追加すると、スカラフォームの設定が表示されます。
> scalariform-preferences
[info] FormattingPreferences(Map(DoubleIndentClassDeclaration -> true, IndentSpaces -> 2, IndentWithTabs -> false, PreserveDanglingCloseParenthesis -> true))
ただし、compileInputs には scalariform-format 依存関係がありません。
> inspect compile:compile::compileInputs
...
[info] Defined at:
[info] (sbt.Defaults) Defaults.scala:792
[info] Dependencies:
[info] compile:compile::incCompileSetup
[info] compile:compile::streams
[info] compile:compile::dependencyClasspath
[info] compile:compile::compileOrder
[info] compile:compile::scalacOptions
[info] compile:compile::classDirectory
[info] compile:compile::javacOptions
[info] compile:compile::sourcePositionMappers
[info] compile:compile::compilers
[info] compile:compile::sources
[info] compile:compile::maxErrors
[info] Reverse dependencies:
[info] compile:compile
...
scalariformコマンドのオーバーライドをプラグインの値として明示的に追加し、それをプロジェクトに明示的に追加すると、正しい依存関係が得られます。
lazy val commandSettings = Seq(
compileInputs in (Compile, compile) <<= (compileInputs in (Compile, compile)) dependsOn (ScalariformKeys.format in Compile),
compileInputs in (Test, compile) <<= (compileInputs in (Test, compile)) dependsOn (ScalariformKeys.format in Test)
)
依存関係:
> inspect compile:compile::compileInputs
...
[info] Defined at:
[info] (sbt.Defaults) Defaults.scala:792
[info] (com.my.plugins.MyScalariformPlugin) MyScalariformPlugin.scala:22
[info] Dependencies:
[info] compile:compile::incCompileSetup
[info] compile:compile::streams
[info] compile:compile::dependencyClasspath
[info] compile:scalariformFormat
[info] compile:compile::compileOrder
[info] compile:compile::scalacOptions
[info] compile:compile::classDirectory
[info] compile:compile::javacOptions
[info] compile:compile::sourcePositionMappers
[info] compile:compile::compilers
[info] compile:compile::sources
[info] compile:compile::maxErrors
[info] Reverse dependencies:
[info] compile:compile
...
autoImport を使用してこの依存関係を自分で指定しようとしましたが、エラーが発生します。
object autoImport {
lazy val commandSettings = Seq(
compileInputs in (Compile, compile) <<= (compileInputs in (Compile, compile)) dependsOn (ScalariformKeys.format in Compile),
compileInputs in (Test, compile) <<= (compileInputs in (Test, compile)) dependsOn (ScalariformKeys.format in Test)
)
}
import autoImport._
エラー:
[error] References to undefined settings:
[error]
[error] */test:compile::compileInputs from */test:compile::compileInputs ((com.my.plugins.MyScalariformPlugin.autoImport) MyScalariformPlugin.scala:24)
[error] Did you mean test:compile::compileInputs ?
[error]
[error] */test:scalariformFormat from */test:compile::compileInputs ((com.my.plugins.MyScalariformPlugin.autoImport) MyScalariformPlugin.scala:24)
[error] Did you mean test:scalariformFormat ?
[error]
[error] */compile:compile::compileInputs from */compile:compile::compileInputs ((com.my.plugins.MyScalariformPlugin.autoImport) MyScalariformPlugin.scala:23)
[error] Did you mean compile:compile::compileInputs ?
[error]
[error] */compile:scalariformFormat from */compile:compile::compileInputs ((com.my.plugins.MyScalariformPlugin.autoImport) MyScalariformPlugin.scala:23)
[error] Did you mean compile:scalariformFormat ?
[error]