From 57b28daf4e4246e50a3ac47715282b8401709435 Mon Sep 17 00:00:00 2001 From: Simen Kirkvik Date: Mon, 12 Jan 2026 13:44:42 +0100 Subject: [PATCH] Add script for updating which dotnet sdk rider uses --- .envrc | 38 ++------------- scripts/update-rider.sh | 101 ++++++++++++++++++++++++++++++++++++++++ shell.nix | 4 ++ 3 files changed, 110 insertions(+), 33 deletions(-) create mode 100755 scripts/update-rider.sh diff --git a/.envrc b/.envrc index 4c7405a2..935e1b85 100644 --- a/.envrc +++ b/.envrc @@ -1,4 +1,8 @@ #!/usr/bin/env bash + +export NPINS_DIRECTORY="nix" +export APP_ENV=$USER + # the shebang is ignored, but nice for editors watch_file nix/sources.json @@ -9,36 +13,4 @@ dotenv_if_exists use nix # HACK: Workaround for direnv bug -unset TMP TMPDIR TEMP TEMPDIR - -export NPINS_DIRECTORY="nix" -export APP_ENV=$USER - -# HACK: Configure Rider to use the correct .NET paths from an ambient .NET -use_rider_dotnet() { - # Get paths - DOTNET_PATH=$(readlink "$(which dotnet)") - SETTINGS_FILE=$(find . -maxdepth 1 -type f -name '*.sln.DotSettings.user') - MSBUILD=$(realpath "$(find "$(dirname "$DOTNET_PATH")/../share/dotnet/sdk" -maxdepth 2 -type f -name MSBuild.dll)") - - # Update Rider settings if they exist - if [ -f "$SETTINGS_FILE" ] ; then - xmlstarlet ed --inplace \ - -N wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation" \ - -N x="http://schemas.microsoft.com/winfx/2006/xaml" \ - -N s="clr-namespace:System;assembly=mscorlib" \ - -N ss="urn:shemas-jetbrains-com:settings-storage-xaml" \ - --update "//s:String[@x:Key='/Default/Environment/Hierarchy/Build/BuildTool/DotNetCliExePath/@EntryValue']" \ - --value "$(realpath "$(dirname "$DOTNET_PATH")/../share/dotnet/dotnet")" \ - "$SETTINGS_FILE" - - xmlstarlet ed --inplace \ - -N wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation" \ - -N x="http://schemas.microsoft.com/winfx/2006/xaml" \ - -N s="clr-namespace:System;assembly=mscorlib" \ - -N ss="urn:shemas-jetbrains-com:settings-storage-xaml" \ - --update "//s:String[@x:Key='/Default/Environment/Hierarchy/Build/BuildTool/CustomBuildToolPath/@EntryValue']" \ - --value "$MSBUILD" \ - "$SETTINGS_FILE" - fi -} \ No newline at end of file +unset TMP TMPDIR TEMP TEMPDIR \ No newline at end of file diff --git a/scripts/update-rider.sh b/scripts/update-rider.sh new file mode 100755 index 00000000..2b5359ca --- /dev/null +++ b/scripts/update-rider.sh @@ -0,0 +1,101 @@ +#!/usr/bin/env nix-shell +#! nix-shell -i bash --pure +#! nix-shell -p bash which xmlstarlet + +if [[ ! $# -eq 1 ]]; then + echo "Usage: $0 " + exit 1 +fi + +dotnet_path=$1 + +function stderr() { + echo "$@" 1>&2; +} + +function create_settings_file() { +cat << EOF + + + ${1} + ${2} + +EOF +} + +# HACK: Configure Rider to use the correct .NET paths from an ambient .NET +function use_rider_dotnet() { + local solution_file=$(find . -maxdepth 1 -type f -name '*.slnx' | cut -d'.' -f2 | cut -d'/' -f2) + local settings_file=$(find . -maxdepth 1 -type f -name '*.sln.DotSettings.user') + # Get paths + local cli_path=$(realpath "$dotnet_path") + local dir=$(dirname $cli_path) + local msbuild_path=$(find "$dir" -maxdepth 3 -type f -name MSBuild.dll) + + # stderr "dotnet path is $dir" + # stderr "Found msbuild: $msbuild_path" + + if [ -f "$settings_file" ] ; then + # stderr "Updating rider settings file: $settings_file" + # stderr "Setting DotNetCliExePath to $cli_path" + + # NOTE: check if dotnet binary in share folder settings exists + xml sel -t -v "wpf:ResourceDictionary/s:String[@x:Key='/Default/Environment/Hierarchy/Build/BuildTool/DotNetCliExePath/@EntryValue']" "$settings_file" + if [[ $? -eq 0 ]]; then + xml ed --inplace \ + -N wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation" \ + -N x="http://schemas.microsoft.com/winfx/2006/xaml" \ + -N s="clr-namespace:System;assembly=mscorlib" \ + -N ss="urn:schemas-jetbrains-com:settings-storage-xaml" \ + --update "//s:String[@x:Key='/Default/Environment/Hierarchy/Build/BuildTool/DotNetCliExePath/@EntryValue']" \ + --value "$cli_path" \ + "$settings_file" + else + xml ed --inplace \ + -N wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation" \ + -N x="http://schemas.microsoft.com/winfx/2006/xaml" \ + -N s="clr-namespace:System;assembly=mscorlib" \ + -N ss="urn:schemas-jetbrains-com:settings-storage-xaml" \ + -s /wpf:ResourceDictionary -t elem -n s:String -v "$cli_path" \ + --var new_node '$prev' \ + -i '$new_node' -t attr -n "x:Key" -v "/Default/Environment/Hierarchy/Build/BuildTool/DotNetCliExePath/@EntryValue" \ + "$settings_file" + fi + + xml sel -t -v "wpf:ResourceDictionary/s:String[@x:Key='/Default/Environment/Hierarchy/Build/BuildTool/CustomBuildToolPath/@EntryValue']" "$settings_file" + if [[ $? -eq 0 ]]; then + xmlstarlet ed --inplace \ + -N wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation" \ + -N x="http://schemas.microsoft.com/winfx/2006/xaml" \ + -N s="clr-namespace:System;assembly=mscorlib" \ + -N ss="urn:schemas-jetbrains-com:settings-storage-xaml" \ + --update "//s:String[@x:Key='/Default/Environment/Hierarchy/Build/BuildTool/CustomBuildToolPath/@EntryValue']" \ + --value "$msbuild_path" \ + "$settings_file" + else + xml ed --inplace \ + -N wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation" \ + -N x="http://schemas.microsoft.com/winfx/2006/xaml" \ + -N s="clr-namespace:System;assembly=mscorlib" \ + -N ss="urn:schemas-jetbrains-com:settings-storage-xaml" \ + -s /wpf:ResourceDictionary -t elem -n s:String -v "$cli_path" \ + --var new_node '$prev' \ + -i '$new_node' -t attr -n "x:Key" -v "/Default/Environment/Hierarchy/Build/BuildTool/CustomBuildToolPath/@EntryValue" \ + "$settings_file" + fi + else + create_settings_file $cli_path $msbuild_path > "$solution_file.sln.DotSettings.user" + fi +} + +function main() { + use_rider_dotnet +} + +main \ No newline at end of file diff --git a/shell.nix b/shell.nix index cba2ef31..9fe53d9c 100644 --- a/shell.nix +++ b/shell.nix @@ -47,6 +47,10 @@ pkgs.mkShellNoCC { DOTNET_ROOT = "${dotnet-sdk}/share/dotnet"; LOG_LEVEL = "verbose"; + shellHook = '' + scripts/update-rider.sh ${dotnet-sdk}/bin/dotnet + ''; + # Alternative shells passthru = pkgs.lib.mapAttrs (name: value: pkgs.mkShellNoCC (value // { inherit name; })) { pre-commit.shellHook = pre-commit.shellHook;