Skip to content

Commit

Permalink
Make sbt-github-actions work with Windows crlf line breaks
Browse files Browse the repository at this point in the history
  • Loading branch information
mdedetrich committed Dec 1, 2022
1 parent 88220b3 commit edd3783
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 60 deletions.
9 changes: 0 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ jobs:
java: [temurin@11, graal_20.3.1@11]
runs-on: ${{ matrix.os }}
steps:
- name: Ignore line ending differences in git
if: contains(runner.os, 'windows')
shell: bash
run: git config --global core.autocrlf false

- name: Checkout current branch (full)
uses: actions/checkout@v2
with:
Expand Down Expand Up @@ -90,10 +85,6 @@ jobs:
java: [temurin@11]
runs-on: ${{ matrix.os }}
steps:
- name: Ignore line ending differences in git
if: contains(runner.os, 'windows')
run: git config --global core.autocrlf false

- name: Checkout current branch (full)
uses: actions/checkout@v2
with:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Any and all settings which affect the behavior of the generative plugin should b
- `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")`).
- `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`.
- `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`.
- `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.
- `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.

#### `publish` Job
Expand Down
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ ThisBuild / endYear := Some(2021)
ThisBuild / crossScalaVersions := Seq("2.12.15")

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

Expand Down
1 change: 1 addition & 0 deletions src/main/scala/sbtghactions/GenerativeKeys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ trait GenerativeKeys {

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)")
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)")
lazy val githubWorkflowAutoCrlfWindows = settingKey[Boolean]("Whether to configure git to auto-convert crlf line ends if using Windows as an os (default: true)")

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 }} })")
lazy val githubWorkflowAddedJobs = settingKey[Seq[WorkflowJob]]("A list of additional jobs to add to the CI workflow (default: [])")
Expand Down
12 changes: 8 additions & 4 deletions src/main/scala/sbtghactions/GenerativePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}
githubWorkflowJavaVersions := Seq(JavaSpec.temurin("11")),
githubWorkflowScalaVersions := crossScalaVersions.value,
githubWorkflowOSes := Seq("ubuntu-latest"),
githubWorkflowAutoCrlfWindows := true,
githubWorkflowDependencyPatterns := Seq("**/*.sbt", "project/build.properties"),
githubWorkflowTargetBranches := Seq("**"),
githubWorkflowTargetTags := Seq(),
Expand Down Expand Up @@ -619,7 +620,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}
},

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

def compare(file: File, expected: String): Unit = {
val actual = IO.read(file)
if (expected != actual) {
if (removeWindowsLineEndings(expected) != removeWindowsLineEndings(actual)) {
reportMismatch(file, expected, actual)
}
}
Expand All @@ -789,8 +790,8 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}
})

private[sbtghactions] def diff(expected: String, actual: String): String = {
val expectedLines = expected.split("\n", -1)
val actualLines = actual.split("\n", -1)
val expectedLines = removeWindowsLineEndings(expected).split("\n", -1)
val actualLines = removeWindowsLineEndings(actual).split("\n", -1)
val (lines, _) = expectedLines.zipAll(actualLines, "", "").foldLeft((Vector.empty[String], false)) {
case ((acc, foundDifference), (expectedLine, actualLine)) if expectedLine == actualLine =>
(acc :+ actualLine, foundDifference)
Expand All @@ -816,4 +817,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}
}
lines.mkString("\n")
}

private[sbtghactions] def removeWindowsLineEndings(string: String): String =
string.replaceAll("\\r\\n?","\n")
}
Loading

0 comments on commit edd3783

Please sign in to comment.