-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
626 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
align.openParenCallSite = false | ||
align.openParenDefnSite = false | ||
continuationIndent.defnSite = 2 | ||
danglingParentheses = true | ||
docstrings = JavaDoc | ||
importSelectors = singleLine | ||
maxColumn = 120 | ||
newlines.afterImplicitKWInVerticalMultiline = true | ||
rewrite.redundantBraces.stringInterpolation = true | ||
rewrite.rules = [ | ||
RedundantParens, | ||
PreferCurlyFors, | ||
SortImports | ||
] | ||
unindentTopLevelOperators = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# | ||
# PHASE: phase scalafmt | ||
# | ||
# Outputs to format the scala files when it is explicitly specified | ||
# | ||
def phase_scalafmt(ctx, p): | ||
if ctx.attr.format: | ||
manifest, files = _build_format(ctx) | ||
_format_runner(ctx, manifest, files) | ||
_format_tester(ctx, manifest, files) | ||
else: | ||
ctx.actions.write( | ||
output = ctx.outputs.scalafmt_runner, | ||
content = "", | ||
is_executable = True, | ||
) | ||
ctx.actions.write( | ||
output = ctx.outputs.scalafmt_testrunner, | ||
content = "", | ||
is_executable = True, | ||
) | ||
|
||
def _build_format(ctx): | ||
files = [] | ||
runner_inputs, _, runner_manifests = ctx.resolve_command(tools = [ctx.attr._fmt]) | ||
manifest_content = [] | ||
for src in ctx.files.srcs: | ||
if src.path.endswith(".scala") and src.is_source: | ||
file = ctx.actions.declare_file(src.short_path) | ||
files.append(file) | ||
args = ctx.actions.args() | ||
args.add("--config") | ||
args.add(ctx.file.config.path) | ||
args.add(src.path) | ||
args.add(file.path) | ||
args.set_param_file_format("multiline") | ||
args.use_param_file("@%s", use_always = True) | ||
ctx.actions.run( | ||
arguments = ["--jvm_flag=-Dfile.encoding=UTF-8", args], | ||
executable = ctx.executable._fmt, | ||
outputs = [file], | ||
input_manifests = runner_manifests, | ||
inputs = [ctx.file.config, src], | ||
tools = runner_inputs, | ||
execution_requirements = {"supports-workers": "1"}, | ||
mnemonic = "ScalaFmt", | ||
) | ||
manifest_content.append("{} {}".format(src.short_path, file.short_path)) | ||
|
||
manifest = ctx.actions.declare_file("format/{}/manifest.txt".format(ctx.label.name)) | ||
ctx.actions.write(manifest, "\n".join(manifest_content) + "\n") | ||
|
||
return manifest, files | ||
|
||
def _format_runner(ctx, manifest, files): | ||
ctx.actions.run_shell( | ||
inputs = [ctx.file._runner, manifest] + files, | ||
outputs = [ctx.outputs.scalafmt_runner], | ||
command = "cat $1 | sed -e s#%workspace%#$2# -e s#%manifest%#$3# > $4", | ||
arguments = [ | ||
ctx.file._runner.path, | ||
ctx.workspace_name, | ||
manifest.short_path, | ||
ctx.outputs.scalafmt_runner.path, | ||
], | ||
execution_requirements = {}, | ||
) | ||
|
||
def _format_tester(ctx, manifest, files): | ||
ctx.actions.run_shell( | ||
inputs = [ctx.file._testrunner, manifest] + files, | ||
outputs = [ctx.outputs.scalafmt_testrunner], | ||
command = "cat $1 | sed -e s#%workspace%#$2# -e s#%manifest%#$3# > $4", | ||
arguments = [ | ||
ctx.file._testrunner.path, | ||
ctx.workspace_name, | ||
manifest.short_path, | ||
ctx.outputs.scalafmt_testrunner.path, | ||
], | ||
execution_requirements = {}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
load("//scala:scala.bzl", "scala_binary") | ||
|
||
filegroup( | ||
name = "runner", | ||
srcs = ["private/format.template.sh"], | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
filegroup( | ||
name = "testrunner", | ||
srcs = ["private/format-test.template.sh"], | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
scala_binary( | ||
name = "scalafmt", | ||
srcs = glob(["scalafmt/ScalafmtRunner.scala"]), | ||
main_class = "io.bazel.rules_scala.scalafmt.ScalafmtRunner", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//src/java/io/bazel/rulesscala/worker", | ||
"//external:io_bazel_rules_scala/scalafmt/geirsson_metaconfig_core", | ||
"//external:io_bazel_rules_scala/scalafmt/argparse4j", | ||
"//external:io_bazel_rules_scala/scalafmt/scalafmt_parsers", | ||
"@scalafmt//:org_scalameta_scalafmt_core_2_11", | ||
], | ||
) | ||
|
||
load( | ||
"//scala/scalafmt:phase_scalafmt_ext.bzl", "scalafmt_singleton", | ||
) | ||
|
||
scalafmt_singleton( | ||
name = "phase_scalafmt", | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
load( | ||
"//scala:advanced_usage/providers.bzl", | ||
_ScalaRulePhase = "ScalaRulePhase", | ||
) | ||
load( | ||
"//scala/private:phases/phases.bzl", | ||
_phase_scalafmt = "phase_scalafmt", | ||
) | ||
|
||
ext_scalafmt = { | ||
"attrs": { | ||
"config": attr.label( | ||
allow_single_file = [".conf"], | ||
default = "@scalafmt_default//:config", | ||
doc = "The Scalafmt configuration file.", | ||
), | ||
"_fmt": attr.label( | ||
cfg = "host", | ||
default = "//scala/scalafmt", | ||
executable = True, | ||
), | ||
"_runner": attr.label( | ||
allow_single_file = True, | ||
default = "//scala/scalafmt:runner", | ||
), | ||
"_testrunner": attr.label( | ||
allow_single_file = True, | ||
default = "//scala/scalafmt:testrunner", | ||
), | ||
"format": attr.bool( | ||
default = False, | ||
), | ||
}, | ||
"outputs": { | ||
"scalafmt_runner": "%{name}.format", | ||
"scalafmt_testrunner": "%{name}.format-test", | ||
}, | ||
"phase_providers": [ | ||
"//scala/scalafmt:phase_scalafmt", | ||
], | ||
} | ||
|
||
def _scalafmt_singleton_implementation(ctx): | ||
return [ | ||
_ScalaRulePhase( | ||
custom_phases = [ | ||
("$", "", "scalafmt", _phase_scalafmt), | ||
], | ||
), | ||
] | ||
|
||
scalafmt_singleton = rule( | ||
implementation = _scalafmt_singleton_implementation, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/bin/bash -e | ||
RUNPATH="${TEST_SRCDIR-$0.runfiles}"/%workspace% | ||
WORKSPACE_ROOT="${1:-$BUILD_WORKSPACE_DIRECTORY}" | ||
NONDEFAULTPATH=(${RUNPATH//bin/ }) | ||
NONDEFAULTPATH="${NONDEFAULTPATH[0]}"bin | ||
|
||
EXIT=0 | ||
while read original formatted; do | ||
if [[ ! -z "$original" ]] && [[ ! -z "$formatted" ]]; then | ||
if ! cmp -s "$WORKSPACE_ROOT/$original" "$NONDEFAULTPATH/$formatted"; then | ||
echo $original | ||
diff "$WORKSPACE_ROOT/$original" "$NONDEFAULTPATH/$formatted" || true | ||
EXIT=1 | ||
fi | ||
fi | ||
done < "$NONDEFAULTPATH"/%manifest% | ||
|
||
exit $EXIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/bin/bash -e | ||
RUNPATH="${TEST_SRCDIR-$0.runfiles}"/%workspace% | ||
WORKSPACE_ROOT="${1:-$BUILD_WORKSPACE_DIRECTORY}" | ||
|
||
if [ -f "$RUNPATH"/%manifest% ]; then | ||
while read original formatted; do | ||
if [[ ! -z "$original" ]] && [[ ! -z "$formatted" ]]; then | ||
if ! cmp -s "$RUNPATH/$original" "$RUNPATH/$formatted"; then | ||
if [ -z "$WORKSPACE_ROOT" ]; then | ||
echo "$original" | ||
diff "$RUNPATH/$original" "$RUNPATH/$formatted" || true | ||
EXIT=1 | ||
else | ||
echo "Formatting $original" | ||
cp "$RUNPATH/$formatted" "$WORKSPACE_ROOT/$original" | ||
fi | ||
fi | ||
fi | ||
done < "$RUNPATH"/%manifest% | ||
else | ||
NONDEFAULTPATH=(${RUNPATH//bin/ }) | ||
NONDEFAULTPATH="${NONDEFAULTPATH[0]}"bin | ||
while read original formatted; do | ||
if [[ ! -z "$original" ]] && [[ ! -z "$formatted" ]]; then | ||
if ! cmp -s "$WORKSPACE_ROOT/$original" "$NONDEFAULTPATH/$formatted"; then | ||
echo "Formatting $original" | ||
cp "$NONDEFAULTPATH/$formatted" "$WORKSPACE_ROOT/$original" | ||
fi | ||
fi | ||
done < "$NONDEFAULTPATH"/%manifest% | ||
fi | ||
|
||
exit $EXIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
load( | ||
"//scala:scala_cross_version.bzl", | ||
_default_scala_version = "default_scala_version", | ||
_extract_major_version = "extract_major_version", | ||
_scala_mvn_artifact = "scala_mvn_artifact", | ||
) | ||
load( | ||
"@io_bazel_rules_scala//scala:scala_maven_import_external.bzl", | ||
_scala_maven_import_external = "scala_maven_import_external", | ||
) | ||
|
||
def scalafmt_default_config(path = ".scalafmt.conf"): | ||
build = [] | ||
build.append("filegroup(") | ||
build.append(" name = \"config\",") | ||
build.append(" srcs = [\"{}\"],".format(path)) | ||
build.append(" visibility = [\"//visibility:public\"],") | ||
build.append(")") | ||
native.new_local_repository(name = "scalafmt_default", build_file_content = "\n".join(build), path = "") | ||
|
||
def scalafmt_repositories( | ||
scala_version = _default_scala_version(), | ||
maven_servers = ["http://central.maven.org/maven2"]): | ||
major_version = _extract_major_version(scala_version) | ||
|
||
scala_jar_shas = { | ||
"2.11": { | ||
"scalafmt_parsers": "acde4faa648c61f1d76f7a1152116738c0b0b80ae2fab8ceae83c061c29aadf1", | ||
"metaconfig_core": "8abb4e48507486d0b323b440bb021bddd56366e502002025fdaf10025d2650c2", | ||
"scalafmt_core": "84bac5ed8c85e61851ef427f045b7bfd149d857cb543b41c85b8353fb8c47aff", | ||
}, | ||
"2.12": { | ||
"scalafmt_parsers": "9dc726dab95870b193dee3ed4d11985fa38ca09640768a7c86d8f80c715c5567", | ||
"metaconfig_core": "495817d90ecb4c432ee0afa7e79b4d005e6a6f90a270e113e15fe7d2d5559dfd", | ||
"scalafmt_core": "02562f176a7d070230ef2da6192f2d15afd62ea173eaf8ca02a7afb89262d233", | ||
}, | ||
} | ||
|
||
scala_version_jar_shas = scala_jar_shas[major_version] | ||
|
||
_scala_maven_import_external( | ||
name = "scalafmt_parsers", | ||
artifact = _scala_mvn_artifact( | ||
"org.scalameta:parsers:4.2.0", | ||
major_version, | ||
), | ||
artifact_sha256 = scala_version_jar_shas["scalafmt_parsers"], | ||
licenses = ["notice"], | ||
server_urls = maven_servers, | ||
) | ||
|
||
native.bind( | ||
name = "io_bazel_rules_scala/scalafmt/scalafmt_parsers", | ||
actual = "@scalafmt_parsers", | ||
) | ||
|
||
_scala_maven_import_external( | ||
name = "geirsson_metaconfig_core", | ||
artifact = _scala_mvn_artifact( | ||
"com.geirsson:metaconfig-core:0.8.3", | ||
major_version, | ||
), | ||
artifact_sha256 = scala_version_jar_shas["metaconfig_core"], | ||
licenses = ["notice"], | ||
server_urls = maven_servers, | ||
) | ||
|
||
native.bind( | ||
name = "io_bazel_rules_scala/scalafmt/geirsson_metaconfig_core", | ||
actual = "@geirsson_metaconfig_core", | ||
) | ||
|
||
_scala_maven_import_external( | ||
name = "scalafmt_core", | ||
artifact = _scala_mvn_artifact( | ||
"org.scalameta:scalafmt-core:2.0.0", | ||
major_version, | ||
), | ||
artifact_sha256 = scala_version_jar_shas["scalafmt_core"], | ||
licenses = ["notice"], | ||
server_urls = maven_servers, | ||
) | ||
|
||
native.bind( | ||
name = "io_bazel_rules_scala/scalafmt/scalafmt_core", | ||
actual = "@scalafmt_core", | ||
) | ||
|
||
_scala_maven_import_external( | ||
name = "argparse4j", | ||
artifact = "net.sourceforge.argparse4j:argparse4j:0.8.1", | ||
artifact_sha256 = "98cb5468cac609f3bc07856f2e34088f50dc114181237c48d20ca69c3265d044", | ||
licenses = ["notice"], | ||
server_urls = maven_servers, | ||
) | ||
|
||
native.bind( | ||
name = "io_bazel_rules_scala/scalafmt/argparse4j", | ||
actual = "@argparse4j", | ||
) |
Oops, something went wrong.