Ejemplo n.º 1
0
		public void PublishDirectory(BuildPublishOptions options)
		{
			if(string.IsNullOrEmpty(options.Directory))
			{
				throw new ArgumentNullException("Missing DirectoryPath");
			}
			if(string.IsNullOrEmpty(options.ApiUrl))
			{
				throw new ArgumentNullException("Missing ApiUrl");
			}
			if(string.IsNullOrEmpty(options.ProjectId))
			{
				throw new ArgumentNullException("Missing ProjectId");
			}
			if(string.IsNullOrEmpty(options.ComponentId))
			{
				throw new ArgumentNullException("Missing ComponentId");
			}
			if(string.IsNullOrEmpty(options.BranchId))
			{
				throw new ArgumentNullException("Missing BranchId");
			}
			if(string.IsNullOrEmpty(options.Version))
			{
				throw new ArgumentNullException("Missing Version");
			}
			_logger.Info("Start publishing directory {0} to URL {1}", options.Directory, options.ApiUrl);
			_regexResolver.ResolveValues(options);
			string zipPath = Path.ChangeExtension(Path.GetTempFileName(), ".zip");
			if(!Directory.Exists(options.Directory))
			{
				throw new DirectoryNotFoundException(string.Format("Publish directory \"{0}\" not found", options.Directory));
			}
			_zipper.ZipDirectory(options.Directory, zipPath);

			var buildData = PublishZip(options.ApiUrl, options.ProjectId, options.ComponentId, options.BranchId, options.Version, zipPath, options.UserName, options.Password);
            DeployBuilds(options.ApiUrl, options.UserName, options.Password, options.DeployEnvironmentName, buildData);

			try 
			{
				File.Delete(zipPath);
			}
			catch(Exception err)
			{
				_logger.ErrorException(string.Format("Failed to delete ZIP file {0}: {1}",  zipPath, err.ToString()), err);
			}
			_logger.Info("Done publishing directory {0} to URL {1}", options.Directory, options.ApiUrl);
		}
Ejemplo n.º 2
0
		public void PublishFilePattern(BuildPublishOptions options)
		{
			if(string.IsNullOrEmpty(options.FilePattern))
			{
				throw new ArgumentNullException("Missing FilePattern");
			}
			var parts = options.FilePattern.Split(new char[] {'|'},StringSplitOptions.RemoveEmptyEntries);
			string directory = parts[0];
			string searchPattern = null;
			int filePatternBatchSize = 4;
			SearchOption searchOption = SearchOption.AllDirectories;
			if(parts.Length > 1)
			{
				searchPattern = parts[1];
			}
			if(parts.Length > 2)
			{
				bool recurseDirectories;
				if(!bool.TryParse(parts[2], out recurseDirectories))
				{
					throw new ArgumentException("Invalid boolean value for 3rd FilePattern value (recurseDirectories): " + parts[2]);
				}
				if(recurseDirectories)
				{
					searchOption = SearchOption.AllDirectories;
				}
				else 
				{
					searchOption = SearchOption.TopDirectoryOnly;
				}
			}
			if(parts.Length > 3)
			{
				if(!int.TryParse(parts[3], out filePatternBatchSize))
				{
					throw new ArgumentException("Invalid int value for 4th FilePattern value (batchSize): " + parts[3]);
				}
			}
			var fileList = Directory.EnumerateFiles(directory, searchPattern, searchOption);
			var batchedFileListList = fileList.Select((x, i) => new { Index = i, Value = x })
										   .GroupBy(x => x.Index / filePatternBatchSize)
										   .Select(x => x.Select(v => v.Value).ToList())
										   .ToList();
			_logger.Info("Publishing file pattern {0}, the following {1} files were found:", options.FilePattern, fileList.Count());
			var taskList = new  List<System.Threading.Tasks.Task>();
			foreach(var filePath in fileList)
			{
				string x = filePath;
				_logger.Info("- {0}", x);
				var task = Task.Factory.StartNew(() =>
				{
					//var fileOptions = AutoMapper.Mapper.Map(options, new BuildPublishOptions());
					// Don't try to use automapper here.  Something about it evaluating it later during the task causes
					//	it to pollute values between threads.  Not sure why, but ouch ouch ouch
					var fileOptions = new BuildPublishOptions
					{
						File = x,
						ApiUrl = options.ApiUrl,
						BranchId = options.BranchId,
						ComponentId = options.ComponentId,
						Directory = options.Directory,
						FilePattern = null,
						NewFileName = options.NewFileName,
						ProjectId = options.ProjectId,
						Version = options.Version,
                        DeployEnvironmentName = options.DeployEnvironmentName
					};
					fileOptions.FilePattern = null;
					this.PublishFile(fileOptions);
				});
				taskList.Add(task);
			}
			//foreach(var fileBatch in batchedFileListList)
			//{
			//	_logger.Info("- {0}", string.Join(",", fileBatch));
			//	var task = Task.Factory.StartNew(()=>
			//	{
			//		foreach(var filePath in fileBatch)
			//		{
			//			//var fileOptions = AutoMapper.Mapper.Map(options, new BuildPublishOptions());
			//			// Don't try to use automapper here.  Something about it evaluating it later during the task causes
			//			//	it to pollute values between threads.  Not sure why, but ouch ouch ouch
			//			var fileOptions = new BuildPublishOptions
			//			{
			//				File = filePath,
			//				ApiUrl = options.ApiUrl,
			//				BranchId = options.BranchId,
			//				ComponentId = options.ComponentId,
			//				Directory = options.Directory,
			//				FilePattern = null,
			//				NewFileName = options.NewFileName,
			//				ProjectId = options.ProjectId,
			//				Version = options.Version
			//			};
			//			fileOptions.File = filePath;
			//			fileOptions.FilePattern = null;
			//			this.PublishFile(fileOptions);
			//		}
			//	});
			//	taskList.Add(task);
			//}
			Task.WaitAll(taskList.ToArray());
			var exceptionList = taskList.Where(i=>i.Exception != null).Select(i=>i.Exception);
			foreach(var exception in exceptionList)
			{
				_logger.ErrorException(exception.ToString(), exception);
			}
			if(exceptionList.Any())
			{
				throw new Exception("Publish exceptions occurred");
			}
		}
Ejemplo n.º 3
0
		public void PublishFile(BuildPublishOptions options)
		{
			if(string.IsNullOrEmpty(options.File))
			{
				throw new ArgumentNullException("Missing FilePath");
			}
			if(string.IsNullOrEmpty(options.ApiUrl))
			{
				throw new ArgumentNullException("Missing ApiUrl");
			}
			if(string.IsNullOrEmpty(options.ProjectId))
			{
				throw new ArgumentNullException("Missing ProjectId");
			}
			if(string.IsNullOrEmpty(options.ComponentId))
			{
				throw new ArgumentNullException("Missing ComponentId");
			}
			if(string.IsNullOrEmpty(options.BranchId))
			{
				throw new ArgumentNullException("Missing BranchId");
			}
			if(string.IsNullOrEmpty(options.Version))
			{
				throw new ArgumentNullException("Missing Version");
			}
			bool deleteFile = false;
			string directoryToDelete = null;
			_logger.Info("Start publishing file {0} to URL {1}", options.File, options.ApiUrl);
			_regexResolver.ResolveValues(options);
			string zipPath = Path.ChangeExtension(Path.GetTempFileName(), ".zip");
			if (!File.Exists(options.File))
			{
				throw new DirectoryNotFoundException(string.Format("File directory \"{0}\" not found", options.File));
			}
			if(!string.IsNullOrWhiteSpace(options.NewFileName))
			{
				_logger.Info("New file name {0} provided", options.NewFileName);
				string oldFilePath = options.File;
				directoryToDelete = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
				Directory.CreateDirectory(directoryToDelete);
				options.File = Path.Combine(directoryToDelete, options.NewFileName);
				_logger.Info("Copying {0} to {1}", oldFilePath, options.File);
				File.Copy(oldFilePath, options.File);
				deleteFile = true;
			}
			_zipper.ZipFile(options.File, zipPath);

			PublishZip(options.ApiUrl, options.ProjectId, options.ComponentId, options.BranchId, options.Version, zipPath, options.UserName, options.Password);

			try
			{
				File.Delete(zipPath);
			}
			catch (Exception err)
			{
				_logger.WarnException(string.Format("Failed to delete ZIP file {0}: {1}", zipPath, err.ToString()), err);
			}

			if(deleteFile)
			{
				try 
				{
					File.Delete(options.File);
				}
				catch(Exception err)
				{
					_logger.WarnException(string.Format("Failed to delete file {0}: {1}", options.File, err.ToString()), err);
				}
			}
			if(!string.IsNullOrEmpty(directoryToDelete))
			{
				try 
				{
					Directory.Delete(directoryToDelete);
				}
				catch(Exception err)
				{
					_logger.WarnException(string.Format("Failed to delete directory {0}: {1}", options.File, err.ToString()), err);
				}
			}

			_logger.Info("Done publishing file {0} to URL {1}", options.File, options.ApiUrl);
		}
Ejemplo n.º 4
0
        private static void PublishDirectory(string directoryPath, string apiUrl, string projectId, string componentId, string branchId, string version, string userName, string password, string deployEnvironmentName)
		{
			var publisher = _diFactory.CreateInjectedObject<IBuildPublisher>();
			var options = new BuildPublishOptions
			{
				Directory = directoryPath,
				ApiUrl = apiUrl,
				ProjectId = projectId,
				ComponentId = componentId,
				BranchId = branchId,
				Version = version,
                UserName = userName,
                Password = password,
                DeployEnvironmentName = deployEnvironmentName
			};
			publisher.PublishDirectory(options);
		}