Inheritance: IPropertyProvider
Esempio n. 1
0
        public void ProjectFactoryInitializesPropertiesForPreprocessor()
        {
            // arrange
            const string inputSpec = @"<?xml version=""1.0""?>
<package>
    <metadata>
        <id>$id$</id>
        <version>$version$</version>
        <description>$description$</description>
        <authors>$author$</authors>
        <copyright>$copyright$</copyright>
        <licenseUrl>http://nuget.codeplex.com/license</licenseUrl>
        <projectUrl>http://nuget.codeplex.com</projectUrl>
        <tags>nuget</tags>
    </metadata>
</package>";
            var metadata = new ManifestMetadata
            {
                Id = "ProjectFactoryTest",
                Version = "2.0.30619.9000",
                Title = "NuGet.Test",
                Description = "",
                Copyright = "\x00a9 Outercurve. All rights reserved.",
                Authors = "Outercurve Foundation",
            };
            var projectMock = new Mock<Project>();
            var factory = new ProjectFactory(projectMock.Object);

            // act
            var author = factory.InitializeProperties(metadata);
            var actual = Preprocessor.Process(inputSpec.AsStream(), factory, false);

            // assert
            Assert.Equal("Outercurve Foundation", author);
            const string expected = @"<?xml version=""1.0""?>
<package>
    <metadata>
        <id>ProjectFactoryTest</id>
        <version>2.0.30619.9000</version>
        <description></description>
        <authors>Outercurve Foundation</authors>
        <copyright>© Outercurve. All rights reserved.</copyright>
        <licenseUrl>http://nuget.codeplex.com/license</licenseUrl>
        <projectUrl>http://nuget.codeplex.com</projectUrl>
        <tags>nuget</tags>
    </metadata>
</package>";
            Assert.Equal(expected, actual);
        }
Esempio n. 2
0
        private IPackage BuildFromProjectFile(string path)
        {
            var factory = new ProjectFactory(path, Properties)
            {
                IsTool = Tool,
                Logger = Console,
                Build  = Build,
                IncludeReferencedProjects = IncludeReferencedProjects
            };

            // Add the additional Properties to the properties of the Project Factory
            foreach (var property in Properties)
            {
                if (factory.ProjectProperties.ContainsKey(property.Key))
                {
                    Console.WriteWarning(LocalizedResourceManager.GetString("Warning_DuplicatePropertyKey"), property.Key);
                }
                factory.ProjectProperties[property.Key] = property.Value;
            }

            // Create a builder for the main package as well as the sources/symbols package
            PackageBuilder mainPackageBuilder = factory.CreateBuilder(BasePath);

            // Build the main package
            IPackage package = BuildPackage(mainPackageBuilder);

            // If we're excluding symbols then do nothing else
            if (!Symbols)
            {
                return(package);
            }

            Console.WriteLine();
            Console.WriteLine(LocalizedResourceManager.GetString("PackageCommandAttemptingToBuildSymbolsPackage"), Path.GetFileName(path));

            factory.IncludeSymbols = true;
            PackageBuilder symbolsBuilder = factory.CreateBuilder(BasePath);

            symbolsBuilder.Version = mainPackageBuilder.Version;

            // Get the file name for the sources package and build it
            string outputPath = GetOutputPath(symbolsBuilder, symbols: true);

            BuildPackage(symbolsBuilder, outputPath);

            // this is the real package, not the symbol package
            return(package);
        }
Esempio n. 3
0
        private IPackage BuildFromProjectFile(string path)
        {
            var factory = new ProjectFactory(path, Properties)
            {
                IsTool = Tool,
                Logger = Console,
                Build  = Build,
            };

            // Add the additional Properties to the properties of the Project Factory
            foreach (var property in Properties)
            {
                factory.ProjectProperties.Add(property.Key, property.Value);
            }

            // Create a builder for the main package as well as the sources/symbols package
            PackageBuilder mainPackageBuilder = factory.CreateBuilder(BasePath);

            // Build the main package
            IPackage package = BuildPackage(mainPackageBuilder);

            // If we're excluding symbols then do nothing else
            if (!Symbols)
            {
                return(package);
            }

            Console.WriteLine();
            Console.WriteLine(NuGetResources.PackageCommandAttemptingToBuildSymbolsPackage, Path.GetFileName(path));

            factory.IncludeSymbols = true;
            PackageBuilder symbolsBuilder = factory.CreateBuilder(BasePath);

            symbolsBuilder.Version = mainPackageBuilder.Version;

            // Get the file name for the sources package and build it
            string outputPath = GetOutputPath(symbolsBuilder, symbols: true);

            BuildPackage(symbolsBuilder, outputPath);

            // this is the real package, not the symbol package
            return(package);
        }
        private PackageDependency CreateDependencyFromProject(Project project)
        {
            try
            {
                var projectFactory = new ProjectFactory(project);
                projectFactory.Build             = Build;
                projectFactory.ProjectProperties = ProjectProperties;
                projectFactory.BaseTargetPath    = BaseTargetPath;
                projectFactory.SolutionName      = SolutionName;
                projectFactory.BuildProject();
                var builder = new PackageBuilder();
                try
                {
                    AssemblyMetadataExtractor.ExtractMetadata(builder, projectFactory.TargetPath);
                }
                catch
                {
                    projectFactory.ExtractMetadataFromProject(builder);
                }

                projectFactory.InitializeProperties(builder);
                projectFactory.ProcessNuspec(builder, null);
                return(new PackageDependency(
                           builder.Id,
                           VersionUtility.ParseVersionSpec(builder.Version.ToString())));
            }
            catch (Exception ex)
            {
                var message = string.Format(
                    CultureInfo.InvariantCulture,
                    LocalizedResourceManager.GetString("Error_ProcessingNuspecFile"),
                    project.FullPath,
                    ex.Message);
                throw new CommandLineException(message, ex);
            }
        }
Esempio n. 5
0
        private IPackage BuildFromProjectFile(string path)
        {
            var factory = new ProjectFactory(path, Properties)
            {
                IsTool = Tool,
                Logger = Console,
                Build = Build,
            };

            // Create a builder for the main package as well as the sources/symbols package
            PackageBuilder mainPackageBuilder = factory.CreateBuilder(BasePath);

            // Build the main package
            IPackage package = BuildPackage(mainPackageBuilder);

            // If we're excluding symbols then do nothing else
            if (!Symbols)
            {
                return package;
            }

            Console.WriteLine();
            Console.WriteLine(NuGetResources.PackageCommandAttemptingToBuildSymbolsPackage, Path.GetFileName(path));

            factory.IncludeSymbols = true;
            PackageBuilder symbolsBuilder = factory.CreateBuilder(BasePath);
            symbolsBuilder.Version = mainPackageBuilder.Version;

            // Get the file name for the sources package and build it
            string outputPath = GetOutputPath(symbolsBuilder, symbols: true);
            BuildPackage(symbolsBuilder, outputPath);

            // this is the real package, not the symbol package
            return package;
        }
Esempio n. 6
0
        private IPackage BuildFromProjectFile(string path)
        {
            var factory = new ProjectFactory(path, Properties)
            {
                IsTool = Tool,
                Logger = Console,
                Build = Build,
                IncludeReferencedProjects = IncludeReferencedProjects
            };

            // Add the additional Properties to the properties of the Project Factory
            foreach (var property in Properties)
            {
                if (factory.ProjectProperties.ContainsKey(property.Key))
                {
                    Console.WriteWarning(LocalizedResourceManager.GetString("Warning_DuplicatePropertyKey"), property.Key);
                }
                factory.ProjectProperties[property.Key] = property.Value;
            }

            // Create a builder for the main package as well as the sources/symbols package
            PackageBuilder mainPackageBuilder = factory.CreateBuilder(BasePath);

            // Build the main package
            IPackage package = BuildPackage(mainPackageBuilder);

            // If we're excluding symbols then do nothing else
            if (!Symbols)
            {
                return package;
            }

            Console.WriteLine();
            Console.WriteLine(LocalizedResourceManager.GetString("PackageCommandAttemptingToBuildSymbolsPackage"), Path.GetFileName(path));

            factory.IncludeSymbols = true;
            PackageBuilder symbolsBuilder = factory.CreateBuilder(BasePath);
            symbolsBuilder.Version = mainPackageBuilder.Version;

            // Get the file name for the sources package and build it
            string outputPath = GetOutputPath(symbolsBuilder, symbols: true);
            BuildPackage(symbolsBuilder, outputPath);

            // this is the real package, not the symbol package
            return package;
        }
Esempio n. 7
0
        public void ProjectFactoryAppliesGlobalProperties()
        {
            // Arrange
            var testAssembly = Assembly.GetExecutingAssembly();
            var projectXml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<Project ToolsVersion=""4.0"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
    <PropertyGroup>
        <ProjectGuid>{F879F274-EFA0-4157-8404-33A19B4E6AEC}</ProjectGuid>
        <OutputType>Library</OutputType>
        <RootNamespace>NuGet.Test</RootNamespace>
        <AssemblyName>" + testAssembly.GetName().Name + @"</AssemblyName>
        <TargetFrameworkProfile Condition="" '$(TargetFrameworkVersion)' == 'v4.0' "">Client</TargetFrameworkProfile>    
        <OutputPath>.</OutputPath> <!-- Force it to look for the assembly in the base path -->
    </PropertyGroup>
    
    <ItemGroup>
        <Compile Include=""..\..\Dummy.cs"">
          <Link>Dummy.cs</Link>
        </Compile>
    </ItemGroup>

    <Import Project=""$(MSBuildToolsPath)\Microsoft.CSharp.targets"" />
    <Import Project=""C:\DoesNotExist.targets"" Condition="" '$(MyGlobalProperty)' != 'true' "" />
</Project>";

            // Set base path to the currently assembly's folder so that it will find the test assembly
            var basePath = Path.GetDirectoryName(testAssembly.CodeBase);
            var cmdLineProperties = new Dictionary<string, string>
                {
                    { "MyGlobalProperty", "true" }
                };
            var project = new Project(XmlReader.Create(new StringReader(projectXml)), cmdLineProperties, null);
            project.FullPath = Path.Combine(project.DirectoryPath, "test.csproj");
            
            // Act
            var factory = new ProjectFactory(project) { Build = false };
            factory.ProjectProperties.Add("MyGlobalProperty", "false"); // This shouldn't be applied
            factory.ProjectProperties.Add("TestProperty", "true"); // This should be applied
            var packageBuilder = factory.CreateBuilder(basePath);

            // Assert
            Assert.True(project.GetProperty("MyGlobalProperty").IsGlobalProperty);
            Assert.False(project.GetProperty("TestProperty").IsGlobalProperty);
            Assert.Equal("true", project.GetProperty("MyGlobalProperty").UnevaluatedValue, StringComparer.OrdinalIgnoreCase);
            Assert.Equal("true", project.GetProperty("TestProperty").UnevaluatedValue, StringComparer.OrdinalIgnoreCase);
        }
Esempio n. 8
0
        private PackageDependency CreateDependencyFromProject(Project project)
        {
            try
            {
                var projectFactory = new ProjectFactory(project);
                projectFactory.Build = Build;
                projectFactory.ProjectProperties = ProjectProperties;
                projectFactory.BaseTargetPath = BaseTargetPath;
                projectFactory.SolutionName = SolutionName;
                projectFactory.BuildProject();
                var builder = new PackageBuilder();
                try
                {
                    AssemblyMetadataExtractor.ExtractMetadata(builder, projectFactory.TargetPath);
                }
                catch
                {
                    projectFactory.ExtractMetadataFromProject(builder);
                }

                projectFactory.InitializeProperties(builder);
                projectFactory.ProcessNuspec(builder, null);
                return new PackageDependency(
                    builder.Id,
                    VersionUtility.ParseVersionSpec(builder.Version.ToString()));
            }
            catch (Exception ex)
            {
                var message = string.Format(
                    CultureInfo.InvariantCulture,
                    LocalizedResourceManager.GetString("Error_ProcessingNuspecFile"),
                    project.FullPath,
                    ex.Message);
                throw new CommandLineException(message, ex);
            }
        }
Esempio n. 9
0
 /// <summary>
 /// Recursively execute the specified action on the current project and
 /// projects referenced by the current project.
 /// </summary>
 /// <param name="action">The action to be executed.</param>
 /// <param name="alreadyAppliedProjects">The collection of projects that have been processed.
 /// It is used to avoid processing the same project more than once.</param>
 private void RecursivelyApply(Action<ProjectFactory> action, ProjectCollection alreadyAppliedProjects)
 {
     action(this);
     foreach (var item in _project.GetItems(ProjectReferenceItemType))
     {
         string fullPath = item.GetMetadataValue("FullPath");
         if (!string.IsNullOrEmpty(fullPath) && 
             !NuspecFileExists(fullPath) &&
             alreadyAppliedProjects.GetLoadedProjects(fullPath).IsEmpty())
         {
             var project = new Project(
                 fullPath, 
                 globalProperties: null, 
                 toolsVersion: null, 
                 projectCollection: alreadyAppliedProjects);
             var referencedProject = new ProjectFactory(project);
             referencedProject.Logger = _logger;
             referencedProject.IncludeSymbols = IncludeSymbols;
             referencedProject.Build = Build;
             referencedProject.IncludeReferencedProjects = IncludeReferencedProjects;
             referencedProject.ProjectProperties = ProjectProperties;
             referencedProject.TargetFramework = TargetFramework;
             referencedProject.BaseTargetPath = BaseTargetPath;
             referencedProject.SolutionName = SolutionName;
             referencedProject.BuildProject();
             referencedProject.RecursivelyApply(action, alreadyAppliedProjects);
         }
     }
 }
Esempio n. 10
0
        private void BuildFromProjectFile(string path)
        {
            var factory = new ProjectFactory(path) {
                IsTool = Tool,
                Logger = Console
            };

            // Specify the configuration
            factory.Properties.Add("Configuration", Configuration ?? "Release");

            // Create a builder for the main package as well as the sources/symbols package
            PackageBuilder mainPackageBuilder = factory.CreateBuilder();
            // Build the main package
            BuildPackage(path, mainPackageBuilder);

            // If we're excluding symbols then do nothing else
            if (!Symbols) {
                return;
            }

            Console.WriteLine();
            Console.WriteLine(NuGetResources.PackageCommandAttemptingToBuildSymbolsPackage, Path.GetFileName(path));

            factory.IncludeSymbols = true;
            PackageBuilder symbolsBuilder = factory.CreateBuilder();
            // Get the file name for the sources package and build it
            string outputPath = GetOutputPath(symbolsBuilder, symbols: true);
            BuildPackage(path, symbolsBuilder, outputPath);
        }
Esempio n. 11
0
        public void ProjectFactoryInitializesPropertiesForPreprocessorFromAssemblyMetadata()
        {
            // Arrange
            var testAssembly = Assembly.GetExecutingAssembly();
            const string inputSpec = @"<?xml version=""1.0""?>
<package>
    <metadata>
        <id>$id$</id>
        <version>$version$</version>
        <description>$description$</description>
        <authors>$owner$</authors>
        <copyright>$copyright$</copyright>
        <licenseUrl>http://nuget.codeplex.com/license</licenseUrl>
        <projectUrl>http://nuget.codeplex.com</projectUrl>
        <tags>nuget</tags>
    </metadata>
</package>";
            var projectXml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<Project ToolsVersion=""4.0"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
    <PropertyGroup>
        <ProjectGuid>{F879F274-EFA0-4157-8404-33A19B4E6AEC}</ProjectGuid>
        <OutputType>Library</OutputType>
        <RootNamespace>NuGet.Test</RootNamespace>
        <AssemblyName>" + testAssembly.GetName().Name + @"</AssemblyName>
        <TargetFrameworkProfile Condition="" '$(TargetFrameworkVersion)' == 'v4.0' "">Client</TargetFrameworkProfile>    
        <OutputPath>.</OutputPath> <!-- Force it to look for the assembly in the base path -->
    </PropertyGroup>
    
    <ItemGroup>
        <Compile Include=""..\..\Dummy.cs"">
          <Link>Dummy.cs</Link>
        </Compile>
    </ItemGroup>

    <Import Project=""$(MSBuildToolsPath)\Microsoft.CSharp.targets"" />
</Project>";

            // Set base path to the currently assembly's folder so that it will find the test assembly
            var basePath = Path.GetDirectoryName(testAssembly.CodeBase);

            var project = new Project(XmlReader.Create(new StringReader(projectXml)));
            project.FullPath = Path.Combine(project.DirectoryPath, "test.csproj");

            // Act
            var factory = new ProjectFactory(project) { Build = false };
            var packageBuilder = factory.CreateBuilder(basePath);
            var actual = Preprocessor.Process(inputSpec.AsStream(), factory, false);

            // assert
            var expected = @"<?xml version=""1.0""?>
<package>
    <metadata>
        <id>" + testAssembly.GetName().Name + @"</id>
        <version>" + testAssembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion + @"</version>
        <description></description>
        <authors>Outercurve</authors>
        <copyright>" + testAssembly.GetCustomAttribute<AssemblyCopyrightAttribute>().Copyright + @"</copyright>
        <licenseUrl>http://nuget.codeplex.com/license</licenseUrl>
        <projectUrl>http://nuget.codeplex.com</projectUrl>
        <tags>nuget</tags>
    </metadata>
</package>";
            Assert.Equal(expected, actual);
        }