Skip to content

Commit

Permalink
Update Targets:
Browse files Browse the repository at this point in the history
- Make Generate targets public
- Make Restore targets private
- *Properly* support solution level restore via Directory.Solution.props
- Update sample/test project to use a solution (for testing solution level restore)
- Update Publish targets to use DependsOn
- Support disabling auto-references to GameLibs and PolySharp
  • Loading branch information
Cryptoc1 committed Jan 6, 2024
1 parent f841b6d commit f777c3f
Show file tree
Hide file tree
Showing 21 changed files with 111 additions and 46 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ jobs:
plugin-cache-${{ runner.os }}-
- name: restore sdk
run: dotnet restore src/LethalCompany.Plugin.Sdk.csproj --locked-mode
run: dotnet restore --locked-mode

- name: build sdk
run: dotnet build src/LethalCompany.Plugin.Sdk.csproj --no-restore
run: dotnet build --no-restore

- name: restore sample
run: dotnet restore test/LethalCompany.SdkSample.csproj --locked-mode
run: dotnet restore sample/sdk-sample.sln --locked-mode

- name: build sample
run: dotnet build test/LethalCompany.SdkSample.csproj --no-restore
run: dotnet build sample/sdk-sample.sln --no-restore

pack:
if: startsWith(github.ref, 'refs/heads/main') || startsWith(github.ref, 'refs/heads/develop')
Expand Down
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,16 @@ In the new `.csproj`, update the `Sdk="Microsoft.NET.Sdk"` attribute at the top
</Project>
```

***Or***, create a `global.json` file in the root of your solution, and specify the `LethalCompany.Plugin.Sdk`:
_**Or** (recommended)_

Create a `global.json` file in the root of your solution, and specify the `LethalCompany.Plugin.Sdk`:
```json
{
"sdk": {
"allowPrerelease": false,
"rollForward": "latestMajor",
"version": "8.0.100"
},
"msbuild-sdk": {
"LethalCompany.Plugin.Sdk": "{VERSION}"
}
Expand Down Expand Up @@ -167,11 +174,13 @@ Assembly resolution can be configured by specifying glob patterns for the `Exclu

Due to limitiations in how MSBuild handles solution files, the Sdk is unable restore Thunderstore dependencies when directly restoring a solution (e.g. via `dotnet restore example-plugin.sln`).

To workaround this, create a `Directory.Solution.targets` file adjacent to the `.sln` file, that directly imports the `Restore.targets` from the Sdk:
To workaround this, create a `Directory.Solution.targets` file adjacent to the `.sln` file, that directly imports `Solution.targets` from the Sdk:
```xml
<Project>

<Import Project="Restore.targets" Sdk="LethalCompany.Plugin.Sdk" />
<Import Project="Solution.targets" Sdk="LethalCompany.Plugin.Sdk" />

<Project>
```
```

> _It is recommended to use `global.json` when using `Directory.Solution.targets` to centralize the versioning of `LethalCompany.Plugin.Sdk`._
6 changes: 0 additions & 6 deletions lc-plugin-sdk.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LethalCompany.Plugin.Sdk", "src\LethalCompany.Plugin.Sdk.csproj", "{85FB813D-1364-46E0-BA26-CA2FC2C273D4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LethalCompany.SdkSample", "test\LethalCompany.SdkSample.csproj", "{3B3EAC57-F36B-47B2-9B90-CE0D6ED5B638}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -17,10 +15,6 @@ Global
{85FB813D-1364-46E0-BA26-CA2FC2C273D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85FB813D-1364-46E0-BA26-CA2FC2C273D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85FB813D-1364-46E0-BA26-CA2FC2C273D4}.Release|Any CPU.Build.0 = Release|Any CPU
{3B3EAC57-F36B-47B2-9B90-CE0D6ED5B638}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3B3EAC57-F36B-47B2-9B90-CE0D6ED5B638}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3B3EAC57-F36B-47B2-9B90-CE0D6ED5B638}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3B3EAC57-F36B-47B2-9B90-CE0D6ED5B638}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
5 changes: 5 additions & 0 deletions sample/Directory.Solution.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project>

<Import Project="..\src\Sdk\Solution.targets" />

</Project>
7 changes: 7 additions & 0 deletions sample/Library/LibraryClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SdkSample.Library
{
public class LibraryClass
{
public const string Value = "Hello, World!";
}
}
7 changes: 7 additions & 0 deletions sample/Library/SdkSample.Library.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>

</Project>
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using BepInEx.Logging;

namespace LethalCompany.SdkSample.Patches;
namespace SdkSample.Plugin.Patches;

[HarmonyPatch(typeof(Terminal))]
public sealed class TerminalPatches
Expand Down
File renamed without changes.
6 changes: 4 additions & 2 deletions test/SamplePlugin.cs → sample/Plugin/SamplePlugin.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
namespace LethalCompany.SdkSample;
using SdkSample.Library;

namespace SdkSample.Plugin;

[BepInPlugin(GeneratedPluginInfo.Identifier, GeneratedPluginInfo.Name, GeneratedPluginInfo.Version)]
public sealed class SamplePlugin : BaseUnityPlugin
{
public void Awake()
{
Logger.LogInfo($"TEST: {LethalLib.Modules.Levels.LevelTypes.OffenseLevel}");
Logger.LogInfo($"TEST: {LethalLib.Modules.Levels.LevelTypes.OffenseLevel}, {LibraryClass.Value}");
_ = Harmony.CreateAndPatchAll(
typeof(SamplePlugin).Assembly,
GeneratedPluginInfo.Identifier);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<Import Project="..\src\Sdk\Sdk.props" />
<Import Project="..\..\src\Sdk\Sdk.props" />

<PropertyGroup>
<Title>Plugin SDK Sample</Title>
Expand All @@ -9,16 +9,21 @@
<PluginId>cryptoc1.lethalcompany.sdksample</PluginId>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<Version>1.0.0</Version>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ThunderDependency Include="Evaisa-LethalLib" Version="0.10.1" />
<!-- <ThunderDependency Include="no00ob-LCSoundTool-1.4.0" /> -->
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Library\SdkSample.Library.csproj" />
</ItemGroup>

<PropertyGroup>
<PluginSdkAssemblyDir>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)..\src\bin\Debug\'))</PluginSdkAssemblyDir>
<PluginSdkAssemblyDir>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)..\..\src\bin\Debug\'))</PluginSdkAssemblyDir>
</PropertyGroup>

<Import Project="..\src\Sdk\Sdk.targets" />
<Import Project="..\..\src\Sdk\Sdk.targets" />
</Project>
File renamed without changes
7 changes: 2 additions & 5 deletions test/packages.lock.json → sample/Plugin/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
"resolved": "8.0.0",
"contentHash": "DxiTgkCl3CGq1rYmBX2wjY7XGbxiBdL4J+/AJIAFLKy5z70NxhnVRnPghnicXZ8oF6JKVXlW3xwznRbI3ioEKg=="
},
"PolySharp": {
"type": "Direct",
"requested": "[1.14.1, )",
"resolved": "1.14.1",
"contentHash": "mOOmFYwad3MIOL14VCjj02LljyF1GNw1wP0YVlxtcPvqdxjGGMNdNJJxHptlry3MOd8b40Flm8RPOM8JOlN2sQ=="
"sdksample.library": {
"type": "Project"
}
}
}
Expand Down
File renamed without changes.
31 changes: 31 additions & 0 deletions sample/sdk-sample.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SdkSample.Plugin", "Plugin\SdkSample.Plugin.csproj", "{30431DF2-9EDE-4739-8EA9-C896A5354262}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SdkSample.Library", "Library\SdkSample.Library.csproj", "{F6CCAE5A-633E-4859-BA3C-3A6AC709F799}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{30431DF2-9EDE-4739-8EA9-C896A5354262}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{30431DF2-9EDE-4739-8EA9-C896A5354262}.Debug|Any CPU.Build.0 = Debug|Any CPU
{30431DF2-9EDE-4739-8EA9-C896A5354262}.Release|Any CPU.ActiveCfg = Release|Any CPU
{30431DF2-9EDE-4739-8EA9-C896A5354262}.Release|Any CPU.Build.0 = Release|Any CPU
{F6CCAE5A-633E-4859-BA3C-3A6AC709F799}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F6CCAE5A-633E-4859-BA3C-3A6AC709F799}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F6CCAE5A-633E-4859-BA3C-3A6AC709F799}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F6CCAE5A-633E-4859-BA3C-3A6AC709F799}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {64A61DA6-C834-4BD5-AC51-435202DF6151}
EndGlobalSection
EndGlobal
4 changes: 2 additions & 2 deletions src/Sdk/Generate.targets
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<!--
Executes the `GeneratePluginInfoCode` task to generate a type containing plugin metadata defined via MSBuild properties.
-->
<Target Name="_GeneratePluginInfo" BeforeTargets="BeforeCompile;CoreCompile" Inputs="$(MSBuildAllProjects)" Outputs="$(IntermediateOutputPath)$(MSBuildProjectName).PluginInfo.g.cs">
<Target Name="GeneratePluginInfo" BeforeTargets="BeforeCompile;CoreCompile" Inputs="$(MSBuildAllProjects)" Outputs="$(IntermediateOutputPath)$(MSBuildProjectName).PluginInfo.g.cs">
<ItemGroup>
<Compile Include="$(IntermediateOutputPath)$(MSBuildProjectName).PluginInfo.g.cs" />
<FileWrites Include="$$(IntermediateOutputPath)$(MSBuildProjectName).PluginInfo.g.cs" />
Expand All @@ -23,7 +23,7 @@
<!--
Executes the `GeneratePluginManifestJson` task to generate the json text of the `manifest.json` used when publishing a Thunderstore package.
-->
<Target Name="_GeneratePluginManifest" BeforeTargets="BeforeCompile;CoreCompile" Inputs="$(MSBuildAllProjects)" Outputs="$(IntermediateOutputPath)$(MSBuildProjectName).manifest.g.json">
<Target Name="GeneratePluginManifest" BeforeTargets="BeforeCompile;CoreCompile" Inputs="$(MSBuildAllProjects)" Outputs="$(IntermediateOutputPath)$(MSBuildProjectName).manifest.g.json">
<ItemGroup>
<Content Include="$(IntermediateOutputPath)$(MSBuildProjectName).manifest.g.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
Expand Down
4 changes: 2 additions & 2 deletions src/Sdk/Publish.targets
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
<!--
Copies custom build assets to the publish directory, prior to the default publishing process.
-->
<Target Name="_CopyToPublishDir" AfterTargets="Publish" BeforeTargets="PublishPlugin">
<Target Name="_CopyToPublishDir">
<Copy SourceFiles="$(IntermediateOutputPath)$(MSBuildProjectName).manifest.g.json" DestinationFiles="$(PublishDir)manifest.json" SkipUnchangedFiles="true" Condition=" Exists('$(IntermediateOutputPath)$(MSBuildProjectName).manifest.g.json') " />
</Target>

<!--
Creates a Thunderstore package from the content of the default publish directory.
-->
<Target Name="PublishPlugin" AfterTargets="Publish">
<Target Name="PublishPlugin" AfterTargets="Publish" DependsOnTargets="_CopyToPublishDir">
<Warning Code="LC001" Text="Plugin was not built in Release mode, users may experience an impact to performance!" Condition=" '$(Configuration)' != 'Release' AND '$(StagePlugin)' != 'true' AND '$(DisableReleaseOptimizations)' != 'true' " />

<Error Text="Failed to Stage Plugin, PluginStagingProfile directory '$(PluginProfileDir)' does not exist." Condition=" '$(StagePlugin)' == 'true' AND !Exists('$(PluginProfileDir)') " />
Expand Down
8 changes: 7 additions & 1 deletion src/Sdk/Restore.targets
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<!--
Executes the `RestorePluginDependencies` task to restore `ThunderDependency` items
-->
<Target Name="RestorePlugins" BeforeTargets="Restore" AfterTargets="_GetAllRestoreProjectPathItems" Inputs="$(MSBuildAllProjects)" Outputs="$(PluginRestoreOutputs)">
<Target Name="_RestorePlugins" BeforeTargets="Restore" AfterTargets="_GetAllRestoreProjectPathItems" Inputs="$(MSBuildAllProjects)" Outputs="$(PluginRestoreOutputs)">
<ItemGroup>
<FileWrites Include="$(PluginLockFile)" Condition=" '$(PluginLockFile)' != '' " />
<FileWrites Include="$(PluginAssetsFile)" />
Expand All @@ -31,4 +31,10 @@
<WriteLinesToFile File="$(PluginAssetsFile)" Lines="$(_GeneratedAssetsJson)" Overwrite="true" WriteOnlyWhenDifferent="true" Condition=" '$(_GeneratedAssetsJson)' != '' " />
</Target>

<Target Name="_IsProjectPluginRestoreSupported" Returns="@(_ValidProjectsForPluginRestore)">
<ItemGroup>
<_ValidProjectsForPluginRestore Include="$(MSBuildProjectFullPath)" />
</ItemGroup>
</Target>

</Project>
7 changes: 1 addition & 6 deletions src/Sdk/Sdk.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" Condition=" '$(MicrosoftCommonPropsHasBeenImported)' != 'true' "/>
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" Condition=" '$(UsingMicrosoftNETSdk)' != 'true' OR '$(MicrosoftCommonPropsHasBeenImported)' != 'true' " />

<ItemDefinitionGroup>
<ThunderDependency>
Expand Down Expand Up @@ -41,11 +41,6 @@
<ItemGroup>
<PackageReference Include="BepInEx.Analyzers" Version="1.0.8" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0" PrivateAssets="all" />
<PackageReference Include="LethalAPI.GameLibs" Version="45.1.0" PrivateAssets="all" ExcludeAssets="all" GeneratePathProperty="true" NoWarn="NU1701" />
<PackageReference Include="PolySharp" Version="1.14.1" PrivateAssets="all" />

<!-- NOTE: no need to reference stdlib -->
<Reference Include="$(PkgLethalAPI_GameLibs)\lib\*.dll" Exclude="$(PkgLethalAPI_GameLibs)\lib\mscorlib.dll;$(PkgLethalAPI_GameLibs)\lib\netstandard.dll;$(PkgLethalAPI_GameLibs)\lib\System*.dll;" Private="false" />
</ItemGroup>

<ItemGroup>
Expand Down
13 changes: 12 additions & 1 deletion src/Sdk/Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@
</Content>
</ItemGroup>

<ItemGroup Condition=" '$(DisableAutoReferenceGameLibs)' != 'true' ">
<PackageReference Include="LethalAPI.GameLibs" Version="45.1.0" PrivateAssets="all" ExcludeAssets="all" GeneratePathProperty="true" NoWarn="NU1701" />

<!-- NOTE: no need to reference stdlib -->
<Reference Include="$(PkgLethalAPI_GameLibs)\lib\*.dll" Exclude="$(PkgLethalAPI_GameLibs)\lib\mscorlib.dll;$(PkgLethalAPI_GameLibs)\lib\netstandard.dll;$(PkgLethalAPI_GameLibs)\lib\System*.dll;" Private="false" />
</ItemGroup>

<ItemGroup Condition=" '$(DisableAutoReferencePolySharp)' != '' ">
<PackageReference Include="PolySharp" Version="1.14.1" PrivateAssets="all" />
</ItemGroup>

<ItemGroup Condition=" '$(ImplicitUsings)' == 'true' OR '$(ImplicitUsings)' == 'enable' ">
<Using Include="System.Collections" />
<Using Include="BepInEx" />
Expand All @@ -30,7 +41,7 @@
<!--
Executes the `ResolvePluginAssemblies` task to resolve the reference assemblies of `ThunderDependency` items.
-->
<Target Name="ResolvePluginReferences" BeforeTargets="CoreCompile" AfterTargets="RestorePlugins;Restore" DependsOnTargets="ResolveProjectReferences">
<Target Name="ResolvePluginReferences" BeforeTargets="CoreCompile" AfterTargets="RestorePlugins;Restore" Returns="@(_PluginReference)">
<ResolvePluginAssemblies AssetsFile="$(PluginAssetsFile)">
<Output ItemName="_PluginReference" TaskParameter="ResolvedAssemblies" />
</ResolvePluginAssemblies>
Expand Down
14 changes: 5 additions & 9 deletions src/Sdk/Solution.targets
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<!--
This file is intended to be imported via a `Directory.Solution.targets` adjacent to a plugins `.sln` file.
It imports Sdk .targets files requried to enable the sdk to function as expected when operating on a solution containing a project using the Sdk:
e.g. via, `dotnet restore path/to/solution.sln`, `dotnet publish path/to/solution.sln`
-->

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<Import Project="Restore.targets" />
<Import Project="Publish.targets" />
<Target Name="_RestorePlugins" BeforeTargets="Restore" AfterTargets="_GetAllRestoreProjectPathItems" Returns="@(_PluginRestoreOutputs)">
<MSBuild BuildInParallel="$(RestoreBuildInParallel)" ContinueOnError="$(RestoreContinueOnError)" Projects="@(FilteredRestoreGraphProjectInputItemsWithoutDuplicates)" SkipNonexistentTargets="true" Targets="_RestorePlugins">
<Output TaskParameter="TargetOutputs" ItemName="_PluginRestoreOutputs" />
</MSBuild>
</Target>

</Project>

0 comments on commit f777c3f

Please sign in to comment.