示例#1
0
        private async Task StageDefinition(Definition definition)
        {
            foreach (var dependency in definition.Dependencies ?? new Dictionary <Name, VersionRange>())
            {
                var repo = definition.Repositories?.FirstOrDefault(r => r.Name == dependency.Key);

                var adapter = new AdapterBuilder(dependency.Key, repo).Adapter();

                var versions = await adapter.GetVersions();

                var versionMatch = versions.LastOrDefault(version => dependency.Value.IsSatisfied(version));
                if (versionMatch == null)
                {
                    throw new Exception("No matching version found");
                }

                Directory.CreateDirectory(Path.Combine(Environment.CurrentDirectory, Program.PluginPath, ".staging", dependency.Key.Vendor, dependency.Key.Project));

                await adapter.Download(versionMatch);

                var dependencyDefinition = Definition.Load(Path.Combine(Environment.CurrentDirectory, Program.PluginPath, ".staging", dependency.Key.Vendor, dependency.Key.Project, Program.DefinitionFile));

                // TODO: What should be validated?
                if (dependencyDefinition.Name != dependency.Key)
                {
                    throw new Exception("Downloaded package does not match requested.");
                }
                if (dependencyDefinition.Version != versionMatch)
                {
                    throw new Exception("Downloaded package does not match requested.");
                }

                await StageDefinition(dependencyDefinition);
            }
        }
示例#2
0
        private static async Task <Tuple <List <Plugin>, List <Plugin> > > StageDefinition(SDK.Core.Plugins.Plugin definition, IDictionary <Name, Tuple <SDK.Core.Plugins.VersionRange, Plugin> > loaded = null)
        {
            var top    = new List <Plugin>();
            var nested = new List <Plugin>();

            foreach (var dependency in definition.Dependencies ?? new Dictionary <Name, SDK.Core.Plugins.VersionRange>())
            {
                var repo    = definition.Repositories?.FirstOrDefault(r => r.Name.ToString() == dependency.Key.ToString());
                var adapter = new AdapterBuilder(dependency.Key, repo).Adapter();

                var versions = await adapter.GetVersions();

                var versionMatch = versions.LastOrDefault(version => dependency.Value.IsSatisfied(version));
                if (versionMatch == null)
                {
                    throw new Exception("No matching version found");
                }

                if (loaded == null)
                {
                    loaded = new Dictionary <Name, Tuple <SDK.Core.Plugins.VersionRange, Plugin> >();
                }

                if (loaded.ContainsKey(dependency.Key))
                {
                    if (dependency.Value.Value != "*" && loaded[dependency.Key].Item1.Value != "*" && !dependency.Value.IsSatisfied(loaded[dependency.Key].Item2.Version))
                    {
                        throw new Exception($"{dependency.Key} was found");
                    }
                }

                var localPath = await adapter.Cache(versionMatch);

                var plugin = Plugin.Load(Path.Combine(localPath, ConfigurationManager.DefinitionFile));

                if (loaded.ContainsKey(dependency.Key))
                {
                    loaded.Add(dependency.Key, new Tuple <SDK.Core.Plugins.VersionRange, Plugin>(dependency.Value, plugin));
                }

                top.Add(plugin);

                // TODO: What should be validated?
                //if (plugin.Name != dependency.Key) throw new Exception("Downloaded package does not match requested.");
                //if (plugin.Version != versionMatch) throw new Exception("Downloaded package does not match requested.");

                nested.AddRange((await StageDefinition(plugin, loaded)).Item1);
            }

            return(new Tuple <List <Plugin>, List <Plugin> >(top, nested));
        }
示例#3
0
        public override async Task <int> Main()
        {
            var definition = LoadDefinition();
            var graph      = LoadGraph();

            // New plugins
            if (this.Plugins.Any())
            {
                foreach (var plugin in this.Plugins)
                {
                    var input = plugin;

                    // Local install
                    if (Directory.Exists(plugin) && File.Exists(Path.Combine(plugin, ConfigurationManager.DefinitionFile)))
                    {
                        var path = Path.GetFullPath(plugin);

                        var pluginDefinition = Plugin.Load(Path.Combine(path, ConfigurationManager.DefinitionFile));

                        if (definition.Repositories == null)
                        {
                            definition.Repositories = new List <Repository>();
                        }

                        definition.Repositories.RemoveAll(r => r.Name == pluginDefinition.Name);
                        definition.Repositories.Add(new Repository
                        {
                            Name = pluginDefinition.Name,
                            Type = "local",
                            Path = path
                        });

                        input = pluginDefinition.Name;
                    }

                    var parts = input.Split(new[] { '@' }, 2);
                    var name  = new Name(parts[0].Trim());

                    var versionInput = parts.Length == 2 ? parts[1].Trim() : "*";

                    Models.VersionRange range   = null;
                    Version             version = null;
                    PartialVersion      partial = null;

                    try
                    {
                        partial = new PartialVersion(versionInput);
                    }
                    catch (Exception)
                    {
                        // ignored
                    }

                    var isSpecific = partial?.Major != null && partial.Minor.HasValue && partial.Patch.HasValue;

                    try
                    {
                        range = new Models.VersionRange(versionInput);
                    }
                    catch (Exception)
                    {
                        // ignored
                    }

                    try
                    {
                        version = new Version(versionInput);
                    }
                    catch (Exception)
                    {
                        // ignored
                    }

                    List <Version> versions;
                    try
                    {
                        var adapter = new AdapterBuilder(name, definition).Adapter();
                        versions = (await adapter.GetVersions()).ToList();
                    }
                    catch (WebException ex) when((ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotFound)
                    {
                        Console.WriteLine("Error ".DarkRed(), $"{name}".Red(), " not found.".DarkRed());

                        return(1);
                    }

                    var versionMatch = range.MaxSatisfying(versions);
                    if (versionMatch == null)
                    {
                        Console.WriteLine("Error ".DarkRed(), $"{name}@{range}".Red(), " not found, available versions: ".DarkRed(), string.Join(" ", versions.Select(v => v.ToString())).Red());

                        return(1);
                    }

                    if (definition.Dependencies == null)
                    {
                        definition.Dependencies = new Dictionary <Name, SDK.Core.Plugins.VersionRange>();
                    }
                    definition.Dependencies[name] = new Models.VersionRange("^" + (isSpecific ? partial.ToZeroVersion() : version ?? versionMatch));

                    Console.WriteLine("+ ", $"{name}@{definition.Dependencies[name]}".White());
                }

                graph = new DefinitionGraph();
                await graph.Apply(definition);

                definition.Save(ConfigurationManager.DefinitionFile);
                graph.Save();
            }
            else
            {
                if (graph != null)
                {
                    await graph.Apply();
                }
                else
                {
                    graph = new DefinitionGraph();

                    await graph.Apply(definition);

                    graph.Save();
                }
            }

            if (PathManager.IsResource())
            {
                ResourceGenerator.Serialize(graph);
            }

            return(0);
        }
示例#4
0
文件: Outdated.cs 项目: lfshr/nfpm
        public override async Task <int> Main()
        {
            var results = new List <ColorToken[]>
            {
                new []
                {
                    "NAME".White(),
                    "CURRENT".White(),
                    "WANTED".White(),
                    "LATEST".White()
                }
            };

            var definition = LoadDefinition();

            foreach (var dependency in definition?.Dependencies ?? new Dictionary <Name, VersionRange>())
            {
                var repo    = definition?.Repositories?.FirstOrDefault(r => r.Name.ToString() == dependency.Key.ToString());
                var adapter = new AdapterBuilder(dependency.Key, repo).Adapter();

                var versions     = (await adapter.GetVersions()).ToList();
                var versionMatch = versions.LastOrDefault(version => dependency.Value.IsSatisfied(version));
                if (versionMatch == null)
                {
                    throw new Exception("No matching version found");
                }

                var        current = "MISSING".Red();
                ColorToken wanted  = versionMatch.ToString();
                ColorToken latest  = versions.Last().ToString();

                var pluginDefinition = new FileInfo(Path.Combine(Environment.CurrentDirectory, ConfigurationManager.PluginPath, dependency.Key.Vendor, dependency.Key.Project, ConfigurationManager.DefinitionFile));

                if (pluginDefinition.Exists)
                {
                    var plugin = Plugin.Load(pluginDefinition.FullName);

                    current = plugin.Version.ToString();

                    current = current.Text != wanted.Text ? current.Red() : current.Green();
                    wanted  = wanted.Text != latest.Text ? wanted.Red() : wanted.Green();

                    if (!this.All && current.Text == wanted.Text && wanted.Text == latest.Text)
                    {
                        continue;
                    }
                }

                results.Add(new[]
                {
                    dependency.Key.ToString(),
                    current,
                    wanted,
                    latest
                });
            }

            if (results.Count < 2)
            {
                return(0);
            }

            var nameLength    = Math.Max(Math.Min(50, results.Max(d => d[0].Text.Length)), 10);
            var currentLength = Math.Max(Math.Min(20, results.Max(d => d[1].Text.ToString().Length)), 7);
            var wantedLength  = Math.Max(Math.Min(20, results.Max(d => d[2].Text.ToString().Length)), 7);
            var latestLength  = Math.Max(Math.Min(20, results.Max(d => d[3].Text.ToString().Length)), 7);

            foreach (var result in results)
            {
                result[0].Text = result[0].Text.Truncate(nameLength).PadRight(nameLength);
                result[1].Text = result[1].Text.Truncate(currentLength).PadLeft(currentLength);
                result[2].Text = result[2].Text.Truncate(wantedLength).PadLeft(wantedLength);
                result[3].Text = result[3].Text.Truncate(latestLength).PadLeft(latestLength);

                Console.WriteLine(result[0], " | ", result[1], " | ", result[2], " | ", result[3]);
            }

            return(await Task.FromResult(0));
        }