Skip to content

Instantly share code, notes, and snippets.

@DanielMPries
Created December 26, 2019 16:06
Show Gist options
  • Save DanielMPries/36da8b8c66c438293c54d276884a71ad to your computer and use it in GitHub Desktop.
Save DanielMPries/36da8b8c66c438293c54d276884a71ad to your computer and use it in GitHub Desktop.
Allows for dotnet_update_packages command
function read_solution() {
echo "Parsing solution $1"
while IFS='' read -r line || [[ -n "$line" ]]; do
if [[ $line =~ \"([^\"]*.csproj)\" ]]; then
project="${BASH_REMATCH[1]}"
read_project "$(echo "$project"|tr '\\' '/')"
fi
done < "$1"
}
function read_project() {
echo "Parsing project $1"
package_regex='PackageReference Include="([^"]*)" Version="([^"]*)"'
while IFS='' read -r line || [[ -n "$line" ]]; do
if [[ $line =~ $package_regex ]]; then
name="${BASH_REMATCH[1]}"
version="${BASH_REMATCH[2]}"
if [[ $version != *-* ]]; then
dotnet add "$1" package "$name"
fi
fi
done < $1
}
function dotnet_update_packages() {
has_read=0
if [[ $1 =~ \.sln$ ]]; then
read_solution "$1"
return 0
elif [[ $1 =~ \.csproj$ ]]; then
read_project "$1"
return 0
elif [[ $1 != "" ]]; then
echo "Invalid file $1"
return 1
fi
for solution in ./*.sln; do
if [ ! -f ${solution} ]; then
continue
fi
read_solution "${solution}"
has_read=1
done
if [[ $has_read -eq 1 ]]; then
return 0
fi
for project in ./*.csproj; do
if [ ! -f ${project} ]; then
continue
fi
read_project "${project}"
done
}
export -f dotnet_update_packages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment