Skip to content
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

ADHOC-169: Bash script to add package to projects in a repo #2

Merged
merged 9 commits into from
Jan 31, 2022
Merged
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@ jobs:
dotnet-version: 5.0.*
- name: Install dependencies
run: dotnet restore -p:NuGetBuild=True
- name: Add package
Piedone marked this conversation as resolved.
Show resolved Hide resolved
id: add_package
run: |
curl https://raw.githubusercontent.com/Lombiq/GitHub-Actions/dev/scripts/add-github-sourcelink.sh > add-github-sourcelink.sh
Piedone marked this conversation as resolved.
Show resolved Hide resolved
bash add-github-sourcelink.sh Microsoft.SourceLink.GitHub
rm add-github-sourcelink.sh
- name: Build
run: dotnet build --configuration Release --no-restore -p:NuGetBuild=True -p:LangVersion=Latest
70 changes: 70 additions & 0 deletions scripts/add-github-sourcelink.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/bin/bash

###################################################################################################
# add-github-sourcelink #
Piedone marked this conversation as resolved.
Show resolved Hide resolved
###################################################################################################
# Adds a NuGet package to projects in the solution or the project file. Use it in the git #
# repository's root directory. #
# #
# Use shellcheck (https://github.com/koalaman/shellcheck) for linting and shell-format #
# (https://marketplace.visualstudio.com/items?itemName=foxundermoon.shell-format) for styling. #
###################################################################################################

package_name="$1"

function err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
}

function panic() {
sarahelsaig marked this conversation as resolved.
Show resolved Hide resolved
error_code="$1"
shift

for line in "$@"; do
err "$line"
done

exit "$error_code"
}

function alter-solution() {
solution_file="$1"
[ -f "$solution_file" ] || panic 1 "Couldn't find the solution '$solution_file' in '$PWD'."

for project_path in $(dotnet sln "$solution_file" list | sed '1,2 D'); do
directory="$(dirname "$solution_file")/$(dirname "$project_path")"

pushd "$directory" || panic 2 "Couldn't open the project directory '$directory'."
alter-project "$(basename "$project_path")"
popd || panic 3 "Couldn't return to the original directory."
done
}

function alter-project() {
project_file="$1"
[ -f "$project_file" ] || panic 4 "Couldn't find the project '$project_file' in '$PWD'."

dotnet add "$project_file" package "$package_name"
}

if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo
basename "$0"
echo
echo "USAGE"
echo -e "\tbash $(basename "$0") package_name [solution.sln]\n"
echo "package_name - The name of a NuGet package. The same you'd pass to 'dotnet add package'."
echo "solution.sln - Optional argument to provide the path to the solution file. If none are"
echo " provided then the script looks for .sln or if none found then for .??proj"
echo " (i.e. csproj, fsproj, vbproj) in the current working directory."
elif [ -f "$2" ]; then
alter-solution "$2"
elif solutions=(./*.sln) && ((${#solutions[@]})) && [ -f "${solutions[0]}" ]; then
for solution in "${solutions[@]}"; do
alter-solution "$solution"
done
else
for project in ./*.??proj; do
alter-project "$project"
done
fi