Skip to content

Commit

Permalink
Fallback to curl direct JWK from devlooped.com if pwsh/jq not available
Browse files Browse the repository at this point in the history
NOTES:
1. `pwsh` cannot be replaced with `powershell` because the output of `curl` is different and breaks `jq`
2. `jq` would still be needed even if replacing `pwsh`
3. Downloading the `jwk` directly from sponsorlink.devlooped.com is done via curl.
   This is suboptimal and slower (typically) than downloading from github, hence it's used as a fallback only.
4. If no curl is available (legacy windows?), use built-in powershell.

This guarantees we can always download the JWK.
  • Loading branch information
kzu committed Jul 22, 2024
1 parent ffba1f3 commit ef41252
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 11 deletions.
40 changes: 36 additions & 4 deletions samples/dotnet/SponsorLink.Analyzer.targets
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,44 @@ partial class SponsorLink
</Target>

<Target Name="DownloadDevloopedJwk" BeforeTargets="GetAssemblyAttributes" Inputs="$(MSBuildProjectFullPath)" Outputs="$(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk">
<Exec Command="pwsh -nop -f $(MSBuildThisFileDirectory)jwk.ps1" ConsoleToMSBuild="true" EchoOff="true">
<Output TaskParameter="ConsoleOutput" PropertyName="RawJwk"/>
<Exec Command="pwsh --version" ContinueOnError="true" IgnoreExitCode="true">
<Output TaskParameter="ExitCode" PropertyName="PwshExitCode" />
</Exec>
<Exec Command="jq --version" ContinueOnError="true" IgnoreExitCode="true">
<Output TaskParameter="ExitCode" PropertyName="JqExitCode" />
</Exec>
<Exec Command="curl --version" ContinueOnError="true" IgnoreExitCode="true">
<Output TaskParameter="ExitCode" PropertyName="CurlExitCode" />
</Exec>

<PropertyGroup>
<JwkDownload Condition="'$(PwshExitCode)' != '0' or '$(JqExitCode)' != '0'">true</JwkDownload>
<CurlDownload Condition="'$(CurlExitCode)' == '0'">true</CurlDownload>
</PropertyGroup>

<!-- Special case for Windows with no curl (and no pwsh/jq), super legacy -->
<Exec Command="powershell -nop -C &quot;invoke-restmethod https://sponsorlink.devlooped.com/jwk -outfile $(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk&quot;"
EchoOff="true"
Condition="'$(JwkDownload)' == 'true' and '$(CurlDownload)' != '0'" />

<!-- General JWK download case for all OSes, when no pwsh/jq is available -->
<Exec Command="curl --output $(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk https://sponsorlink.devlooped.com/jwk"
EchoOff="true"
Condition="'$(JwkDownload)' == 'true' and '$(CurlDownload)' == '0'" />

<!-- Base case when pwsh+jq are available, download from github -->
<Exec Command="pwsh -NonInteractive -NoProfile -File '$(MSBuildThisFileDirectory)jwk.ps1'"
Condition="'$(JwkDownload)' != 'true'"
ConsoleToMSBuild="true"
EchoOff="true">
<Output TaskParameter="ConsoleOutput" PropertyName="RawJwk" />
<Output TaskParameter="ExitCode" PropertyName="MSBuildLastExitCode" />
</Exec>
<Error Text="$(RawJwk)" Condition="'$(MSBuildLastExitCode)' != '0'" />
<WriteLinesToFile File="$(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk" Lines="$(RawJwk)" Overwrite="true" />
<Error Text="$(RawJwk)" Condition="'$(JwkDownload)' != 'true' and '$(MSBuildLastExitCode)' != '0'" />
<WriteLinesToFile File="$(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk"
Lines="$(RawJwk)"
Overwrite="true"
Condition="'$(JwkDownload)' != 'true'" />
</Target>

<Target Name="ReadDevloopedJwk" DependsOnTargets="DownloadDevloopedJwk" BeforeTargets="GetAssemblyAttributes">
Expand Down
46 changes: 39 additions & 7 deletions samples/dotnet/SponsorLink/SponsorLink.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Resources.es.resx" ManifestResourceName="Devlooped.Sponsors.%(Filename)"/>
<EmbeddedResource Update="Resources.es.resx" ManifestResourceName="Devlooped.Sponsors.%(Filename)" />
<EmbeddedResource Update="Resources.resx" ManifestResourceName="Devlooped.Sponsors.%(Filename)" StronglyTypedManifestPrefix="Devlooped.Sponsors" StronglyTypedClassName="%(Filename)" StronglyTypedNamespace="Devlooped.Sponsors" StronglyTypedLanguage="$(Language)" />
</ItemGroup>

Expand All @@ -39,8 +39,7 @@
</ItemGroup>

<Target Name="EmitFunding" BeforeTargets="GenerateMSBuildEditorConfigFileShouldRun" Inputs="$(MSBuildAllProjects)" Outputs="$(IntermediateOutputPath)SponsorLink.g.cs">
<Warning Condition="'$(FundingPackageId)' == ''" Code="SL001"
Text="Could not determine value of FundingPackageId (defaulted to PackageId). Defaulting it to FundingProduct ('$(FundingProduct)'). Make sure this matches the containing package id, or set an explicit value." />
<Warning Condition="'$(FundingPackageId)' == ''" Code="SL001" Text="Could not determine value of FundingPackageId (defaulted to PackageId). Defaulting it to FundingProduct ('$(FundingProduct)'). Make sure this matches the containing package id, or set an explicit value." />
<PropertyGroup>
<!-- Default to Product, which is most common for single-package products (i.e. Moq) -->
<FundingPackageId Condition="'$(FundingPackageId)' == ''">$(FundingProduct)</FundingPackageId>
Expand All @@ -64,13 +63,46 @@ partial class SponsorLink
</ItemGroup>
</Target>

<!-- Keep in sync with ..\SponsorLink.Analyzer.targets -->
<Target Name="DownloadDevloopedJwk" BeforeTargets="GetAssemblyAttributes" Inputs="$(MSBuildProjectFullPath)" Outputs="$(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk">
<Exec Command="pwsh -nop -f $(MSBuildThisFileDirectory)..\jwk.ps1" ConsoleToMSBuild="true" EchoOff="true">
<Output TaskParameter="ConsoleOutput" PropertyName="DevloopedJwk" />
<Exec Command="pwsh --version" ContinueOnError="true" IgnoreExitCode="true">
<Output TaskParameter="ExitCode" PropertyName="PwshExitCode" />
</Exec>
<Exec Command="jq --version" ContinueOnError="true" IgnoreExitCode="true">
<Output TaskParameter="ExitCode" PropertyName="JqExitCode" />
</Exec>
<Exec Command="curl --version" ContinueOnError="true" IgnoreExitCode="true">
<Output TaskParameter="ExitCode" PropertyName="CurlExitCode" />
</Exec>

<PropertyGroup>
<JwkDownload Condition="'$(PwshExitCode)' != '0' or '$(JqExitCode)' != '0'">true</JwkDownload>
<CurlDownload Condition="'$(CurlExitCode)' == '0'">true</CurlDownload>
</PropertyGroup>

<!-- Special case for Windows with no curl (and no pwsh/jq), super legacy -->
<Exec Command="powershell -nop -C &quot;invoke-restmethod https://sponsorlink.devlooped.com/jwk -outfile $(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk&quot;"
EchoOff="true"
Condition="'$(JwkDownload)' == 'true' and '$(CurlDownload)' != '0'" />

<!-- General JWK download case for all OSes, when no pwsh/jq is available -->
<Exec Command="curl --output $(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk https://sponsorlink.devlooped.com/jwk"
EchoOff="true"
Condition="'$(JwkDownload)' == 'true' and '$(CurlDownload)' == '0'" />

<!-- Base case when pwsh+jq are available, download from github -->
<Exec Command="pwsh -NonInteractive -NoProfile -File '$(MSBuildThisFileDirectory)jwk.ps1'"
Condition="'$(JwkDownload)' != 'true'"
ConsoleToMSBuild="true"
EchoOff="true">
<Output TaskParameter="ConsoleOutput" PropertyName="RawJwk" />
<Output TaskParameter="ExitCode" PropertyName="MSBuildLastExitCode" />
</Exec>
<Error Text="$(DevloopedJwk)" Condition="'$(MSBuildLastExitCode)' != '0'" />
<WriteLinesToFile File="$(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk" Lines="$(DevloopedJwk)" Overwrite="true" />
<Error Text="$(RawJwk)" Condition="'$(JwkDownload)' != 'true' and '$(MSBuildLastExitCode)' != '0'" />
<WriteLinesToFile File="$(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk"
Lines="$(RawJwk)"
Overwrite="true"
Condition="'$(JwkDownload)' != 'true'" />
</Target>

<Target Name="ReadDevloopedJwk" DependsOnTargets="DownloadDevloopedJwk" BeforeTargets="GetAssemblyAttributes">
Expand Down

0 comments on commit ef41252

Please sign in to comment.