-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
121 lines (108 loc) · 3.5 KB
/
Program.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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace vscmd {
class Program {
static VisualStudio vs;
[STAThread]
static void Main(string[] args) {
try {
vs = VisualStudio.GetOrCreate();
bool activate = false;
for (var i = 0; i < args.Length; i++) {
var arg = args[i];
if ("args".StartsWith(arg)) {
HandleDebugArguments(args.Skip(1));
} else if ("attach".StartsWith(arg)) {
HandleAttach(args.Skip(1));
} else if ("start".StartsWith(arg)) {
HandleDebugStart(args.Skip(1));
} else {
activate |= HandleFileArguments(args);
}
break;
}
if (activate)
vs.ActivateMainWindow();
} catch (Exception err) {
Console.Error.WriteLine(err.ToString());
}
}
static FileInfo[] GetFiles(string arg) {
var directoryName = Path.GetDirectoryName(arg);
var dir = new DirectoryInfo(string.IsNullOrEmpty(directoryName) ? Directory.GetCurrentDirectory() : directoryName);
return dir.GetFiles(Path.GetFileName(arg));
}
static bool HandleFileArguments(IEnumerable<string> args) {
bool handled = false;
foreach (var arg in args) {
if (arg.Contains('*') || arg.Contains('?')) {
foreach (var file in GetFiles(arg)) {
vs.OpenFile(file);
handled = true;
}
continue;
}
vs.OpenFile(new FileInfo(arg));
handled = true;
}
return handled;
}
static void HandleDebugStart(IEnumerable<string> args) {
var config = vs.StartupProject.ActiveConfiguration;
var program = args.FirstOrDefault();
if (program == null) {
Console.Out.WriteLine(config.DebugStartProgram);
Console.Out.WriteLine(config.DebugStartArguments);
return;
}
config.DebugStartProgram = Path.GetFullPath(program);
args = args.Skip(1);
if (args.Any())
HandleDebugArguments(args);
vs.Debugger.Go(false);
}
static void HandleDebugArguments(IEnumerable<string> args) {
var config = vs.StartupProject.ActiveConfiguration;
if (!args.Any()) {
Console.Out.WriteLine(config.DebugStartArguments);
return;
}
args = args.Select(arg =>
QuoteIfNeeded(File.Exists(arg) ? Path.GetFullPath(arg) : arg));
config.DebugStartArguments = string.Join(" ", args);
}
static void HandleAttach(IEnumerable<string> args) {
var filters = args
.Select<string, Func<Process, bool>>(s => {
int id;
if (int.TryParse(s, out id))
return p => p.ProcessID == id;
return p => p.Name.Contains(s);
}).ToList();
var debugger = vs.Debugger;
if (!filters.Any()) {
var processes = debugger.LocalProcesses
.OrderBy(p => p.Name);
foreach (var process in processes)
Console.WriteLine($"{process.ProcessID,5} {process.Name}");
return;
}
foreach (var process in debugger.LocalProcesses) {
if (!filters.Any(f => f(process)))
continue;
Console.WriteLine($"{process.ProcessID,5} {process.Name}");
process.Attach();
}
}
static string QuoteIfNeeded(string arg) {
if (!arg.Contains(' '))
return arg;
return string.Concat("\"", arg, "\"");
}
}
}