private void ProcessFile(string filePath, dynamic parameters) { try { //Process the file name HandlebarsTemplate <object, object> templateFileName = Handlebars.Compile(filePath.Replace("\\", "//")); string processedFileName = templateFileName(parameters); processedFileName = processedFileName.Replace("//", "\\"); if ((Template.SearchFileExtensions as IList <string>).IndexOf(Path.GetExtension(filePath)) >= 0) { string fileContents = File.ReadAllText(filePath); //Process the file contents HandlebarsTemplate <object, object> templateFileContents = Handlebars.Compile(fileContents); string processedFileContents = templateFileContents(parameters); OnProcessFile?.Invoke(GetTruncateFilePath(processedFileName, Template.SearchDirectory), Encoding.ASCII.GetBytes(processedFileContents)); } else { OnProcessFile?.Invoke(GetTruncateFilePath(processedFileName, Template.SearchDirectory), File.ReadAllBytes(filePath)); } } catch (Exception e) { OnProcessFileError?.Invoke(GetTruncateFilePath(filePath, Template.SearchDirectory), e); } }
private static void UpdateInternal(string msg) { LocalRunInfo localRunInfo = null; UpdateUrlInfo updateUrlInfo = null; try { OnStatus?.Invoke("正在读取本地配置..."); localRunInfo = GetLocalRunInfo(); OnStatus?.Invoke("正在验证更新地址..."); if (string.IsNullOrEmpty(localRunInfo.UpdateUrl)) { OnNotSpecifyUpdateUrl?.Invoke(localRunInfo); return; } if ((updateUrlInfo = UpdateUrlInfo.Parse(localRunInfo.UpdateUrl)) == null) { OnInvalidUpdateUrl?.Invoke(localRunInfo); return; } OnStatus?.Invoke("正在读取服务器..."); var host = updateUrlInfo.Host; var systemId = updateUrlInfo.SystemId; var remoteRunInfo = GetRemoteRunInfo(GetRemoteInfoUrl(host, systemId)); OnStatus?.Invoke("正在检测差异..."); var initStart = (localRunInfo.RunFiles.Count == 0); var fileDiffResults = CalcDiff(localRunInfo, remoteRunInfo).ToList(); var filesToUpdate = fileDiffResults.Where(f => f.Status == UpdateRunFileStatus.Update || f.Status == UpdateRunFileStatus.Delete).ToList(); var progress = 0; var total = filesToUpdate.Count; if (total > 0) { OnStatus?.Invoke("正在检测运行程序..."); if (IsThisAppRunning() || (!string.IsNullOrEmpty(localRunInfo.AppRunCmd) && IsMainAppRunning(localRunInfo))) { OnMainAppAlreadyRunning?.Invoke(localRunInfo); return; } } OnStatus?.Invoke("开始更新程序..."); OnBeginUpdate?.Invoke(localRunInfo); var runDir = Path.GetDirectoryName(Application.ExecutablePath); var thisExeName = Path.GetFileName(Application.ExecutablePath); var runBat = false; var runFileResults = new DicIgnoreCase <string>(); var failedUpdateFiles = new List <string>(); var failedDeleteFiles = new List <string>(); foreach (var file in filesToUpdate) { if (Constants.BatFile.Equals(file.Path, StringComparison.OrdinalIgnoreCase)) { runBat = true; } if (file.Status == UpdateRunFileStatus.Update) { var url = GetRunFileUrl(host, systemId, file.Path); var localPath = Path.Combine(runDir, file.Path); var updateSelf = file.Path.Equals(thisExeName, StringComparison.OrdinalIgnoreCase); // 自更新 OnProcessFile?.Invoke("正在安装文件:" + localPath); var success = DownloadRunFile(url, localPath, possibllyInUse: updateSelf); if (!success) { failedUpdateFiles.Add(file.Path); // 对于更新失败的文件(非新增),新的配置文件应该仍包含更新前的文件标识 if (!string.IsNullOrEmpty(file.OldTag)) { runFileResults.Add(file.Path, file.OldTag); } } else { runFileResults.Add(file.Path, file.NewTag); } } else if (file.Status == UpdateRunFileStatus.Delete) { // 删除文件 var localPath = Path.Combine(runDir, file.Path); OnProcessFile?.Invoke("正在删除文件:" + localPath); if (!DeleteRunFile(localPath)) { failedDeleteFiles.Add(file.Path); // 对于删除失败的文件,新的配置文件应该仍包含旧的文件标识 runFileResults.Add(file.Path, file.OldTag); } } OnProgress?.Invoke((++progress) * 1.0 / total); } foreach (var f in fileDiffResults.Where(f => f.Status == UpdateRunFileStatus.NotModified)) { runFileResults.Add(f.Path, f.OldTag); } localRunInfo.Ver = remoteRunInfo.Ver; localRunInfo.RunFiles = runFileResults; OnComplete?.Invoke(localRunInfo, fileDiffResults, failedUpdateFiles, failedDeleteFiles, initStart, runBat, msg); } catch (Exception ex) { if (localRunInfo != null) { LogHelper.LogErrorToServer($"客户端更新失败({msg})", ex, localRunInfo); } OnError?.Invoke(localRunInfo, ex); } }