Skip to content

Commit

Permalink
JacocoInstrumenter from Argparse4j to scopt
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-skrodzka committed Oct 4, 2024
1 parent b753a35 commit e7d38d1
Show file tree
Hide file tree
Showing 23 changed files with 59 additions and 67 deletions.
2 changes: 1 addition & 1 deletion rules/private/phases/phase_binary_launcher.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
load("//rules/common:private/get_toolchain.bzl", "get_toolchain")
load(
"//rules/common:private/utils.bzl",
_write_launcher = "write_launcher",
)
load("//rules/common:private/get_toolchain.bzl", "get_toolchain")

#
# PHASE: binary_launcher
Expand Down
5 changes: 1 addition & 4 deletions rules/private/phases/phase_bootstrap_compile.bzl
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
load(
"//rules/common:private/utils.bzl",
_strip_margin = "strip_margin",
)
load(
"//rules/common:private/utils.bzl",
_resolve_execution_reqs = "resolve_execution_reqs",
_strip_margin = "strip_margin",
)

#
Expand Down
8 changes: 4 additions & 4 deletions rules/private/phases/phase_coverage_jacoco.bzl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
load(
"@rules_scala3//rules/private:coverage_replacements_provider.bzl",
_coverage_replacements_provider = "coverage_replacements_provider",
)
load(
"@rules_scala3//rules/common:private/utils.bzl",
_resolve_execution_reqs = "resolve_execution_reqs",
)
load(
"@rules_scala3//rules/private:coverage_replacements_provider.bzl",
_coverage_replacements_provider = "coverage_replacements_provider",
)
load("//rules/common:private/get_toolchain.bzl", "get_toolchain")

def phase_coverage_jacoco(ctx, g):
Expand Down
2 changes: 1 addition & 1 deletion rules/private/phases/phase_test_launcher.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def phase_test_launcher(ctx, g):
args = ctx.actions.args()
args.add("--parallel", ctx.attr.parallel)
if ctx.attr.parallel_n:
args.add(ctx.attr.parallel_n, format = "--parallel-n=%s")
args.add(ctx.attr.parallel_n, format = "--parallel-n=%s")
args.add("--apis", g.compile.zinc_info.apis.short_path)
args.add_all(ctx.attr.frameworks, format_each = "--framework=%s")
if ctx.attr.isolation == "classloader":
Expand Down
2 changes: 1 addition & 1 deletion rules/scala/private/doc.bzl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
load("//rules/common:private/get_toolchain.bzl", "get_toolchain")
load(
"//rules/common:private/utils.bzl",
_collect = "collect",
_resolve_execution_reqs = "resolve_execution_reqs",
)
load("//rules/common:private/get_toolchain.bzl", "get_toolchain")

scaladoc_private_attributes = {
"_runner": attr.label(
Expand Down
2 changes: 1 addition & 1 deletion rules/scala/private/repl.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("//rules/common:private/utils.bzl", "write_launcher", _collect = "collect")
load("//rules/common:private/get_toolchain.bzl", "get_toolchain")
load("//rules/common:private/utils.bzl", "write_launcher", _collect = "collect")

def scala_repl_implementation(ctx):
toolchain = get_toolchain(ctx)
Expand Down
2 changes: 1 addition & 1 deletion rules/scala/private/scalajs.bzl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
load("@rules_scala3//rules:scala.bzl", "scala_library")
load(
"@rules_scala3//rules:providers.bzl",
_ScalaConfiguration = "ScalaConfiguration",
_ScalaInfo = "ScalaInfo",
)
load("@rules_scala3//rules:scala.bzl", "scala_library")

def scalajs_library(name, srcs, deps = [], visibility = None, scalacopts = [], scala = None, deps_used_whitelist = []):
"""Make scalajs library for provided sources"""
Expand Down
2 changes: 1 addition & 1 deletion rules/scala/workspace.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,4 @@ def scala_repositories():
["rules_proto", "rules_proto-" + rules_proto_tag, "https://github.com/bazelbuild/rules_proto/archive/{}.tar.gz".format(rules_proto_tag), "6fb6767d1bef535310547e03247f7518b03487740c11b6c6adb7952033fe1295"],
]
for dep in rules_deps:
maybe(http_archive, name = dep[0], strip_prefix = dep[1], url = dep[2], sha256 = dep[3] if len(dep) == 4 else "")
maybe(http_archive, name = dep[0], strip_prefix = dep[1], url = dep[2], sha256 = dep[3] if len(dep) == 4 else "")
5 changes: 1 addition & 4 deletions rules/scala_proto/private/core.bzl
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
load(
"//rules/common:private/utils.bzl",
_safe_name = "safe_name",
)
load(
"//rules/common:private/utils.bzl",
_resolve_execution_reqs = "resolve_execution_reqs",
_safe_name = "safe_name",
)

scala_proto_library_private_attributes = {}
Expand Down
8 changes: 4 additions & 4 deletions rules/scalafmt/ext.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ load(
"@rules_scala3//rules:providers.bzl",
_ScalaRulePhase = "ScalaRulePhase",
)
load(
"//rules/private:phases.bzl",
_phase_scalafmt_nondefault_outputs = "phase_scalafmt_nondefault_outputs",
)
load(
"//rules/scalafmt:private/test.bzl",
_scala_format_attributes = "scala_format_attributes",
_scala_non_default_format_attributes = "scala_non_default_format_attributes",
)
load(
"//rules/private:phases.bzl",
_phase_scalafmt_nondefault_outputs = "phase_scalafmt_nondefault_outputs",
)

ext_with_non_default_format = {
"attrs": _dicts.add(
Expand Down
2 changes: 1 addition & 1 deletion scala/common/sbt-testing/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("//rules:scalafmt.bzl", "scala_format_test")
load("//rules:scala.bzl", "scala_library")
load("//rules:scalafmt.bzl", "scala_format_test")

_subprocess_srcs = ["SubprocessRunner.scala"]

Expand Down
2 changes: 1 addition & 1 deletion scala/common/worker/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("//scala3:defs.bzl", "worker_scala_library")
load("//rules:scalafmt.bzl", "scala_format_test")
load("//scala3:defs.bzl", "worker_scala_library")

proto_library(
name = "worker_protocol_proto",
Expand Down
2 changes: 1 addition & 1 deletion scala/workers/common/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("//scala3:defs.bzl", "worker_scala_library")
load("//rules:scalafmt.bzl", "scala_format_test")
load("//scala3:defs.bzl", "worker_scala_library")

worker_scala_library(
name = "common",
Expand Down
2 changes: 1 addition & 1 deletion scala/workers/deps/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("//scala3:defs.bzl", "worker_scala_binary")
load("//rules:scalafmt.bzl", "scala_format_test")
load("//scala3:defs.bzl", "worker_scala_binary")

worker_scala_binary(
name = "deps",
Expand Down
2 changes: 1 addition & 1 deletion scala/workers/jacoco/instrumenter/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("//scala3:defs.bzl", "worker_scala_binary")
load("//rules:scalafmt.bzl", "scala_format_test")
load("//scala3:defs.bzl", "worker_scala_binary")

worker_scala_binary(
name = "instrumenter",
Expand Down
53 changes: 26 additions & 27 deletions scala/workers/jacoco/instrumenter/JacocoInstrumenter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,44 @@ import java.net.URI
import java.nio.file.*
import java.nio.file.attribute.BasicFileAttributes
import java.util.Collections
import java.util.List as JList
import org.jacoco.core.instr.Instrumenter
import org.jacoco.core.runtime.OfflineInstrumentationAccessGenerator
import scala.jdk.CollectionConverters.*
import scopt.OParser

object JacocoInstrumenter extends WorkerMain[Unit]:
private val argParser =
val parser = ArgumentParsers.newFor("jacoco-instrumenter").addHelp(true).fromFilePrefix("@").build
parser
.addArgument("--jar")
.action(Arguments.append)
.help("jar to instrument")
.metavar("inpath=outpath")
.nargs("+")
parser
final case class JacocoArgs(jars: Vector[(Path, Path)] = Vector.empty)

object JacocoArgs:
private val builder = OParser.builder[JacocoArgs]
import builder.*

private val parser = OParser.sequence(
opt[String]("jar")
.unbounded()
.required()
.action((arg, c) => c.copy(jars = c.jars :+ parseJar(arg)))
.text("Jar to instrument in the format inpath=outpath")
)

def apply(args: collection.Seq[String]): Option[JacocoArgs] =
OParser.parse(parser, args, JacocoArgs())

private def parseJar(arg: String): (Path, Path) =
arg.split("=") match
case Array(in, out) => (Paths.get(in), Paths.get(out))
case _ => sys.error(s"Expected input=output for argument: $arg")

object JacocoInstrumenter extends WorkerMain[Unit]:
override def init(args: collection.Seq[String]): Unit = ()

override def work(ctx: Unit, args: collection.Seq[String]): Unit =
val namespace = argParser.parseArgs(args)

val pathPairs: List[(Path, Path)] = namespace
.getList[JList[String]]("jar")
.asScala
.flatMap(_.asScala)
.map(other =>
other.split("=") match
case Array(in, out) => (Paths.get(in), Paths.get(out))
case _ =>
sys.error("expected input=output for argument: " + other)
)
.toList
val jacocoArgs = JacocoArgs(args).getOrElse(throw IllegalArgumentException(s"Invalid arguments: ${args.mkString(" ")}"))

val jacoco = new Instrumenter(new OfflineInstrumentationAccessGenerator)

pathPairs.foreach { case (inPath, outPath) =>
jacocoArgs.jars.foreach { case (inPath, outPath) =>
val inFS = FileSystems.newFileSystem(inPath, null)
val outFS =
FileSystems.newFileSystem(URI.create("jar:" + outPath.toUri), Collections.singletonMap("create", "true"))
val outFS = FileSystems.newFileSystem(URI.create("jar:" + outPath.toUri), Collections.singletonMap("create", "true"))

val roots = inFS.getRootDirectories.asScala.toList
val instrumentVisitor = new SimpleFileVisitor[Path]:
Expand Down
10 changes: 5 additions & 5 deletions scala/workers/zinc/compile/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("//rules:scala.bzl", "scala_test", "scala_library")
load("//rules:scala.bzl", "scala_library", "scala_test")
load("//scala3:defs.bzl", "worker_scala_binary")

proto_library(
Expand All @@ -20,6 +20,7 @@ worker_scala_binary(
main_class = "rules_scala.workers.zinc.compile.ZincRunner",
visibility = ["//visibility:public"],
deps = [
":diagnostics_java_proto",
"@annex//:com_github_scopt_scopt_3",
"@annex//:org_scala_sbt_compiler_interface",
"@annex//:org_scala_sbt_util_interface",
Expand All @@ -32,15 +33,16 @@ worker_scala_binary(
"@rules_scala3//rules/third_party/jarhelper",
"@rules_scala3//scala/common/worker",
"@rules_scala3//scala/workers/common",
":diagnostics_java_proto",
],
)

scala_library(
name = "lib",
srcs = glob(["main/*.scala"]),
scala = "@//:zinc_3",
visibility = ["//visibility:public"],
deps = [
":diagnostics_java_proto",
"@annex//:com_github_scopt_scopt_3",
"@annex//:org_scala_sbt_compiler_interface",
"@annex//:org_scala_sbt_util_interface",
Expand All @@ -53,18 +55,16 @@ scala_library(
"@rules_scala3//rules/third_party/jarhelper",
"@rules_scala3//scala/common/worker",
"@rules_scala3//scala/workers/common",
":diagnostics_java_proto",
],
scala = "@//:zinc_3",
)

scala_test(
name = "tests",
srcs = glob(["test/*.scala"]),
scala = "@//:zinc_3",
deps = [
":lib",
"@annex//:org_scalameta_munit_3",
"@rules_scala3//scala/workers/common",
],
scala = "@//:zinc_3",
)
2 changes: 1 addition & 1 deletion scala/workers/zinc/repl/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ load("//rules:scalafmt.bzl", "scala_format_test")
scala_binary(
name = "repl",
srcs = glob(["**/*.scala"]),
visibility = ["//visibility:public"],
main_class = "rules_scala.workers.zinc.repl.ReplRunner",
visibility = ["//visibility:public"],
runtime_deps = [
"@annex//:org_jline_jline_reader",
],
Expand Down
5 changes: 2 additions & 3 deletions tests/compile/zinc-inc/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
load("@rules_scala3//rules:scala.bzl", "scala_library")


config_setting(
name = "a2",
values = {"define": "a2=true"},
Expand All @@ -10,8 +9,8 @@ config_setting(
scala_library(
name = "a",
srcs = select({
":a2": ["A2.scala"],
"//conditions:default": ["A1.scala"],
":a2": ["A2.scala"],
"//conditions:default": ["A1.scala"],
}),
)

Expand Down
2 changes: 1 addition & 1 deletion tests/dependencies/indirect/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@rules_scala3//rules:scala.bzl", "scala_library")
load("@rules_scala3//rules:providers.bzl", "reconfigure_deps_configuration")
load("@rules_scala3//rules:scala.bzl", "scala_library")

# used_deps off
reconfigure_deps_configuration(
Expand Down
2 changes: 1 addition & 1 deletion tests/format/scalafmt/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@rules_scala3//rules:scalafmt.bzl", "scala_format_test")
load("@rules_scala3//rules:scala_with_scalafmt.bzl", "scala_binary", "scala_library", "scala_test")
load("@rules_scala3//rules:scalafmt.bzl", "scala_format_test")

### scala_format_test ###
scala_format_test(
Expand Down
2 changes: 1 addition & 1 deletion tests/grpc/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_scala3//rules:scala_proto.bzl", "scala_proto_library")
load("@rules_scala3//rules:scala.bzl", "scala_library")
load("@rules_scala3//rules:scala_proto.bzl", "scala_proto_library")

proto_library(
name = "grpc_proto",
Expand Down
2 changes: 1 addition & 1 deletion tests/proto/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_scala3//rules:scala_proto.bzl", "scala_proto_library")
load("@rules_scala3//rules:scala.bzl", "scala_library")
load("@rules_scala3//rules:scala_proto.bzl", "scala_proto_library")

proto_library(
name = "events_proto",
Expand Down

0 comments on commit e7d38d1

Please sign in to comment.