|
| 1 | +"""Rule for running paket commands using the Bazel-managed dotnet toolchain.""" |
| 2 | + |
| 3 | +def _paket_deps_impl(ctx): |
| 4 | + toolchain = ctx.toolchains["@rules_dotnet//dotnet:toolchain_type"] |
| 5 | + dotnet = toolchain.runtime.files_to_run.executable |
| 6 | + |
| 7 | + is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo]) |
| 8 | + |
| 9 | + if is_windows: |
| 10 | + script = _create_windows_script(ctx, dotnet) |
| 11 | + else: |
| 12 | + script = _create_unix_script(ctx, dotnet) |
| 13 | + |
| 14 | + runfiles = ctx.runfiles(files = [dotnet]) |
| 15 | + runfiles = runfiles.merge(toolchain.runtime.default_runfiles) |
| 16 | + |
| 17 | + return [ |
| 18 | + DefaultInfo( |
| 19 | + executable = script, |
| 20 | + runfiles = runfiles, |
| 21 | + ), |
| 22 | + ] |
| 23 | + |
| 24 | +def _to_runfiles_path(short_path): |
| 25 | + """Convert a short_path to a runfiles path.""" |
| 26 | + if short_path.startswith("../"): |
| 27 | + return short_path[3:] |
| 28 | + return "_main/" + short_path |
| 29 | + |
| 30 | +def _create_unix_script(ctx, dotnet): |
| 31 | + """Create bash script for Unix/macOS/Linux.""" |
| 32 | + dotnet_runfiles_path = _to_runfiles_path(dotnet.short_path) |
| 33 | + mode = ctx.attr.mode |
| 34 | + |
| 35 | + script_content = """#!/usr/bin/env bash |
| 36 | +set -euo pipefail |
| 37 | +
|
| 38 | +# Locate runfiles directory |
| 39 | +if [[ -d "$0.runfiles/_main" ]]; then |
| 40 | + RUNFILES_DIR="$0.runfiles" |
| 41 | +elif [[ -n "${{RUNFILES_DIR:-}}" ]]; then |
| 42 | + RUNFILES_DIR="$RUNFILES_DIR" |
| 43 | +else |
| 44 | + echo "ERROR: Could not locate runfiles directory" >&2 |
| 45 | + exit 1 |
| 46 | +fi |
| 47 | +
|
| 48 | +DOTNET="$RUNFILES_DIR/{dotnet}" |
| 49 | +
|
| 50 | +# Find the workspace root (where dotnet/.config/dotnet-tools.json lives) |
| 51 | +WORKSPACE_ROOT="${{BUILD_WORKSPACE_DIRECTORY:-$RUNFILES_DIR/_main}}" |
| 52 | +DOTNET_DIR="$WORKSPACE_ROOT/dotnet" |
| 53 | +
|
| 54 | +if [[ ! -f "$DOTNET_DIR/.config/dotnet-tools.json" ]]; then |
| 55 | + echo "ERROR: Could not find dotnet/.config/dotnet-tools.json" >&2 |
| 56 | + echo "Make sure you're running from the workspace root" >&2 |
| 57 | + exit 1 |
| 58 | +fi |
| 59 | +
|
| 60 | +cd "$DOTNET_DIR" |
| 61 | +
|
| 62 | +echo "Restoring dotnet tools..." |
| 63 | +"$DOTNET" tool restore |
| 64 | +
|
| 65 | +echo "Running paket {mode}..." |
| 66 | +"$DOTNET" tool run paket {mode} |
| 67 | +
|
| 68 | +echo "Done. Now run: bazel run @rules_dotnet//tools/paket2bazel:paket2bazel -- --dependencies-file $(pwd)/paket.dependencies --output-folder $(pwd)" |
| 69 | +""".format( |
| 70 | + dotnet = dotnet_runfiles_path, |
| 71 | + mode = mode, |
| 72 | + ) |
| 73 | + |
| 74 | + script = ctx.actions.declare_file(ctx.label.name + ".sh") |
| 75 | + ctx.actions.write( |
| 76 | + output = script, |
| 77 | + content = script_content, |
| 78 | + is_executable = True, |
| 79 | + ) |
| 80 | + return script |
| 81 | + |
| 82 | +def _create_windows_script(ctx, dotnet): |
| 83 | + """Create batch script for Windows.""" |
| 84 | + dotnet_runfiles_path = _to_runfiles_path(dotnet.short_path).replace("/", "\\") |
| 85 | + mode = ctx.attr.mode |
| 86 | + |
| 87 | + script_content = """@echo off |
| 88 | +setlocal |
| 89 | +
|
| 90 | +set RUNFILES_DIR=%~dp0%~n0.runfiles |
| 91 | +set DOTNET=%RUNFILES_DIR%\\{dotnet_path} |
| 92 | +
|
| 93 | +if defined BUILD_WORKSPACE_DIRECTORY ( |
| 94 | + set WORKSPACE_ROOT=%BUILD_WORKSPACE_DIRECTORY% |
| 95 | +) else ( |
| 96 | + set WORKSPACE_ROOT=%RUNFILES_DIR%\\_main |
| 97 | +) |
| 98 | +set DOTNET_DIR=%WORKSPACE_ROOT%\\dotnet |
| 99 | +
|
| 100 | +if not exist "%DOTNET_DIR%\\.config\\dotnet-tools.json" ( |
| 101 | + echo ERROR: Could not find dotnet\\.config\\dotnet-tools.json >&2 |
| 102 | + exit /b 1 |
| 103 | +) |
| 104 | +
|
| 105 | +cd /d "%DOTNET_DIR%" |
| 106 | +
|
| 107 | +echo Restoring dotnet tools... |
| 108 | +"%DOTNET%" tool restore |
| 109 | +if %ERRORLEVEL% neq 0 exit /b %ERRORLEVEL% |
| 110 | +
|
| 111 | +echo Running paket {mode}... |
| 112 | +"%DOTNET%" tool run paket {mode} |
| 113 | +if %ERRORLEVEL% neq 0 exit /b %ERRORLEVEL% |
| 114 | +
|
| 115 | +echo Done. Now run: bazel run @rules_dotnet//tools/paket2bazel:paket2bazel -- --dependencies-file %cd%\\paket.dependencies --output-folder %cd% |
| 116 | +""".format( |
| 117 | + dotnet_path = dotnet_runfiles_path, |
| 118 | + mode = mode, |
| 119 | + ) |
| 120 | + |
| 121 | + script = ctx.actions.declare_file(ctx.label.name + ".bat") |
| 122 | + ctx.actions.write( |
| 123 | + output = script, |
| 124 | + content = script_content, |
| 125 | + is_executable = True, |
| 126 | + ) |
| 127 | + return script |
| 128 | + |
| 129 | +paket_deps = rule( |
| 130 | + implementation = _paket_deps_impl, |
| 131 | + attrs = { |
| 132 | + "mode": attr.string( |
| 133 | + doc = "Paket command to run: 'update' for latest versions, 'install' to sync lockfile", |
| 134 | + mandatory = True, |
| 135 | + values = ["update", "install"], |
| 136 | + ), |
| 137 | + "_windows_constraint": attr.label( |
| 138 | + default = "@platforms//os:windows", |
| 139 | + ), |
| 140 | + }, |
| 141 | + executable = True, |
| 142 | + toolchains = ["@rules_dotnet//dotnet:toolchain_type"], |
| 143 | +) |
0 commit comments