|
1 | 1 | #!/usr/bin/env bash |
| 2 | +set -euo pipefail |
2 | 3 |
|
3 | 4 | if [ ${#} -le 1 ] |
4 | 5 | then |
5 | 6 | echo "Usage: generate.sh <openapi.yaml> <version>" >&2 |
6 | 7 | exit 1 |
7 | 8 | fi |
8 | 9 |
|
9 | | -rm src docs *.nupkg *.snupkg -rf |
| 10 | +spec="${1}" |
| 11 | +version="${2}" |
10 | 12 |
|
| 13 | +# Applies a sed expression to a file, but only after confirming the text it targets is still |
| 14 | +# there. sed exits 0 when it matches nothing, so without this guard a patch that stopped |
| 15 | +# applying after a generator upgrade would fail silently. |
| 16 | +patch_generated() { |
| 17 | + local file="${1}" probe="${2}" expression="${3}" |
| 18 | + |
| 19 | + if ! grep -qF "${probe}" "${file}" |
| 20 | + then |
| 21 | + echo "generate.sh: '${probe}' is no longer present in ${file}; the patch below is stale" >&2 |
| 22 | + exit 1 |
| 23 | + fi |
| 24 | + |
| 25 | + sed -i "${expression}" "${file}" |
| 26 | +} |
| 27 | + |
| 28 | +rm -rf src docs ./*.nupkg ./*.snupkg |
| 29 | + |
| 30 | +# Output this repository does not ship is listed in .openapi-generator-ignore, so it is never |
| 31 | +# written. Everything VRChat-specific about the generated code lives in ./templates, and the |
| 32 | +# csproj customisation in ./templates/netcore_project.additions.mustache. |
11 | 33 | openapi-generator generate \ |
12 | 34 | -g csharp \ |
13 | 35 | -t ./templates \ |
14 | 36 | --library httpclient \ |
15 | | ---additional-properties=packageGuid=1c420561-97f1-4810-ad2d-cd344d27170a,packageName=VRChat.API,packageTags=vrchat,packageVersion="${2}",targetFramework=net8.0,licenseId=MIT,equatable=true \ |
| 37 | +--additional-properties=packageGuid=1c420561-97f1-4810-ad2d-cd344d27170a,packageName=VRChat.API,packageVersion="${version}",targetFramework=net8.0,licenseId=MIT,equatable=true,packageAuthors="VRChat API Docs Community",packageCompany="VRChat API Docs Community",packageTitle="VRChat API Library for .NET",packageDescription="VRChat API Library for .NET",packageCopyright="Copyright © 2021 Owners of GitHub organisation \"vrchatapi\" and individual contributors." \ |
16 | 38 | --git-user-id=vrchatapi \ |
17 | 39 | --git-repo-id=vrchatapi-csharp \ |
18 | 40 | -o . \ |
19 | | --i "${1}" \ |
| 41 | +-i "${spec}" \ |
20 | 42 | --http-user-agent="vrchatapi-csharp" |
21 | 43 |
|
22 | | -rm build.sh |
23 | | -rm build.bat |
24 | | -rm git_push.sh |
25 | | -rm mono_nunit_test.sh |
26 | | -rm nuget.exe |
27 | | -rm appveyor.yml |
28 | | - |
29 | | -rm -rf docs/ |
30 | | -rm -rf api/ |
31 | | -rm -rf src/VRChat.API.Test/ |
32 | | - |
33 | | -# Move wrapper code to src/VRChat.API/Client/ |
34 | | -cp -r wrapper/VRChat.API/Client/* src/VRChat.API/Client/ |
35 | | - |
36 | 44 | cp wrapper/VRChat.API.Extensions.Hosting/vrc_cat.ico src/VRChat.API/vrc_cat.ico |
37 | 45 | cp wrapper/VRChat.API.Extensions.Hosting/vrc_cat.png src/VRChat.API/vrc_cat.png |
38 | 46 |
|
39 | | -# Fix cookie handling |
40 | | -for file in $(find ./src/VRChat.API -name '*.cs'); do |
41 | | - sed -i 's/new Cookie("auth", this.Configuration.GetApiKeyWithPrefix("auth"))/new Cookie("auth", this.Configuration.GetApiKeyWithPrefix("auth"), "\/", "api.vrchat.cloud")/g' $file |
42 | | - sed -i 's/new Cookie("twoFactorAuth", this.Configuration.GetApiKeyWithPrefix("twoFactorAuth"))/new Cookie("twoFactorAuth", this.Configuration.GetApiKeyWithPrefix("twoFactorAuth"), "\/", "api.vrchat.cloud")/g' $file |
43 | | - sed -i 's/new Cookie(cookie.Name, cookie.Value)/new Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain)/g' $file |
44 | | -done |
45 | | - |
46 | | -# Add ITwoFactorCode to two-factor authentication types |
47 | | -sed -i 's/\(class.*IValidatableObject\)/\1, ITwoFactorCode/' src/VRChat.API/Model/TwoFactorAuthCode.cs |
48 | | -sed -i 's/\(class.*IValidatableObject\)/\1, ITwoFactorCode/' src/VRChat.API/Model/TwoFactorEmailCode.cs |
49 | | - |
50 | | -# Add icons and readme to package |
51 | | -sed -i ':a;N;$!ba;s|\(.*\)</ItemGroup>|\1\t <Content Include="vrc_cat.ico" />\n\t <None Include="..\\README.md">\n\t <Pack>True</Pack>\n\t <PackagePath>\\</PackagePath>\n\t </None>\n\t <None Include="vrc_cat.png">\n\t <Pack>True</Pack>\n\t <PackagePath>\\</PackagePath>\n\t </None>\n </ItemGroup>|' src/VRChat.API/VRChat.API.csproj |
52 | | -sed -i '/<PackageTags>vrchat<\/PackageTags>/a\ <ApplicationIcon>vrc_cat.ico</ApplicationIcon>' src/VRChat.API/VRChat.API.csproj |
53 | | - |
54 | | -# Adjust package tags |
55 | | -sed -i 's/<PackageTags>vrchat<\/PackageTags>/<PackageTags>vrchat,vrcapi,vrc-api,vrc<\/PackageTags>/' src/VRChat.API/VRChat.API.csproj |
56 | | - |
57 | | -# Fix username and password encoding |
58 | | -sed -i 's/VRChat.API.Client.ClientUtils.Base64Encode(this.Configuration.Username + \":\" + this.Configuration.Password)/VRChat.API.Client.ClientUtils.Base64Encode(System.Web.HttpUtility.UrlEncode(this.Configuration.Username) + ":" + System.Web.HttpUtility.UrlEncode(this.Configuration.Password))/g' src/VRChat.API/Api/AuthenticationApi.cs |
59 | | - |
60 | | -# Fix fields in csproj |
61 | | -sed -i 's/OpenAPI Library/VRChat API Library for .NET/' src/VRChat.API/VRChat.API.csproj |
62 | | -sed -i 's/A library generated from a OpenAPI doc/VRChat API Library for .NET/' src/VRChat.API/VRChat.API.csproj |
63 | | -sed -i 's/No Copyright/Copyright © 2021 Owners of GitHub organisation "vrchatapi" and individual contributors./' src/VRChat.API/VRChat.API.csproj |
64 | | -sed -i 's/OpenAPI/VRChat API Docs Community/' src/VRChat.API/VRChat.API.csproj |
65 | | -sed -i 's/Minor update/Automated deployment/' src/VRChat.API/VRChat.API.csproj |
66 | | - |
67 | | -# Fix failure to compile |
68 | | -sed -i 's/<GenerateAssemblyInfo>false<\/GenerateAssemblyInfo>/<GenerateAssemblyInfo>true<\/GenerateAssemblyInfo>/' src/VRChat.API/VRChat.API.csproj |
69 | | - |
70 | | -# Update VRChat.API.Extensions.Hosting version |
71 | | -sed -i "s|<Version>[^<]*</Version>|<Version>${2}</Version>|g" wrapper/VRChat.API.Extensions.Hosting/VRChat.API.Extensions.Hosting.csproj |
72 | | - |
73 | | -# Add README.md to fields |
74 | | -sed -i '/PackageTags/a \ <PackageReadmeFile>README.md<\/PackageReadmeFile>' src/VRChat.API/VRChat.API.csproj |
75 | | -sed -i '/System.ComponentModel.Annotations/a \ <None Include="..\\README.md" Pack="true" PackagePath="\\"/>' src/VRChat.API/VRChat.API.csproj |
76 | | - |
77 | | -# Add Otp.NET package to project |
78 | | -sed -i '/JsonSubTypes/a \ <PackageReference Include="Otp.NET" Version="1.4.0" \/>' src/VRChat.API/VRChat.API.csproj |
79 | | - |
80 | | -# Make CurrentUser fields optional for 2FA response compatibility |
81 | | -sed -i 's/IsRequired = true/IsRequired = false/g' src/VRChat.API/Model/CurrentUser.cs |
82 | | - |
83 | | -# Add RequiresTwoFactorAuth property to CurrentUser |
84 | | -sed -i '/public string UserIcon { get; set; }/a\\n /// <summary>\n /// An array of two-factor authentication methods available to use to with two factor authentication.\n /// </summary>\n [DataMember(Name = "requiresTwoFactorAuth", IsRequired = false, EmitDefaultValue = true)]\n public List<string> RequiresTwoFactorAuth { get; set; }' src/VRChat.API/Model/CurrentUser.cs |
85 | | - |
86 | | -# Remove messily pasted markdown at top of every file |
87 | | -for i in src/VRChat.API/*/*.cs; do |
88 | | - sed -i '/VRChat API Banner/d' $i |
89 | | -done |
90 | | - |
91 | 47 | cp README.md src/VRChat.API/ |
92 | 48 | cp README.md src/ |
| 49 | + |
| 50 | +# The only edit to generated code that no template or partial class can express: CurrentUser is |
| 51 | +# also returned mid-login, before two-factor verification, with most of its fields absent. The |
| 52 | +# specification marks them required, which makes deserialising that response throw, so the |
| 53 | +# generated attributes are relaxed for this one model. |
| 54 | +patch_generated src/VRChat.API/Model/CurrentUser.cs \ |
| 55 | + 'IsRequired = true' 's/IsRequired = true/IsRequired = false/g' |
| 56 | + |
| 57 | +# VRChat.API.Extensions.Hosting is hand-maintained, so its version is stamped rather than generated. |
| 58 | +patch_generated wrapper/VRChat.API.Extensions.Hosting/VRChat.API.Extensions.Hosting.csproj \ |
| 59 | + '<Version>' "s|<Version>[^<]*</Version>|<Version>${version}</Version>|g" |
0 commit comments