Skip to content

Commit edd3783

Browse files
committed
Make sbt-github-actions work with Windows crlf line breaks
1 parent 88220b3 commit edd3783

File tree

6 files changed

+59
-60
lines changed

6 files changed

+59
-60
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ jobs:
2626
java: [temurin@11, graal_20.3.1@11]
2727
runs-on: ${{ matrix.os }}
2828
steps:
29-
- name: Ignore line ending differences in git
30-
if: contains(runner.os, 'windows')
31-
shell: bash
32-
run: git config --global core.autocrlf false
33-
3429
- name: Checkout current branch (full)
3530
uses: actions/checkout@v2
3631
with:
@@ -90,10 +85,6 @@ jobs:
9085
java: [temurin@11]
9186
runs-on: ${{ matrix.os }}
9287
steps:
93-
- name: Ignore line ending differences in git
94-
if: contains(runner.os, 'windows')
95-
run: git config --global core.autocrlf false
96-
9788
- name: Checkout current branch (full)
9889
uses: actions/checkout@v2
9990
with:

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ Any and all settings which affect the behavior of the generative plugin should b
122122
- `githubWorkflowJavaVersions` : `Seq[JavaSpec]` – A list of Java versions to be used for the build job. The publish job will use the *first* of these versions. Defaults to `JavaSpec.temurin("11")`).
123123
- `githubWorkflowScalaVersions` : `Seq[String]` – A list of Scala versions which will be used to `build` your project. Defaults to `crossScalaVersions` in `build`, and simply `scalaVersion` in `publish`.
124124
- `githubWorkflowOSes` : `Seq[String]` – A list of operating systems, which will be ultimately passed to [the `runs-on:` directive](https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on), on which to `build` your project. Defaults to `ubuntu-latest`. Note that, regardless of the value of this setting, only `ubuntu-latest` will be used for the `publish` job. This setting only affects `build`.
125+
- `githubWorkflowAutoCrlfWindows` : `Boolean` – Whether sbt-github-actions should configure git to automatically convert crlf (carriage return plus line feed) line ending characters which is used on Windows (using `core.autocrlf`). Note that regardless of what value this is set to, workflows generated by sbt-github-actions will still continue to function. Defaults to true.
125126
- `githubWorkflowBuildRunsOnExtraLabels` : `Seq[String]` - A list of additional runs-on labels, which will be combined with the matrix.os from `githubWorkflowOSes` above allowing for singling out more specific runners.
126127

127128
#### `publish` Job

build.sbt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ ThisBuild / endYear := Some(2021)
2828
ThisBuild / crossScalaVersions := Seq("2.12.15")
2929

3030
ThisBuild / githubWorkflowOSes := Seq("ubuntu-latest", "macos-latest", "windows-latest")
31+
// So we can test that sbt-github-actions still works even with Windows crlf line endings
32+
ThisBuild / githubWorkflowAutoCrlfWindows := false
3133
ThisBuild / githubWorkflowBuild := Seq(WorkflowStep.Sbt(List("test", "scripted")))
3234
ThisBuild / githubWorkflowJavaVersions += JavaSpec.graalvm("20.3.1", "11")
3335

src/main/scala/sbtghactions/GenerativeKeys.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ trait GenerativeKeys {
6060

6161
lazy val githubWorkflowArtifactUpload = settingKey[Boolean]("Controls whether or not to upload target directories in the event that multiple jobs are running sequentially. Can be set on a per-project basis (default: true)")
6262
lazy val githubWorkflowJobSetup = settingKey[Seq[WorkflowStep]]("The automatically-generated checkout, setup, and cache steps which are common to all jobs which touch the build (default: autogenerated)")
63+
lazy val githubWorkflowAutoCrlfWindows = settingKey[Boolean]("Whether to configure git to auto-convert crlf line ends if using Windows as an os (default: true)")
6364

6465
lazy val githubWorkflowEnv = settingKey[Map[String, String]](s"A map of static environment variable assignments global to the workflow (default: { GITHUB_TOKEN: $${{ secrets.GITHUB_TOKEN }} })")
6566
lazy val githubWorkflowAddedJobs = settingKey[Seq[WorkflowJob]]("A list of additional jobs to add to the CI workflow (default: [])")

src/main/scala/sbtghactions/GenerativePlugin.scala

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}
498498
githubWorkflowJavaVersions := Seq(JavaSpec.temurin("11")),
499499
githubWorkflowScalaVersions := crossScalaVersions.value,
500500
githubWorkflowOSes := Seq("ubuntu-latest"),
501+
githubWorkflowAutoCrlfWindows := true,
501502
githubWorkflowDependencyPatterns := Seq("**/*.sbt", "project/build.properties"),
502503
githubWorkflowTargetBranches := Seq("**"),
503504
githubWorkflowTargetTags := Seq(),
@@ -619,7 +620,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}
619620
},
620621

621622
githubWorkflowJobSetup := {
622-
val autoCrlfOpt = if (githubWorkflowOSes.value.exists(_.contains("windows"))) {
623+
val autoCrlfOpt = if (githubWorkflowAutoCrlfWindows.value && githubWorkflowOSes.value.exists(_.contains("windows"))) {
623624
List(
624625
WorkflowStep.Run(
625626
List("git config --global core.autocrlf false"),
@@ -777,7 +778,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}
777778

778779
def compare(file: File, expected: String): Unit = {
779780
val actual = IO.read(file)
780-
if (expected != actual) {
781+
if (removeWindowsLineEndings(expected) != removeWindowsLineEndings(actual)) {
781782
reportMismatch(file, expected, actual)
782783
}
783784
}
@@ -789,8 +790,8 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}
789790
})
790791

791792
private[sbtghactions] def diff(expected: String, actual: String): String = {
792-
val expectedLines = expected.split("\n", -1)
793-
val actualLines = actual.split("\n", -1)
793+
val expectedLines = removeWindowsLineEndings(expected).split("\n", -1)
794+
val actualLines = removeWindowsLineEndings(actual).split("\n", -1)
794795
val (lines, _) = expectedLines.zipAll(actualLines, "", "").foldLeft((Vector.empty[String], false)) {
795796
case ((acc, foundDifference), (expectedLine, actualLine)) if expectedLine == actualLine =>
796797
(acc :+ actualLine, foundDifference)
@@ -816,4 +817,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}
816817
}
817818
lines.mkString("\n")
818819
}
820+
821+
private[sbtghactions] def removeWindowsLineEndings(string: String): String =
822+
string.replaceAll("\\r\\n?","\n")
819823
}

0 commit comments

Comments
 (0)