public void Execute(WebsiteContext context) { Logger.Log("Building the solution..."); var solutionFilePath = Directory.GetFiles(context.CurrentDirectory).FirstOrDefault(x => x.EndsWith(".sln")); var properties = new Dictionary<string, string> { {"Configuration", "Debug"} }; var buildParameters = new BuildParameters(); var buildLoggger = new InMemoryBuildLogger(); buildParameters.Loggers = new[] {buildLoggger}; var buildRequest = new BuildRequestData(solutionFilePath, properties, null, new[] { "Build" }, null); var buildResult = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequest); if (buildResult.OverallResult == BuildResultCode.Failure) { Logger.Error("Failed to build the solution!"); Logger.Space(); foreach (var buildError in buildLoggger.BuildErrors) { Logger.Error(buildError); } Logger.Space(); } else Logger.Success("Solution successfully built."); }
private static int GetEpiserverVersion(WebsiteContext context) { if (context.ExitAtNextCheck) return -1; if (context.EpiserverVersion > 0) return context.EpiserverVersion; // try to find an episerver .dll var episerverDllFilePath = Directory.EnumerateFiles(context.CurrentDirectory, "EPiServer.dll", SearchOption.AllDirectories).FirstOrDefault(); var packagesConfigPath = Path.Combine(context.GetWebProjectDirectory(), "packages.config"); if (episerverDllFilePath != null) { var version = FileVersionInfo.GetVersionInfo(episerverDllFilePath); return version.FileMajorPart; } // look in the package.config file for episerver if (File.Exists(packagesConfigPath)) { var doc = XDocument.Load(packagesConfigPath); var episerverPackage = doc.Descendants("package").FirstOrDefault(x => x.Attribute("id").Value == "EPiServer.CMS.Core"); if (episerverPackage != null) { var versionString = episerverPackage.Attribute("version").Value; var version = Version.Parse(versionString); return version.Major; } } return -1; }
private static string GetFrameworkVersion(WebsiteContext context) { var frameworkVersion = context.FrameworkVersion; if (frameworkVersion <= 3.5) { frameworkVersion = 2; } else if (frameworkVersion <= 4.5) { frameworkVersion = 4; } return string.Format("v{0:0.0}", frameworkVersion); }
private static void DisplayContext(WebsiteContext context) { Logger.TabIndention = 1; string[] ignoreProps = { "CurrentDirectory", "ExitAtNextCheck", "AppPoolIdentityVerb", "Force", "SkipHosts", "SkipTasks" }; foreach (var prop in context.GetType().GetProperties()) { if (!ignoreProps.Contains(prop.Name)) { Logger.Log("{0} = {1}", prop.Name, prop.GetValue(context, null)); } } Logger.TabIndention = 0; }
public static void ResolveContextDetails(WebsiteContext context) { if (string.IsNullOrWhiteSpace(context.CurrentDirectory)) context.CurrentDirectory = Environment.CurrentDirectory; else context.CurrentDirectory = Path.Combine(Environment.CurrentDirectory, context.CurrentDirectory); context.ProjectName = GetProjectName(context); context.WebProjectName = GetWebProjectName(context); context.ProjectUrl = GetProjectUrl(context); context.FrameworkVersion = GetFrameworkVersion(context); context.EpiserverVersion = GetEpiserverVersion(context); }
public void Execute(WebsiteContext context) { var projectUri = new Uri(context.ProjectUrl.ToLower()); Logger.Log("Requesting {0}...", projectUri.Host); try { WebClient client = new WebClient(); client.DownloadStringAsync(projectUri); Logger.Success("Site is being requested in the background."); } catch (Exception ex) { Logger.Error("Failed to ping site. " + ex.Message); } }
private void UpdateEpiserverFrameworkFile(WebsiteContext context, long siteId) { var episerverFrameworkFile = Directory.EnumerateFiles(context.GetWebProjectDirectory(), "EPiServerFramework.config", SearchOption.AllDirectories).FirstOrDefault(); if (episerverFrameworkFile == null) { Logger.Error("Could not find an EPiServerFramework.config file."); return; } var doc = XDocument.Load(episerverFrameworkFile); var key = string.Format("/LM/W3SVC/{0}/ROOT:{1}", siteId, Environment.MachineName); var automaticSiteMappingElement = doc.Descendants("automaticSiteMapping").FirstOrDefault(); if (automaticSiteMappingElement == null) { Logger.Warn("AutomaticSiteMapping element could not be found in EPiServerFramework.config"); return; } var alreadyUpdated = automaticSiteMappingElement.Descendants().Any(element => element.Attribute("key").Value.Equals(key)); if (alreadyUpdated) { Logger.Warn("No change needed."); return; } var fileAttributes = File.GetAttributes(episerverFrameworkFile); var hadReadOnly = false; var epiSiteId = doc.Descendants("siteHosts").FirstOrDefault().Attribute("siteId").Value; if (IsReadOnly(fileAttributes)) { fileAttributes = RemoveAttribute(fileAttributes, FileAttributes.ReadOnly); File.SetAttributes(episerverFrameworkFile, fileAttributes); hadReadOnly = true; } // add new site mapping automaticSiteMappingElement.Add(new XElement("add", new XAttribute("key", key), new XAttribute("siteId", epiSiteId))); doc.Save(episerverFrameworkFile); if (hadReadOnly) { fileAttributes = SetAttribute(fileAttributes, FileAttributes.ReadOnly); File.SetAttributes(episerverFrameworkFile, fileAttributes); } Logger.Success("EPiServerFramework.config has been updated."); Logger.Success("Do not forget to update the file in source control."); }
private static int GetEpiserverVersion(WebsiteContext context) { if (context.ExitAtNextCheck) return -1; if (context.EpiserverVersion > 0) return context.EpiserverVersion; var episerverDllFilePath = Directory.EnumerateFiles(context.CurrentDirectory, "EPiServer.dll", SearchOption.AllDirectories) .FirstOrDefault(); if (episerverDllFilePath != null) { var version = FileVersionInfo.GetVersionInfo(episerverDllFilePath); return version.FileMajorPart; } return -1; }
public void Execute(WebsiteContext context) { if (context.SkipHosts) { Logger.Log("Will skip adding HOSTS entry."); return; } string host = new Uri(context.ProjectUrl).Authority; if (host == "localhost") { Logger.Log("Skip because host is \"localhost\"."); return; } try { var hostsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), @"drivers\etc\hosts"); var hostLines = File.ReadAllLines(hostsPath); var hostAlreadyAdded = hostLines.Any(h => h.Contains(host)); if (hostAlreadyAdded) { Logger.Warn("No change needed."); return; } if (!context.HasAdministratorPrivileges()) { Logger.Error("Could not execute Hosts task without administrator rights."); return; } using (StreamWriter sw = File.AppendText(hostsPath)) { string prefix = ""; if (!string.IsNullOrWhiteSpace(hostLines.Last())) prefix = Environment.NewLine; sw.Write("{1}127.0.0.1\t\t{0}", host, prefix); } Logger.Success("Successfully added HOSTS entry."); } catch (Exception ex) { Logger.Error("Failed to add HOSTS entry. " + ex); } }
public void Execute(WebsiteContext context) { // EPiServerFramework.config came with CMS 6 if (context.EpiserverVersion < 6) { Logger.Warn("No change needed."); return; } using (ServerManager manager = new ServerManager()) { var webProjectDirectory = context.GetWebProjectDirectory(); var site = manager.Sites.SingleOrDefault(s => s.Applications.Any(app => app.VirtualDirectories.Any(dir => dir.PhysicalPath == webProjectDirectory))); if (site == null) { Logger.Warn("Could not find a matching site in IIS."); return; } UpdateEpiserverFrameworkFile(context, site.Id); } }
private static double GetFrameworkVersion(WebsiteContext context) { if (context.ExitAtNextCheck) return -1; if (context.FrameworkVersion > 0) return context.FrameworkVersion; var webProjectFilePath = Directory.EnumerateFiles(context.GetWebProjectDirectory(), "*.csproj").FirstOrDefault(); if (string.IsNullOrEmpty(webProjectFilePath) || !File.Exists(webProjectFilePath)) { context.ExitAtNextCheck = true; Console.WriteLine("The .csproj file for the \"{1}\" (web) project could not be found. Looked at: {0}", webProjectFilePath, context.WebProjectName); return -1; } var doc = XDocument.Load(webProjectFilePath); var targetFrameworkVersion = doc.Descendants().FirstOrDefault(x => x.Name.LocalName == "TargetFrameworkVersion"); if (targetFrameworkVersion == null) { context.ExitAtNextCheck = true; Console.WriteLine("Could not find TargetFrameworkVersion in Web project."); return -1; } var versionString = targetFrameworkVersion.Value.Replace("v", string.Empty); double version; if (!Double.TryParse(versionString, out version)) { context.ExitAtNextCheck = true; Console.WriteLine("Could not parse Framework Version from {0}.", versionString); return -1; } return version; }
public void Execute(WebsiteContext context) { var licenseDirectory = Config.Instance.EpiserverLicensePath; var webProjectPath = context.GetWebProjectDirectory(); var licensePath = Path.Combine(webProjectPath, LICENSE_FILENAME); if (File.Exists(licensePath)) { Logger.Warn("License file already exists."); return; } if (licenseDirectory.IsNullOrEmpty()) { Logger.Warn("Skipping because config is missing for EPiServer license directory."); Logger.Log(@"To update run command:"); Logger.TabIndention += 1; Logger.Log(@"wia config license.directory c:\path\to\directory"); Logger.TabIndention -= 1; return; } try { string licenseFileNeeded = Path.Combine(licenseDirectory, "CMS" + context.EpiserverVersion, LICENSE_FILENAME); if (!File.Exists(licenseFileNeeded)) { Logger.Error("Required license file could not be found."); Logger.Error("Path: " + licenseFileNeeded); return; } File.Copy(licenseFileNeeded, licensePath); } catch (Exception ex) { Logger.Error("Failed to copy license file to website: " + ex.Message); return; } Logger.Success("Copied license file for EPiServer CMS {0} to website.", context.EpiserverVersion); }
public void Execute(WebsiteContext context) { if (!context.HasAdministratorPrivileges()) { Logger.Error("Could not execute Webserver task without administrator rights."); return; } using (ServerManager manager = new ServerManager()) { var webProjectDirectory = context.GetWebProjectDirectory(); var siteAlreadyExists = manager.Sites.Any(s => s.Applications.Any(app => app.VirtualDirectories.Any(dir => dir.PhysicalPath == webProjectDirectory))); if (siteAlreadyExists) { Logger.Warn("Site already exists in IIS."); return; } var name = context.ProjectName; var host = new Uri(context.ProjectUrl).Host; // Create appool with project name var appPool = manager.ApplicationPools.Add(name); appPool.ManagedRuntimeVersion = GetFrameworkVersion(context); appPool.ProcessModel.PingingEnabled = false; appPool.Enable32BitAppOnWin64 = context.Enable32Bit; appPool.ManagedPipelineMode = context.AppPoolMode == AppPoolMode.Integrated ? ManagedPipelineMode.Integrated : ManagedPipelineMode.Classic; if (!string.IsNullOrEmpty(Config.Instance.AppPoolUsername)) { Logger.Log("Setting AppPool identity..."); var password = Config.Instance.AppPoolPassword; if (string.IsNullOrEmpty(password)) { Logger.Warn("Please fill in password for AppPool identity user!"); Console.WriteLine("\tUsername: "******"\tPassword: "******"Created a new AppPool with .NET {0} named {1}", appPool.ManagedRuntimeVersion, name); // Create site with appool. Site site = manager.Sites.Add(name, webProjectDirectory, 80); site.ServerAutoStart = true; site.Applications[0].ApplicationPoolName = name; Logger.Log("Created a new site named " + name); site.Bindings.Clear(); site.Bindings.Add("*:80:" + host, "http"); Logger.Log("Added binding for " + host); try { manager.CommitChanges(); Logger.Success("Site and AppPool has been created in IIS."); } catch (Exception ex) { Logger.Error(ex.ToString()); context.ExitAtNextCheck = true; } } }
private static string GetProjectName(WebsiteContext context) { if (context.ExitAtNextCheck) return null; string solutionFileName = Directory.EnumerateFiles(context.CurrentDirectory).FirstOrDefault(x => x.EndsWith(".sln")); string projectName = context.ProjectName; if (solutionFileName.IsNullOrEmpty()) { context.ExitAtNextCheck = true; Console.WriteLine("Could not find a solution file in the current directory."); } if (string.IsNullOrWhiteSpace(projectName)) { projectName = Path.GetFileNameWithoutExtension(solutionFileName); } return projectName; }
private static string GetWebProjectName(WebsiteContext context) { if (context.ExitAtNextCheck) return null; string webProjectName = context.WebProjectName; if (string.IsNullOrWhiteSpace(webProjectName)) { if (Directory.GetFiles(context.CurrentDirectory).Any(item => item.ToLower().EndsWith("\\web.config"))) { webProjectName = "."; } else{ var webDirectories = Directory.EnumerateDirectories(context.CurrentDirectory) .Where(dir => Path.GetFileName(dir).Contains("Web", StringComparison.OrdinalIgnoreCase)) .ToList(); if (!webDirectories.Any()) { context.ExitAtNextCheck = true; Console.WriteLine("Web project could not be resolved. Please specifiy using \"--webproject Project.Web\". \n"); return null; } if (webDirectories.Count > 1) { context.ExitAtNextCheck = true; Console.WriteLine("You need to specificy which web project to use: \n--webproject " + webDirectories.Select(path => Path.GetFileName(path)).Aggregate((a, b) => a + "\n--webproject " + b) + "\n"); return null; } webProjectName = Path.GetFileName(webDirectories.FirstOrDefault()); } } var projectDirectory = Path.Combine(context.CurrentDirectory, webProjectName); if (!Directory.Exists(projectDirectory)) { context.ExitAtNextCheck = true; Console.WriteLine("Web project directory does not seem to exist at " + projectDirectory); return null; } return webProjectName; }
private static string GetProjectUrl(WebsiteContext context) { if (context.ExitAtNextCheck) return null; var projectUrl = context.ProjectUrl; if (!string.IsNullOrWhiteSpace(projectUrl)) { if (!projectUrl.StartsWith("http://")) projectUrl = "http://" + projectUrl; return projectUrl; } var webProjectFolderPath = context.GetWebProjectDirectory(); var episerverConfigPath = Directory.GetFiles(webProjectFolderPath, "episerver.config", SearchOption.AllDirectories) .FirstOrDefault(); if (!File.Exists(episerverConfigPath)) { // the episerver config section might be in web.config var webConfigPath = Path.Combine(webProjectFolderPath, "web.config"); if (!File.Exists(webConfigPath)) { // serious trouble if this file can not be found. context.ExitAtNextCheck = true; Console.WriteLine("The web.config file could not be found in " + webProjectFolderPath); return null; } episerverConfigPath = webConfigPath; } var doc = XDocument.Load(episerverConfigPath); var siteSettings = doc.Descendants().FirstOrDefault(x => x.Name.LocalName == "siteSettings"); if (siteSettings == null) { context.ExitAtNextCheck = true; Console.WriteLine("Could not find the EPiServer configuration section in neither episerver.config or web.config."); return null; } projectUrl = siteSettings.Attribute("siteUrl").Value; return projectUrl; }
private static void ProcessTasks(WebsiteContext context) { Console.WriteLine("\nInstallation started!\n"); var tasks = GetTasksInAssembly(); foreach (var task in tasks) { var taskName = task.GetType().Name.Replace("Task", string.Empty); if (context.SkipTasks.Any(t => t.Equals(taskName, StringComparison.OrdinalIgnoreCase))) continue; Logger.Log(taskName + ":"); Logger.TabIndention = 1; task.Execute(context); Logger.TabIndention = 0; if (context.ExitAtNextCheck) { break; } } Logger.Space(); if (context.ExitAtNextCheck) { Logger.Error("Installation failed."); } else { Logger.Success("Installation finished successfully (and URL has been copied to clipboard)"); Clipboard.SetText(context.ProjectUrl); } }
private static void InitiateInstallTask(WebsiteContext context) { ContextResolver.ResolveContextDetails(context); if (context.ExitAtNextCheck) return; Console.WriteLine("\nConfiguration:"); DisplayContext(context); if (!context.HasAdministratorPrivileges()) { Logger.Space(); Logger.Warn("WIA needs to run with administrator privileges to modify IIS and HOSTS-file.\nOpen new command prompt with \"Run as administrator\" and try again."); return; } if (context.Force) { ProcessTasks(context); } else { Console.WriteLine("\nDo you want to continue installation with this configuration? (y/n)"); var response = Console.ReadLine(); if (response != null && response.Equals("y", StringComparison.InvariantCultureIgnoreCase)) { ProcessTasks(context); } } }