Skip to content

Commit

Permalink
Add windows support (#1011)
Browse files Browse the repository at this point in the history
* Add Windows Support

* Add Windows Support

* Revert line ending change
  • Loading branch information
PatriceVignola authored Jan 23, 2024
1 parent 3c721d0 commit 304e38a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
5 changes: 4 additions & 1 deletion buildifier/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ go_library(
)

exports_files(
["runner.bash.template"],
[
"runner.bash.template",
"runner.bat.template",
],
visibility = ["//visibility:public"],
)

Expand Down
2 changes: 1 addition & 1 deletion buildifier/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type).

See also the [full list](../WARNINGS.md) or the supported warnings.

## Setup and usage via Bazel (not supported on Windows)
## Setup and usage via Bazel

You can also invoke buildifier via the Bazel rule.
`WORKSPACE` file:
Expand Down
15 changes: 14 additions & 1 deletion buildifier/internal/factory.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ def buildifier_attr_factory(test_rule = False):
default = "//buildifier:runner.bash.template",
allow_single_file = True,
),
"_windows_runner": attr.label(
default = "@com_github_bazelbuild_buildtools//buildifier:runner.bat.template",
allow_single_file = True,
),
}

if test_rule:
Expand Down Expand Up @@ -165,14 +169,23 @@ def buildifier_impl_factory(ctx, test_rule = False):
workspace = ctx.file.workspace.path

out_file = ctx.actions.declare_file(ctx.label.name + ".bash")

substitutions = {
"@@ARGS@@": shell.array_literal(args),
"@@BUILDIFIER_SHORT_PATH@@": shell.quote(ctx.executable.buildifier.short_path),
"@@EXCLUDE_PATTERNS@@": exclude_patterns_str,
"@@WORKSPACE@@": workspace,
}

if ctx.executable.buildifier.extension.lower() == "exe":
out_file = ctx.actions.declare_file(ctx.label.name + ".bat")
runner_template = ctx.file._windows_runner
else:
out_file = ctx.actions.declare_file(ctx.label.name + ".bash")
runner_template = ctx.file._runner

ctx.actions.expand_template(
template = ctx.file._runner,
template = runner_template,
output = out_file,
substitutions = substitutions,
is_executable = True,
Expand Down
45 changes: 45 additions & 0 deletions buildifier/runner.bat.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
@echo off

set ARGS=@@ARGS@@

rem Remove the leading '(' and trailing ')' characters that surround the arguments
set stripped_args=%ARGS:~1,-1%

rem Unquote the arguments
set stripped_args=%stripped_args:'=%

rem Get the absolute path to the buildifier executable
for /f "tokens=2" %%i in ('findstr /r "\<buildifier\.exe\>" MANIFEST') do (set buildifier_abs_path=%%i)

powershell ^
function Buildify($Root)^
{^
$Folder = (New-Object -Com Scripting.FileSystemObject).GetFolder($Root);^
$Files = $Folder.Files ^| Where-Object {^
$_.Name -eq 'BUILD.bazel' `^
-or $_.Name -eq 'BUILD' `^
-or $_.Name -eq 'WORKSPACE' `^
-or $_.Name -eq 'WORKSPACE.bazel' `^
-or $_.Name -eq 'WORKSPACE.oss' `^
-or $_.Name -clike '*.bzl' `^
-or $_.Name -clike '*.sky' `^
-or $_.Name -clike '*.BUILD' `^
-or $_.Name -clike 'BUILD.*.bazel' `^
-or $_.Name -clike 'BUILD.*.oss' `^
-or $_.Name -clike 'WORKSPACE.*.bazel' `^
-or $_.Name -clike 'WORKSPACE.*.oss'^
};^
foreach ($File in $Files)^
{^
^& '%buildifier_abs_path%' %stripped_args% $File.Path;^
};^
foreach ($SubFolder in $Folder.Subfolders)^
{^
$CurrentItem = Get-Item $SubFolder.Path -ErrorAction SilentlyContinue;^
if ($CurrentItem -and !$CurrentItem.Attributes.ToString().Contains('ReparsePoint'))^
{^
Buildify($SubFolder.Path);^
};^
};^
};^
Buildify('%BUILD_WORKSPACE_DIRECTORY%');

0 comments on commit 304e38a

Please sign in to comment.