-
Notifications
You must be signed in to change notification settings - Fork 63
/
SbtScalariform.scala
147 lines (126 loc) · 5.05 KB
/
SbtScalariform.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright 2011-2012 Typesafe Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.typesafe.sbt
import sbt._
import sbt.{IntegrationTest => It}
import sbt.Keys._
import scala.collection.immutable.Seq
import scalariform.formatter.preferences.{
IFormattingPreferences,
FormattingPreferences
}
object SbtScalariform
extends AutoPlugin {
override def requires = plugins.JvmPlugin
override def trigger = allRequirements
object autoImport {
val scalariformFormat = taskKey[Seq[File]]("Format (Scala) sources using scalariform")
val scalariformPreferences = settingKey[IFormattingPreferences]("Scalariform formatting preferences")
val scalariformAutoformat = settingKey[Boolean]("Whether Scala sources should be auto formatted when compile is run (default: true)")
val scalariformDoAutoformat = taskKey[Seq[File]]("Format sources if autoformat is configured")
val scalariformWithBaseDirectory = settingKey[Boolean]("Whether or not to format sources in project root (default: false)")
@deprecated("Use scalariformAutoformat to turn autoformat on or off", "1.8.1")
def scalariformSettings(autoformat: Boolean): Seq[Setting[_]] = Nil
@deprecated("Use scalariformItSettings", "1.8.1")
def scalariformSettingsWithIt(autoformat: Boolean): Seq[Setting[_]] = scalariformItSettings
def scalariformItSettings: Seq[Setting[_]] = inputs(It) ++ inConfig(It)(configScalariformSettings)
}
import autoImport._
override def globalSettings = Seq(
scalariformPreferences := defaultPreferences,
includeFilter in scalariformFormat := "*.scala",
scalariformAutoformat := (
PreferencesFile(None).map(_.autoformat.autoformat).getOrElse(Defaults.autoformat)
),
scalariformWithBaseDirectory := (
PreferencesFile(None).map(_.withBaseDirectory).getOrElse(Defaults.withBaseDirectory)
)
)
override def projectSettings = compileSettings ++
inConfig(Compile)(configScalariformSettings) ++
inConfig(Test)(configScalariformSettings)
object ScalariformKeys {
val format = scalariformFormat
val preferences = scalariformPreferences
val autoformat = scalariformAutoformat
val withBaseDirectory = scalariformWithBaseDirectory
}
private[sbt] object Defaults {
val autoformat = true
val withBaseDirectory = false
}
val defaultPreferences = FormattingPreferences()
private val compileSettings = inputs(Compile) ++ inputs(Test)
private def inputs(config: Configuration): Seq[Setting[_]] = {
val input = compileInputs in (config, compile)
Seq(
input := (input dependsOn (scalariformDoAutoformat in config)).value
)
}
def configScalariformSettings: Seq[Setting[_]] = {
List(
(sourceDirectories in scalariformFormat) :=
unmanagedSourceDirectories.value ++ (
if (!scalariformWithBaseDirectory.value) Seq.empty
else Seq(baseDirectory.in(LocalRootProject).value)
),
scalariformPreferences := getPreferences(scalariformPreferences.value)(None),
scalariformFormat := Scalariform(
getPreferences(scalariformPreferences.value)(Some(streams.value)),
(sourceDirectories in scalariformFormat).value.toList,
(includeFilter in scalariformFormat).value,
(excludeFilter in scalariformFormat).value,
thisProjectRef.value,
configuration.value,
streams.value,
scalaVersion.value
),
scalariformDoAutoformat := Def.taskDyn {
if (scalariformAutoformat.value) {
Def.task {
scalariformFormat.value
}
}
else {
Def.task {
Seq.empty[File]
}
}
}.value
)
}
private val getPreferences = (buildPrefs: IFormattingPreferences) =>
(PreferencesFile(_)).
andThen(_.flatMap(_.prefs).getOrElse(
buildPrefs
))
// DEPRECATIONS
//
@deprecated("use scalariformSettings(autoformat: Boolean)", "1.8.0")
def scalariformSettings: Seq[Setting[_]] = {
autoImport.scalariformSettings(autoformat = true)
}
@deprecated("use scalariformSettingsWithIt(autoformat: Boolean)", "1.8.0")
def scalariformSettingsWithIt: Seq[Setting[_]] = {
autoImport.scalariformSettingsWithIt(autoformat = true)
}
@deprecated("use scalariformSettings(autoformat: Boolean)", "1.8.0")
def defaultScalariformSettings: Seq[Setting[_]] =
autoImport.scalariformSettings(autoformat = true)
@deprecated("use scalariformSettingsWithIt(autoformat: Boolean)", "1.8.0")
def defaultScalariformSettingsWithIt: Seq[Setting[_]] =
autoImport.scalariformSettingsWithIt(autoformat = true)
}