public void EvaluateMSBuildThisFileProperty() { string xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'> <PropertyGroup> <A>$(MSBuildThisFile)</A> </PropertyGroup> <Import Project='test_imported.proj' /> </Project>"; string imported = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'> <PropertyGroup> <B>$(MSBuildThisFile)</B> </PropertyGroup> </Project>"; using (var ts = File.CreateText(temp_file_name)) ts.Write(imported); try { var reader = XmlReader.Create(new StringReader(xml)); var root = ProjectRootElement.Create(reader); var proj = new Project(root); var a = proj.GetProperty("A"); Assert.AreEqual(string.Empty, a.EvaluatedValue, "#1"); var b = proj.GetProperty("B"); Assert.AreEqual(temp_file_name, b.EvaluatedValue, "#2"); var pi = new ProjectInstance(root); var ai = pi.GetProperty("A"); Assert.AreEqual(string.Empty, ai.EvaluatedValue, "#3"); var bi = pi.GetProperty("B"); Assert.AreEqual(temp_file_name, bi.EvaluatedValue, "#4"); } finally { File.Delete(temp_file_name); } }
public void EvaluatePropertyWithQuotation() { string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'> <ItemGroup> <Foo Include='abc/xxx.txt' /> </ItemGroup> <PropertyGroup> <B>foobar</B> </PropertyGroup> <Target Name='default'> <CreateProperty Value=""@(Foo->'%(Filename)%(Extension)')""> <Output TaskParameter='Value' PropertyName='P' /> </CreateProperty> <CreateProperty Value='$(B)|$(P)'> <Output TaskParameter='Value' PropertyName='Q' /> </CreateProperty> </Target> </Project>"; var xml = XmlReader.Create(new StringReader(project_xml)); var root = ProjectRootElement.Create(xml); root.FullPath = "ProjectInstanceTest.EvaluatePropertyWithQuotation.proj"; var proj = new ProjectInstance(root); proj.Build(); var p = proj.GetProperty("P"); Assert.AreEqual("xxx.txt", p.EvaluatedValue, "#1"); var q = proj.GetProperty("Q"); Assert.AreEqual("foobar|xxx.txt", q.EvaluatedValue, "#2"); }
public void CloneProperties() { ProjectInstance first = GetSampleProjectInstance(); ProjectInstance second = first.DeepCopy(); Assert.False(Object.ReferenceEquals(first.GetProperty("p1"), second.GetProperty("p1"))); ProjectPropertyInstance newProperty = first.SetProperty("p1", "v1b"); Assert.True(Object.ReferenceEquals(newProperty, first.GetProperty("p1"))); Assert.Equal("v1b", first.GetPropertyValue("p1")); Assert.Equal("v1", second.GetPropertyValue("p1")); }
[Ignore] // "Cannot use a project instance if the project is created from a file." public void OutputFromTaskUpdatesProperty() { string projectFileContents = String.Format(ObjectModelHelpers.CleanupFileContents( @"<Project ToolsVersion='msbuilddefaulttoolsversion' xmlns='msbuildnamespace'> <UsingTask TaskName='QAMockTaskForIntegrationTests' AssemblyFile='{0}' /> <PropertyGroup> <SomeProperty>oldvalue</SomeProperty> </PropertyGroup> <Target Name='t'> <QAMockTaskForIntegrationTests ExpectedOutput='Foo' TaskShouldThrowException='false'> <Output TaskParameter='TaskOutput' PropertyName='SomeProperty'/> </QAMockTaskForIntegrationTests> </Target> </Project>"), _assemblyPath); ProjectInstance projectInstance = null; RequestDefinition r1 = GetRequestUsingProject(projectFileContents, "1.proj", "t", out projectInstance); r1.SubmitBuildRequest(); r1.WaitForResults(); ProjectPropertyInstance property = projectInstance.GetProperty("SomeProperty"); Assert.IsTrue(property.EvaluatedValue == "Foo", "SomeProperty=Foo"); }
public void Choose() { string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'> <Choose> <When Condition="" '$(DebugSymbols)' != '' ""> <PropertyGroup> <DebugXXX>True</DebugXXX> </PropertyGroup> </When> <Otherwise> <PropertyGroup> <DebugXXX>False</DebugXXX> </PropertyGroup> </Otherwise> </Choose> </Project>"; var xml = XmlReader.Create(new StringReader(project_xml)); var root = ProjectRootElement.Create(xml); root.FullPath = "ProjectInstanceTest.Choose.proj"; var proj = new ProjectInstance(root); var p = proj.GetProperty("DebugXXX"); Assert.IsNotNull(p, "#1"); Assert.AreEqual("False", p.EvaluatedValue, "#2"); }
public static ProjectPropertyInstance AssertPropertyExists(ProjectInstance projectInstance, string propertyName) { ProjectPropertyInstance propertyInstance = projectInstance.GetProperty(propertyName); Assert.IsNotNull(propertyInstance, "The expected property does not exist: {0}", propertyName); return(propertyInstance); }
public void ItemsAndProperties() { string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'> <ItemGroup> <X Condition='false' Include='bar.txt' /> <X Include='foo.txt'> <M>m</M> <N>=</N> </X> </ItemGroup> <PropertyGroup> <P Condition='false'>void</P> <P Condition='true'>valid</P> </PropertyGroup> </Project>"; var xml = XmlReader.Create(new StringReader(project_xml)); var root = ProjectRootElement.Create(xml); var proj = new ProjectInstance(root); var item = proj.Items.First(); Assert.AreEqual("foo.txt", item.EvaluatedInclude, "#1"); var prop = proj.Properties.First(p => p.Name == "P"); Assert.AreEqual("valid", prop.EvaluatedValue, "#2"); Assert.IsNotNull(proj.GetProperty("MSBuildProjectDirectory"), "#3"); Assert.AreEqual("4.0", proj.ToolsVersion, "#4"); }
public static void AssertPropertyDoesNotExist(ProjectInstance projectInstance, string propertyName) { ProjectPropertyInstance propertyInstance = projectInstance.GetProperty(propertyName); string value = propertyInstance == null ? null : propertyInstance.EvaluatedValue; Assert.IsNull(propertyInstance, "Not expecting the property to exist. Property: {0}, Value: {1}", propertyName, value); }
public static void AssertPropertyDoesNotExist(ProjectInstance projectInstance, string propertyName) { var propertyInstance = projectInstance.GetProperty(propertyName); var value = propertyInstance?.EvaluatedValue; propertyInstance.Should().BeNull("Not expecting the property to exist. Property: {0}, Value: {1}", propertyName, value); }
private static void ValidateOutputPath(ProjectInstance project) { // Ensure that the loaded has an OutputPath defined. This checks for // projects that may have an invalid default configuration|platform, which // needs to be fixed first. if ((!project.GetProperty("OutputPath")?.EvaluatedValue.HasValue()) ?? false) { var evaluatedConfig = project.GetProperty("Configuration")?.EvaluatedValue; var evaluatedPlatform = project.GetProperty("Platform")?.EvaluatedValue; throw new Exception( $"The current project does not define an OutputPath property for [{evaluatedConfig}|{evaluatedPlatform}]. " + $"Validate that the fallback platform at the top of [{project.FullPath}] matches one of the " + $"sections defining an OutputPath property with the [{evaluatedConfig}|{evaluatedPlatform}] condition." ); } }
public string GetProjectPropertyByName(IProject project, string name) { Dictionary <string, string> cachedProperties; if (this.myData.TryGetValue(project, out cachedProperties)) { string value; if (cachedProperties.TryGetValue(name, out value)) { return(value); } } else { cachedProperties = new Dictionary <string, string>(); this.myData.Add(project, cachedProperties); } try { const string resolveassemblyreference = "ResolveAssemblyReferences"; IProjectFile projectFile = project.ProjectFile; if (projectFile == null) { return(null); } List <Project> loadedProjects = ProjectCollection.GlobalProjectCollection.GetLoadedProjects( projectFile.Location.FullPath).ToList(); if (loadedProjects.Count != 1) { return(null); } Project loadedProject = loadedProjects[0]; ProjectInstance projectInstance = BuildManager.DefaultBuildManager.GetProjectInstanceForBuild(loadedProject); if (projectInstance.Build(resolveassemblyreference, JetBrains.Util.EmptyList <ILogger> .InstanceList)) { ICollection <ProjectPropertyInstance> allProperties = projectInstance.Properties; foreach (ProjectPropertyInstance property in allProperties) { cachedProperties.Add(property.Name, property.EvaluatedValue); } ProjectPropertyInstance projectPropertyInstance = projectInstance.GetProperty(name); if (projectPropertyInstance != null) { return(projectPropertyInstance.EvaluatedValue); } } } catch (Exception e) { Logger.LogExceptionSilently(e); } return(""); }
public static void AssertExpectedPropertyValue(ProjectInstance projectInstance, string propertyName, string expectedValue) { var propertyInstance = projectInstance.GetProperty(propertyName); if (expectedValue == null && propertyInstance == null) { return; } Assert.IsNotNull(propertyInstance, "The expected property does not exist: {0}", propertyName); Assert.AreEqual(expectedValue, propertyInstance.EvaluatedValue, "Property '{0}' does not have the expected value", propertyName); }
public static void AssertExpectedPropertyValue(ProjectInstance projectInstance, string propertyName, string expectedValue) { var propertyInstance = projectInstance.GetProperty(propertyName); if (expectedValue == null && propertyInstance == null) { return; } propertyInstance.Should().NotBeNull("The expected property does not exist: {0}", propertyName); propertyInstance.EvaluatedValue.Should().Be(expectedValue, "Property '{0}' does not have the expected value", propertyName); }
public virtual string GetConfigurationProperty(string propertyName, bool resetCache) { if (snapshot == null) { snapshot = proj.CreateProjectInstance(config, platform); } ProjectPropertyInstance property = snapshot.GetProperty(propertyName); if (property == null) { return(null); } return(property.EvaluatedValue); }
Environment.GetEnvironmentVariable("APPVEYOR").HasValue(); // https://www.appveyor.com/docs/environment-variables/ private void InitTelemetry(ProjectInstance msbProject) { var telemetryOptOut = msbProject.GetProperty("UnoPlatformTelemetryOptOut")?.EvaluatedValue ?? ""; bool?isTelemetryOptout() => telemetryOptOut.Equals("true", StringComparison.OrdinalIgnoreCase) || telemetryOptOut.Equals("1", StringComparison.OrdinalIgnoreCase); _telemetry = new Telemetry.Telemetry(isTelemetryOptout); if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Information)) { this.Log().InfoFormat($"Telemetry enabled: {_telemetry.Enabled}"); } }
public void ConditionalExpression() { string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'> <PropertyGroup> <NoCompilerStandardLib>true</NoCompilerStandardLib> <ResolveAssemblyReferencesDependsOn>$(ResolveAssemblyReferencesDependsOn);_AddCorlibReference</ResolveAssemblyReferencesDependsOn> </PropertyGroup> </Project>"; var xml = XmlReader.Create(new StringReader(project_xml)); var root = ProjectRootElement.Create(xml); root.FullPath = "ProjectInstanceTest.ConditionalExpression.proj"; var proj = new ProjectInstance(root); var p = proj.GetProperty("ResolveAssemblyReferencesDependsOn"); Assert.IsNotNull(p, "#1"); Assert.AreEqual(";_AddCorlibReference", p.EvaluatedValue, "#2"); }
private void ReadBuildSettings() { var configFileName = _projectInstance.GetProperty(StaticSerializationMsBuildProperty)?.EvaluatedValue; _configFile = _projectInstance .GetItems("None") .FirstOrDefault(i => i.EvaluatedInclude.EndsWith(configFileName)) ?.EvaluatedInclude; if (_configFile == null) { if (_projectInstance .GetItems("Content") .Any(i => i.EvaluatedInclude.EndsWith(configFileName))) { throw new Exception($"The [{configFileName}] build action in {Path.GetFileName(_projectInstance.FullPath)} must be set to [None]."); } } _analyzerSuppressions = _projectInstance .GetItems("StaticSerializerAnalyzerSuppressions") .Select(i => i.EvaluatedInclude) .ToArray(); }
#pragma warning restore 649 // Unused member public XamlCodeGeneration(Compilation sourceCompilation, ProjectInstance msbProject, Project roslynProject) { // To easily debug XAML code generation: // 1. Uncomment the line below // 2. Build Uno.UI.SourceGenerators and override local files (following instructions here: doc/articles/uno-development/debugging-uno-ui.md#debugging-unoui) // 3. Build project containing your XAML. When prompted to attach a Visual Studio instance: // - if it's in an external solution, attach the VS instance running Uno.UI // - if you're debugging XAML generation inside the Uno solution, opt to create a new VS instance // //Debugger.Launch(); InitTelemetry(msbProject); _legacyTypes = msbProject .GetItems("LegacyTypes") .Select(i => i.EvaluatedInclude) .ToList() .ToDictionary(fullyQualifiedName => fullyQualifiedName.Split('.').Last()); _metadataHelper = new RoslynMetadataHelper("Debug", sourceCompilation, msbProject, roslynProject, null, _legacyTypes); _assemblySearchPaths = new string[0]; _projectInstance = msbProject; _configuration = msbProject.GetProperty("Configuration")?.EvaluatedValue ?? throw new InvalidOperationException("The configuration property must be provided"); _isDebug = string.Equals(_configuration, "Debug", StringComparison.OrdinalIgnoreCase); var xamlItems = msbProject.GetItems("Page") .Concat(msbProject.GetItems("ApplicationDefinition")); _xamlSourceFiles = xamlItems.Select(d => d.EvaluatedInclude) .ToArray(); _xamlSourceLinks = xamlItems.Select(GetSourceLink) .ToArray(); _excludeXamlNamespaces = msbProject .GetItems("ExcludeXamlNamespaces") .Select(i => i.EvaluatedInclude) .ToArray(); _includeXamlNamespaces = msbProject .GetItems("IncludeXamlNamespaces") .Select(i => i.EvaluatedInclude) .ToArray(); _analyzerSuppressions = msbProject .GetItems("XamlGeneratorAnalyzerSuppressions") .Select(i => i.EvaluatedInclude) .ToArray(); _resourceFiles = msbProject .GetItems("PRIResource") .Select(i => i.EvaluatedInclude) .ToArray(); if (bool.TryParse(msbProject.GetProperty("UseUnoXamlParser")?.EvaluatedValue, out var useUnoXamlParser) && useUnoXamlParser) { XamlRedirection.XamlConfig.IsUnoXaml = useUnoXamlParser || XamlRedirection.XamlConfig.IsMono; } if (bool.TryParse(msbProject.GetProperty("UnoSkipUserControlsInVisualTree")?.EvaluatedValue, out var skipUserControlsInVisualTree)) { _skipUserControlsInVisualTree = skipUserControlsInVisualTree; } if (bool.TryParse(msbProject.GetProperty("ShouldWriteErrorOnInvalidXaml")?.EvaluatedValue, out var shouldWriteErrorOnInvalidXaml)) { XamlFileGenerator.ShouldWriteErrorOnInvalidXaml = shouldWriteErrorOnInvalidXaml; } if (!bool.TryParse(msbProject.GetProperty("IsUiAutomationMappingEnabled")?.EvaluatedValue ?? "", out _isUiAutomationMappingEnabled)) { _isUiAutomationMappingEnabled = false; } if (bool.TryParse(msbProject.GetProperty("ShouldAnnotateGeneratedXaml")?.EvaluatedValue, out var shouldAnnotateGeneratedXaml)) { _shouldAnnotateGeneratedXaml = shouldAnnotateGeneratedXaml; } _targetPath = Path.Combine( Path.GetDirectoryName(msbProject.FullPath), msbProject.GetProperty("IntermediateOutputPath").EvaluatedValue ); _defaultLanguage = msbProject.GetProperty("DefaultLanguage")?.EvaluatedValue; _analyzerSuppressions = msbProject .GetItems("XamlGeneratorAnalyzerSuppressions") .Select(i => i.EvaluatedInclude) .ToArray(); _uiAutomationMappings = msbProject .GetItems("CustomUiAutomationMemberMapping") .Select(i => new { Key = i.EvaluatedInclude, Value = i.MetadataNames .Select(i.GetMetadataValue) .FirstOrDefault() ?.Split() .Select(m => m.Trim()) .Where(m => m.HasValueTrimmed()) }) .GroupBy(p => p.Key) .ToDictionary(p => p.Key, p => p.SelectMany(x => x.Value.Safe()).ToArray()); _defaultNamespace = msbProject.GetPropertyValue("RootNamespace"); _isWasm = msbProject.GetProperty("DefineConstants").EvaluatedValue?.Contains("__WASM__") ?? false; }
#pragma warning restore 649 // Unused member public XamlCodeGeneration(Compilation sourceCompilation, ProjectInstance msbProject, Project roslynProject) { InitTelemetry(msbProject); _legacyTypes = msbProject .GetItems("LegacyTypes") .Select(i => i.EvaluatedInclude) .ToList() .ToDictionary(fullyQualifiedName => fullyQualifiedName.Split('.').Last()); _medataHelper = new RoslynMetadataHelper("Debug", sourceCompilation, msbProject, roslynProject, null, _legacyTypes); _assemblySearchPaths = new string[0]; _projectInstance = msbProject; _configuration = msbProject.GetProperty("Configuration")?.EvaluatedValue ?? throw new InvalidOperationException("The configuration property must be provided"); _isDebug = string.Equals(_configuration, "Debug", StringComparison.OrdinalIgnoreCase); var xamlPages = msbProject.GetItems("Page") .Select(d => d.EvaluatedInclude); _xamlSourceFiles = msbProject.GetItems("ApplicationDefinition") .Select(d => d.EvaluatedInclude) .Concat(xamlPages) .ToArray(); _excludeXamlNamespaces = msbProject .GetItems("ExcludeXamlNamespaces") .Select(i => i.EvaluatedInclude) .ToArray(); _includeXamlNamespaces = msbProject .GetItems("IncludeXamlNamespaces") .Select(i => i.EvaluatedInclude) .ToArray(); _analyzerSuppressions = msbProject .GetItems("XamlGeneratorAnalyzerSuppressions") .Select(i => i.EvaluatedInclude) .ToArray(); _resourceFiles = msbProject .GetItems("PRIResource") .Select(i => i.EvaluatedInclude) .ToArray(); if (bool.TryParse(msbProject.GetProperty("UseUnoXamlParser")?.EvaluatedValue, out var useUnoXamlParser) && useUnoXamlParser) { XamlRedirection.XamlConfig.IsUnoXaml = useUnoXamlParser || XamlRedirection.XamlConfig.IsMono; } if (bool.TryParse(msbProject.GetProperty("ShouldWriteErrorOnInvalidXaml")?.EvaluatedValue, out var shouldWriteErrorOnInvalidXaml)) { XamlFileGenerator.ShouldWriteErrorOnInvalidXaml = shouldWriteErrorOnInvalidXaml; } if (!bool.TryParse(msbProject.GetProperty("IsUiAutomationMappingEnabled")?.EvaluatedValue ?? "", out _isUiAutomationMappingEnabled)) { _isUiAutomationMappingEnabled = false; } _targetPath = Path.Combine( Path.GetDirectoryName(msbProject.FullPath), msbProject.GetProperty("IntermediateOutputPath").EvaluatedValue ); _defaultLanguage = msbProject.GetProperty("DefaultLanguage")?.EvaluatedValue; _analyzerSuppressions = msbProject .GetItems("XamlGeneratorAnalyzerSuppressions") .Select(i => i.EvaluatedInclude) .ToArray(); _uiAutomationMappings = msbProject .GetItems("CustomUiAutomationMemberMapping") .Select(i => new { Key = i.EvaluatedInclude, Value = i.MetadataNames .Select(i.GetMetadataValue) .FirstOrDefault() ?.Split() .Select(m => m.Trim()) .Where(m => m.HasValueTrimmed()) }) .GroupBy(p => p.Key) .ToDictionary(p => p.Key, p => p.SelectMany(x => x.Value.Safe()).ToArray()); _defaultNamespace = msbProject.GetPropertyValue("RootNamespace"); _isWasm = msbProject.GetProperty("DefineConstants").EvaluatedValue?.Contains("__WASM__") ?? false; }
private static void AssertAnalysisTargetsAreImported(ProjectInstance projectInstance) { var propertyInstance = projectInstance.GetProperty(DummyAnalysisTargetsMarkerProperty); propertyInstance.Should().NotBeNull("Failed to import the SonarQube Analysis targets"); }
private static void AssertAnalysisTargetsAreNotImported(ProjectInstance projectInstance) { ProjectPropertyInstance propertyInstance = projectInstance.GetProperty(DummyAnalysisTargetsMarkerProperty); Assert.IsNull(propertyInstance, "SonarQube Analysis targets should not have been imported"); }
private static void AssertAnalysisTargetsAreImported(ProjectInstance projectInstance) { ProjectPropertyInstance propertyInstance = projectInstance.GetProperty(DummyAnalysisTargetsMarkerProperty); Assert.IsNotNull(propertyInstance, "Failed to import the SonarQube Analysis targets"); }