forked from migueldeicaza/muget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmuget.cs
231 lines (198 loc) · 5.08 KB
/
muget.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
using NuGet;
using Mono.Options;
class MuGet {
OptionSet options;
public bool Verbose;
public string ApiKey;
public string SourceUrl = "http://go.microsoft.com/fwlink/?LinkID=206669";
bool showHelp;
public List<string> Arguments;
IPackageRepository repoFactory;
public IPackageRepository PackageRepository {
get {
if (repoFactory == null)
repoFactory = PackageRepositoryFactory.Default.CreateRepository (new PackageSource (SourceUrl, "feed"));
return repoFactory;
}
}
IPackage [] packages;
public IPackage [] Packages {
get {
var wait = new ManualResetEvent (false);
Task.Factory.StartNew (delegate {
if (packages == null)
packages = PackageRepository.GetPackages ().ToArray ();
wait.Set ();
});
while (!wait.WaitOne (TimeSpan.FromSeconds (1)))
Spin ("Loading ");
Console.WriteLine ();
return packages;
}
}
static char [] spinner = new char [] { '|', '/', '-', '\\' };
static int spinnerIdx;
public void Spin (string prefix)
{
Console.Write ("{0}{1}\r", prefix, spinner [spinnerIdx]);
spinnerIdx = (spinnerIdx+1)%spinner.Length;
}
public int InstallCommand ()
{
if (Arguments.Count < 1){
Console.WriteLine ("muget: you must specify a package to install");
return 1;
}
var package = Arguments [0];
var version = Arguments.Count > 1 ? new Version (Arguments [1]) : null;
var pm = new PackageManager (PackageRepository, "packages"){
Logger = new Logger ()
};
pm.InstallPackage (package, version);
return 0;
}
public int ListCommand ()
{
var packages = Packages;
string terms = Arguments.Count != 0 ? Arguments [0].ToLower () : null;
bool hasPackages = false;
foreach (var package in packages){
if (terms != null)
if (package.Id.ToLower ().IndexOf (terms) == -1)
continue;
hasPackages = true;
if (Verbose)
Console.WriteLine ("{0}\n Version: {1}\n {2}", package.Id, package.Version, Fmt (" ", "Description: " + package.Description));
else
Console.WriteLine (package.GetFullName ());
}
if (!hasPackages)
Console.WriteLine ("muget: no packages");
return 0;
}
public void ShowHelp (bool interactive)
{
Console.WriteLine ("Usage is: muget command [OPTIONS]");
Console.WriteLine ("Commands:");
Console.WriteLine (" list [pattern]");
Console.WriteLine (" install PACKAGE [VERSION]");
Console.WriteLine (" delete (rm), install (in), list (ls), pack, publish (pub), push, update (up)");
Console.WriteLine ("Options:");
options.WriteOptionDescriptions (Console.Out);
}
public MuGet ()
{
options = new OptionSet () {
{ "verbose", "Operate in verbose mode", f => Verbose = true },
{ "a|apikey=", "Specifies the API key", f => ApiKey = f },
{ "h|help", "Show this help", f => showHelp = true },
{ "s|source", "Sets the source repository url", f => SourceUrl = f }
};
}
public void Interactive ()
{
var lineEditor = new Mono.Terminal.LineEditor ("muget");
string s;
while ((s = lineEditor.Edit ("muget> ", "")) != null){
Run (s.Split (' '), true);
}
}
public int Run (string [] args, bool interactive)
{
try {
Arguments = options.Parse (args);
} catch (OptionException e){
if (interactive)
Console.WriteLine ("Parsing error: {0}", e.Message);
else
Console.WriteLine ("Error: {0}\nTry using --help", e.Message);
return 1;
}
if (showHelp){
ShowHelp (interactive);
return 0;
}
if (Arguments.Count == 0){
Interactive ();
return 0;
}
string command = Arguments [0].ToLower ();
Arguments = Arguments.Skip (1).ToList ();
switch (command){
case "delete": case "rm":
case "install": case "in":
return InstallCommand ();
case "list": case "ls":
return ListCommand ();
case "pack":
return PackCommand ();
case "publish": case "pub":
case "update": case "up":
default:
Console.Error.WriteLine ("muget: unknown command {0}", command);
ShowHelp (interactive);
return 1;
}
if (!interactive)
return 1;
// Interactive commands
switch (command){
case "quit":
if (interactive)
Environment.Exit (0);
break;
case "help":
ShowHelp ();
break;
}
}
public static int Main (string [] args)
{
var muget = new MuGet ();
return muget.Run (args, false);
}
int WindowWidth ()
{
try {
return Console.WindowWidth;
} catch {
return 80;
}
}
string Fmt (string prefix, string text)
{
int wrap = Console.WindowWidth-8;
var result = new StringBuilder (prefix);
int col = prefix.Length;
foreach (char c in text){
if (col > wrap){
result.Append ("\n");
result.Append (prefix);
col = prefix.Length;
}
if (col == prefix.Length && c == ' ')
continue;
if (c == '\n')
col = 0;
if (c == '\t')
col = (col/8+1)*8;
if (c == '\r')
continue;
result.Append (c);
}
return result.ToString ();
}
}
class Logger : ILogger {
public void Log (MessageLevel level, string format, params object [] args)
{
Console.WriteLine (format, args);
}
}