-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDirectory.Build.targets
149 lines (134 loc) · 8.08 KB
/
Directory.Build.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
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="GetGuids" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<Mode ParameterType="System.String" Required="true" />
<HyperVGuid ParameterType="System.String" Output="true" />
<VirtualBoxGuid ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Linq" />
<Code Type="Fragment" Language="cs">
<![CDATA[
const string fileText = "EfiSharp Build Properties\r\n" +
"Do not move the guid lines! They must remain lines 9 and 15.\r\n\r\n" +
"Virtual Machine Guids:\r\n\r\n" +
"https://ashwaniashwin.wordpress.com/2019/07/27/account-does-not-have-permission-to-open-the-attachment-hyper-v-vm-error/\r\n" +
"Uuid will have to be changed for each different vm and should be stored below.\r\n" +
"This may not be required if the current user has full permissions for the project folder before vhd generation.\r\n" +
"Hyper V VM Guid: replace with guid\r\n\r\n" +
"https://forums.virtualbox.org/viewtopic.php?f=8&t=50639\r\n" +
"Use \"VBoxManage list hdds\" to find the correct uuid after first manually adding the vhd to virtualbox and store below.\r\n" +
"Unlike hyperv which boots directly into the image, virtualbox in efi mode boots into an efi console. \r\n" +
"Use \"exit\" to get to into the menu, then go to \"Boot Manager\" followed by \"UEFI VBOX HARDDISK *\".\r\n" +
"VirtualBox Image Guid: replace with guid";
if (!File.Exists("Properties.txt"))
{
Console.WriteLine(" Properties.txt does not exist.\r\n A copy has been generated in the project folder, please edit this file if using Hyper-V or VirtualBox and then restart the build.");
File.AppendAllText("Properties.txt", fileText);
Mode = "";
}
if (Mode == "hyperv")
{
HyperVGuid = File.ReadLines("Properties.txt").Skip(8).First();
HyperVGuid = HyperVGuid.Substring(HyperVGuid.IndexOf(':') + 1).Trim();
if (!Guid.TryParse(HyperVGuid, out _))
{
Console.WriteLine(" Hyper-V guid in Properties.txt is malformed, please correct this and then restart the build.");
HyperVGuid = "";
}
}
else if (Mode == "virtualbox")
{
VirtualBoxGuid = File.ReadLines("Properties.txt").Skip(14).First();
VirtualBoxGuid = VirtualBoxGuid.Substring(VirtualBoxGuid.IndexOf(':') + 1).Trim();
if (!Guid.TryParse(VirtualBoxGuid, out _))
{
Console.WriteLine(" VirtualBox guid in Properties.txt is malformed, please correct this and then restart the build.");
VirtualBoxGuid = "";
}
}
]]>
</Code>
</Task>
</UsingTask>
<Target Name="ValidateBuildVariables">
<Error
Text="Debugging Efi builds is not currently possible."
Condition="'$(Configuration)' == 'Debug' AND '$(Platform.StartsWith(Efi))'" />
</Target>
<Target Name="EfiClean" AfterTargets="AfterClean" Condition="'$(OutputType)' == 'Exe'">
<RemoveDir Directories="$(PublishDir)" Condition="'$(PublishDir)' != ''"/>
<Delete Files="$(MSBuildProjectDirectory)\$(NativeOutputPath)$(TargetName).vhd" />
<Delete Files="$(MSBuildProjectDirectory)\$(NativeOutputPath)$(TargetName).exe" />
<Delete Files="$(MSBuildProjectDirectory)\$(OutputPath)$(TargetName).exe" />
</Target>
<Target Name="NativePublish" AfterTargets="Build" DependsOnTargets="ValidateBuildVariables" Condition="'$(OutputType)' == 'Exe'">
<!-- I tried making this target just trigger Publish but had issues with properties not being defined in time -->
<Exec WorkingDirectory="$(MSBuildProjectDirectory)" Condition="'$(Configuration)' != 'Release' OR '$(Platform.StartsWith(Efi))'"
Command="dotnet publish -r win-x64 -c $(Configuration) /p:Platform=$(Platform) --no-build" />
<Exec WorkingDirectory="$(MSBuildProjectDirectory)" Condition="'$(Configuration)' == 'Release' AND '$(Platform.StartsWith(Windows))'"
Command="dotnet publish -r win-x64 -c Release /p:Platform=$(Platform) --no-build -o $(OutputPath)" />
</Target>
<Target Name="ForceDebug" AfterTargets="NativePublish" Condition="'$(OutputType)' == 'Exe' AND '$(Configuration)' == 'Debug' AND '$(Platform.StartsWith(Windows))'">
<Exec EchoOff="true" Command="git grep --untracked --full-name -l "Main("" Condition="'$(StartupObject)' == ''"
StandardOutputImportance="Low" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="GrepWindowsStartupObject" />
</Exec>
<PropertyGroup>
<WindowsStartupObject Condition="'$(StartupObject)' != ''">$(SolutionDir)$(StartupObject.Replace('.', '\')).cs</WindowsStartupObject>
<WindowsStartupObject Condition="'$(StartupObject)' == ''">$(SolutionDir)$(GrepWindowsStartupObject.Replace('/', '\'))</WindowsStartupObject>
</PropertyGroup>
<Exec EchoOff="true" WorkingDirectory="$(DirectoryBuildPropsPath)\.." Command="LaunchDebugger.vbs $(MSBuildProjectDirectory)\$(NativeOutputPath)$(TargetName).exe $(WindowsStartupObject)" />
</Target>
<Target Name="GenerateVirtualDisk" AfterTargets="NativePublish" Condition="'$(OutputType)' == 'Exe' AND '$(Mode)' != 'nolinker' AND '$(Platform.StartsWith(Efi))'">
<PropertyGroup>
<FullNativePath>$(MSBuildProjectDirectory)\$(NativeOutputPath)</FullNativePath>
<VHD>$(FullNativePath)$(TargetName).vhd</VHD>
<CreatePartitionCommand>
create vdisk file="$(VHD)" maximum=40
select vdisk file="$(VHD)"
attach vdisk
convert gpt
create partition efi
format quick fs=fat32 label="System"
assign letter="X"
exit
</CreatePartitionCommand>
<CreatePartitionCommand2>
select vdisk file="$(VHD)"
select partition 1
remove letter=X
detach vdisk
exit
</CreatePartitionCommand2>
</PropertyGroup>
<Delete Files="$(VHD).vhd" />
<WriteLinesToFile File="$(NativeOutputPath)diskpart1.txt" Overwrite="true" Lines="$(CreatePartitionCommand)" />
<WriteLinesToFile File="$(NativeOutputPath)diskpart2.txt" Overwrite="true" Lines="$(CreatePartitionCommand2)" />
<Exec EchoOff="true" Command="diskpart /s "$(FullNativePath)diskpart1.txt"" />
<Copy SourceFiles="$(FullNativePath)$(TargetName)$(NativeBinaryExt)" DestinationFiles="X:\EFI\BOOT\BOOTX64.efi" />
<Exec EchoOff="true" Command="diskpart /s "$(FullNativePath)diskpart2.txt"" />
<GetGuids Condition="'$(Mode)' != ''" Mode="$(Mode)">
<Output TaskParameter="HyperVGuid" PropertyName="HyperVGuid"/>
<Output TaskParameter="VirtualBoxGuid" PropertyName="VirtualBoxGuid"/>
</GetGuids>
<Exec EchoOff="true" Condition="'$(Mode)' == 'hyperv' and '$(HyperVGuid)' != ''" Command="icacls $(VHD) /grant $(HyperVGuid):F" />
<Exec EchoOff="true" Condition="'$(Mode)' == 'virtualbox' and '$(VirtualBoxGuid)' != ''" Command=""C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" internalcommands sethduuid $(VHD) $(VirtualBoxGuid)" />
</Target>
<Target Name="FixPartialVirtualDisk">
<PropertyGroup>
<VHD>"$(MSBuildProjectDirectory)\$(NativeOutputPath)\$(TargetName).vhd"</VHD>
<FixPartialVirtualDisk>
select vdisk file=$(VHD)
select partition 1
remove letter=X
detach vdisk
exit
</FixPartialVirtualDisk>
</PropertyGroup>
<WriteLinesToFile File="$(NativeOutputPath)diskpart.txt" Overwrite="true" Lines="$(FixPartialVirtualDisk)" />
<Exec EchoOff="true" Command="diskpart /s "$(MSBuildProjectDirectory)\$(NativeOutputPath)diskpart.txt"" />
</Target>
</Project>