/// <summary> /// Uploads web resource /// </summary> /// <param name="webResource">Web resource to be updated</param> /// <param name="filePath">File with a content to be set for web resource</param> /// <returns>Returns true if web resource is updated</returns> private bool UpdateWebResourceByFile(Entity webResource, string filePath) { var webResourceName = Path.GetFileName(filePath); Logger.WriteLine("Uploading " + webResourceName, _extendedLog); var project = ProjectHelper.GetSelectedProject(); var projectRootPath = ProjectHelper.GetProjectRoot(project); var localContent = ProjectHelper.GetEncodedFileContent(filePath); var remoteContent = webResource.GetAttributeValue <string>("content"); var hasContentChanged = remoteContent.Length != localContent.Length || remoteContent != localContent; if (hasContentChanged == false) { Logger.WriteLine("Uploading of " + webResourceName + " was skipped: there aren't any change in the web resource", _extendedLog); Logger.WriteLine(webResourceName + " has no changes", !_extendedLog); return(false); } var version = VersionsHelper.UpdateJS(filePath); if (version != null) { version = $", new version => {version}"; localContent = ProjectHelper.GetEncodedFileContent(filePath); // reload with new version } else { version = string.Empty; } UpdateWebResourceByContent(webResource, localContent); var relativePath = filePath.Replace(projectRootPath + "\\", ""); webResourceName = webResource.GetAttributeValue <string>("name"); Logger.WriteLine($"{webResourceName} uploaded from {relativePath}{version}", !_extendedLog); return(true); }
/// <summary> /// Uploads and publishes files to CRM /// </summary> public void PublishWebResources() { ProjectHelper.SaveAll(); Logger.Clear(); ProjectHelper.SetStatusBar("Uploading..."); Logger.WriteLineWithTime(_autoPublish ? "Publishing web resources..." : "Uploading web resources..."); Logger.WriteLine("Connecting to CRM..."); Logger.WriteLine("URL: " + _connectionDetail.WebApplicationUrl); Logger.WriteLine("Solution Name: " + _connectionDetail.SolutionFriendlyName); Logger.WriteLine("--------------------------------------------------------------"); Logger.WriteLine("Loading files paths", _extendedLog); var projectFiles = GetSelectedFiles(); if (projectFiles == null || projectFiles.Count == 0) { Logger.WriteLine("Failed to load files paths", _extendedLog); return; } Logger.WriteLine(projectFiles.Count + " path" + (projectFiles.Count == 1 ? " was" : "s were") + " loaded", _extendedLog); try { Logger.WriteLine("Starting uploading process", _extendedLog); var webresources = UploadWebResources(projectFiles); Logger.WriteLine("Uploading process was finished", _extendedLog); if (webresources.Count > 0) { Logger.WriteLine("--------------------------------------------------------------"); foreach (var name in webresources.Values) { Logger.WriteLine(name + " successfully uploaded"); } Logger.WriteLine("Updating Solution Version ..."); var solutionVersion = VersionsHelper.UpdateSolution(_orgService, _connectionDetail.SolutionId); Logger.WriteLine($"New Solution Version: {solutionVersion} "); } Logger.WriteLine("--------------------------------------------------------------"); Logger.WriteLineWithTime(webresources.Count + " file" + (webresources.Count == 1 ? " was" : "s were") + " uploaded"); if (_autoPublish) { ProjectHelper.SetStatusBar("Publishing..."); PublishWebResources(webresources.Keys); } if (_autoPublish) { ProjectHelper.SetStatusBar(webresources.Count + " web resource" + (webresources.Count == 1 ? " was" : "s were") + " published"); } else { ProjectHelper.SetStatusBar(webresources.Count + " web resource" + (webresources.Count == 1 ? " was" : "s were") + " uploaded"); } } catch (Exception ex) { ProjectHelper.SetStatusBar("Failed to publish script" + (projectFiles.Count == 1 ? "" : "s")); Logger.WriteLine("Failed to publish script" + (projectFiles.Count == 1 ? "." : "s.")); Logger.WriteLine(ex.Message); Logger.WriteLine(ex.StackTrace, _extendedLog); } Logger.WriteLineWithTime("Done."); }