-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
178 lines (156 loc) · 5.9 KB
/
Copy pathProgram.cs
File metadata and controls
178 lines (156 loc) · 5.9 KB
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
using System.Text;
using System.Text.Json;
// quicksheet-gomod — QuickSheet extension that shows Go module info
// (latest version, publish date, number of published versions) from the
// public Go module proxy. Reads JSON-lines from stdin, writes JSON-lines to
// stdout. Zero NuGet dependencies; uses only the BCL HttpClient.
var http = new HttpClient();
http.DefaultRequestHeaders.Add("Accept", "application/json");
http.DefaultRequestHeaders.Add("User-Agent", "quicksheet-gomod/1.0");
http.Timeout = TimeSpan.FromSeconds(10);
var cache = new Dictionary<string, (List<(int r, int c, string v)> cells, DateTime exp)>(StringComparer.OrdinalIgnoreCase);
string? line;
string activationId = "";
while ((line = Console.ReadLine()) != null)
{
if (string.IsNullOrWhiteSpace(line)) continue;
JsonElement msg;
try { msg = JsonDocument.Parse(line).RootElement; } catch { continue; }
var type = msg.TryGetProperty("type", out var t) ? t.GetString() : null;
if (type == "init")
{
Console.WriteLine(JsonSerializer.Serialize(new
{
type = "register",
prefix = "gomod:",
name = "go module info",
description = "Show the latest version and publish date of a Go module from the public Go module proxy.",
version = "1.0.0",
width = 2,
height = 5
}));
continue;
}
if (type == "activate")
{
activationId = msg.TryGetProperty("id", out var idEl) ? idEl.GetString() ?? "" : "";
var rawValue = msg.TryGetProperty("value", out var vEl) ? vEl.GetString() ?? "" : "";
var param = rawValue.StartsWith("gomod:", StringComparison.OrdinalIgnoreCase)
? rawValue["gomod:".Length..].Trim()
: rawValue.Trim();
if (string.IsNullOrWhiteSpace(param))
{
WriteStatus("Usage: gomod: github.com/gin-gonic/gin or gomod: github.com/gin-gonic/gin,gorm.io/gorm");
continue;
}
var modules = param.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
var capturedId = activationId;
var task = Task.Run(async () =>
{
List<(int r, int c, string v)> cells;
if (modules.Length == 1)
{
cells = await FetchSingleAsync(modules[0]);
}
else
{
cells = new List<(int r, int c, string v)>
{
(0, 0, "Module"), (0, 1, "Version"), (0, 2, "Released")
};
for (int i = 0; i < modules.Length && i < 20; i++)
{
var info = await FetchInfoAsync(modules[i]);
cells.Add((i + 1, 0, ShortName(modules[i])));
cells.Add((i + 1, 1, info.version));
cells.Add((i + 1, 2, info.released));
}
}
var cellObjs = cells.Select(x => new { r = x.r, c = x.c, v = x.v }).ToArray();
var output = JsonSerializer.Serialize(new { type = "write", id = capturedId, cells = cellObjs });
Console.WriteLine(output);
});
task.Wait();
continue;
}
}
void WriteStatus(string message) =>
Console.WriteLine(JsonSerializer.Serialize(new { type = "status", id = activationId, message }));
async Task<List<(int r, int c, string v)>> FetchSingleAsync(string mod)
{
if (cache.TryGetValue(mod, out var hit) && hit.exp > DateTime.UtcNow)
return hit.cells;
var info = await FetchInfoAsync(mod);
var versions = await FetchVersionCountAsync(mod);
var cells = new List<(int r, int c, string v)>
{
(0, 0, "Module"), (0, 1, ShortName(mod)),
(1, 0, "Version"), (1, 1, info.version),
(2, 0, "Released"), (2, 1, info.released),
};
if (versions > 0)
{
cells.Add((3, 0, "Versions"));
cells.Add((3, 1, versions.ToString()));
}
cells.Add((4, 0, mod));
cache[mod] = (cells, DateTime.UtcNow.AddMinutes(30));
return cells;
}
async Task<(string version, string released)> FetchInfoAsync(string mod)
{
try
{
var url = $"https://proxy.golang.org/{CaseEncode(mod.Trim('/'))}/@latest";
var json = await http.GetStringAsync(url);
var doc = JsonDocument.Parse(json).RootElement;
var version = doc.TryGetProperty("Version", out var ver) ? ver.GetString() ?? "?" : "?";
string released = "";
if (doc.TryGetProperty("Time", out var tm) && DateTime.TryParse(tm.GetString(), out var dt))
released = dt.ToString("yyyy-MM-dd");
return (version, released);
}
catch
{
return ("not found", "");
}
}
async Task<int> FetchVersionCountAsync(string mod)
{
try
{
var url = $"https://proxy.golang.org/{CaseEncode(mod.Trim('/'))}/@v/list";
var text = await http.GetStringAsync(url);
return text.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).Length;
}
catch { return 0; }
}
// Go module proxy case-encoding: to stay safe on case-insensitive file systems,
// every uppercase letter in the module path is replaced with "!" + lowercase.
// See https://go.dev/ref/mod#goproxy-protocol.
static string CaseEncode(string s)
{
var sb = new StringBuilder(s.Length + 4);
foreach (var ch in s)
{
if (ch >= 'A' && ch <= 'Z')
{
sb.Append('!');
sb.Append(char.ToLowerInvariant(ch));
}
else
{
sb.Append(ch);
}
}
return sb.ToString();
}
// Display the last one or two path segments to keep the cell compact,
// e.g. github.com/gin-gonic/gin → gin-gonic/gin.
static string ShortName(string mod)
{
var parts = mod.Trim('/').Split('/', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length <= 1) return mod;
if (parts.Length == 2) return string.Join('/', parts);
return string.Join('/', parts[^2], parts[^1]);
}