forked from OpenRCT2/OpenRCT2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenrct2.targets
126 lines (115 loc) · 4.75 KB
/
openrct2.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
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- 7z Task -->
<UsingTask TaskName="_7z"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Inputs Required="true" ParameterType="System.String" />
<Output Required="true" ParameterType="System.String" />
</ParameterGroup>
<Task>
<Using Namespace="System"/>
<Using Namespace="System.Diagnostics"/>
<Using Namespace="System.IO"/>
<Code Type="Method" Language="cs">
<![CDATA[
public override bool Execute()
{
string appPath = Find7zPath();
if (appPath == null)
{
Log.LogError("Unable to find 7z.exe.");
return false;
}
var argsSB = new StringBuilder();
argsSB.Append("a -tzip -mx9 -mtc=off ");
argsSB.Append("\"" + Output + "\" ");
foreach (string input in Inputs.Split(';'))
{
argsSB.Append("\"" + input + "\" ");
}
string args = argsSB.ToString();
Log.LogMessage(MessageImportance.Normal, "7z " + args);
var psi = new ProcessStartInfo(appPath, args);
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
var process = Process.Start(psi);
process.WaitForExit();
if (process.ExitCode != 0)
{
string appError = process.StandardError.ReadToEnd();
appError = appError.Replace("\r\n", "\n");
appError = appError.Replace("\r", "\n");
appError = appError.Replace("\n", " ");
int colonIndex = appError.IndexOf(":");
if (colonIndex != -1)
{
appError = appError.Substring(colonIndex + 1);
}
appError = appError.Trim();
Log.LogError(appError);
return false;
}
string appOutput = process.StandardOutput.ReadToEnd();
Log.LogMessage(MessageImportance.Normal, appOutput);
return true;
}
private string Find7zPath()
{
const string DefaultAppFileName = "7z.exe";
string DefaultAppPath = Path.Combine("7-Zip", DefaultAppFileName);
// HACK needed as SpecialFolder.ProgramFiles returns x86 for a 32-bit process
string programFiles = Path.Combine(Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)), "Program Files");
string appPath = Path.Combine(programFiles, DefaultAppPath);
if (File.Exists(appPath))
{
return appPath;
}
programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
appPath = Path.Combine(programFiles, DefaultAppPath);
if (File.Exists(appPath))
{
return appPath;
}
programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
appPath = Path.Combine(programFiles, DefaultAppPath);
if (File.Exists(appPath))
{
return appPath;
}
string[] envPaths = Environment.GetEnvironmentVariable("PATH").Split(';');
foreach (string envPath in envPaths)
{
appPath = Path.Combine(envPath, DefaultAppFileName);
if (File.Exists(appPath))
{
return appPath;
}
}
return null;
}
]]>
</Code>
</Task>
</UsingTask>
<UsingTask TaskName="Unzip"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Input Required="true" ParameterType="System.String" />
<OutputDirectory Required="true" ParameterType="System.String" />
</ParameterGroup>
<Task>
<Reference Include="System.IO.Compression.FileSystem" />
<Code Type="Fragment" Language="cs">
<![CDATA[
Log.LogMessage(MessageImportance.Normal, String.Format("Extracting '{0}' to '{1}'.", Input, OutputDirectory));
System.IO.Compression.ZipFile.ExtractToDirectory(Input, OutputDirectory);
]]>
</Code>
</Task>
</UsingTask>
</Project>