/// <summary> /// Publish website using MSDeploy or Ftp /// </summary> /// <param name="siteRoot"></param> /// <param name="siteName"></param> /// <param name="publishMethod"></param> /// <param name="siteRootRelativePath"></param> /// <param name="deleteExisting"></param> /// <param name="paramResolverFunc"></param> /// <returns></returns> public bool PublishWebSite(string siteRoot, string siteName, PublishMethod publishMethod = PublishMethod.MSDeploy, string siteRootRelativePath = "", bool deleteExisting = true, Func <string, string> paramResolverFunc = null) { if (!Directory.Exists(siteRoot)) { throw new DirectoryNotFoundException(string.Format("Publishing error: site directory '{0}' does not exist.", siteRoot)); } var result = true; TestEasyLog.Instance.Info(string.Format("Publishing web site '{0}'", siteName)); var publishProfile = GetPublishingProfile(siteName, publishMethod); switch (publishMethod) { case PublishMethod.MSDeploy: WebDeployHelper.DeployWebSite(siteRoot, siteName, publishProfile.GetWebDeployUrl(), publishProfile.UserName, publishProfile.UserPassword, deleteExisting, paramResolverFunc); break; case PublishMethod.Ftp: result = FtpHelper.Authorize(publishProfile.UserName, publishProfile.UserPassword) .UploadDir(siteRoot, publishProfile.PublishUrl, siteRootRelativePath); break; default: throw new Exception(string.Format("Deployment method '{0}' is not supported.", publishMethod)); } return(result); }
/// <summary> /// Publish website using MSDeploy or Ftp /// </summary> /// <param name="file"></param> /// <param name="siteName"></param> /// <param name="publishMethod"></param> /// <param name="siteRootRelativePath"></param> /// <param name="deleteExisting"></param> /// <param name="paramResolverFunc"></param> /// <returns></returns> public bool PublishFile(string file, string siteName, PublishMethod publishMethod = PublishMethod.MSDeploy, string siteRootRelativePath = "", bool deleteExisting = true, Func <string, string> paramResolverFunc = null) { if (!File.Exists(file)) { throw new FileNotFoundException(string.Format("Publishing error: file '{0}' does not exist.", file)); } var result = true; var publishProfile = GetPublishingProfile(siteName, publishMethod); var fileRelativePath = siteName + "/"; if (!string.IsNullOrEmpty(siteRootRelativePath)) { fileRelativePath += (siteRootRelativePath + "/"); } fileRelativePath += Path.GetFileName(file); TestEasyLog.Instance.Info(string.Format("Publishing file '{0}' to web site '{1}'", file, siteName)); switch (publishMethod) { case PublishMethod.MSDeploy: WebDeployHelper.DeployFile(file, fileRelativePath, publishProfile.GetWebDeployUrl(), publishProfile.UserName, publishProfile.UserPassword, deleteExisting, paramResolverFunc); break; case PublishMethod.Ftp: result = FtpHelper.Authorize(publishProfile.UserName, publishProfile.UserPassword) .UploadFile(file, Path.Combine(publishProfile.PublishUrl, fileRelativePath)); break; default: throw new Exception(string.Format("Deployment method '{0}' is not supported.", publishMethod)); } return(result); }
/// <summary> /// Publish directory using MSDeploy or Ftp /// </summary> /// <param name="directory"></param> /// <param name="siteName"></param> /// <param name="publishMethod"></param> /// <param name="siteRootRelativePath"></param> /// <param name="deleteExisting"></param> /// <param name="paramResolverFunc"></param> /// <returns></returns> public bool PublishDirectory(string directory, string siteName, PublishMethod publishMethod = PublishMethod.MSDeploy, string siteRootRelativePath = "", bool deleteExisting = true, Func <string, string> paramResolverFunc = null) { if (!Directory.Exists(directory)) { throw new DirectoryNotFoundException(string.Format("Publishing error: directory '{0}' does not exist.", directory)); } var result = true; var levelUpRequested = 0; if (!string.IsNullOrEmpty(siteRootRelativePath)) { // remove .., / and \ from relative path siteRootRelativePath = siteRootRelativePath.Trim('\\').Trim('/'); if (siteRootRelativePath.StartsWith("..")) { while (siteRootRelativePath.StartsWith("..") || siteRootRelativePath.StartsWith("/") || siteRootRelativePath.StartsWith("\\")) { levelUpRequested++; siteRootRelativePath = siteRootRelativePath.Trim('.').Trim('\\').Trim('/'); } siteRootRelativePath = siteRootRelativePath.Trim('.'); if (levelUpRequested > 2) { levelUpRequested = 2; } } } var publishProfile = GetPublishingProfile(siteName, publishMethod); switch (publishMethod) { case PublishMethod.MSDeploy: { var dirRelativePath = siteName + "/"; if (!string.IsNullOrEmpty(siteRootRelativePath)) { dirRelativePath += (siteRootRelativePath + "/"); } var destinationUrl = publishProfile.GetWebDeployUrl(); TestEasyLog.Instance.Info(string.Format("Publishing directory '{0}' to website path '{1}'", directory, destinationUrl)); WebDeployHelper.DeployDirectory(directory, dirRelativePath, destinationUrl, publishProfile.UserName, publishProfile.UserPassword, deleteExisting, paramResolverFunc); break; } case PublishMethod.Ftp: { var publishUrl = publishProfile.PublishUrl; while (levelUpRequested > 0) { var index = publishUrl.LastIndexOf('/'); publishUrl = publishUrl.Substring(0, index); levelUpRequested--; } result = FtpHelper.Authorize(publishProfile.UserName, publishProfile.UserPassword) .UploadDir(directory, publishUrl, siteRootRelativePath); break; } default: throw new Exception(string.Format("Deployment method '{0}' is not supported.", publishMethod)); } return(result); }