Repository navigation
Expand file tree
/
Copy pathSkillInstaller.cs
More file actions
143 lines (131 loc) · 6.46 KB
/
Copy pathSkillInstaller.cs
File metadata and controls
143 lines (131 loc) · 6.46 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
namespace NpgsqlRestClient;
/// <summary>
/// Implements the <c>--install-skill</c> command: downloads the three Claude Code skill files
/// (SKILL.md, annotations-reference.md, configuration-reference.jsonc) from the version branch
/// matching the running binary - falling back to master when that branch is not available - into
/// <c>./.claude/skills/npgsqlrest</c> (project scope, the default) or
/// <c>~/.claude/skills/npgsqlrest</c> (global scope).
/// </summary>
public static class SkillInstaller
{
private static readonly string[] SkillFiles = ["SKILL.md", "annotations-reference.md", "configuration-reference.jsonc"];
/// <summary>Path of the skill folder inside the repository (same in every ref).</summary>
private const string RepoPath = ".claude/skills/npgsqlrest";
/// <summary>
/// Base URL up to (excluding) the git ref. Overridable via the NPGSQLREST_SKILL_BASE_URL
/// environment variable for tests and enterprise mirrors exposing the same
/// <c>{base}/{ref}/.claude/skills/npgsqlrest/{file}</c> layout.
/// </summary>
private static string BaseUrl =>
Environment.GetEnvironmentVariable("NPGSQLREST_SKILL_BASE_URL")?.TrimEnd('/')
?? "https://raw.githubusercontent.com/NpgsqlRest/NpgsqlRest";
/// <summary>The running version as the release branch name, e.g. "3.21.0".</summary>
public static string VersionBranch
{
get
{
var v = System.Reflection.Assembly.GetAssembly(typeof(SkillInstaller))?.GetName().Version;
return v is null ? "master" : $"{v.Major}.{v.Minor}.{Math.Max(v.Build, 0)}";
}
}
private static string? FindSymbolicLink(string targetDir, IEnumerable<string> files)
{
if (new DirectoryInfo(targetDir) is { Exists: true, LinkTarget: not null })
{
return targetDir;
}
foreach (var file in files)
{
if (new FileInfo(file) is { LinkTarget: not null })
{
return file;
}
}
return null;
}
public static async Task<int> InstallAsync(bool global)
{
var targetRoot = global
? Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
: Directory.GetCurrentDirectory();
var targetDir = Path.Combine(targetRoot, ".claude", "skills", "npgsqlrest");
var version = VersionBranch;
using var http = new HttpClient { Timeout = TimeSpan.FromSeconds(30) };
http.DefaultRequestHeaders.UserAgent.ParseAdd($"npgsqlrest/{version}");
// All files must come from the SAME ref for a consistent skill: try the branch matching
// this version first, then master. Files are downloaded fully into memory before anything
// is written, so a network failure never leaves a partial install behind.
(string GitRef, bool IsFallback)[] refs = string.Equals(version, "master", StringComparison.Ordinal)
? [("master", false)]
: [(version, false), ("master", true)];
foreach (var (gitRef, isFallback) in refs)
{
var contents = new List<(string Name, string Content)>(SkillFiles.Length);
string? failure = null;
foreach (var file in SkillFiles)
{
var url = $"{BaseUrl}/{gitRef}/{RepoPath}/{file}";
try
{
using var response = await http.GetAsync(url);
if (response.IsSuccessStatusCode is false)
{
failure = $"{url}: {(int)response.StatusCode} {response.ReasonPhrase}";
break;
}
contents.Add((file, await response.Content.ReadAsStringAsync()));
}
catch (Exception ex) when (ex is HttpRequestException or TaskCanceledException)
{
failure = $"{url}: {ex.Message}";
break;
}
}
if (failure is not null)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Error.WriteLine(isFallback
? $"Could not download the skill from 'master': {failure}"
: $"Skill files for version branch '{gitRef}' are not available ({failure}); trying 'master'...");
Console.ResetColor();
continue;
}
// Never write through a symbolic link: a cloned repository could ship the skill folder or one of its
// files as a link to somewhere else (e.g. SKILL.md -> ~/.bashrc), which writing would overwrite.
// (.claude and .claude/skills may be links - dotfile managers commonly link ~/.claude.)
var link = FindSymbolicLink(targetDir, contents.Select(c => Path.Combine(targetDir, c.Name)));
if (link is not null)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine($"Refusing to install: {link} is a symbolic link. Remove it and run --install-skill again.");
Console.ResetColor();
return 1;
}
Directory.CreateDirectory(targetDir);
foreach (var (name, content) in contents)
{
File.WriteAllText(Path.Combine(targetDir, name), content);
Console.WriteLine($" {name}");
}
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Installed the NpgsqlRest Claude Code skill ({gitRef}) into {targetDir}");
Console.ResetColor();
if (isFallback)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"Note: installed from 'master' because the '{version}' branch was not available - references may not exactly match this version. The skill instructs the agent to trust the installed binary (--config, --annotations) when they differ.");
Console.ResetColor();
}
if (global is false)
{
Console.WriteLine("Commit the .claude/skills/npgsqlrest folder so the whole team (and CI agents) get the skill.");
}
return 0;
}
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine("Failed to install the Claude Code skill. Check network access to raw.githubusercontent.com and try again.");
Console.ResetColor();
return 1;
}
}