Пример #1
0
 public static MemoryStream ModifyCsproj(FileInfo csproj, Action <Project> changingAction, string toolsVersion = null)
 {
     MsBuildLocationHelper.InitPathToMsBuild();
     return(FuncUtils.Using(
                new ProjectCollection(),
                projectCollection =>
     {
         var proj = new Project(csproj.FullName, null, toolsVersion, projectCollection);
         return ModifyCsproj(proj, changingAction);
     },
                projectCollection => projectCollection.UnloadAllProjects()));
 }
Пример #2
0
        public void ReplaceLinksWithItems()
        {
            MsBuildLocationHelper.InitPathToMsBuild();
            var project = CreateTestProject();
            var copies  = ProjModifier.ReplaceLinksWithItemsAndReturnWhatToCopy(project);

            copies.Should().HaveCount(1);
            var writer = new StringWriter();

            foreach (var fileToCopy in copies)
            {
                writer.WriteLine("Copy " + fileToCopy.SourceFile + " to " + fileToCopy.DestinationFile);
            }

            project.Save(writer);
            Approvals.Verify(writer.ToString());
            project.Save(Path.Combine(project.DirectoryPath, "res.csproj"));
        }
Пример #3
0
        private void ReportErrorIfStudentsZipHasErrors()
        {
            var tempExZipFilePath = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).GetFile($"{slide.Id}.exercise.zip");
            var tempExFolder      = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ExerciseFolder_From_StudentZip"));

            exerciseStudentZipBuilder.BuildStudentZip(slide, tempExZipFilePath);
            Utils.UnpackZip(tempExZipFilePath.ReadAllContent(), tempExFolder.FullName);
            try
            {
                ReportErrorIfStudentsZipHasWrongAnswerOrSolutionFiles(tempExFolder);

                var csprojFile = tempExFolder.GetFile(ex.CsprojFileName);
                MsBuildLocationHelper.InitPathToMsBuild();
                FuncUtils.Using(
                    new ProjectCollection(),
                    projectCollection =>
                {
                    var csproj = new Project(csprojFile.FullName, null, null, projectCollection);
                    ReportErrorIfCsprojHasUserCodeOfNotCompileType(tempExFolder, csproj);
                    ReportErrorIfCsprojHasWrongAnswerOrSolutionItems(tempExFolder, csproj);
                },
                    projectCollection => projectCollection.UnloadAllProjects());

                if (!ex.StudentZipIsCompilable)
                {
                    return;
                }

                var buildResult = MsBuildRunner.BuildProject(CsSandboxRunnerSettings.MsBuildSettings, ex.CsprojFile.Name, tempExFolder);
                ReportErrorIfStudentsZipNotBuilding(buildResult);
            }
            finally
            {
                tempExFolder.Delete(true);
                tempExZipFilePath.Delete();
            }
        }
Пример #4
0
        public static MSbuildResult BuildProject(MsBuildSettings settings, string projectFileName, DirectoryInfo dir)
        {
            var result = new MSbuildResult();
            var path   = Path.Combine(dir.FullName, projectFileName);

            MsBuildLocationHelper.InitPathToMsBuild();
            return(FuncUtils.Using(
                       new ProjectCollection(),
                       projectCollection =>
            {
                var project = new Project(path, null, settings.MsBuildToolsVersion, projectCollection);
                project.SetProperty("CscToolPath", settings.CompilerDirectory.FullName);

                /* Workaround for MSB4216 (we don't know why it appears at some moment)
                 * https://medium.com/@kviat/msb4216-fix-83d9e891a47b
                 */
                project.SetProperty("DisableOutOfProcTaskHost", "true");

                /* WPF markups should be compiled in separate AppDomain, otherwise MsBuild raises NRE while building:
                 * https://stackoverflow.com/questions/1552092/microsoft-build-buildengine-engine-throws-error-when-building-wpf-application
                 */
                project.SetProperty("AlwaysCompileMarkupFilesInSeparateDomain", "True");

                /* We don't know why, but MSBuild on server set BaseIntermediateOutputPath to "\".
                 * Here we return default value "obj\".
                 */
                project.SetProperty("BaseIntermediateOutputPath", @"obj\");

                foreach (var libName in obligatoryLibs)
                {
                    if (!project.HasReference(libName))
                    {
                        project.AddReference(libName);
                    }
                }

                project.ReevaluateIfNecessary();

                var includes = new HashSet <string>(
                    project.AllEvaluatedItems
                    .Where(i => i.ItemType == "None" || i.ItemType == "Content")
                    .Select(i => Path.GetFileName(i.EvaluatedInclude.ToLowerInvariant())));

                foreach (var dll in settings.WellKnownLibsDirectory.GetFiles("*.dll"))
                {
                    if (!includes.Contains(dll.Name.ToLowerInvariant()))
                    {
                        project.AddItem("None", dll.FullName);
                    }
                }

                project.Save();
                using (var stringWriter = new StringWriter())
                {
                    var log = new ConsoleLogger(LoggerVerbosity.Minimal, stringWriter.Write, color => { }, () => { });
                    result.Success = SyncBuild(project, log);
                    if (result.Success)
                    {
                        result.PathToExe = Path.Combine(project.DirectoryPath,
                                                        project.GetPropertyValue("OutputPath"),
                                                        project.GetPropertyValue("AssemblyName") + ".exe");
                    }
                    else
                    {
                        result.ErrorMessage = stringWriter.ToString();
                    }
                    return result;
                }
            },
                       projectCollection =>
            {
                projectCollection.UnloadAllProjects();                         // https://github.com/Microsoft/msbuild/pull/474
            }));
        }