Skip to content

Commit f6e6c04

Browse files
authored
Merge pull request #22 from 2m/wip-write-authors-to-file-2m
New task that writes authors summary to file
2 parents f30601c + df435e2 commit f6e6c04

File tree

5 files changed

+59
-25
lines changed

5 files changed

+59
-25
lines changed

plugin/src/main/scala/AuthorsKeys.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ package lt.dvim.authors
33
import sbt._
44

55
trait AuthorsKeys {
6-
val authors = inputKey[Unit]("Generate authors report.")
6+
val authors = inputKey[Unit]("Generate authors report to clipboard.")
7+
val authorsFile = inputKey[File]("Generate authors report to a file.")
78
}

plugin/src/main/scala/AuthorsPlugin.scala

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,24 @@ package lt.dvim.authors
22

33
import sbt._
44
import sbt.Keys._
5-
import complete.DefaultParsers._
5+
import sbt.complete.DefaultParsers._
66

7-
import scala.concurrent.Await
7+
import scala.concurrent.{Await, Future}
88
import scala.concurrent.duration._
99

1010
object AuthorsPlugin extends AutoPlugin {
1111
object autoImport extends AuthorsKeys
1212
import autoImport._
1313

1414
override def trigger = AllRequirements
15-
override def projectSettings: Seq[Setting[_]] = authorsSettings(Compile)
15+
override def buildSettings: Seq[Setting[_]] = authorsBuildSettings
1616

17-
def authorsSettings(config: Configuration): Seq[Setting[_]] = authorsGlobalSettings ++ inConfig(config)(Seq())
17+
private final val ArgsParser = spaceDelimited("<from> <to>")
1818

19-
def authorsGlobalSettings: Seq[Setting[_]] = Seq(
19+
def authorsBuildSettings: Seq[Setting[_]] = Seq(
2020
authors := {
21-
val (from, to) = spaceDelimited("<from> <to>").parsed match {
22-
case Seq(from, to) => (from, to)
23-
case _ =>
24-
sys.error("Please specify the <from> and <to> tags as the arguments to the task.")
25-
}
26-
27-
// get the org/repo name from the scm info
28-
val repo = scmInfo.value.map(_.browseUrl.getPath.drop(1)).getOrElse {
29-
sys.error("Please set the scmInfo setting.")
30-
}
31-
32-
streams.value.log.info(s"Fetching authors summary for $repo between $from and $to")
33-
3421
import scala.concurrent.ExecutionContext.Implicits.global
35-
val summary = Authors
36-
.summary(repo, from, to, baseDirectory.value.getAbsolutePath)
22+
val summary = authorsSummary(ArgsParser.parsed, scmInfo.value, baseDirectory.value, streams.value)
3723
.map { s =>
3824
import java.awt.Toolkit
3925
import java.awt.datatransfer._
@@ -46,6 +32,41 @@ object AuthorsPlugin extends AutoPlugin {
4632

4733
Await.result(summary, 30.seconds)
4834
},
49-
aggregate in authors := false
35+
authors / aggregate := false,
36+
authorsFile := {
37+
import scala.concurrent.ExecutionContext.Implicits.global
38+
val summary = authorsSummary(ArgsParser.parsed, scmInfo.value, baseDirectory.value, streams.value)
39+
.map { s =>
40+
val file = baseDirectory.value / "target" / "authors.md"
41+
IO.write(file, s)
42+
streams.value.log.info(s"Authors summary written to ${file.getAbsoluteFile}")
43+
file
44+
}
45+
46+
Await.result(summary, 30.seconds)
47+
},
48+
authorsFile / aggregate := false
5049
)
50+
51+
private def authorsSummary(
52+
args: Seq[String],
53+
scmInfo: Option[ScmInfo],
54+
baseDirectory: File,
55+
streams: TaskStreams
56+
): Future[String] = {
57+
val (from, to) = args match {
58+
case Seq(from, to) => (from, to)
59+
case _ =>
60+
sys.error("Please specify the <from> and <to> tags as the arguments to the task.")
61+
}
62+
63+
// get the org/repo name from the scm info
64+
val repo = scmInfo.map(_.browseUrl.getPath.drop(1)).getOrElse {
65+
sys.error("Please set the scmInfo setting.")
66+
}
67+
68+
streams.log.info(s"Fetching authors summary for $repo between $from and $to")
69+
70+
Authors.summary(repo, from, to, baseDirectory.getAbsolutePath)
71+
}
5172
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
scmInfo := Some(ScmInfo(
1+
ThisBuild / scmInfo := Some(ScmInfo(
22
url("https://github.com/2m/authors"),
3-
"git@github.com:2m/authors.git"))
3+
"git@github.com:2m/authors.git"))
4+
5+
ThisBuild / baseDirectory := baseDirectory.value / "target" / "authors"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
| Author | Commits | Lines added | Lines removed |
3+
| ------ | ------- | ----------- | ------------- |
4+
| [<img width="20" alt="2m" src="https://avatars3.githubusercontent.com/u/422086?v=4&amp;s=40"/> **2m**](https://github.com/2m) | 11 | 130 | 94 |
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
> authors v0.1 v0.2
1+
# Will be using local clone of the repo to test against
2+
$ exec git clone https://github.com/2m/authors.git target/authors
3+
4+
> authors v1.0.0 v1.0.1
5+
6+
> authorsFile v1.0.0 v1.0.1
7+
$ must-mirror target/authors/target/authors.md expected-authors.md

0 commit comments

Comments
 (0)