변경 목적: Agent Compare 아래 비교본의 개발 문서와 런처 소스를 기준으로 현재 AX Commander에 빠져 있던 신규 런처 기능을 동일한 흐름으로 옮겨, 비교본 수준의 기능 폭을 현재 제품에 반영했습니다. 핵심 수정사항: 비교본의 신규 런처 핸들러 다수를 src/AxCopilot/Handlers로 이식하고 App.xaml.cs 등록 흐름에 연결했습니다. 빠른 링크, 파일 태그, 알림 센터, 포모도로, 파일 브라우저, 핫키 관리, OCR, 세션/스케줄/매크로, Git/정규식/네트워크/압축/해시/UUID/JWT/QR 등 AX Commander 기능을 추가했습니다. 핵심 수정사항: 신규 기능이 실제 동작하도록 AppSettings 확장, SchedulerService/FileTagService/NotificationCenterService/IconCacheService/UrlTemplateEngine/PomodoroService 추가, 배치 이름변경/세션/스케줄/매크로 편집 창 추가, NotificationService와 Symbols 보강, QR/OCR용 csproj 의존성과 Windows 타겟 프레임워크를 반영했습니다. 문서 반영: README.md와 docs/DEVELOPMENT.md에 비교본 기반 런처 기능 이식 이력과 검증 결과를 업데이트했습니다. 검증 결과: dotnet build src/AxCopilot/AxCopilot.csproj -c Release -v minimal -p:OutputPath=bin\\verify\\ -p:IntermediateOutputPath=obj\\verify\\ 실행 기준 경고 0개, 오류 0개를 확인했습니다.
341 lines
13 KiB
C#
341 lines
13 KiB
C#
using System.Windows;
|
|
using AxCopilot.SDK;
|
|
using AxCopilot.Services;
|
|
using AxCopilot.Themes;
|
|
|
|
namespace AxCopilot.Handlers;
|
|
|
|
/// <summary>
|
|
/// L11-3: Cron 표현식 설명기 핸들러. "cron" 프리픽스로 사용합니다.
|
|
///
|
|
/// 예: cron * * * * * → 매 분 실행 (설명 + 다음 5회 실행 시간)
|
|
/// cron 0 9 * * 1-5 → 평일 오전 9시 실행
|
|
/// cron 0 0 1 * * → 매월 1일 자정
|
|
/// cron 30 18 * * 5 → 매주 금요일 오후 6시 30분
|
|
/// cron @daily → 매일 자정 (특수 키워드)
|
|
/// cron @hourly → 매시간
|
|
/// Enter → 표현식을 클립보드에 복사.
|
|
///
|
|
/// 지원 형식: 분(0-59) 시(0-23) 일(1-31) 월(1-12) 요일(0-7)
|
|
/// </summary>
|
|
public class CronHandler : IActionHandler
|
|
{
|
|
public string? Prefix => "cron";
|
|
|
|
public PluginMetadata Metadata => new(
|
|
"Cron",
|
|
"Cron 표현식 설명기 — 다음 실행 시간 · 한국어 설명",
|
|
"1.0",
|
|
"AX");
|
|
|
|
// 특수 키워드
|
|
private static readonly Dictionary<string, string> SpecialKeywords = new(StringComparer.OrdinalIgnoreCase)
|
|
{
|
|
["@yearly"] = "0 0 1 1 *",
|
|
["@annually"] = "0 0 1 1 *",
|
|
["@monthly"] = "0 0 1 * *",
|
|
["@weekly"] = "0 0 * * 0",
|
|
["@daily"] = "0 0 * * *",
|
|
["@midnight"] = "0 0 * * *",
|
|
["@hourly"] = "0 * * * *",
|
|
};
|
|
|
|
// 자주 쓰는 예제
|
|
private static readonly (string Expr, string Desc)[] CommonExamples =
|
|
[
|
|
("* * * * *", "매 분 실행"),
|
|
("0 * * * *", "매 시간 정각"),
|
|
("0 9 * * *", "매일 오전 9시"),
|
|
("0 9 * * 1-5", "평일 오전 9시"),
|
|
("0 0 * * *", "매일 자정"),
|
|
("0 0 1 * *", "매월 1일 자정"),
|
|
("0 0 1 1 *", "매년 1월 1일"),
|
|
("*/5 * * * *", "5분마다"),
|
|
("0 9,18 * * 1-5", "평일 오전 9시·오후 6시"),
|
|
("30 23 * * 5", "매주 금요일 오후 11시 30분"),
|
|
];
|
|
|
|
public Task<IEnumerable<LauncherItem>> GetItemsAsync(string query, CancellationToken ct)
|
|
{
|
|
var q = query.Trim();
|
|
var items = new List<LauncherItem>();
|
|
|
|
if (string.IsNullOrWhiteSpace(q))
|
|
{
|
|
items.Add(new LauncherItem("Cron 표현식 설명기",
|
|
"예: cron 0 9 * * 1-5 / cron @daily / cron */15 * * * *",
|
|
null, null, Symbol: "\uE823"));
|
|
foreach (var (expr, desc) in CommonExamples.Take(6))
|
|
items.Add(new LauncherItem(expr, desc, null, ("copy", expr), Symbol: "\uE823"));
|
|
return Task.FromResult<IEnumerable<LauncherItem>>(items);
|
|
}
|
|
|
|
// 특수 키워드 처리
|
|
var expr_ = q;
|
|
if (SpecialKeywords.TryGetValue(q, out var expanded))
|
|
expr_ = expanded;
|
|
|
|
if (!TryParseCron(expr_, out var cron))
|
|
{
|
|
items.Add(new LauncherItem("파싱 실패",
|
|
$"'{q}'은 유효한 cron 표현식이 아닙니다. 형식: 분 시 일 월 요일",
|
|
null, null, Symbol: "\uE783"));
|
|
return Task.FromResult<IEnumerable<LauncherItem>>(items);
|
|
}
|
|
|
|
// 한국어 설명
|
|
var description = Describe(cron);
|
|
items.Add(new LauncherItem(
|
|
description,
|
|
$"표현식: {expr_} · Enter 복사",
|
|
null, ("copy", expr_), Symbol: "\uE823"));
|
|
|
|
// 다음 5회 실행 시간
|
|
var nextRuns = GetNextRuns(cron, DateTime.Now, 5);
|
|
if (nextRuns.Count > 0)
|
|
{
|
|
items.Add(new LauncherItem("─ 다음 실행 시간 ─", "", null, null, Symbol: "\uE823"));
|
|
foreach (var run in nextRuns)
|
|
items.Add(new LauncherItem(
|
|
run.ToString("yyyy-MM-dd HH:mm (ddd)"),
|
|
GetRelativeTime(run),
|
|
null, ("copy", run.ToString("yyyy-MM-dd HH:mm:ss")),
|
|
Symbol: "\uE823"));
|
|
}
|
|
|
|
// 필드별 설명
|
|
items.Add(new LauncherItem("─ 필드 분석 ─", "", null, null, Symbol: "\uE823"));
|
|
items.Add(new LauncherItem("분", DescribeField(cron.Minute, 0, 59, "분"), null, null, Symbol: "\uE823"));
|
|
items.Add(new LauncherItem("시", DescribeField(cron.Hour, 0, 23, "시"), null, null, Symbol: "\uE823"));
|
|
items.Add(new LauncherItem("일", DescribeField(cron.Day, 1, 31, "일"), null, null, Symbol: "\uE823"));
|
|
items.Add(new LauncherItem("월", DescribeField(cron.Month, 1, 12, "월"), null, null, Symbol: "\uE823"));
|
|
items.Add(new LauncherItem("요일", DescribeField(cron.DayOfWeek, 0, 7, "요일"), null, null, Symbol: "\uE823"));
|
|
|
|
return Task.FromResult<IEnumerable<LauncherItem>>(items);
|
|
}
|
|
|
|
public Task ExecuteAsync(LauncherItem item, CancellationToken ct)
|
|
{
|
|
if (item.Data is ("copy", string text))
|
|
{
|
|
try
|
|
{
|
|
System.Windows.Application.Current.Dispatcher.Invoke(
|
|
() => Clipboard.SetText(text));
|
|
NotificationService.Notify("Cron", "클립보드에 복사했습니다.");
|
|
}
|
|
catch { /* 비핵심 */ }
|
|
}
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
// ── Cron 파서 ─────────────────────────────────────────────────────────────
|
|
|
|
private record CronExpr(string Minute, string Hour, string Day, string Month, string DayOfWeek);
|
|
|
|
private static bool TryParseCron(string expr, out CronExpr result)
|
|
{
|
|
result = new CronExpr("*", "*", "*", "*", "*");
|
|
var parts = expr.Trim().Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
|
if (parts.Length != 5) return false;
|
|
|
|
// 각 필드 유효성 검사
|
|
if (!IsValidCronField(parts[0], 0, 59)) return false;
|
|
if (!IsValidCronField(parts[1], 0, 23)) return false;
|
|
if (!IsValidCronField(parts[2], 1, 31)) return false;
|
|
if (!IsValidCronField(parts[3], 1, 12)) return false;
|
|
if (!IsValidCronField(parts[4], 0, 7)) return false;
|
|
|
|
result = new CronExpr(parts[0], parts[1], parts[2], parts[3], parts[4]);
|
|
return true;
|
|
}
|
|
|
|
private static bool IsValidCronField(string field, int min, int max)
|
|
{
|
|
if (field == "*") return true;
|
|
foreach (var part in field.Split(','))
|
|
{
|
|
if (part.Contains('/'))
|
|
{
|
|
var sp = part.Split('/');
|
|
if (sp.Length != 2) return false;
|
|
if (sp[0] != "*" && !int.TryParse(sp[0], out _)) return false;
|
|
if (!int.TryParse(sp[1], out var step) || step < 1) return false;
|
|
}
|
|
else if (part.Contains('-'))
|
|
{
|
|
var sp = part.Split('-');
|
|
if (sp.Length != 2) return false;
|
|
if (!int.TryParse(sp[0], out var a) || !int.TryParse(sp[1], out var b)) return false;
|
|
if (a < min || b > max || a > b) return false;
|
|
}
|
|
else
|
|
{
|
|
if (!int.TryParse(part, out var v)) return false;
|
|
if (v < min || v > max) return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// ── 다음 실행 시간 계산 ────────────────────────────────────────────────────
|
|
|
|
private static List<DateTime> GetNextRuns(CronExpr cron, DateTime from, int count)
|
|
{
|
|
var results = new List<DateTime>();
|
|
// 다음 분부터 시작
|
|
var current = from.AddSeconds(-from.Second).AddMinutes(1);
|
|
var limit = from.AddDays(366); // 최대 1년 탐색
|
|
|
|
while (results.Count < count && current < limit)
|
|
{
|
|
if (MatchesMonth(cron.Month, current.Month) &&
|
|
MatchesDay(cron.Day, current.Day) &&
|
|
MatchesDayOfWeek(cron.DayOfWeek, (int)current.DayOfWeek) &&
|
|
MatchesHour(cron.Hour, current.Hour) &&
|
|
MatchesMinute(cron.Minute, current.Minute))
|
|
{
|
|
results.Add(current);
|
|
current = current.AddMinutes(1);
|
|
}
|
|
else
|
|
{
|
|
current = AdvanceCron(cron, current);
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
private static DateTime AdvanceCron(CronExpr cron, DateTime dt)
|
|
{
|
|
// 빠른 스킵: 분 단위로 증가
|
|
return dt.AddMinutes(1);
|
|
}
|
|
|
|
private static bool MatchesField(string field, int value, int min, int max)
|
|
{
|
|
if (field == "*") return true;
|
|
foreach (var part in field.Split(','))
|
|
{
|
|
if (part.Contains('/'))
|
|
{
|
|
var sp = part.Split('/');
|
|
var step = int.Parse(sp[1]);
|
|
var start = sp[0] == "*" ? min : int.Parse(sp[0]);
|
|
for (var v = start; v <= max; v += step)
|
|
if (v == value) return true;
|
|
}
|
|
else if (part.Contains('-'))
|
|
{
|
|
var sp = part.Split('-');
|
|
var a = int.Parse(sp[0]);
|
|
var b = int.Parse(sp[1]);
|
|
if (value >= a && value <= b) return true;
|
|
}
|
|
else
|
|
{
|
|
if (int.Parse(part) == value) return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private static bool MatchesMinute(string f, int v) => MatchesField(f, v, 0, 59);
|
|
private static bool MatchesHour(string f, int v) => MatchesField(f, v, 0, 23);
|
|
private static bool MatchesDay(string f, int v) => MatchesField(f, v, 1, 31);
|
|
private static bool MatchesMonth(string f, int v) => MatchesField(f, v, 1, 12);
|
|
private static bool MatchesDayOfWeek(string f, int v)
|
|
{
|
|
// 0과 7 모두 일요일
|
|
if (f == "*") return true;
|
|
return MatchesField(f, v, 0, 7) || (v == 0 && MatchesField(f, 7, 0, 7));
|
|
}
|
|
|
|
// ── 한국어 설명 ───────────────────────────────────────────────────────────
|
|
|
|
private static string Describe(CronExpr c)
|
|
{
|
|
var parts = new List<string>();
|
|
|
|
// 분
|
|
var minDesc = c.Minute == "*" ? "매 분" : DescribeField(c.Minute, 0, 59, "분");
|
|
// 시
|
|
var hourDesc = c.Hour == "*" ? "매 시간" : DescribeField(c.Hour, 0, 23, "시");
|
|
// 일
|
|
var dayDesc = c.Day == "*" ? "" : DescribeField(c.Day, 1, 31, "일");
|
|
// 월
|
|
var monDesc = c.Month == "*" ? "" : DescribeField(c.Month, 1, 12, "월");
|
|
// 요일
|
|
var dowDesc = c.DayOfWeek == "*" ? "" : DescribeWeekday(c.DayOfWeek);
|
|
|
|
if (c.Minute == "0" && c.Hour == "0" && c.Day == "*" && c.Month == "*" && c.DayOfWeek == "*")
|
|
return "매일 자정(00:00)";
|
|
if (c.Minute == "0" && c.Hour == "*")
|
|
return "매 시간 정각";
|
|
if (c.Minute == "*" && c.Hour == "*" && c.Day == "*" && c.Month == "*" && c.DayOfWeek == "*")
|
|
return "매 분 실행";
|
|
|
|
// 조합
|
|
var sb = new System.Text.StringBuilder();
|
|
if (!string.IsNullOrEmpty(monDesc)) { sb.Append(monDesc); sb.Append(' '); }
|
|
if (!string.IsNullOrEmpty(dayDesc)) { sb.Append(dayDesc); sb.Append(' '); }
|
|
if (!string.IsNullOrEmpty(dowDesc)) { sb.Append(dowDesc); sb.Append(' '); }
|
|
sb.Append(hourDesc);
|
|
sb.Append(' ');
|
|
sb.Append(minDesc);
|
|
sb.Append(" 실행");
|
|
return sb.ToString().Trim();
|
|
}
|
|
|
|
private static string DescribeField(string field, int min, int max, string unit)
|
|
{
|
|
if (field == "*") return $"모든 {unit}";
|
|
if (field.StartsWith("*/"))
|
|
{
|
|
var step = field[2..];
|
|
return $"{step}{unit}마다";
|
|
}
|
|
if (field.Contains('-'))
|
|
{
|
|
var sp = field.Split('-');
|
|
return $"{sp[0]}~{sp[1]}{unit}";
|
|
}
|
|
if (field.Contains(','))
|
|
{
|
|
return string.Join(",", field.Split(',')) + unit;
|
|
}
|
|
return $"{field}{unit}";
|
|
}
|
|
|
|
private static string DescribeWeekday(string field)
|
|
{
|
|
string[] days = ["일", "월", "화", "수", "목", "금", "토", "일"];
|
|
if (field.Contains('-'))
|
|
{
|
|
var sp = field.Split('-');
|
|
if (int.TryParse(sp[0], out var a) && int.TryParse(sp[1], out var b))
|
|
return $"{days[a]}~{days[Math.Min(b, 7)]}요일";
|
|
}
|
|
if (field.Contains(','))
|
|
{
|
|
var parts = field.Split(',')
|
|
.Where(p => int.TryParse(p, out _))
|
|
.Select(p => days[int.Parse(p) % 8]);
|
|
return string.Join(",", parts) + "요일";
|
|
}
|
|
if (int.TryParse(field, out var d))
|
|
return days[d % 8] + "요일";
|
|
return field;
|
|
}
|
|
|
|
private static string GetRelativeTime(DateTime dt)
|
|
{
|
|
var diff = dt - DateTime.Now;
|
|
if (diff.TotalMinutes < 1) return "1분 이내";
|
|
if (diff.TotalHours < 1) return $"{(int)diff.TotalMinutes}분 후";
|
|
if (diff.TotalDays < 1) return $"{(int)diff.TotalHours}시간 {diff.Minutes}분 후";
|
|
if (diff.TotalDays < 7) return $"{(int)diff.TotalDays}일 {diff.Hours}시간 후";
|
|
return $"{(int)diff.TotalDays}일 후";
|
|
}
|
|
}
|