public IProcedure GetProcedure(Project project, Build build)
        {
            var assembly = GetAssembly(project, build);
            var packageName = project.DeployPackageName;

            var procedures = assembly.GetTypes().Where(x => typeof(IProcedure).IsAssignableFrom(x) && (x.Name == project.ProcedureName || x.FullName == project.ProcedureName)).ToList();
            if (!procedures.Any())
            {
                throw new CouldNotLoadProcedureException(string.Format("Error: Could not find any procedure named {0} in deploy package named {1}", project.ProcedureName, packageName));
            }

            if (procedures.Count > 1)
            {
                throw new CouldNotLoadProcedureException(string.Format("Error: Found more than one matching procedure in deploy package named {0}", packageName));
            }

            var procedureType = procedures.Single();

            var procedure = CreateProcedure(procedureType);
            if (procedure == null)
            {
                throw new CouldNotLoadProcedureException(string.Format("Error: Procedure found in deploy package named {0}, does not have parameterless constructor or a constructor taking an IMessenger", packageName));
            }

            return procedure;
        }
        public Assembly GetAssembly(Project project, Build build)
        {
            var packageName = project.DeployPackageName;

            var package = build.GetPackage(packageName);
            if (package == null)
            {
                throw new CouldNotLoadAssemblyException(string.Format("Could not find any deploy package named {0}", packageName));
            }

            var filesInPackage = Zip.Extract(File.Open(package.Path, FileMode.Open));
            var assemblyFileName = project.DeployPackageName + ".dll";
            var assemblyFile = filesInPackage.SingleOrDefault(x => x.Filename == assemblyFileName);
            if (assemblyFile == null)
            {
                throw new CouldNotLoadAssemblyException(string.Format("Could not find assemblyfile named {0} in package named {1}", assemblyFileName, packageName));
            }

            var assembly = Assembly.Load(assemblyFile.Data);
            if (assembly == null)
            {
                throw new CouldNotLoadAssemblyException(packageName);
            }

            return assembly;
        }
        public bool Execute(Project project, string buildId)
        {
            messenger.Publish(string.Format("Executing procedure for build {0}", buildId));
            Build build = buildRepository.GetBuild(buildId);

            IProcedure procedure;
            try
            {
                procedure = procedureLoader.GetProcedure(project, build);
            }
            catch(Exception e)
            {
                messenger.Publish("Error: " + e.Message);
                return false;
            }

            return procedure.Perform(project, build);
        }
 public ProjectWithBuildsModel(Project project, List<Build> builds)
     : this(project, builds, null)
 {
 }
 public ProjectWithBuildsModel(Project project, List<Build> builds, string error)
 {
     this.error = error;
     this.project = project;
     this.builds = builds;
 }
        public ActionResult Edit(ProjectModel model)
        {
            if (ModelState.IsValid)
            {
                using (var session = store.OpenSession())
                {
                    if (model.Id != null)
                    {
                        var project = session.Load<Project>(model.Id);
                        project.Name = model.Name;
                        project.DeployPackageName = model.DeployPackageName;
                        project.ProcedureName = model.ProcedureName;
                        project.ConfigureArgument(TeamcityBuildRepository.TeamcityBuildTypeId, model.TeamcityBuildTypeId);
                    }
                    else
                    {
                        var project = new Project
                        {
                            Name = model.Name,
                            DeployPackageName = model.DeployPackageName,
                            ProcedureName = model.ProcedureName
                        };
                        project.ConfigureArgument(TeamcityBuildRepository.TeamcityBuildTypeId, model.TeamcityBuildTypeId);
                        session.Store(project);
                    }
                    session.SaveChanges();
                }

                return RedirectToAction("Index");
            }

            return View(model);
        }
        ProjectWithBuildsModel GetModelFromProject(Project project)
        {
            ProjectWithBuildsModel model;
            try
            {
                var builds = buildRepository.GetBuildsFor(project);
                model = new ProjectWithBuildsModel(project, builds.ToList());
            }
            catch (MissingProcedureArgumentException e)
            {
                model = new ProjectWithBuildsModel(project, new List<Build>(), e.Message);
            }

            return model;
        }