Exemple #1
0
        /// <summary>
        ///     Build web application project
        /// </summary>
        /// <param name="appPhysicalPath"></param>
        /// <param name="projectName"></param>
        public virtual void Build(string appPhysicalPath, string projectName)
        {
            if (!_fileSystem.DirectoryExists(appPhysicalPath))
            {
                throw new Exception(string.Format("Application directory '{0}' does not exist.", appPhysicalPath));
            }

            if (string.IsNullOrEmpty(Path.GetExtension(projectName)))
            {
                projectName = projectName + ".csproj";
            }

            var projectFilePath = Path.Combine(appPhysicalPath, projectName);

            if (!_fileSystem.FileExists(projectFilePath))
            {
                throw new Exception(string.Format("Application project file '{0}' does not exist.", projectFilePath));
            }

            var psi = new ProcessStartInfo
            {
                FileName               = GetMsBuildPath(_fileSystem, _environmentSystem),
                Arguments              = string.Format("\"{0}\"", projectFilePath),
                WorkingDirectory       = appPhysicalPath,
                RedirectStandardOutput = true,
                UseShellExecute        = false,
                CreateNoWindow         = true
            };

            var msbuild = new Process {
                StartInfo = psi
            };

            if (!_processRunner.Start(msbuild))
            {
                throw new Exception(string.Format("Failed to start msbuild to build a project '{0}' at '{1}'", projectName, appPhysicalPath));
            }

            var output = _processRunner.GetProcessOutput(msbuild);

            if (!_processRunner.WaitForExit(msbuild, 10000))
            {
                _processRunner.Stop(msbuild);
            }

            var exitCode = _processRunner.GetProcessExitCode(msbuild);

            if (exitCode != 0)
            {
                throw new Exception(string.Format("There were some errors while trying to build a project '{0}' at '{1}': {2}",
                                                  projectName, appPhysicalPath, output));
            }
        }