-
Notifications
You must be signed in to change notification settings - Fork 5
/
KSPCommon.targets
184 lines (166 loc) · 9.62 KB
/
KSPCommon.targets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Condition=" '$(KSPCommonPropsImported)' == '' And Exists('$(MSBuildThisFileDirectory)KSPCommon.props') " Project="$(MSBuildThisFileDirectory)KSPCommon.props"/>
<!-- NuGet dependencies -->
<ItemGroup>
<!-- For GenerateKSPVersionFile -->
<PackageReference Include="JsonPoke" Version="1.2.0" Condition="@(KSPVersionFile) != ''"/>
</ItemGroup>
<!--Custom Targets-->
<Target Name="CheckForKSPRoot" BeforeTargets="ResolveReferences">
<Error Text="KSPBuildTools was unable to find a KSP installation. Please set the ReferencePath or KSPRoot property
in your csproj.user file to a valid KSP installation directory (the one with GameData in it)
See https://kspbuildtools.readthedocs.io/en/latest/msbuild/getting-started.html#locating-your-ksp-install for more information"
Condition="'$(KSPRoot)' == ''"/>
<ItemGroup>
<_KSPRootMessage Include="KSPRoot Candidates:"/>
<_KSPRootMessage Include="Candidate: %(KSPRootCandidate.source) from %(KSPRootCandidate.identity)"/>
<_KSPRootMessage Include="Chosen KSPRoot: $(KSPRoot) in $(KSPRootSource)" Condition="'$(KSPRoot)' != ''"/>
</ItemGroup>
<Message Text="@(_KSPRootMessage, '%0a')" Importance="normal"/>
</Target>
<!-- Copy output files to mod folder -->
<Target Name="CopyBinariesToRepo" AfterTargets="CopyFilesToOutputDirectory">
<ItemGroup>
<BinariesToCopy Include="$(TargetDir)/**"/>
</ItemGroup>
<MakeDir Directories="$(RepoRootPath)/$(BinariesOutputRelativePath)"/>
<Copy SourceFiles="@(BinariesToCopy)" DestinationFolder="$(RepoRootPath)/$(BinariesOutputRelativePath)/%(RecursiveDir)"/>
</Target>
<!-- Use CKAN to install mods for any references tagged with a CKAN Identifier -->
<PropertyGroup>
<CKANCommandFile>$(BaseIntermediateOutputPath)ckancommands.cache</CKANCommandFile>
</PropertyGroup>
<Target Name="CKANInstallScriptGen">
<ItemGroup>
<_CKANCompatibleVersionItems Include="$(CKANCompatibleVersions.Split(' '))"/>
<_CKANDependency Include="%(Reference.CKANIdentifier)" Condition="%(Reference.CKANVersion) == ''"/>
<_CKANDependency Include="%(Reference.CKANIdentifier)=%(Reference.CKANVersion)" Condition="%(Reference.CKANVersion) != ''"/>
<_CKANDependency Include="@(CKANDependency)"/>
</ItemGroup>
<PropertyGroup>
<CKANDependencyList>@(_CKANDependency, ' ')</CKANDependencyList>
</PropertyGroup>
<ItemGroup>
<!-- Not using `ckan compat set` because as of 2024-08-16 it is still only available in the dev branch -->
<_CKANCommands Include="compat add --gamedir "$(KSPROOT)" %(_CKANCompatibleVersionItems.Identity)"
Condition=" '$(CKANCompatibleVersions)' != '' "/>
<_CKANCommands Include="install --no-recommends --gamedir "$(KSPROOT)" $(CKANDependencyList)" Condition="$(CKANDependencyList) != ''"/>
</ItemGroup>
<Message Text="Writing CKAN commands to $(CKANCommandFile)"/>
<Message Text="@(_CKANCommands, '%0a')" Importance="Low"/>
<WriteLinesToFile File="$(CKANCommandFile)" Lines="@(_CKANCommands)" Overwrite="true" WriteOnlyWhenDifferent="true"/>
</Target>
<ItemGroup>
<ResolveAssemblyReferencesDependsOn Include="CKANInstall"/>
</ItemGroup>
<!-- Execute generated CKAN command list -->
<Target Name="CKANInstall" DependsOnTargets="CKANInstallScriptGen" BeforeTargets="_GenerateRestoreProjectSpec;Restore;ResolveAssemblyReferences" Inputs="$(CKANCommandFile)" Outputs="$(KSPRoot)/CKAN/registry.json">
<Exec Command="ckan prompt --headless < '$(CKANCommandFile)'" Condition="'$(CKANDependencyList)' != ''"/>
</Target>
<Target Name="CKANClean" AfterTargets="Clean">
<Delete Files="$(CKANCommandFile)"/>
</Target>
<!-- For use like so: `msbuild -t:"GetRequiredExternalTools" -verbosity:minimal -nologo`, then pipe into your destination of choice -->
<Target Name="GetRequiredExternalTools">
<ItemGroup>
<CKANDependencyList Include="%(Reference.CKANIdentifier)"/>
<RequiredExternalTool Include="CKAN" Condition="@(CKANDependencyList) != ''"/>
</ItemGroup>
<Message Text="@(RequiredExternalTool)" Importance="high"/>
</Target>
<!--
Generate the KSPAssembly attribute based on the FileVersion property
-->
<Target Name="GenerateKSPAssemblyAttribute" BeforeTargets="CoreGenerateAssemblyInfo"
Condition="$(GenerateKSPAssemblyAttribute)">
<ItemGroup>
<AssemblyAttribute Include="KSPAssembly">
<_Parameter1>$(AssemblyName)</_Parameter1>
<_Parameter1_TypeName>System.String</_Parameter1_TypeName>
<_Parameter2>$(FileVersion.Split('.')[0])</_Parameter2>
<_Parameter2_TypeName>System.Int32</_Parameter2_TypeName>
<_Parameter3>$(FileVersion.Split('.')[1])</_Parameter3>
<_Parameter3_TypeName>System.Int32</_Parameter3_TypeName>
<_Parameter4>$(FileVersion.Split('.')[2])</_Parameter4>
<_Parameter4_TypeName>System.Int32</_Parameter4_TypeName>
</AssemblyAttribute>
</ItemGroup>
</Target>
<!--
Generate the KSPAssemblyDependency attributes based on input references
Reference items must have a CKANIdentifier or a KSPAssemblyName.
KSPAssemblyVersion can be optionally specified.
Otherwise CKANVersion is used.
Otherwise 0.0.0 is used (no minimum version)
-->
<Target Name="GenerateKSPAssemblyDependencyAttributes" BeforeTargets="CoreGenerateAssemblyInfo"
Condition="$(GenerateKSPAssemblyDependencyAttributes) == 'true'">
<ItemGroup>
<Reference Update="%(Reference.identity)" Condition="'%(Reference.CKANIdentifier)%(Reference.KSPAssemblyName)' != ''">
<KSPAssemblyName Condition="%(Reference.KSPAssemblyName) == ''">$([System.String]::Copy('%(Reference.identity)').Split(',')[0])</KSPAssemblyName>
<KSPAssemblyVersion Condition="%(Reference.KSPAssemblyVersion) == ''">%(Reference.CKANVersion)</KSPAssemblyVersion>
<KSPAssemblyVersion Condition="%(Reference.KSPAssemblyVersion) == ''">0.0.0</KSPAssemblyVersion>
</Reference>
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="KSPAssemblyDependency" Condition="%(Reference.KSPAssemblyName) != ''">
<_Parameter1>%(Reference.KSPAssemblyName)</_Parameter1>
<_Parameter1_TypeName>System.String</_Parameter1_TypeName>
<_Parameter2>$([System.String]::Copy('%(Reference.KSPAssemblyVersion)').Split('.')[0])</_Parameter2>
<_Parameter2_TypeName>System.Int32</_Parameter2_TypeName>
<_Parameter3>$([System.String]::Copy('%(Reference.KSPAssemblyVersion)').Split('.')[1])</_Parameter3>
<_Parameter3_TypeName>System.Int32</_Parameter3_TypeName>
<_Parameter4>$([System.String]::Copy('%(Reference.KSPAssemblyVersion)').Split('.')[2])</_Parameter4>
<_Parameter4_TypeName>System.Int32</_Parameter4_TypeName>
</AssemblyAttribute>
</ItemGroup>
</Target>
<ItemDefinitionGroup>
<KSPVersionFile>
<KSP_Version>1.12</KSP_Version>
<KSP_Version_Min>1.8</KSP_Version_Min>
<KSP_Version_Max>1.12</KSP_Version_Max>
</KSPVersionFile>
</ItemDefinitionGroup>
<!-- Target to generate the KSP version json file for AVC/CKAN etc-->
<Target Name="GenerateKSPVersionFile" AfterTargets="Build" Inputs="@(KSPVersionFile);$(FileVersion)" Outputs="%(KSPVersionFile.destination)">
<ItemGroup>
<KSPVersionFile Update="@(KSPVersionFile)">
<Name Condition="%(KSPVersionFile.Name) == ''">$(ProjectName)</Name>
<Version Condition="%(KSPVersionFile.Version) == ''">$(FileVersion.Split(`.`)[0]).$(FileVersion.Split(`.`)[1]).$(FileVersion.Split(`.`)[2])</Version>
</KSPVersionFile>
</ItemGroup>
<ReadLinesFromFile File="%(KSPVersionFile.identity)" Condition="Exists('@(KSPVersionFile)')">
<Output TaskParameter="Lines" ItemName="_JSONLines"/>
</ReadLinesFromFile>
<PropertyGroup>
<_JSON>@(_JSONLines, '%0a')</_JSON>
<_JSON Condition="@(_JSONLines) == ''">{}</_JSON>
</PropertyGroup>
<JsonPoke Content="$(_JSON)" Query="$.NAME" RawValue="'%(KSPVersionFile.Name)'">
<Output TaskParameter="Content" PropertyName="_JSON"/>
</JsonPoke>
<JsonPoke Content="$(_JSON)" Query="$.VERSION" RawValue="'%(KSPVersionFile.Version)'">
<Output TaskParameter="Content" PropertyName="_JSON"/>
</JsonPoke>
<JsonPoke Content="$(_JSON)" Query="$.URL" RawValue="'%(KSPVersionFile.URL)'" Condition="%(KSPVersionFile.URL) != ''">
<Output TaskParameter="Content" PropertyName="_JSON"/>
</JsonPoke>
<JsonPoke Content="$(_JSON)" Query="$.DOWNLOAD" RawValue="'%(KSPVersionFile.Download)'" Condition="%(KSPVersionFile.Download) != ''">
<Output TaskParameter="Content" PropertyName="_JSON"/>
</JsonPoke>
<JsonPoke Content="$(_JSON)" Query="$.KSP_VERSION" RawValue="'%(KSPVersionFile.KSP_Version)'" Condition="%(KSPVersionFile.KSP_Version) != ''">
<Output TaskParameter="Content" PropertyName="_JSON"/>
</JsonPoke>
<JsonPoke Content="$(_JSON)" Query="$.KSP_VERSION_MIN" RawValue="'%(KSPVersionFile.KSP_Version_Min)'" Condition="%(KSPVersionFile.KSP_Version_Min) != ''">
<Output TaskParameter="Content" PropertyName="_JSON"/>
</JsonPoke>
<JsonPoke Content="$(_JSON)" Query="$.KSP_VERSION_MAX" Value="'%(KSPVersionFile.KSP_Version_Max)'" Condition="%(KSPVersionFile.KSP_Version_Max) != ''">
<Output TaskParameter="Content" PropertyName="_JSON"/>
</JsonPoke>
<WriteLinesToFile File="%(KSPVersionFile.Destination)" Lines="$(_JSON)" Overwrite="true"/>
<Message Text="Writing JSON version file to %(KSPVersionFile.Destination)"/>
<Message Text="Contents:%0a$(_JSON)" Importance="low"/>
</Target>
</Project>