Exemplo n.º 1
0
        /// <summary>
        /// Executes the task.
        /// </summary>
        /// <exception cref="BuildException">
        /// Project build failed.
        /// </exception>
        protected override void ExecuteTask()
        {
            Log(Level.Info, "Starting solution build.");

            if (SolutionFile != null)
            {
                if (!SolutionFile.Exists)
                {
                    throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                                           "Couldn't find solution file '{0}'.", SolutionFile.FullName),
                                             Location);
                }
            }

            if (Projects.FileNames.Count > 0)
            {
                Log(Level.Verbose, "Included projects:");
                foreach (string projectFile in Projects.FileNames)
                {
                    Log(Level.Verbose, " - {0}", projectFile);
                }
            }

            if (ReferenceProjects.FileNames.Count > 0)
            {
                Log(Level.Verbose, "Reference projects:");
                foreach (string projectFile in ReferenceProjects.FileNames)
                {
                    Log(Level.Verbose, " - {0}", projectFile);
                }
            }

            string basePath = null;

            try {
                using (TempFileCollection tfc = new TempFileCollection()) {
                    // store the temp dir so we can clean it up later
                    basePath = tfc.BasePath;

                    // ensure temp directory exists
                    if (!Directory.Exists(tfc.BasePath))
                    {
                        Directory.CreateDirectory(tfc.BasePath);
                    }

                    // create temporary domain
                    PermissionSet tempDomainPermSet = new PermissionSet(PermissionState.Unrestricted);

                    AppDomain temporaryDomain = AppDomain.CreateDomain("temporaryDomain", AppDomain.CurrentDomain.Evidence,
                                                                       AppDomain.CurrentDomain.SetupInformation, tempDomainPermSet);

                    try {
                        ReferencesResolver referencesResolver =
                            ((ReferencesResolver)temporaryDomain.CreateInstanceFrom(Assembly.GetExecutingAssembly().Location,
                                                                                    typeof(ReferencesResolver).FullName).Unwrap());

                        using (GacCache gacCache = new GacCache(this.Project)) {
                            SolutionBase sln = SolutionFactory.LoadSolution(this,
                                                                            tfc, gacCache, referencesResolver);
                            if (!sln.Compile(_configuration))
                            {
                                throw new BuildException("Project build failed.", Location);
                            }
                        }
                    } finally {
                        // unload temporary domain
                        AppDomain.Unload(temporaryDomain);
                    }
                }
            } finally {
                if (basePath != null && Directory.Exists(basePath))
                {
                    Log(Level.Debug, "Cleaning up temp folder '{0}'.", basePath);

                    // delete temporary directory and all files in it
                    DeleteTask deleteTask = new DeleteTask();
                    deleteTask.Project = Project;
                    deleteTask.Parent  = this;
                    deleteTask.InitializeTaskConfiguration();
                    deleteTask.Directory = new DirectoryInfo(basePath);
                    deleteTask.Threshold = Level.None; // no output in build log
                    deleteTask.Execute();
                }
            }
        }