forked from coapp-test/AutoBuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommands.cs
94 lines (84 loc) · 3.87 KB
/
Commands.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using CoApp.Toolkit.Collections;
using CoApp.Toolkit.Extensions;
namespace AutoBuilder
{
[XmlRoot(ElementName = "CommandScript", Namespace = "http://coapp.org/automation/build")]
public class CommandScript
{
[XmlArray(IsNullable = false)]
public List<string> Commands;
public CommandScript()
{
Commands = new List<string>();
}
public CommandScript(IEnumerable<string> lines)
{
Commands = new List<string>(lines);
}
public int Run(string path, string project, XDictionary<string, string> macros)
{
ProcessUtility _cmdexe = new ProcessUtility("cmd.exe");
return Run(_cmdexe, path, macros);
}
public int Run(ProcessUtility exe, string path, XDictionary<string, string> macros)
{
// Reset my working directory.
Environment.CurrentDirectory = path;
string tmpFile = Path.GetTempFileName();
File.Move(tmpFile, tmpFile+".bat");
tmpFile += ".bat";
FileStream file = new FileStream(tmpFile,FileMode.Open);
StreamWriter FS = new StreamWriter(file);
macros.Default = null;
foreach (string command in Commands)
{
FS.WriteLine(command.FormatWithMacros((input) =>
{
string Default = null;
if (input.Contains("??"))
{
var parts = input.Split(new[] { '?' }, StringSplitOptions.RemoveEmptyEntries);
Default = parts.Length > 1 ? parts[1].Trim() : string.Empty;
input = parts[0];
}
return (macros[input.ToLower()] ?? Default) ?? input;
}
));
}
FS.Close();
int ret = exe.ExecNoStdInRedirect(@"/c """ + tmpFile + @"""");
File.Delete(tmpFile);
return ret;
}
public string Flatten(XDictionary<string, string> macros = null)
{
StringBuilder Out = new StringBuilder();
if (macros == null)
foreach (string s in Commands)
Out.AppendLine(s);
else
foreach (string s in Commands)
Out.AppendLine(s.FormatWithMacros((input) =>
{
string Default = null;
if (input.Contains("??"))
{
var parts = input.Split(new[] {'?'},
StringSplitOptions.
RemoveEmptyEntries);
Default = parts.Length > 1
? parts[1].Trim()
: string.Empty;
input = parts[0];
}
return (macros[input.ToLower()] ?? Default) ?? String.Empty;
}));
return Out.ToString();
}
}
}