예제 #1
0
 public ScriptExtensions(PackageConfiguration configuration, string outputPath)
 {
     packageConfiguration = configuration;
     packageMap           = DependencyInspector.GetPackageReferenceMap(configuration);
     assemblyFolder       = new TempDirectory(outputPath);
     ProjectFileName      = Path.GetFullPath(DefaultProjectFileName);
 }
예제 #2
0
        public static PackageBuilder CreateExecutablePackage(string path, Manifest manifest, PackageConfiguration configuration, out bool updateDependencies)
        {
            if (string.IsNullOrEmpty(path) || !File.Exists(path))
            {
                throw new ArgumentException("Invalid workflow file path.", "path");
            }

            var packageBuilder = new PackageBuilder();
            var basePath       = Path.GetDirectoryName(path) + "\\";

            packageBuilder.Populate(manifest.Metadata);
            packageBuilder.Tags.Add(NuGet.Constants.BonsaiDirectory);
            packageBuilder.Tags.Add(NuGet.Constants.GalleryDirectory);
            packageBuilder.PackageTypes = new[] { new PackageType(NuGet.Constants.GalleryPackageType, PackageType.EmptyVersion) };
            var files = manifest.Files ?? GetContentFiles(basePath);

            packageBuilder.PopulateFiles(basePath, files);
            var manifestDependencies = new Dictionary <string, PackageDependency>(StringComparer.OrdinalIgnoreCase);

            foreach (var dependency in packageBuilder.DependencyGroups.Where(group => group.TargetFramework == NuGetFramework.AnyFramework)
                     .SelectMany(group => group.Packages))
            {
                manifestDependencies.Add(dependency.Id, dependency);
            }

            updateDependencies = false;
            var workflowFiles = packageBuilder.Files.Select(file => file as PhysicalPackageFile)
                                .Where(file => file != null && Path.GetExtension(file.SourcePath) == NuGet.Constants.BonsaiExtension)
                                .Select(file => file.SourcePath)
                                .ToArray();
            var workflowDependencies = DependencyInspector.GetWorkflowPackageDependencies(workflowFiles, configuration).ToArray().Wait();

            foreach (var dependency in workflowDependencies)
            {
                if (!manifestDependencies.TryGetValue(dependency.Id, out PackageDependency manifestDependency) ||
                    !DependencyEqualityComparer.Default.Equals(dependency, manifestDependency))
                {
                    updateDependencies = true;
                    manifestDependencies[dependency.Id] = dependency;
                }
            }

            var dependencyGroup = new PackageDependencyGroup(NuGetFramework.AnyFramework, manifestDependencies.Values);

            packageBuilder.DependencyGroups.Clear();
            packageBuilder.DependencyGroups.Add(dependencyGroup);
            return(packageBuilder);
        }
예제 #3
0
        public void AddAssemblyReferences(IEnumerable <string> assemblyReferences)
        {
            XElement root;

            if (!File.Exists(ProjectFileName))
            {
                root = XElement.Parse(ProjectFileTemplate, LoadOptions.PreserveWhitespace);
                EnsureNuGetSettings();
            }
            else
            {
                root = XElement.Load(ProjectFileName, LoadOptions.PreserveWhitespace);
            }

            var projectReferences = root.Descendants(PackageReferenceElement).ToArray();
            var lastReference     = projectReferences.LastOrDefault();

            var packageReferences = DependencyInspector.GetAssemblyPackageReferences(packageConfiguration, assemblyReferences, packageMap);

            foreach (var reference in packageReferences)
            {
                var includeAttribute  = new XAttribute(PackageIncludeAttribute, reference.Id);
                var versionAttribute  = new XAttribute(PackageVersionAttribute, reference.Version);
                var existingReference = (from element in projectReferences
                                         let id = element.Attribute(PackageIncludeAttribute)
                                                  where id != null && id.Value == reference.Id
                                                  select element).FirstOrDefault();
                if (existingReference != null)
                {
                    var version = existingReference.Attribute(PackageVersionAttribute);
                    if (version == null)
                    {
                        existingReference.Add(versionAttribute);
                    }
                    else if (SemanticVersion.Parse(version.Value) < SemanticVersion.Parse(reference.Version))
                    {
                        version.SetValue(reference.Version);
                    }

                    continue;
                }

                var referenceElement = new XElement(PackageReferenceElement, includeAttribute, versionAttribute);
                if (lastReference == null)
                {
                    var itemGroup = new XElement(ItemGroupElement);
                    itemGroup.Add(Environment.NewLine + InnerIndent);
                    itemGroup.Add(referenceElement);
                    itemGroup.Add(Environment.NewLine + OuterIndent);

                    root.Add(OuterIndent);
                    root.Add(itemGroup);
                    root.Add(Environment.NewLine);
                    root.Add(Environment.NewLine);
                }
                else
                {
                    lastReference.AddAfterSelf(referenceElement);
                    if (lastReference.PreviousNode != null && lastReference.PreviousNode.NodeType >= XmlNodeType.Text)
                    {
                        lastReference.AddAfterSelf(lastReference.PreviousNode);
                    }
                }
                lastReference = referenceElement;
            }

            File.WriteAllText(ProjectFileName, root.ToString(SaveOptions.DisableFormatting));
        }