如何从命令行合并所有适用的项目和nuget包?

时间:2017-03-28 09:18:31

标签: c# nuget visual-studio-2017

我正在合并两个解决方案,现在有以下情况

enter image description here

enter image description here

这是一个大型项目,整合单个包需要足够的时间。巩固26,我会整天待在这里。有没有办法批量合并,所以我可以去吃午饭,当我回来时它会完成?

2 个答案:

答案 0 :(得分:4)

关于评论,我将在此处发布我的解决方案。它是更大工具的一部分,所以我将在这里发布关键类,将它们连接在一起应该非常简单。 已安装的软件包(您可以使用较新的版本):

"NuGet.Core": "2.12.0-rtm-815",
"NuGet.Packaging": "3.5.0-beta2-1484",
"NuGet.ProjectManagement": "3.5.0-beta2-1484",

来源:

public class NugetSource
{
    public string Name { get; set; }
    public string Value { get; set; }
}

public class MyAppVersion
{
    public double Id { get; set; }
    public ObservableCollection<Dependency> Dependencies { get; set; }

    public MyAppVersion()
    {
        Dependencies = new ObservableCollection<Dependency>();
    }
}

public class Dependency : ReactiveObject
{
    public Dependency()
    {
        AvailableVersions = new List<SemanticVersion>();
    }

    private SemanticVersion _version;
    private string _name;
    private List<SemanticVersion> _availableVersions;

    [JsonProperty]
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            this.RaiseAndSetIfChanged(ref _name, value);
        }
    }

    [JsonProperty]
    public SemanticVersion Version
    {
        get { return _version; }
        set { this.RaiseAndSetIfChanged(ref _version, value); }
    }

    [JsonIgnore]
    public List<SemanticVersion> AvailableVersions
    {
        get { return _availableVersions; }
        set { this.RaiseAndSetIfChanged(ref _availableVersions, value); }
    }

    public override string ToString()
    {
        return $"Name: {Name}, Version: {Version}";
    }
}

public class NugetUpdater : INugetUpdater
{
    private readonly List<IPackageRepository> _supportedRepositories;

    public NugetUpdater()
    {
        _supportedRepositories =
            GetSources().Select(x => PackageRepositoryFactory.Default.CreateRepository(x.Value)).ToList();
    }

    public NugetSource[] GetSources()
    {

        var sources = new[]
        {
            new NugetSource() {Name = nameof(AppPaths.Dev), Value = AppPaths.Dev},
            new NugetSource() {Name = nameof(AppPaths.Uat), Value = AppPaths.Uat},
            new NugetSource() {Name = nameof(AppPaths.ThirdParty), Value = AppPaths.ThirdParty},
        };

        return sources;
    }

    public List<SemanticVersion> GetVersions(IEnumerable<string> feedUrls, string packageId)
    {
        var versions = new List<SemanticVersion>();
        var repos = GetRepositories(feedUrls);

        foreach (var currentRepo in repos)
        {
            var packages = currentRepo.FindPackagesById(packageId).ToList();
            versions.AddRange(packages.Select(x => x.Version));
        }

        return versions;
    }

    public SemanticVersion GetLatestVersion(IEnumerable<string> feedUrls, string packageId)
    {
        var versions = GetVersions(feedUrls, packageId);
        return versions.Any() ? versions.Max() : null;
    }

    public SemanticVersion GetLatestVersion(string feedUrl, string packageId)
    {
        return GetLatestVersion(new[] {feedUrl}, packageId);
    }

    public List<SemanticVersion> GetVersions(string feedUrl, string packageId)
    {
        return GetVersions(new[] {feedUrl}, packageId);
    }

    public List<Dependency> GetSolutionDependencies(string baseDir)
    {
        return Directory.EnumerateFiles(baseDir, "project.json", SearchOption.AllDirectories)
            .Select(File.ReadAllText)
            .Select(JObject.Parse)
            .Select(GetDependencies)
            .SelectMany(x => x)
            .DistinctBy(x => x.Name)
            .ToList();
    }

    private List<IPackageRepository> GetRepositories(IEnumerable<string> feedUrls)
    {
        return _supportedRepositories.Where(x => feedUrls.Contains(x.Source)).ToList();
    }

    public void Update(string baseDir, MyAppVersion version)
    {
        IEnumerable<string> jsonFiles =
            Directory.EnumerateFiles(baseDir, "project.json", SearchOption.AllDirectories).ToList();

        foreach (var projectJsonPath in jsonFiles)
        {
            var content = File.ReadAllText(projectJsonPath);
            JObject json = JObject.Parse(content);
            var projectDependencies = GetDependencies(json);

            if (!projectDependencies.Any())
                continue;

            var projectDepNames = projectDependencies.Select(x => x.Name).ToList();
            var toUpdateDependencies = version.Dependencies.Where(x => projectDepNames.Contains(x.Name)).ToList();

            if (toUpdateDependencies.Count != projectDependencies.Count)
                throw new Exception("Dependencies count is not equal. Something went wrong");

            var dependenciesPairs = toUpdateDependencies.OrderBy(x => x.Name)
                .Zip(projectDependencies.OrderBy(x => x.Name), (x, y) => new {ToUpdate = x, Project = y}).ToList();


            bool anyChanged = false;

            foreach (var dependencyPair in dependenciesPairs)
            {
                if (dependencyPair.Project.Version != dependencyPair.ToUpdate.Version)
                {
                    anyChanged = true;
                    dependencyPair.Project.Version = dependencyPair.ToUpdate.Version;
                }
            }

            if (anyChanged)
            {
                JObject obj = new JObject(projectDependencies.Select(x => new JProperty(x.Name, x.Version.ToNormalizedString())));
                json["dependencies"] = obj;
                File.WriteAllText(projectJsonPath, json.ToString(Formatting.Indented));
            }
        }
    }

    private static List<Dependency> GetDependencies(JObject json)
    {
        JObject dependenciesObject = (JObject) json["dependencies"];

        var dependencies = dependenciesObject.Properties().Select(x => new Dependency
        {
            Name = x.Name,
            Version = SemanticVersion.Parse(x.Value.Value<string>())
        }).ToList();

        return dependencies;
    }
}

基本上,应用程序使用NugetUpdater GetSolutionDependencies向用户显示解决方案中的所有依赖项。

然后GetVersions方法用于获取所选包的可用版本。用户可以选择他感兴趣的版本。当他完成时,他点击更新,Update函数将使用用户选择的版本来替换解决方案中所有project.jsons中的所有依赖项。

用户无需逐个选择版本,而是可以为所有包选择最新版本,结合

非常简单 对于每个单独的包+ GetSolutionDependencies

GetLatestVersion + Update

基本上,结果是使用最新版本的软件包更新了project.json的列表。

您所要做的就是运行nuget restore或使用VS构建解决方案,该解决方案将自动调用恢复。

答案 1 :(得分:0)

没有用于“整合所有内容”的按钮,我认为微软没有添加它是有充分的理由的,在过去的几周中,我只是在处理此确切问题时才意识到自己。

系统无法自动知道您要合并软件包的方式。您是否要将所有内容升级到相同版本或降级某些内容?

如果您只想将所有内容升级到相同版本,则可以使用“全部升级”选项。