using LibGit2Sharp; using Newtonsoft.Json.Linq; using NLog; using Server.System; using System.Diagnostics; namespace Server.Git { public abstract class AbstractGit { public Crypto crypto = new Crypto(); private static readonly NLog.ILogger logger = LogManager.GetCurrentClassLogger(); public bool isRestart; string _repositoryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "excel"); public string repositoryPath { get { return _repositoryPath; } } /// /// 가장먼저 시작해야 하는 스크립트 /// public void Init() { restart: isRestart = false; Pull(); if (isRestart) goto restart; string excel = ChangeScript(); if (isRestart) goto restart; Push(excel); if (isRestart) goto restart; } /// /// 엑셀 불러오기, 저장, 혹은 배포 까지 작업해야하는 함수 /// public abstract string 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, EnvironmentVariables = { ["GIT_ASKPASS"] = "echo" } }; using (Process process = new Process { StartInfo = psi }) { process.Start(); process.WaitForExit(); // Git 명령 실행 결과 출력 logger.Info(process.StandardOutput.ReadToEnd()); } } private void Push(string excel) { if(excel == "") { return; } //json 저장 using (StreamWriter writer = new StreamWriter(repositoryPath + @"/excel.json")) { writer.Write(excel); logger.Info($"save file : {repositoryPath + @"/excel.json"}"); } //이곳에서 json변경후 저장 //압축 //string EncryptoExcel = crypto.Compress(excel); //암호화 ProtocolProcessor.cryptoData = crypto.Compress(excel); // 스테이징 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; } } }