Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.

Removes SBT dependency from core module #633

Merged
merged 2 commits into from
Oct 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions core/src/main/scala/sbtorgpolicies/github/GitHubOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ import com.github.marklister.base64.Base64._
import github4s.Github
import github4s.GithubResponses._
import github4s.free.domain._
// import sbt.io.IO
import sbt.IO
import sbtorgpolicies.exceptions.{GitHubException, IOException, OrgPolicyException}
import sbtorgpolicies.github.config._
import sbtorgpolicies.github.instances._
import sbtorgpolicies.github.syntax._
import sbtorgpolicies.io.syntax._
import sbtorgpolicies.io.{FileReader, IOResult}
import sbtorgpolicies.io.{FileReader, IO, IOResult}

class GitHubOps(owner: String, repo: String, accessToken: Option[String]) {

Expand Down
6 changes: 3 additions & 3 deletions core/src/main/scala/sbtorgpolicies/io/FileHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

package sbtorgpolicies.io

import java.io.File
import java.net.URL

import cats.instances.either._
import cats.instances.list._
import cats.syntax.cartesian._
import cats.syntax.either._
import cats.syntax.foldable._
import cats.syntax.traverse._
import cats.syntax.traverseFilter._
// import sbt.io._
// import sbt.io.syntax._
import sbt.{File, URL}
import sbtorgpolicies.exceptions.IOException
import sbtorgpolicies.io.syntax._
import sbtorgpolicies.templates._
Expand Down
7 changes: 2 additions & 5 deletions core/src/main/scala/sbtorgpolicies/io/FileReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ package sbtorgpolicies.io
import java.io.File

import cats.syntax.either._
// import sbt.io._
// import sbt.io.syntax._
import sbt.{file, IO}
import sbtorgpolicies.exceptions._
import sbtorgpolicies.io.syntax._

Expand All @@ -31,14 +28,14 @@ class FileReader {

def exists(path: String): Boolean =
Either
.catchNonFatal(file(path).exists()) getOrElse false
.catchNonFatal(IO.file(path).exists()) getOrElse false

def withFileContent[T](filePath: String, f: String => IOResult[T]): IOResult[T] =
getFileContent(filePath) flatMap f

def getFileContent(filePath: String): IOResult[String] =
Either
.catchNonFatal(IO.readLines(file(filePath)).mkString("\n"))
.catchNonFatal(IO.readLines(IO.file(filePath)).mkString("\n"))
.leftMap(e => IOException(s"Error loading $filePath content", Some(e)))

def getFileBytes(file: File): IOResult[Array[Byte]] =
Expand Down
8 changes: 3 additions & 5 deletions core/src/main/scala/sbtorgpolicies/io/FileWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package sbtorgpolicies.io

import java.io.{File, FileOutputStream}
import java.net.URL
import java.nio.file.Files.{copy => fcopy}
import java.nio.file.Path
import java.nio.file.StandardCopyOption.REPLACE_EXISTING
Expand All @@ -26,9 +27,6 @@ import cats.instances.either._
import cats.instances.list._
import cats.syntax.either._
import cats.syntax.traverse._
// import sbt.io._
// import sbt.io.syntax._
import sbt.{file, File, IO, URL}
import sbtorgpolicies.exceptions._
import sbtorgpolicies.io.syntax._

Expand All @@ -38,7 +36,7 @@ class FileWriter {

def writeContentToFile(content: String, output: String): IOResult[Unit] = {

def writeFile: Either[Throwable, Unit] = Either.catchNonFatal(IO.write(file(output), content))
def writeFile: Either[Throwable, Unit] = Either.catchNonFatal(IO.write(IO.file(output), content))

(for {
result <- createFile(output)
Expand Down Expand Up @@ -103,7 +101,7 @@ class FileWriter {

val buffer = new Array[Byte](1024)
Stream.continually(zipIn.getNextEntry).takeWhile(_ != null).foreach { entry =>
val newFile = new File(output + File.separator + entry.getName)
val newFile = IO.file(output + File.separator + entry.getName)

(
entry.isDirectory,
Expand Down
52 changes: 37 additions & 15 deletions core/src/main/scala/sbtorgpolicies/io/ReplaceTextEngine.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,20 @@ class ReplaceTextEngine {
in: List[File],
isFileSupported: (File) => Boolean): IOResult[List[ProcessedFile]] =
fileReader.fetchFilesRecursively(in, isFileSupported) map { files =>
files.map(replaceBlocksInFile(startBlockRegex, endBlockRegex, replacements, _))
files.map(replaceBlocksInFile(Some(startBlockRegex), Some(endBlockRegex), replacements, _))
}

final def replaceTexts(
replacements: Map[String, String],
in: List[File],
isFileSupported: (File) => Boolean): IOResult[List[ProcessedFile]] =
fileReader.fetchFilesRecursively(in, isFileSupported) map { files =>
files.map(replaceBlocksInFile(None, None, replacements, _))
}

private[this] def replaceBlocksInFile(
startBlockRegex: Regex,
endBlockRegex: Regex,
startBlockRegex: Option[Regex],
endBlockRegex: Option[Regex],
replacements: Map[String, String],
in: File): ProcessedFile = {

Expand All @@ -66,25 +74,39 @@ class ReplaceTextEngine {
@tailrec
private[this] final def replaceContent(
unprocessed: String,
startBlockRegex: Regex,
endBlockRegex: Regex,
startBlockRegex: Option[Regex],
endBlockRegex: Option[Regex],
replacements: Map[String, String],
replaced: String = ""): String = {

def tryToReplace: Option[(String, Int)] =
(startBlockRegex.findFirstMatchIn(unprocessed), endBlockRegex.findFirstMatchIn(unprocessed)) match {
case class TextBlock(startEnd: Int, text: String, endStart: Int, endEnd: Int)

def textBetween(startR: Regex, endR: Regex): Option[TextBlock] =
(startR.findFirstMatchIn(unprocessed), endR.findFirstMatchIn(unprocessed)) match {
case (Some(startMatch), Some(endMatch)) if startMatch.end < endMatch.start =>
val textToBeReplaced = unprocessed.subStr(startMatch.end, endMatch.start)
val replaced = replacements.foldLeft(textToBeReplaced) {
case (text, (target, replacement)) => text.replaceAll(target, replacement)
}
val newContent = unprocessed.subStr(0, startMatch.end) + replaced + unprocessed.subStr(
endMatch.start,
endMatch.end)
Some((newContent, endMatch.end))
Some(
TextBlock(startMatch.end, unprocessed.subStr(startMatch.end, endMatch.start), endMatch.start, endMatch.end))
case _ => None
}

def textToReplace: Option[TextBlock] =
(unprocessed.trim.nonEmpty, startBlockRegex, endBlockRegex) match {
case (true, Some(startR), Some(endR)) => textBetween(startR, endR)
case (true, None, None) => Some(TextBlock(0, unprocessed, unprocessed.length, unprocessed.length))
case _ => None
}

def tryToReplace: Option[(String, Int)] =
textToReplace.map { textBlock =>
val replaced = replacements.foldLeft(textBlock.text) {
case (text, (target, replacement)) => text.replaceAll(target, replacement)
}
val newContent = unprocessed.subStr(0, textBlock.startEnd) + replaced + unprocessed.subStr(
textBlock.endStart,
textBlock.endEnd)
(newContent, textBlock.endEnd)
}

tryToReplace match {
case None => replaced + unprocessed
case Some((replacedBlock, endPosition)) =>
Expand Down
53 changes: 48 additions & 5 deletions core/src/main/scala/sbtorgpolicies/io/io.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

package sbtorgpolicies

import java.io.File
import java.io._
import java.net.URL
import java.nio.charset.Charset
import java.nio.file.Path
import java.nio.file.Paths.get

import cats.syntax.either._
// import sbt.io._
// import sbt.io.syntax._
import sbt.{file, File}
import sbtorgpolicies.exceptions.IOException

import scala.io.Source
import scala.language.implicitConversions

package object io {
Expand Down Expand Up @@ -53,7 +53,7 @@ package object io {

def toPath: Path = get(filename)

def toFile: File = file(filename.fixPath)
def toFile: File = new File(filename.fixPath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IO.file to be consistent?


def fixPath: String = filename.replaceAll("/", File.separator)

Expand All @@ -63,4 +63,47 @@ package object io {
else File.separator)
}
}

object IO {

def file(path: String): File = new File(path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IO.file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inception?


def url(address: String): URL = new URL(address)

def readLines(file: File): Iterator[String] =
Source.fromFile(file).getLines()

def readBytes(file: File): Array[Byte] = {
val is: InputStream = new FileInputStream(file)
val array: Array[Byte] = Stream.continually(is.read).takeWhile(_ != -1).map(_.toByte).toArray
is.close()
array
}

def write(file: File, content: String, charset: Charset = Charset.forName("UTF-8")): Unit = {
val writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), charset))
writer.write(content)
writer.close()
}

def relativize(base: File, file: File): Option[String] = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


def ensureEndingSlash: Option[String] = {
val path = base.getAbsolutePath
path.lastOption.map {
case c if c == File.separatorChar => path
case _ => path + File.separatorChar
}
}

val baseFileString = if (base.isDirectory) ensureEndingSlash else None
val pathString = file.getAbsolutePath
baseFileString flatMap {
case baseString if pathString.startsWith(baseString) =>
Some(pathString.substring(baseString.length))
case _ => None
}
}

}
}
25 changes: 6 additions & 19 deletions core/src/main/scala/sbtorgpolicies/model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@

package sbtorgpolicies

import java.net.URL

import net.jcazevedo.moultingyaml._
import sbt.Append.Value
// import sbt.io._
// import sbt.io.syntax._
import sbt.{url, URL}
import sbtorgpolicies.runnable.RunnableItemConfigScope
import sbtorgpolicies.runnable.syntax._
import sbtorgpolicies.io.IO

object model {

Expand All @@ -32,10 +29,10 @@ object model {
}

/** Apache 2.0 License.*/
case object ApacheLicense extends License("Apache License", url("http://www.apache.org/licenses/LICENSE-2.0.txt"))
case object ApacheLicense extends License("Apache License", IO.url("http://www.apache.org/licenses/LICENSE-2.0.txt"))

/** MIT License.*/
case object MITLicense extends License("MIT", url("http://opensource.org/licenses/MIT"))
case object MITLicense extends License("MIT", IO.url("http://opensource.org/licenses/MIT"))

/** Custom License. */
class CustomLicense(name: String, url: URL) extends License(name, url)
Expand All @@ -48,11 +45,6 @@ object model {

}

implicit val settingAppender: Value[Seq[(String, java.net.URL)], License] =
new Value[Seq[(String, URL)], License] {
override def appendValue(a: Seq[(String, URL)], b: License): Seq[(String, URL)] = a :+ b.tupled
}

lazy val scoverageMinimum = 80d

lazy val scalacCommonOptions = Seq(
Expand Down Expand Up @@ -90,11 +82,6 @@ object model {
/** Combines all scalac options.*/
lazy val scalacAllOptions: Seq[String] = scalacCommonOptions ++ scalacLanguageOptions ++ scalacStrictOptions

/**
* Alias helper for the publishMicrosite task when docs module is located in the "docs" sbt module.
*/
lazy val defaultPublishMicrosite: RunnableItemConfigScope[Unit] = ";project docs;publishMicrosite".asRunnableItem

/** Github settings and related settings usually found in a Github README.*/
case class GitHubSettings(
organization: String,
Expand All @@ -104,7 +91,7 @@ object model {
organizationHomePage: URL,
organizationEmail: String) {
def home: String = s"https://github.com/$organization/$project"
def homePage: URL = url(s"https://$organization.github.io/$project/")
def homePage: URL = IO.url(s"https://$organization.github.io/$project/")
def repo: String = s"git@github.com:$organization/$project.git"
def api: String = s"https://$organization.github.io/$project/api/"
def organisation: String = s"com.github.$organization"
Expand Down
12 changes: 12 additions & 0 deletions src/main/scala/sbtorgpolicies/settings/AllSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import com.typesafe.sbt.pgp.PgpKeys
import com.typesafe.sbt.pgp.PgpKeys._
import dependencies.DependenciesPlugin
import dependencies.DependenciesPlugin.autoImport._
import sbtorgpolicies.runnable.RunnableItemConfigScope
import sbtorgpolicies.runnable.syntax._
import microsites.MicrositeKeys._
import scoverage.ScoverageKeys
import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport._
Expand Down Expand Up @@ -156,6 +158,11 @@ trait AllSettings
scalacOptions ++= scalacAllOptions
)

implicit val settingAppender: sbt.Append.Value[Seq[(String, java.net.URL)], License] =
new sbt.Append.Value[Seq[(String, URL)], License] {
override def appendValue(a: Seq[(String, URL)], b: License): Seq[(String, URL)] = a :+ b.tupled
}

/**
* Publish settings common to all projects.
*
Expand Down Expand Up @@ -245,6 +252,11 @@ trait AllSettings
depGithubTokenSetting := getEnvVar(orgGithubTokenSetting.value)
)

/**
* Alias helper for the publishMicrosite task when docs module is located in the "docs" sbt module.
*/
lazy val defaultPublishMicrosite: RunnableItemConfigScope[Unit] = ";project docs;publishMicrosite".asRunnableItem

/**
* Sets the default properties for the sbt-microsites plugin.
*
Expand Down