Skip to content

Commit

Permalink
remove hack of ignoring the scripts dir
Browse files Browse the repository at this point in the history
  • Loading branch information
fhackett committed Nov 8, 2024
1 parent 8aa7fe7 commit ef741b8
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 91 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ jobs:
with:
jvm: temurin:21
- run: scala-cli format --check .
- run: scala-cli format --check scripts/
license-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: VirtusLab/scala-cli-setup@main
with:
jvm: temurin:21
- run: scala-cli run scripts/updateLicense.scala
- run: ./scripts/updateLicense.sc
2 changes: 0 additions & 2 deletions project.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
//> using dep dev.zio::izumi-reflect:2.3.10
//> using test.dep org.scalameta::munit:1.0.2

//> using exclude scripts/

//> using javaProp distcompiler.Node.assertErrorRefCorrectness=no

// discarded flags: -Yrequire-targetName
92 changes: 92 additions & 0 deletions scripts/updateLicense.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env -S scala-cli shebang
// Copyright 2024 DCal Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//> using dep com.lihaoyi::os-lib:0.11.3
import scala.util.matching.Regex
import java.time.LocalDate
import java.time.format.DateTimeFormatter

final case class Options(
check: Boolean = true
)

val shouldRewrite =
args match
case Array("rewrite") => true
case Array("dry-run") | Array() => false
case _ =>
println(
s"unrecognized command-line arguments: ${args.map(arg => s"`$arg`").mkString(", ")}"
)
System.exit(1)
false

val licenseTemplate =
""" Copyright ____ DCal Team
|
| Licensed under the Apache License, Version 2.0 (the "License");
| you may not use this file except in compliance with the License.
| You may obtain a copy of the License at
|
| http://www.apache.org/licenses/LICENSE-2.0
|
| Unless required by applicable law or agreed to in writing, software
| distributed under the License is distributed on an "AS IS" BASIS,
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
| See the License for the specific language governing permissions and
| limitations under the License.""".stripMargin.linesIterator
.map(line => s"//$line")
.mkString(System.lineSeparator())

val yearString = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy"))
val licenseText = licenseTemplate.replace("____", yearString)
val licenseRegex = Regex(
raw"""(?s)// Copyright \d\d\d\d.*// limitations under the License\."""
)
val licenseReplacement = Regex.quoteReplacement(licenseText)

var checkFailed = false

os.walk(os.pwd)
.iterator
.filterNot(_.segments.exists(_.startsWith(".")))
.filter(_.last.endsWith(".scala"))
.foreach: sourceFile =>
val contents = os.read(sourceFile)

val modifiedContents =
licenseRegex.findFirstIn(contents) match
case None =>
licenseText
++ System.lineSeparator()
++ System.lineSeparator()
++ contents
case Some(str) =>
licenseRegex.replaceFirstIn(contents, licenseReplacement)

if shouldRewrite
then os.write.over(sourceFile, modifiedContents)
else if contents != modifiedContents
then
checkFailed = true
println(s"license needs updating in $sourceFile")

if shouldRewrite
then println("all changes made.")
else if checkFailed
then
println("check failed. TODO: update license info")
System.exit(1)
else println("check ok, all licenses up to date.")
87 changes: 0 additions & 87 deletions scripts/updateLicense.scala

This file was deleted.

0 comments on commit ef741b8

Please sign in to comment.