Beispiel #1
0
        public bool TryPredictInputsAndOutputs(
            Project project,
            ProjectInstance projectInstance,
            string repositoryRootDirectory,
            out StaticPredictions predictions)
        {
            string intermediateOutputPath = project.GetPropertyValue(IntermediateOutputPathMacro);

            if (string.IsNullOrWhiteSpace(intermediateOutputPath))
            {
                // It is not defined, so we don't return a result.
                predictions = null;
                return(false);
            }

            // If the path is relative, it is interpreted as relative to the project directory path
            string predictedOutputDirectory;

            if (!Path.IsPathRooted(intermediateOutputPath))
            {
                predictedOutputDirectory = Path.Combine(project.DirectoryPath, intermediateOutputPath);
            }
            else
            {
                predictedOutputDirectory = intermediateOutputPath;
            }

            predictions = new StaticPredictions(
                buildInputs: null,
                buildOutputDirectories: new[] { new BuildOutputDirectory(Path.GetFullPath(predictedOutputDirectory)) });
            return(true);
        }
Beispiel #2
0
        public void DistinctInputsAndOutputsAreAggregated()
        {
            var predictors = new IProjectStaticPredictor[]
            {
                new MockPredictor(new StaticPredictions(
                                      new[] { new BuildInput(@"foo\bar1", false) },
                                      new[] { new BuildOutputDirectory(@"blah\boo1") })),
                new MockPredictor2(new StaticPredictions(
                                       new[] { new BuildInput(@"foo\bar2", false) },
                                       new[] { new BuildOutputDirectory(@"blah\boo2") })),
            };

            var executor = new ProjectStaticPredictionExecutor(@"c:\repo", predictors);

            var project = TestHelpers.CreateProjectFromRootElement(ProjectRootElement.Create());

            StaticPredictions predictions = executor.PredictInputsAndOutputs(project);

            BuildInput[] expectedInputs =
            {
                new BuildInput(@"foo\bar1", false, "MockPredictor"),
                new BuildInput(@"foo\bar2", false, "MockPredictor2"),
            };

            BuildOutputDirectory[] expectedBuildOutputDirectories =
            {
                new BuildOutputDirectory(@"blah\boo1", "MockPredictor"),
                new BuildOutputDirectory(@"blah\boo2", "MockPredictor2"),
            };

            predictions.AssertPredictions(expectedInputs, expectedBuildOutputDirectories);
        }
        /// <inheritdoc/>
        public bool TryPredictInputsAndOutputs(
            Project project,
            ProjectInstance projectInstance,
            string repositoryRootDirectory,
            out StaticPredictions predictions)
        {
            // TODO: Need to determine how to normalize evaluated include selected below and determine if it is relative to project.
            var availableItemNames = new HashSet <string>(
                project.GetItems(AvailableItemName).Select(item => item.EvaluatedInclude),
                StringComparer.OrdinalIgnoreCase);

            List <BuildInput> itemInputs = availableItemNames.SelectMany(
                availableItemName => project.GetItems(availableItemName).Select(
                    item => new BuildInput(
                        Path.Combine(project.DirectoryPath, item.EvaluatedInclude),
                        isDirectory: false)))
                                           .ToList();

            if (itemInputs.Count > 0)
            {
                predictions = new StaticPredictions(itemInputs, null);
                return(true);
            }

            predictions = null;
            return(false);
        }
        public static void AssertPredictions(
            this StaticPredictions predictions,
            IReadOnlyCollection <BuildInput> expectedBuildInputs,
            IReadOnlyCollection <BuildOutputDirectory> expectedBuildOutputDirectories)
        {
            Assert.NotNull(predictions);

            if (expectedBuildInputs == null)
            {
                Assert.Equal(0, predictions.BuildInputs.Count);
            }
            else
            {
                CheckCollection(expectedBuildInputs, predictions.BuildInputs, BuildInput.ComparerInstance, "inputs");
            }

            if (expectedBuildOutputDirectories == null)
            {
                Assert.Equal(0, predictions.BuildOutputDirectories.Count);
            }
            else
            {
                CheckCollection(expectedBuildOutputDirectories, predictions.BuildOutputDirectories, BuildOutputDirectory.ComparerInstance, "outputs");
            }
        }
Beispiel #5
0
            public bool TryPredictInputsAndOutputs(
                Project project,
                ProjectInstance projectInstance,
                string repositoryRootDirectory,
                out StaticPredictions predictions)
            {
                if (_predictionsToReturn == null)
                {
                    predictions = null;
                    return(false);
                }

                predictions = _predictionsToReturn;
                return(true);
            }
        public bool TryPredictInputsAndOutputs(
            Project project,
            ProjectInstance projectInstance,
            string repositoryRootDirectory,
            out StaticPredictions predictions)
        {
            string outDir     = project.GetPropertyValue(OutDirMacro);
            string outputPath = project.GetPropertyValue(OutputPathMacro);

            // For an MSBuild project, the output goes to $(OutDir) by default. Usually $(OutDir)
            // equals $(OutputPath). Many targets expect OutputPath/OutDir to be defined and
            // MsBuild.exe reports an error if these macros are undefined.
            string finalOutputPath;

            if (!string.IsNullOrWhiteSpace(outDir))
            {
                finalOutputPath = outDir;
            }
            else if (!string.IsNullOrWhiteSpace(outputPath))
            {
                // Some projects use custom code with $(OutputPath) set instead of following the common .targets pattern.
                // Fall back to $(OutputPath) first when $(OutDir) is not set.
                finalOutputPath = outputPath;
            }
            else
            {
                // Neither is defined, we don't return a result.
                predictions = null;
                return(false);
            }

            // If the path is relative, it is interpreted as relative to the project directory path
            string predictedOutputDirectory;

            if (!Path.IsPathRooted(finalOutputPath))
            {
                predictedOutputDirectory = Path.Combine(project.DirectoryPath, finalOutputPath);
            }
            else
            {
                predictedOutputDirectory = finalOutputPath;
            }

            predictions = new StaticPredictions(
                buildInputs: null,
                buildOutputDirectories: new[] { new BuildOutputDirectory(Path.GetFullPath(predictedOutputDirectory)) });
            return(true);
        }
        /// <inheritdoc/>
        public bool TryPredictInputsAndOutputs(Project project, ProjectInstance projectInstance, string repositoryRootDirectory, out StaticPredictions predictions)
        {
            var inputs = new List <BuildInput>()
            {
                new BuildInput(project.FullPath, false)
            };

            foreach (ResolvedImport import in project.Imports)
            {
                inputs.Add(new BuildInput(import.ImportedProject.FullPath, isDirectory: false));
            }

            predictions = new StaticPredictions(inputs, null);

            return(true);
        }
Beispiel #8
0
        public void EmptyPredictionsResultInEmptyAggregateResult()
        {
            var predictors = new IProjectStaticPredictor[]
            {
                new MockPredictor(new StaticPredictions(null, null)),
                new MockPredictor(new StaticPredictions(null, null)),
            };

            var executor = new ProjectStaticPredictionExecutor(@"c:\repo", predictors);

            var project = TestHelpers.CreateProjectFromRootElement(ProjectRootElement.Create());
            StaticPredictions predictions = executor.PredictInputsAndOutputs(project);

            Assert.NotNull(predictions);
            Assert.Equal(0, predictions.BuildInputs.Count);
            Assert.Equal(0, predictions.BuildOutputDirectories.Count);
        }
Beispiel #9
0
        /// <inheritdoc />
        public bool TryPredictInputsAndOutputs(
            Project project,
            ProjectInstance projectInstance,
            string repositoryRootDirectory,
            out StaticPredictions predictions)
        {
            // Determine the active Targets in this Project.
            var activeTargets = new Dictionary <string, ProjectTargetInstance>(StringComparer.OrdinalIgnoreCase);

            // Start with the default Build target and all of its parent targets, the closure of its dependencies.
            project.AddToActiveTargets(MsBuildHelpers.BuildTargetAsCollection, activeTargets);

            // Aside from InitialTargets and DefaultTargets, for completeness of inputs/outputs detection,
            // include custom targets defined directly in this Project.
            // Note that this misses targets defined in any custom targets files.
            foreach (ProjectTargetInstance target in projectInstance.Targets.Values
                     .Where(t => string.Equals(t.Location.File, project.ProjectFileLocation.File, PathComparer.Comparison)))
            {
                project.AddToActiveTargets(new[] { target.Name }, activeTargets);
            }

            project.AddBeforeAndAfterTargets(activeTargets);

            // Then parse copy tasks for these targets.
            var buildInputs            = new HashSet <BuildInput>(BuildInput.ComparerInstance);
            var buildOutputDirectories = new HashSet <string>(PathComparer.Instance);

            foreach (KeyValuePair <string, ProjectTargetInstance> target in activeTargets)
            {
                ParseCopyTask(target.Value, projectInstance, buildInputs, buildOutputDirectories);
            }

            if (buildInputs.Count > 0)
            {
                predictions = new StaticPredictions(
                    buildInputs,
                    buildOutputDirectories.Select(o => new BuildOutputDirectory(o)).ToList());
                return(true);
            }

            predictions = null;
            return(false);
        }
Beispiel #10
0
        /// <inheritdoc/>
        public bool TryPredictInputsAndOutputs(
            Project project,
            ProjectInstance projectInstance,
            string repositoryRootDirectory,
            out StaticPredictions predictions)
        {
            // TODO: Need to determine how to normalize evaluated include selected below and determine if it is relative to project.
            List <BuildInput> itemInputs = project.GetItems(CompileItemName)
                                           .Select(item => new BuildInput(
                                                       Path.Combine(project.DirectoryPath, item.EvaluatedInclude),
                                                       isDirectory: false))
                                           .ToList();

            if (itemInputs.Count > 0)
            {
                predictions = new StaticPredictions(itemInputs, buildOutputDirectories: null);
                return(true);
            }

            predictions = null;
            return(false);
        }
Beispiel #11
0
        public void DuplicateInputsAndOutputsMergePredictedBys()
        {
            var predictors = new IProjectStaticPredictor[]
            {
                new MockPredictor(new StaticPredictions(
                                      new[] { new BuildInput(@"foo\bar", false) },
                                      new[] { new BuildOutputDirectory(@"blah\boo") })),
                new MockPredictor2(new StaticPredictions(
                                       new[] { new BuildInput(@"foo\bar", false) },
                                       new[] { new BuildOutputDirectory(@"blah\boo") })),
            };

            var executor = new ProjectStaticPredictionExecutor(@"c:\repo", predictors);

            var project = TestHelpers.CreateProjectFromRootElement(ProjectRootElement.Create());

            StaticPredictions predictions = executor.PredictInputsAndOutputs(project);

            predictions.AssertPredictions(
                new[] { new BuildInput(@"foo\bar", false, "MockPredictor", "MockPredictor2") },
                new[] { new BuildOutputDirectory(@"blah\boo", "MockPredictor", "MockPredictor2") });
        }
Beispiel #12
0
 /// <inheritdoc/>
 public bool TryPredictInputsAndOutputs(Project project, ProjectInstance projectInstance, string repositoryRootDirectory, out StaticPredictions predictions)
 {
     throw new InvalidOperationException();
 }
Beispiel #13
0
 public MockPredictor2(StaticPredictions predictionsToReturn)
     : base(predictionsToReturn)
 {
 }
Beispiel #14
0
 public MockPredictor(StaticPredictions predictionsToReturn)
 {
     _predictionsToReturn = predictionsToReturn;
 }