-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature/dynamicversion: added support for remote config files
- Loading branch information
1 parent
1832499
commit 4a04808
Showing
13 changed files
with
311 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
version = "1.5.1" | ||
maxColumn = 120 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/scala/org/antipathy/mvn_scalafmt/builder/LocalConfigBuilder.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.antipathy.mvn_scalafmt.builder | ||
|
||
import java.nio.file.Path | ||
|
||
import org.antipathy.mvn_scalafmt.io.{Reader, Writer} | ||
import org.antipathy.mvn_scalafmt.model.RemoteConfig | ||
import org.antipathy.mvn_scalafmt.validation.Validator | ||
import org.apache.commons.validator.routines.UrlValidator | ||
import org.antipathy.mvn_scalafmt.validation.ConfigFileValidator | ||
import org.apache.maven.plugin.logging.Log | ||
import org.antipathy.mvn_scalafmt.io.{RemoteConfigReader, RemoteConfigWriter} | ||
|
||
/** | ||
* Class for building a local config from a remote location and validating the path is correct | ||
* @param urlValidator Class for validating if string is a valid url | ||
* @param configValidator Class for validating a local config's path | ||
* @param remoteConfigReader Class for reading a remote config | ||
* @param remoteConfigWriter Class for writing a remote config to a local path | ||
* @param log The maven logger | ||
*/ | ||
class LocalConfigBuilder( | ||
urlValidator: UrlValidator, | ||
configValidator: Validator[String, Path], | ||
remoteConfigReader: Reader[String, RemoteConfig], | ||
remoteConfigWriter: Writer[RemoteConfig], | ||
log: Log | ||
) extends Builder[String, Path] { | ||
|
||
/** | ||
* Read an object from the specified location | ||
* | ||
* @param location The location to read from | ||
* @return The object at the location | ||
*/ | ||
override def build(location: String): Path = | ||
if (urlValidator.isValid(location)) { | ||
val remoteConfig = remoteConfigReader.read(location) | ||
remoteConfigWriter.write(remoteConfig) | ||
configValidator.validate(remoteConfig.location.toAbsolutePath.toString) | ||
} else { | ||
configValidator.validate(location) | ||
} | ||
} | ||
|
||
object LocalConfigBuilder { | ||
|
||
def apply( | ||
log: Log | ||
): LocalConfigBuilder = | ||
new LocalConfigBuilder( | ||
new UrlValidator(Array("http", "https")), | ||
new ConfigFileValidator(log), | ||
new RemoteConfigReader(log), | ||
new RemoteConfigWriter(log), | ||
log | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.antipathy.mvn_scalafmt.io | ||
|
||
/** | ||
* Base trait for reading | ||
* @tparam I The input type | ||
* @tparam O The output type | ||
*/ | ||
trait Reader[I, O] { | ||
|
||
/** | ||
* Read an object from the specified location | ||
* @param location The location to read from | ||
* @return The object at the location | ||
*/ | ||
def read(location: I): O | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/scala/org/antipathy/mvn_scalafmt/io/RemoteConfigReader.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.antipathy.mvn_scalafmt.io | ||
|
||
import java.net.URL | ||
|
||
import org.antipathy.mvn_scalafmt.model.RemoteConfig | ||
import org.apache.maven.plugin.logging.Log | ||
import scala.util.{Failure, Success, Try} | ||
|
||
/** | ||
* Class for retrieving a config from a remote location | ||
* | ||
* @param log The maven logger | ||
*/ | ||
class RemoteConfigReader(log: Log) extends Reader[String, RemoteConfig] { | ||
|
||
/** | ||
* Read an object from the specified location | ||
* | ||
* @param location The url to read from | ||
* @return A remote config | ||
*/ | ||
override def read(location: String): RemoteConfig = | ||
Try { | ||
log.info(s"Reading config from $location") | ||
RemoteConfig( | ||
contents = scala.io.Source.fromURL(new URL(location)).mkString | ||
) | ||
} match { | ||
case Success(value) => value | ||
case Failure(exception) => | ||
log.error(s"error retrieving remote config: ${exception.getMessage}", exception) | ||
throw exception | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/scala/org/antipathy/mvn_scalafmt/io/RemoteConfigWriter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.antipathy.mvn_scalafmt.io | ||
|
||
import org.antipathy.mvn_scalafmt.model.RemoteConfig | ||
import java.io.File | ||
import java.nio.charset.StandardCharsets | ||
import org.apache.commons.io.FileUtils | ||
import org.apache.maven.plugin.logging.Log | ||
import java.nio.file.Files | ||
|
||
/** | ||
* Class for writing a remote config to a local path | ||
* @param log The maven logger | ||
*/ | ||
class RemoteConfigWriter(log: Log) extends Writer[RemoteConfig] { | ||
|
||
/** | ||
* Write the passed in remote config to a local file | ||
* | ||
* @param input The input to write | ||
*/ | ||
override def write(input: RemoteConfig): Unit = { | ||
|
||
log.info(s"Writing remote config to ${input.location.toAbsolutePath}") | ||
|
||
if (Files.exists(input.location)) { | ||
Files.delete(input.location) | ||
} | ||
|
||
FileUtils.writeStringToFile( | ||
new File(input.location.toAbsolutePath.toString), | ||
input.contents, | ||
StandardCharsets.UTF_8 | ||
) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/scala/org/antipathy/mvn_scalafmt/model/RemoteConfig.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.antipathy.mvn_scalafmt.model | ||
|
||
import java.nio.file.{Path, Paths} | ||
|
||
/** | ||
* Container class for a remote scalafmt config | ||
* | ||
* @param contents The contents of the config | ||
* @param location The local path where the config will be stored | ||
*/ | ||
case class RemoteConfig(contents: String, location: Path = Paths.get(".scalafmt.conf")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/test/scala/org/antipathy/mvn_scalafmt/builder/LocalConfigBuilderSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.antipathy.mvn_scalafmt.builder | ||
|
||
import org.scalatest.{FlatSpec, GivenWhenThen, Matchers} | ||
import org.apache.maven.plugin.logging.SystemStreamLog | ||
import java.io.File | ||
|
||
class LocalConfigBuilderSpec extends FlatSpec with GivenWhenThen with Matchers { | ||
|
||
behavior of "LocalConfigBuilder" | ||
|
||
it should "Ensure a local config is correct" in { | ||
|
||
val builder = LocalConfigBuilder(new SystemStreamLog) | ||
val path = ".scalafmt.conf" | ||
|
||
val resultPath = builder.build(path) | ||
|
||
resultPath.toString should be(path) | ||
} | ||
|
||
it should "Retrieve a remote config and store it locally" in { | ||
|
||
val builder = LocalConfigBuilder(new SystemStreamLog) | ||
val path = "https://raw.githubusercontent.com/SimonJPegg/mvn_scalafmt/master/.scalafmt.conf" | ||
val expectedContent = scala.io.Source | ||
.fromURL("https://raw.githubusercontent.com/SimonJPegg/mvn_scalafmt/master/.scalafmt.conf") | ||
.mkString | ||
|
||
builder.build(path) | ||
|
||
val result = scala.io.Source.fromFile(new File(".scalafmt.conf")).getLines().mkString | ||
|
||
result.trim should be(expectedContent.trim) | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/test/scala/org/antipathy/mvn_scalafmt/io/RemoteConfigReaderSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.antipathy.mvn_scalafmt.io | ||
|
||
import org.scalatest.{FlatSpec, GivenWhenThen, Matchers} | ||
import org.apache.maven.plugin.logging.SystemStreamLog | ||
import java.net.MalformedURLException | ||
|
||
class RemoteConfigReaderSpec extends FlatSpec with GivenWhenThen with Matchers { | ||
|
||
behavior of "RemoteConfigReader" | ||
|
||
it should "read a config from a remote location" in { | ||
|
||
val url = | ||
"https://raw.githubusercontent.com/SimonJPegg/mvn_scalafmt/35f3863c501b43beb59d84cb49fe124ee99c70a5/.scalafmt.conf" | ||
val reader = new RemoteConfigReader(new SystemStreamLog) | ||
val expectedResult = "maxColumn = 120\n" | ||
|
||
reader.read(url).contents should be(expectedResult) | ||
} | ||
|
||
it should "raise an exception when unable to retrieve a config" in { | ||
val url = "Skyrim belongs to the Nords" | ||
val reader = new RemoteConfigReader(new SystemStreamLog) | ||
|
||
an[MalformedURLException] should be thrownBy { | ||
reader.read(url) | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.