using LibGit2Sharp; using NLog; using Server.System; using System.Diagnostics; namespace Server.Git { public abstract class AbstractGit { private static readonly NLog.ILogger logger = LogManager.GetCurrentClassLogger(); public bool isRestart; string _repositoryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "excel"); public string excel = ""; public string repositoryPath { get { return _repositoryPath; } } /// /// 가장먼저 시작해야 하는 스크립트 /// public void Init() { restart: isRestart = false; Pull(); if (isRestart) goto restart; ChangeScript(); if (isRestart) goto restart; Push(); if (isRestart) goto restart; } /// /// 엑셀 불러오기, 저장, 혹은 배포 까지 작업해야하는 함수 /// public abstract void ChangeScript(); private void Pull() { if (!Directory.Exists(_repositoryPath)) { Directory.CreateDirectory(_repositoryPath); RepositorySet($"clone {STATICS.remoteUrl} {_repositoryPath}", null); //main브렌치로 세팅 RepositorySet("branch -m main", _repositoryPath); RepositorySet("branch --set-upstream-to=origin/main main", _repositoryPath); } // pull 명령어 실행 RepositorySet("pull", _repositoryPath); } private static void RepositorySet(string command, string workingDirectory) { ProcessStartInfo psi = new ProcessStartInfo { FileName = "git", Arguments = command, WorkingDirectory = workingDirectory, RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true }; using (Process process = new Process { StartInfo = psi }) { process.Start(); process.WaitForExit(); // Git 명령 실행 결과 출력 Console.WriteLine(process.StandardOutput.ReadToEnd()); // 오류 메시지 출력 Console.WriteLine(process.StandardError.ReadToEnd()); } } private void Push() { if(excel == "") { return; } //json 저장 using (StreamWriter writer = new StreamWriter(repositoryPath + "\\excel.json")) { writer.Write(excel); Console.WriteLine($"파일이 저장되었습니다 : {repositoryPath + "\\excel.json"}"); } // 스테이징 RepositorySet("add .", repositoryPath); // 커밋 RepositorySet($"commit -m \"update excel data\"", repositoryPath); // 푸시 RepositorySet("push origin main", repositoryPath); } /// /// 모든 파일리스트 반환 /// /// 검색할 폴더 경로 /// 확장자 /// public List GetFiles(string directoryPath, string extension) { List xlsxFileList = new List(); try { string[] files = Directory.GetFiles(directoryPath); foreach (string file in files) { if (Path.GetExtension(file).Equals(".xlsx", StringComparison.OrdinalIgnoreCase)) { xlsxFileList.Add(file); } } string[] subdirectories = Directory.GetDirectories(directoryPath); foreach (string subdirectory in subdirectories) { //git시스템 파일은 뛰어넘기 if (subdirectory == directoryPath + "\\.git") continue; //제귀 xlsxFileList.AddRange(GetFiles(subdirectory, extension)); } } catch (Exception ex) { logger.Error(ex); } return xlsxFileList; } } }