-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSmallSharp.targets
More file actions
202 lines (175 loc) · 10.4 KB
/
SmallSharp.targets
File metadata and controls
202 lines (175 loc) · 10.4 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<Project>
<UsingTask AssemblyFile="SmallSharp.dll" TaskName="EmitTargets" />
<UsingTask AssemblyFile="JsonPoke.dll" TaskName="JsonPoke"/>
<PropertyGroup>
<UserProjectNamespace>
<Namespace Prefix="msb" Uri="http://schemas.microsoft.com/developer/msbuild/2003" />
</UserProjectNamespace>
<!-- Backs compat -->
<ActiveFile Condition="'$(ActiveCompile)' != ''">$(ActiveCompile)</ActiveFile>
<StartupFile>$(ActiveFile)</StartupFile>
<StartupFile Condition="'$(StartupFile)' == ''">$(ActiveDebugProfile)</StartupFile>
<FindStartupFile Condition="'$(StartupFile)' == '' or !Exists('$(StartupFile)')">true</FindStartupFile>
<StartupFileDependsOn>EnsureProperties;CollectStartupFile;SelectStartupFile;SelectTopLevelCompile;UpdateLaunchSettings;EmitTargets</StartupFileDependsOn>
<!-- For CLI dotnet run, users must set ImportProjectExtensionProps/ImportProjectExtensionTargets=true -->
<SmallSharpPackagesProps>$(MSBuildProjectExtensionsPath)$(MSBuildProjectFile).smallsharp.props</SmallSharpPackagesProps>
<SmallSharpPackagesTargets>$(MSBuildProjectExtensionsPath)$(MSBuildProjectFile).smallsharp.targets</SmallSharpPackagesTargets>
</PropertyGroup>
<ItemGroup>
<!-- Ensures all top-level files show up in the IDE -->
<None Include="*$(DefaultLanguageSourceExtension)" Exclude="$(ActiveDebugProfile);$(ActiveFile)" />
<Compile Remove="*$(DefaultLanguageSourceExtension)" />
<!-- Ensure changes we make to this file trigger a new DTB -->
<UpToDateCheckBuilt Include="Properties\launchSettings.json" />
<UpToDateCheckBuilt Include="$(SmallSharpPackagesProps);$(SmallSharpPackagesTargets)" />
</ItemGroup>
<!-- When restoring, if we include the source files, we'd get duplicate references. -->
<ItemGroup Condition="'$(MSBuildIsRestoring)' != 'true'">
<Compile Include="$(ActiveDebugProfile)" Condition="Exists('$(ActiveDebugProfile)')" />
<Compile Include="$(ActiveFile)" Condition="Exists('$(ActiveFile)') and '$(ActiveFile)' != '$(ActiveDebugProfile)'" />
</ItemGroup>
<Target Name="StartupFile" BeforeTargets="ResolvePackageAssets;CompileDesignTime;CollectUpToDateCheckInputDesignTime" DependsOnTargets="$(StartupFileDependsOn)" />
<Target Name="EnsureProperties" Condition="'$(CheckSmallSharpRequirements)' != 'false'">
<Error Code="SCS02" Condition="'$(_ImportProjectExtensionProps)' != 'true' or '$(_ImportProjectExtensionTargets)' != 'true'"
Text="Setting ImportProjectExtensionProps and ImportProjectExtensionTargets project properties to 'true' is required by SmallSharp to support C# package and project directives." />
<Error Code="SCS03" Condition="'$(ManagePackageVersionsCentrally)' == 'true'"
Text="Setting ManagePackageVersionsCentrally to 'true' is not supported by SmallSharp since C# program files can declare package references via #:package directives." />
<Warning Code="SCS04" Condition="'$(UsingSmallSharpSDK)' != 'true'"
Text='For maximum compatibility with file-based apps, use SmallSharp as an SDK instead of a package reference: <Project Sdk="SmallSharp/$(SmallSharpVersion)">' />
</Target>
<Target Name="CollectStartupFile">
<ItemGroup>
<StartupFile Include="*$(DefaultLanguageSourceExtension)" />
</ItemGroup>
</Target>
<!-- Defaults the startup file to the first Compile, if none previously selected. -->
<Target Name="SelectStartupFile" Condition="'$(FindStartupFile)' == 'true'" Returns="$(StartupFile)">
<ItemGroup>
<ReversedCompile Include="@(StartupFile -> Reverse())" />
</ItemGroup>
<PropertyGroup>
<StartupFile>%(ReversedCompile.Identity)</StartupFile>
</PropertyGroup>
<!-- If .user file doesn't exist at all, create it now -->
<PropertyGroup Condition="!Exists('$(MSBuildProjectFullPath).user')">
<UserProject>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup>
<ActiveDebugProfile>$(StartupFile)</ActiveDebugProfile>
</PropertyGroup>
</Project>
</UserProject>
<WriteStartupFile>false</WriteStartupFile>
</PropertyGroup>
<WriteLinesToFile File="$(MSBuildProjectFullPath).user"
Lines="$(UserProject)"
Condition="!Exists('$(MSBuildProjectFullPath).user')" />
<XmlPoke XmlInputPath="$(MSBuildProjectFullPath).user"
Value="$(StartupFile)"
Query="/msb:Project/msb:PropertyGroup/msb:ActiveDebugProfile"
Namespaces="$(UserProjectNamespace)"
Condition="'$(WriteStartupFile)' != 'false' and '$(StartupFile)' != ''"/>
<XmlPeek XmlInputPath="$(MSBuildProjectFullPath).user"
Query="/msb:Project/msb:PropertyGroup/msb:ActiveDebugProfile/text()"
Namespaces="$(UserProjectNamespace)">
<Output TaskParameter="Result" PropertyName="StartupDebugProfile" />
</XmlPeek>
<PropertyGroup Condition="'$(StartupFile)' != '' and '$(StartupDebugProfile)' != '$(StartupFile)'">
<ActiveDebugProfileProperty>
<ActiveDebugProfile>$(StartupFile)</ActiveDebugProfile>
</ActiveDebugProfileProperty>
</PropertyGroup>
<!-- The ActiveDebugProfile property element may be missing, failing to write the value -->
<XmlPoke XmlInputPath="$(MSBuildProjectFullPath).user"
Value="$(ActiveDebugProfileProperty)"
Query="/msb:Project/msb:PropertyGroup"
Namespaces="$(UserProjectNamespace)"
Condition="'$(StartupFile)' != '' and '$(StartupDebugProfile)' != '$(StartupFile)'"/>
<!-- Read again after we poke the ActiveDebugProfile property -->
<XmlPeek XmlInputPath="$(MSBuildProjectFullPath).user"
Query="/msb:Project/msb:PropertyGroup/msb:ActiveDebugProfile/text()"
Namespaces="$(UserProjectNamespace)">
<Output TaskParameter="Result" PropertyName="StartupDebugProfile" />
</XmlPeek>
<!-- The entire PropertyGroup could have been missing, failing to write the value -->
<PropertyGroup Condition="'$(StartupFile)' != '' and '$(StartupDebugProfile)' != '$(StartupFile)'">
<UserPropertyGroup>
<PropertyGroup>
<ActiveDebugProfile>$(StartupFile)</ActiveDebugProfile>
</PropertyGroup>
</UserPropertyGroup>
</PropertyGroup>
<XmlPoke XmlInputPath="$(MSBuildProjectFullPath).user"
Value="$(UserPropertyGroup)"
Query="/msb:Project"
Namespaces="$(UserProjectNamespace)"
Condition="'$(StartupFile)' != '' and '$(StartupDebugProfile)' != '$(StartupFile)'"/>
<!-- Read again after we poke the PropertyGroup -->
<XmlPoke XmlInputPath="$(MSBuildProjectFullPath).user"
Value="$(StartupFile)"
Query="/msb:Project/msb:PropertyGroup/msb:ActiveDebugProfile"
Namespaces="$(UserProjectNamespace)"
Condition="'$(StartupFile)' != '' and '$(StartupDebugProfile)' != '$(StartupFile)'"/>
<Warning Text="Could not set ActiveDebugProfile=$(StartupFile). Run the project once to fix it."
Condition="'$(StartupFile)' != '' and '$(StartupDebugProfile)' != '$(StartupFile)'"/>
</Target>
<Target Name="SelectTopLevelCompile">
<ItemGroup>
<!-- We remove all top-level from Compile because copy/pasting startup files may end up
causing those items to be hardcoded in the .csproj -->
<Compile Remove="@(Compile -> WithMetadataValue('RelativeDir', ''))" />
<Compile Include="$(StartupFile)" Condition="'$(StartupFile)' != '' and Exists('$(StartupFile)')" />
<UpToDateCheckInput Include="$(StartupFile)" Condition="'$(StartupFile)' != '' and Exists('$(StartupFile)')" />
</ItemGroup>
</Target>
<Target Name="AfterClean">
<Delete Files="$(MSBuildProjectDirectory)\Properties\launchSettings.json"
Condition="Exists('$(MSBuildProjectDirectory)\Properties\launchSettings.json')" />
</Target>
<Target Name="UpdateLaunchSettings">
<WriteLinesToFile File="$(MSBuildProjectDirectory)\Properties\launchSettings.json"
Lines="{ }"
Condition="!Exists('$(MSBuildProjectDirectory)\Properties\launchSettings.json')" />
<SortItems Items="@(StartupFile)">
<Output TaskParameter="SortedItems" ItemName="SortedStartupFile" />
</SortItems>
<JsonPoke ContentPath="$(MSBuildProjectDirectory)\Properties\launchSettings.json"
Query="$.profiles['%(SortedStartupFile.Filename)%(SortedStartupFile.Extension)'].commandName"
Value="Project" />
</Target>
<Target Name="EmitTargets" DependsOnTargets="CollectStartupFile;SelectTopLevelCompile;SelectStartupFile"
Inputs="@(Compile);$(ActiveDebugProfile);$(ActiveFile);Properties\launchSettings.json"
Outputs="$(SmallSharpPackagesProps);$(SmallSharpPackagesTargets)">
<EmitTargets StartupFile="$(StartupFile)"
UsingSDK="$(UsingSmallSharpSDK)"
PackageReferences="@(PackageReferences)"
PropsFile="$(SmallSharpPackagesProps)"
TargetsFile="$(SmallSharpPackagesTargets)"
BaseIntermediateOutputPath="$(BaseIntermediateOutputPath)">
<Output TaskParameter="Packages" ItemName="FileBasedPackage" />
<Output TaskParameter="Properties" PropertyName="FileBasedProperty" />
<Output TaskParameter="Sdks" PropertyName="FileBasedSdk" />
<Output TaskParameter="RestoreNeeded" PropertyName="RestoreNeeded" />
</EmitTargets>
<Error Code="SCS001" Condition="'@(FileBasedSdk)' != '' and '$(UsingSmallSharpSDK)' != 'true'"
Text="Using #:sdk directives requires using SmallSharp as an SDK instead of a package reference: <Project Sdk='SmallSharp/$(SmallSharpVersion)'>" />
</Target>
<UsingTask TaskName="SortItems" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<!-- ITaskItem[] input and sorted output -->
<Items ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<SortedItems ParameterType="Microsoft.Build.Framework.ITaskItem[]" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System" />
<Code Type="Fragment" Language="cs">
<![CDATA[
SortedItems = Items.OrderBy(i => i.ItemSpec).ToArray();
]]>
</Code>
</Task>
</UsingTask>
</Project>