Skip to content

Commit

Permalink
More release job fixes (#26)
Browse files Browse the repository at this point in the history
* More fixes for the release jobs
  • Loading branch information
aishfenton authored Jan 11, 2023
1 parent 574082a commit a1fd26b
Show file tree
Hide file tree
Showing 14 changed files with 331 additions and 427 deletions.
19 changes: 8 additions & 11 deletions .github/ci_scripts/currentVersion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,19 @@ def extractVersion(buildContent: String): Option[SemVer] =
SemVer(major.toInt, minor.toInt, patch.toInt)
}

def currentVersion(buildFile: String, cwd: os.Path, console: Console): Unit =
/** Return the current version number from the build file
*
* @param buildFile
* Path to build.sbt
*/
@main def currentVersion(buildFile: String): Unit =

val buildPath = os.Path(buildFile, cwd)
val buildPath = os.Path(buildFile, os.pwd)
require(os.exists(buildPath), s"$buildPath file wasn't found")

val content = os.read(buildPath)
extractVersion(content) match
case Some(semVer) =>
console.println(semVer.asString)
println(semVer.asString)
case None =>
throw new Exception(s"Didn't find version in build file: $content")

/** Return the current version number from the build file
*
* @param buildFile
* Path to build.sbt
*/
@main def currentVersion(buildFile: String): Unit =
runWith(currentVersion(buildFile, _, _))
32 changes: 0 additions & 32 deletions .github/ci_scripts/project.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,35 +70,3 @@ def sed(

if didReplace then Some(lines.mkString("\n")) else None

trait Console:
def print(s: String): Unit
def println(s: String): Unit
def error(s: String): Unit
def errorln(s: String): Unit

def outLines: List[String]
def errLines: List[String]

def printOuts: Unit = System.out.print(outLines.mkString)
def printErrs: Unit = System.err.print(errLines.mkString)

object Console:
def default: Console = new ConsoleImpl

private class ConsoleImpl extends Console:
private val outBuffer: ListBuffer[String] = ListBuffer.empty
private val errBuffer: ListBuffer[String] = ListBuffer.empty

def print(str: String): Unit = outBuffer.append(str)
def println(str: String): Unit = outBuffer.append(s"$str\n")
def error(str: String): Unit = errBuffer.append(str)
def errorln(str: String): Unit = errBuffer.append(s"$str\n")

def outLines: List[String] = outBuffer.toList
def errLines: List[String] = errBuffer.toList

def runWith[A](fn: (os.Path, Console) => Unit): Unit =
val console = Console.default
fn(os.pwd, console)
console.printOuts
console.printErrs
76 changes: 1 addition & 75 deletions .github/ci_scripts/project.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,78 +49,4 @@ lazy dog
assertEquals(result2, None)
}

}

object FixtureData:

val ArtifactShas: Map[String, String] = Map(
"bazel-bsp-linux-x86.sha256" -> "83da2ffc0ab594a348f7828888d9e4c761ec128c38b1f013434f51f258cd6b9f",
"bazel-bsp-macos-x86.sha256" -> "2d450d6cb18c8e0f436389ff0fd439694336276f53966e6d01206de4bc1f376f",
"bazel_rules.tar.gz.sha256" -> "11c6fd27bc6cb5d4194dffe772b285a22dc5355a7c64ed53bea5c735ac9ddf04"
)

val BazelRule = raw"""
load("//private:load_tool.bzl", "load_tool")

# <--- Updated automatically by release job
_bazel_bsp_version = "0.0.19"
_build_artifact_shas = {
"linux-x86": "a3453ktnegakjdngf43yt334g34g3g34g34g",
"amiga-m68000": "34534lsbflsldfb43mh34hlmbrebml34h34h",
}
# --->

def _bazel_bsp_load(platform):
name = "bazel-bsp-{}".format(platform)
...

def bazel_bsp_setup():
_bazel_bsp_load("linux-x86")
_bazel_bsp_load("macos-x86")
""".trim

val Readme = """
# Bazel BSP
Current Version: [0.0.19](https://github.com/aishfenton/bazel-bsp/releases/tag/v0.0.19)
```starlark
bazel_bsp_version = "0.0.19"
http_archive(
name = "bazel-bsp-rules",
sha256 = "5da0e3c951a7ea50be908d3b97bf2f0b2da8713b99c6035e826dfce3302d5b39",
strip_prefix = "bazel_rules",
type = "tar.gz",
url = "https://github.com/aishfenton/bazel-bsp/releases/download/v{}/bazel_rules.tar.gz" % bazel_bsp_version,
)
load("@bazel-bsp-rules//bazel_rules:bazel_bsp_setup.bzl", "bazel_bsp_setup")
bazel_bsp_setup()
```
More blah blah blah
""".trim

val BuildFile = """
val scala3Version = "3.2.1"
// <--- Updated automatically by release job
val bazelBspVersion = "0.0.19"
// --->
lazy val root = project
.in(file("."))
.settings(
name := "bazel-bsp",
organization := "afenton",
version := bazelBspVersion,
scalaVersion := scala3Version,
buildInfoKeys := Seq[BuildInfoKey](
name,
version,
scalaVersion,
sbtVersion,
"bspVersion" -> "2.0.0-M2"
),
buildInfoPackage := "afenton.bazel.bsp"
)
""".trim
}
18 changes: 0 additions & 18 deletions .github/ci_scripts/update.sh

This file was deleted.

79 changes: 36 additions & 43 deletions .github/ci_scripts/updateArtifactShas.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import scala.util.matching.Regex

private def join(l1: String, l2: String): String =
def join(l1: String, l2: String): String =
if l1.isEmpty then l2
else s"$l1\n$l2"

// Substitutes bazel_rule SHA into README
def substituteReadme(readmeContent: String, newSha: String): String =
sed(readmeContent, "sha256 =".r, s"\"[a-f0-9]{64}\"".r, s"\"$newSha\"") match
case Some(c) => c
Expand All @@ -15,7 +14,6 @@ def substituteReadme(readmeContent: String, newSha: String): String =
s"No substitutions made in README.md, with content: $readmeContent"
)

// Substitutes new SHA defintions into Python
def substituteBazelRule(
ruleContent: String,
newDefinitions: String
Expand Down Expand Up @@ -76,61 +74,56 @@ def generatePythonMap(artifacts: Map[String, String]): String =
"}"
).mkString("\n")

private def binEntries(map: Map[String, String]): Map[String, String] =
map.filterKeys(_.contains("bazel-bsp")).toMap

def updateArtifactShas(
bazelRuleFile: String,
readmeFile: String,
artifactDir: String,
cwd: os.Path,
console: Console
def updateFile(
filePath: os.Path,
artifactPath: os.Path,
fn: (String, Map[String, String]) => String
): Unit =

val bazelRulePath = os.Path(bazelRuleFile, cwd)
val readmePath = os.Path(readmeFile, cwd)
val artifactPath = os.Path(artifactDir, cwd)

require(os.exists(filePath), s"$filePath wasn't found")
require(os.isDir(artifactPath), s"${artifactPath} is not a directory")
List(bazelRulePath, readmePath).foreach { f =>
require(os.exists(f), s"$f file wasn't found")
}

val artifactShas: Map[String, String] = os
.list(artifactPath)
.filter(_.ext == "sha256")
.map(a => (a.last, os.read(a).trim))
.toMap

val newRule = substituteBazelRule(
os.read(bazelRulePath),
generatePythonMap(binEntries(artifactShas))
)
require(!artifactShas.isEmpty, s"No artifacts SHAs found at $artifactPath")

val newContent = fn(os.read(filePath), artifactShas)

println(s"Writing new $bazelRulePath")
os.write.over(bazelRulePath, newRule)
println(s"Writing new $filePath")
os.write.over(filePath, newContent)

val newReadme = substituteReadme(
os.read(readmePath),
artifactShas("bazel_rules.tar.gz.sha256")
/** Update bazee_rule.tar.gz artifcat SHA in README.md.
*
* @param artifactDir
* The directory where artifacts are stored
*/
@main def updateReadme(artifactDir: String): Unit =
val artifactPath = os.Path(artifactDir, os.pwd)

updateFile(
os.pwd / "README.md",
artifactPath,
(content, artifacts) =>
substituteReadme(content, artifacts("bazel_rules.tar.gz.sha256"))
)

println(s"Writing new $readmePath")
os.write.over(readmePath, newReadme)
def binEntries(map: Map[String, String]): Map[String, String] =
map.filterKeys(_.contains("bazel-bsp")).toMap

/** Update the given inputFile with the SHA256 of the artifacts in artifactDir.
* And print to stdout.
/** Update bin artifact SHA's in bazel_rules.
*
* @param bazelRuleFile
* The bazel_rule file to update
* @param readmeFile
* The README file to update
* @param artifactDir
* The directory where artifacts are stored
*/
@main def updateArtifactShas(
bazelRuleFile: String,
readmeFile: String,
artifactDir: String
): Unit =
runWith(updateArtifactShas(bazelRuleFile, readmeFile, artifactDir, _, _))
@main def updateBazelRule(artifactDir: String): Unit =
val artifactPath = os.Path(artifactDir, os.pwd)

updateFile(
os.pwd / "bazel_rules" / "bazel_bsp_setup.bzl",
artifactPath,
(content, artifacts) => substituteBazelRule(content, generatePythonMap(binEntries(artifacts)))
)

36 changes: 36 additions & 0 deletions .github/ci_scripts/updateVersion.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//> using file "project.scala"

import math.Ordering.Implicits.infixOrderingOps

/**
* Update a SemVer version in a given file
*
* @param file The file to update
* @param linePattern The pattern to find the line to update
* @param newVersion The version string to replace with
*/
@main def updateVersion(
file: String,
linePattern: String,
newVersionStr: String,
): Unit =
val filePath = os.Path(file, os.pwd)
require(os.exists(filePath), s"$filePath file wasn't found")

val newVersion = SemVer.fromString(newVersionStr) match
case Some(semVer) => semVer
case None =>
throw new Exception(s"Invalid version string: $newVersionStr")

val newContent =
sed(
os.read(filePath),
linePattern.r,
raw"(\d+)\.(\d+).(\d+)".r,
newVersion.asString
) match
case Some(value) => value
case None => throw new Exception(s"No substitutions made in $filePath")

println(s"Updating $filePath")
os.write.over(filePath, newContent)
74 changes: 0 additions & 74 deletions .github/ci_scripts/updateVersions.scala

This file was deleted.

Loading

0 comments on commit a1fd26b

Please sign in to comment.