-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[dotnet] Add bazel rules for dotnet format and paket deps #16986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+295
−2
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a823b5e
[dotnet] Add bazel rules for dotnet format and paket deps
titusfortner 43dd87d
[dotnet] Integrate dotnet format into format.sh and rake tasks
titusfortner 4453739
[dotnet] Fix Unix WORKSPACE_ROOT fallback to mirror Windows
titusfortner b9c3b7c
[build] Fix Rakefile lint issues
titusfortner f4e21ac
[build] Sync Rakefile with trunk's split structure
titusfortner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,125 @@ | ||
| """Rule for running dotnet format using the Bazel-managed dotnet toolchain.""" | ||
|
|
||
| def _dotnet_format_impl(ctx): | ||
| toolchain = ctx.toolchains["@rules_dotnet//dotnet:toolchain_type"] | ||
| dotnet = toolchain.runtime.files_to_run.executable | ||
|
|
||
| is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo]) | ||
|
|
||
| if is_windows: | ||
| script = _create_windows_script(ctx, dotnet) | ||
| else: | ||
| script = _create_unix_script(ctx, dotnet) | ||
|
|
||
| runfiles = ctx.runfiles(files = [dotnet]) | ||
| runfiles = runfiles.merge(toolchain.runtime.default_runfiles) | ||
|
|
||
| return [ | ||
| DefaultInfo( | ||
| executable = script, | ||
| runfiles = runfiles, | ||
| ), | ||
| ] | ||
|
|
||
| def _to_runfiles_path(short_path): | ||
| """Convert a short_path to a runfiles path.""" | ||
| if short_path.startswith("../"): | ||
| return short_path[3:] | ||
| return "_main/" + short_path | ||
|
|
||
| def _create_unix_script(ctx, dotnet): | ||
| """Create bash script for Unix/macOS/Linux.""" | ||
| dotnet_runfiles_path = _to_runfiles_path(dotnet.short_path) | ||
|
|
||
| script_content = """#!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| # Locate runfiles directory | ||
| if [[ -d "$0.runfiles/_main" ]]; then | ||
| RUNFILES_DIR="$0.runfiles" | ||
| elif [[ -n "${{RUNFILES_DIR:-}}" ]]; then | ||
| RUNFILES_DIR="$RUNFILES_DIR" | ||
| else | ||
| echo "ERROR: Could not locate runfiles directory" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| DOTNET="$RUNFILES_DIR/{dotnet}" | ||
|
|
||
| # Find the workspace root | ||
| WORKSPACE_ROOT="${{BUILD_WORKSPACE_DIRECTORY:-$RUNFILES_DIR/_main}}" | ||
| DOTNET_DIR="$WORKSPACE_ROOT/dotnet" | ||
|
|
||
| cd "$DOTNET_DIR" | ||
|
|
||
| echo "Running dotnet format on all projects..." | ||
| find "$DOTNET_DIR/src" "$DOTNET_DIR/test" -name "*.csproj" 2>/dev/null | while read -r proj; do | ||
titusfortner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| echo " Formatting $proj..." | ||
| "$DOTNET" format "$proj" || exit 1 | ||
| done || exit 1 | ||
|
|
||
| echo "Done." | ||
titusfortner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """.format( | ||
| dotnet = dotnet_runfiles_path, | ||
| ) | ||
|
|
||
| script = ctx.actions.declare_file(ctx.label.name + ".sh") | ||
| ctx.actions.write( | ||
| output = script, | ||
| content = script_content, | ||
| is_executable = True, | ||
| ) | ||
| return script | ||
|
|
||
| def _create_windows_script(ctx, dotnet): | ||
| """Create batch script for Windows.""" | ||
| dotnet_runfiles_path = _to_runfiles_path(dotnet.short_path).replace("/", "\\") | ||
|
|
||
| script_content = """@echo off | ||
| setlocal | ||
|
|
||
| set RUNFILES_DIR=%~dp0%~n0.runfiles | ||
titusfortner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| set DOTNET=%RUNFILES_DIR%\\{dotnet_path} | ||
|
|
||
| if defined BUILD_WORKSPACE_DIRECTORY ( | ||
| set WORKSPACE_ROOT=%BUILD_WORKSPACE_DIRECTORY% | ||
| ) else ( | ||
| set WORKSPACE_ROOT=%RUNFILES_DIR%\\_main | ||
| ) | ||
| set DOTNET_DIR=%WORKSPACE_ROOT%\\dotnet | ||
|
|
||
| cd /d "%DOTNET_DIR%" | ||
|
|
||
| echo Running dotnet format on all projects... | ||
| for /r "%DOTNET_DIR%\\src" %%%%p in (*.csproj) do ( | ||
| echo Formatting %%%%p... | ||
| "%DOTNET%" format "%%%%p" || exit /b 1 | ||
| ) | ||
| for /r "%DOTNET_DIR%\\test" %%%%p in (*.csproj) do ( | ||
| echo Formatting %%%%p... | ||
| "%DOTNET%" format "%%%%p" || exit /b 1 | ||
| ) | ||
|
|
||
| echo Done. | ||
| """.format( | ||
| dotnet_path = dotnet_runfiles_path, | ||
| ) | ||
|
|
||
| script = ctx.actions.declare_file(ctx.label.name + ".bat") | ||
| ctx.actions.write( | ||
| output = script, | ||
| content = script_content, | ||
| is_executable = True, | ||
| ) | ||
| return script | ||
|
|
||
| dotnet_format = rule( | ||
| implementation = _dotnet_format_impl, | ||
| attrs = { | ||
| "_windows_constraint": attr.label( | ||
| default = "@platforms//os:windows", | ||
| ), | ||
| }, | ||
| executable = True, | ||
| toolchains = ["@rules_dotnet//dotnet:toolchain_type"], | ||
| ) | ||
This file contains hidden or 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,143 @@ | ||
| """Rule for running paket commands using the Bazel-managed dotnet toolchain.""" | ||
|
|
||
| def _paket_deps_impl(ctx): | ||
| toolchain = ctx.toolchains["@rules_dotnet//dotnet:toolchain_type"] | ||
| dotnet = toolchain.runtime.files_to_run.executable | ||
|
|
||
| is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo]) | ||
|
|
||
| if is_windows: | ||
| script = _create_windows_script(ctx, dotnet) | ||
| else: | ||
| script = _create_unix_script(ctx, dotnet) | ||
|
|
||
| runfiles = ctx.runfiles(files = [dotnet]) | ||
| runfiles = runfiles.merge(toolchain.runtime.default_runfiles) | ||
|
|
||
| return [ | ||
| DefaultInfo( | ||
| executable = script, | ||
| runfiles = runfiles, | ||
| ), | ||
| ] | ||
|
|
||
| def _to_runfiles_path(short_path): | ||
| """Convert a short_path to a runfiles path.""" | ||
| if short_path.startswith("../"): | ||
| return short_path[3:] | ||
| return "_main/" + short_path | ||
titusfortner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def _create_unix_script(ctx, dotnet): | ||
| """Create bash script for Unix/macOS/Linux.""" | ||
| dotnet_runfiles_path = _to_runfiles_path(dotnet.short_path) | ||
| mode = ctx.attr.mode | ||
|
|
||
| script_content = """#!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| # Locate runfiles directory | ||
| if [[ -d "$0.runfiles/_main" ]]; then | ||
| RUNFILES_DIR="$0.runfiles" | ||
| elif [[ -n "${{RUNFILES_DIR:-}}" ]]; then | ||
| RUNFILES_DIR="$RUNFILES_DIR" | ||
| else | ||
| echo "ERROR: Could not locate runfiles directory" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| DOTNET="$RUNFILES_DIR/{dotnet}" | ||
|
|
||
| # Find the workspace root (where dotnet/.config/dotnet-tools.json lives) | ||
| WORKSPACE_ROOT="${{BUILD_WORKSPACE_DIRECTORY:-$RUNFILES_DIR/_main}}" | ||
| DOTNET_DIR="$WORKSPACE_ROOT/dotnet" | ||
|
|
||
| if [[ ! -f "$DOTNET_DIR/.config/dotnet-tools.json" ]]; then | ||
| echo "ERROR: Could not find dotnet/.config/dotnet-tools.json" >&2 | ||
| echo "Make sure you're running from the workspace root" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| cd "$DOTNET_DIR" | ||
|
|
||
| echo "Restoring dotnet tools..." | ||
| "$DOTNET" tool restore | ||
|
|
||
| echo "Running paket {mode}..." | ||
| "$DOTNET" tool run paket {mode} | ||
|
|
||
| echo "Done. Now run: bazel run @rules_dotnet//tools/paket2bazel:paket2bazel -- --dependencies-file $(pwd)/paket.dependencies --output-folder $(pwd)" | ||
| """.format( | ||
| dotnet = dotnet_runfiles_path, | ||
| mode = mode, | ||
| ) | ||
|
|
||
| script = ctx.actions.declare_file(ctx.label.name + ".sh") | ||
| ctx.actions.write( | ||
| output = script, | ||
| content = script_content, | ||
| is_executable = True, | ||
| ) | ||
| return script | ||
|
|
||
| def _create_windows_script(ctx, dotnet): | ||
| """Create batch script for Windows.""" | ||
| dotnet_runfiles_path = _to_runfiles_path(dotnet.short_path).replace("/", "\\") | ||
| mode = ctx.attr.mode | ||
|
|
||
| script_content = """@echo off | ||
| setlocal | ||
|
|
||
| set RUNFILES_DIR=%~dp0%~n0.runfiles | ||
| set DOTNET=%RUNFILES_DIR%\\{dotnet_path} | ||
|
|
||
| if defined BUILD_WORKSPACE_DIRECTORY ( | ||
| set WORKSPACE_ROOT=%BUILD_WORKSPACE_DIRECTORY% | ||
| ) else ( | ||
| set WORKSPACE_ROOT=%RUNFILES_DIR%\\_main | ||
| ) | ||
| set DOTNET_DIR=%WORKSPACE_ROOT%\\dotnet | ||
titusfortner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if not exist "%DOTNET_DIR%\\.config\\dotnet-tools.json" ( | ||
| echo ERROR: Could not find dotnet\\.config\\dotnet-tools.json >&2 | ||
| exit /b 1 | ||
| ) | ||
|
|
||
| cd /d "%DOTNET_DIR%" | ||
|
|
||
| echo Restoring dotnet tools... | ||
| "%DOTNET%" tool restore | ||
| if %ERRORLEVEL% neq 0 exit /b %ERRORLEVEL% | ||
|
|
||
| echo Running paket {mode}... | ||
| "%DOTNET%" tool run paket {mode} | ||
| if %ERRORLEVEL% neq 0 exit /b %ERRORLEVEL% | ||
|
|
||
| echo Done. Now run: bazel run @rules_dotnet//tools/paket2bazel:paket2bazel -- --dependencies-file %cd%\\paket.dependencies --output-folder %cd% | ||
| """.format( | ||
| dotnet_path = dotnet_runfiles_path, | ||
| mode = mode, | ||
| ) | ||
|
|
||
| script = ctx.actions.declare_file(ctx.label.name + ".bat") | ||
| ctx.actions.write( | ||
| output = script, | ||
| content = script_content, | ||
| is_executable = True, | ||
| ) | ||
| return script | ||
|
|
||
| paket_deps = rule( | ||
| implementation = _paket_deps_impl, | ||
| attrs = { | ||
| "mode": attr.string( | ||
| doc = "Paket command to run: 'update' for latest versions, 'install' to sync lockfile", | ||
| mandatory = True, | ||
| values = ["update", "install"], | ||
| ), | ||
| "_windows_constraint": attr.label( | ||
| default = "@platforms//os:windows", | ||
| ), | ||
| }, | ||
| executable = True, | ||
| toolchains = ["@rules_dotnet//dotnet:toolchain_type"], | ||
| ) | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.