1+ #! /bin/bash
2+
3+ # Script to convert GitHub Actions workflows in linksplatform/Interfaces from nuget.exe (with Mono) to dotnet CLI
4+ # Assumes workflows are in .github/workflows/*.yml or *.yaml
5+ # Targets nuget.exe push commands and replaces them with dotnet nuget push
6+ # Ensures actions/setup-dotnet is included for .NET SDK setup
7+
8+ # Exit on error
9+ set -e
10+
11+ # Directory containing GitHub Actions workflows
12+ WORKFLOW_DIR=" .github/workflows"
13+
14+ # Check if workflow directory exists
15+ if [ ! -d " $WORKFLOW_DIR " ]; then
16+ echo " Error: Workflow directory $WORKFLOW_DIR not found. Please run this script from the repository root."
17+ exit 1
18+ fi
19+
20+ # Find all .yml and .yaml files in the workflow directory
21+ WORKFLOW_FILES=$( find " $WORKFLOW_DIR " -type f \( -name " *.yml" -o -name " *.yaml" \) )
22+
23+ # Check if any workflow files exist
24+ if [ -z " $WORKFLOW_FILES " ]; then
25+ echo " Error: No workflow files (.yml or .yaml) found in $WORKFLOW_DIR ."
26+ exit 1
27+ fi
28+
29+ # Function to check if actions/setup-dotnet is already included
30+ has_setup_dotnet () {
31+ local file=" $1 "
32+ grep -q " uses: actions/setup-dotnet@v[1-4]" " $file "
33+ }
34+
35+ # Function to add actions/setup-dotnet step if missing
36+ add_setup_dotnet () {
37+ local file=" $1 "
38+ # Find the first step in the job (after 'steps:') and insert setup-dotnet before it
39+ sed -i.bak ' /steps:/a\
40+ - name: Setup .NET\
41+ uses: actions/setup-dotnet@v4\
42+ with:\
43+ dotnet-version: ' \' ' 8.0.x' \' ' \
44+ ' " $file "
45+ echo " Added actions/setup-dotnet@v4 to $file "
46+ }
47+
48+ # Function to process a workflow file
49+ process_workflow () {
50+ local file=" $1 "
51+ echo " Processing workflow file: $file "
52+
53+ # Create a backup of the original file
54+ cp " $file " " $file .bak"
55+
56+ # Replace nuget source Add with dotnet nuget add source (simplified pattern)
57+ sed -i.bak " s|nuget source Add -Name \" GitHub\" -Source \" https://nuget.pkg.github.com/linksplatform/index.json\" -UserName linksplatform -Password \$ {{ secrets.GITHUB_TOKEN }}|dotnet nuget add source https://nuget.pkg.github.com/linksplatform/index.json --name GitHub --username linksplatform --password \$ {{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text|g" " $file "
58+
59+ # Replace nuget push with dotnet nuget push
60+ sed -i.bak -E " s|nuget push ([^[:space:]]+) -Source \" ([^\" ]+)\" -SkipDuplicate|dotnet nuget push \1 --source \2 --skip-duplicate|g" " $file "
61+
62+ # Replace nuget.exe push with dotnet nuget push (more general pattern)
63+ sed -i.bak -E " s|run:[[:space:]]*(mono[[:space:]]+)?/?([a-zA-Z0-9/._-]*\/)?nuget(\.exe)?[[:space:]]+push[[:space:]]+([^[:space:]].*nupkg)([[:space:]]+--api-key[[:space:]]+\$ \{\{[[:space:]]*[a-zA-Z0-9._-]+\}\})?([[:space:]]+--source[[:space:]]+[a-zA-Z0-9:/._-]+)?|run: dotnet nuget push \4 --api-key \5 --source \6 --skip-duplicate|g" " $file "
64+
65+ # Replace nuget.exe restore with dotnet restore (if present)
66+ sed -i.bak -E " s|run:[[:space:]]*(mono[[:space:]]+)?/?([a-zA-Z0-9/._-]*\/)?nuget(\.exe)?[[:space:]]+restore[[:space:]]+([^[:space:]].*)|run: dotnet restore \4|g" " $file "
67+
68+ # Remove nuget/setup-nuget@v1 action as it's no longer needed
69+ sed -i.bak -E " /- uses: nuget\/setup-nuget@v1/d" " $file "
70+
71+ # Check for changes
72+ if ! diff " $file " " $file .bak" > /dev/null; then
73+ echo " Modified $file to use dotnet CLI instead of nuget.exe"
74+ else
75+ echo " No nuget.exe commands found in $file ; no changes made"
76+ rm " $file .bak" # Remove backup if no changes
77+ fi
78+
79+ # Add actions/setup-dotnet if not present
80+ if ! has_setup_dotnet " $file " ; then
81+ add_setup_dotnet " $file "
82+ fi
83+ }
84+
85+ # Process each workflow file
86+ for file in $WORKFLOW_FILES ; do
87+ process_workflow " $file "
88+ done
89+
90+ # Clean up any remaining backup files if no errors
91+ echo " Cleaning up backup files..."
92+ find " $WORKFLOW_DIR " -name " *.bak" -exec rm {} \;
93+
94+ echo " Conversion complete! Please review changes in $WORKFLOW_DIR and test the updated workflows."
95+ echo " If publishing to nuget.org, ensure NUGET_API_KEY is set in GitHub Secrets."
96+ echo " If targeting a different NuGet feed (e.g., GitHub Packages), update the --source URL and authentication as needed."
0 commit comments