|
| 1 | +<Project> |
| 2 | + <!-- These targets run in consuming projects when they build --> |
| 3 | + <!-- CopilotCliVersion is imported from GitHub.Copilot.SDK.props (generated at SDK build time, packaged alongside) --> |
| 4 | + <Import Project="$(MSBuildThisFileDirectory)GitHub.Copilot.SDK.props" Condition="'$(CopilotCliVersion)' == '' And Exists('$(MSBuildThisFileDirectory)GitHub.Copilot.SDK.props')" /> |
| 5 | + |
| 6 | + <!-- Resolve RID: use explicit RuntimeIdentifier, or infer from current machine --> |
| 7 | + <PropertyGroup> |
| 8 | + <_CopilotRid Condition="'$(RuntimeIdentifier)' != ''">$(RuntimeIdentifier)</_CopilotRid> |
| 9 | + <_CopilotRid Condition="'$(_CopilotRid)' == '' And $([MSBuild]::IsOSPlatform('Windows')) And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X64'">win-x64</_CopilotRid> |
| 10 | + <_CopilotRid Condition="'$(_CopilotRid)' == '' And $([MSBuild]::IsOSPlatform('Windows')) And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'Arm64'">win-arm64</_CopilotRid> |
| 11 | + <_CopilotRid Condition="'$(_CopilotRid)' == '' And $([MSBuild]::IsOSPlatform('Linux')) And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X64'">linux-x64</_CopilotRid> |
| 12 | + <_CopilotRid Condition="'$(_CopilotRid)' == '' And $([MSBuild]::IsOSPlatform('Linux')) And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'Arm64'">linux-arm64</_CopilotRid> |
| 13 | + <_CopilotRid Condition="'$(_CopilotRid)' == '' And $([MSBuild]::IsOSPlatform('OSX')) And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X64'">osx-x64</_CopilotRid> |
| 14 | + <_CopilotRid Condition="'$(_CopilotRid)' == '' And $([MSBuild]::IsOSPlatform('OSX')) And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'Arm64'">osx-arm64</_CopilotRid> |
| 15 | + </PropertyGroup> |
| 16 | + |
| 17 | + <!-- Map RID to platform name used in npm packages --> |
| 18 | + <PropertyGroup> |
| 19 | + <_CopilotPlatform Condition="'$(_CopilotRid)' == 'win-x64'">win32-x64</_CopilotPlatform> |
| 20 | + <_CopilotPlatform Condition="'$(_CopilotRid)' == 'win-arm64'">win32-arm64</_CopilotPlatform> |
| 21 | + <_CopilotPlatform Condition="'$(_CopilotRid)' == 'linux-x64'">linux-x64</_CopilotPlatform> |
| 22 | + <_CopilotPlatform Condition="'$(_CopilotRid)' == 'linux-arm64'">linux-arm64</_CopilotPlatform> |
| 23 | + <_CopilotPlatform Condition="'$(_CopilotRid)' == 'osx-x64'">darwin-x64</_CopilotPlatform> |
| 24 | + <_CopilotPlatform Condition="'$(_CopilotRid)' == 'osx-arm64'">darwin-arm64</_CopilotPlatform> |
| 25 | + <_CopilotBinary Condition="$(_CopilotRid.StartsWith('win-'))">copilot.exe</_CopilotBinary> |
| 26 | + <_CopilotBinary Condition="'$(_CopilotBinary)' == ''">copilot</_CopilotBinary> |
| 27 | + </PropertyGroup> |
| 28 | + |
| 29 | + <!-- Download and extract CLI binary --> |
| 30 | + <Target Name="_DownloadCopilotCli" BeforeTargets="BeforeBuild" Condition="'$(_CopilotPlatform)' != ''"> |
| 31 | + <Error Condition="'$(CopilotCliVersion)' == ''" Text="CopilotCliVersion is not set. The GitHub.Copilot.SDK.props file may be missing from the NuGet package." /> |
| 32 | + |
| 33 | + <!-- Compute paths using version (now available) --> |
| 34 | + <PropertyGroup> |
| 35 | + <_CopilotCacheDir>$(IntermediateOutputPath)copilot-cli\$(CopilotCliVersion)\$(_CopilotPlatform)</_CopilotCacheDir> |
| 36 | + <_CopilotCliBinaryPath>$(_CopilotCacheDir)\$(_CopilotBinary)</_CopilotCliBinaryPath> |
| 37 | + <_CopilotArchivePath>$(_CopilotCacheDir)\copilot.tgz</_CopilotArchivePath> |
| 38 | + <_CopilotDownloadUrl>https://registry.npmjs.org/@github/copilot-$(_CopilotPlatform)/-/copilot-$(_CopilotPlatform)-$(CopilotCliVersion).tgz</_CopilotDownloadUrl> |
| 39 | + </PropertyGroup> |
| 40 | + |
| 41 | + <!-- Delete archive if binary missing (handles partial/corrupted downloads) --> |
| 42 | + <Delete Files="$(_CopilotArchivePath)" Condition="!Exists('$(_CopilotCliBinaryPath)') And Exists('$(_CopilotArchivePath)')" /> |
| 43 | + |
| 44 | + <!-- Download if not cached --> |
| 45 | + <MakeDir Directories="$(_CopilotCacheDir)" Condition="!Exists('$(_CopilotCliBinaryPath)')" /> |
| 46 | + <Message Importance="high" Text="Downloading Copilot CLI $(CopilotCliVersion) for $(_CopilotPlatform)..." Condition="!Exists('$(_CopilotCliBinaryPath)')" /> |
| 47 | + <DownloadFile SourceUrl="$(_CopilotDownloadUrl)" DestinationFolder="$(_CopilotCacheDir)" DestinationFileName="copilot.tgz" |
| 48 | + Condition="!Exists('$(_CopilotCliBinaryPath)')" /> |
| 49 | + |
| 50 | + <!-- Extract using tar (use Windows system tar explicitly to avoid Git bash tar issues) --> |
| 51 | + <PropertyGroup> |
| 52 | + <_TarCommand Condition="$([MSBuild]::IsOSPlatform('Windows'))">$(SystemRoot)\System32\tar.exe</_TarCommand> |
| 53 | + <_TarCommand Condition="'$(_TarCommand)' == ''">tar</_TarCommand> |
| 54 | + </PropertyGroup> |
| 55 | + <Exec Command=""$(_TarCommand)" -xzf "$(_CopilotArchivePath)" --strip-components=1 -C "$(_CopilotCacheDir)"" |
| 56 | + Condition="!Exists('$(_CopilotCliBinaryPath)')" /> |
| 57 | + |
| 58 | + <Error Condition="!Exists('$(_CopilotCliBinaryPath)')" Text="Failed to extract Copilot CLI binary to $(_CopilotCliBinaryPath)" /> |
| 59 | + </Target> |
| 60 | + |
| 61 | + <!-- Copy CLI binary to output runtimes folder and register for transitive copy --> |
| 62 | + <Target Name="_CopyCopilotCliToOutput" AfterTargets="Build" DependsOnTargets="_DownloadCopilotCli" Condition="'$(_CopilotPlatform)' != ''"> |
| 63 | + <PropertyGroup> |
| 64 | + <_CopilotCacheDir>$(IntermediateOutputPath)copilot-cli\$(CopilotCliVersion)\$(_CopilotPlatform)</_CopilotCacheDir> |
| 65 | + <_CopilotCliBinaryPath>$(_CopilotCacheDir)\$(_CopilotBinary)</_CopilotCliBinaryPath> |
| 66 | + <_CopilotOutputDir>$(OutDir)runtimes\$(_CopilotRid)\native</_CopilotOutputDir> |
| 67 | + </PropertyGroup> |
| 68 | + <MakeDir Directories="$(_CopilotOutputDir)" /> |
| 69 | + <Copy SourceFiles="$(_CopilotCliBinaryPath)" DestinationFolder="$(_CopilotOutputDir)" SkipUnchangedFiles="true" /> |
| 70 | + </Target> |
| 71 | + |
| 72 | + <!-- Register CLI binary as content so it flows through project references --> |
| 73 | + <Target Name="_RegisterCopilotCliForCopy" BeforeTargets="GetCopyToOutputDirectoryItems" DependsOnTargets="_DownloadCopilotCli" Condition="'$(_CopilotPlatform)' != ''"> |
| 74 | + <PropertyGroup> |
| 75 | + <_CopilotCacheDir>$(IntermediateOutputPath)copilot-cli\$(CopilotCliVersion)\$(_CopilotPlatform)</_CopilotCacheDir> |
| 76 | + <_CopilotCliBinaryPath>$(_CopilotCacheDir)\$(_CopilotBinary)</_CopilotCliBinaryPath> |
| 77 | + </PropertyGroup> |
| 78 | + <ItemGroup> |
| 79 | + <ContentWithTargetPath Include="$(_CopilotCliBinaryPath)" |
| 80 | + TargetPath="runtimes\$(_CopilotRid)\native\$(_CopilotBinary)" |
| 81 | + CopyToOutputDirectory="PreserveNewest" /> |
| 82 | + </ItemGroup> |
| 83 | + </Target> |
| 84 | +</Project> |
0 commit comments