• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

HicServices / RDMP / 13318089130

13 Feb 2025 10:13PM UTC coverage: 57.398% (+0.004%) from 57.394%
13318089130

Pull #2134

github

jas88
Update ChildProviderTests.cs

Fix up TestUpTo method
Pull Request #2134: CodeQL fixups

11346 of 21308 branches covered (53.25%)

Branch coverage included in aggregate %.

104 of 175 new or added lines in 45 files covered. (59.43%)

362 existing lines in 23 files now uncovered.

32218 of 54590 relevant lines covered (59.02%)

17091.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

8.21
/Rdmp.Core/Curation/Data/LoadModuleAssembly.cs
1
// Copyright (c) The University of Dundee 2018-2024
2
// This file is part of the Research Data Management Platform (RDMP).
3
// RDMP is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4
// RDMP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5
// You should have received a copy of the GNU General Public License along with RDMP. If not, see <https://www.gnu.org/licenses/>.
6

7
using System;
8
using System.Collections.Generic;
9
using System.IO;
10
using System.Linq;
11
using System.Xml;
12
using ICSharpCode.SharpZipLib.Zip;
13
using Rdmp.Core.ReusableLibraryCode.Checks;
14

15
namespace Rdmp.Core.Curation.Data;
16

17
/// <summary>
18
/// This entity wraps references to plugin .nupkg files on disk.
19
/// </summary>
20
public sealed class LoadModuleAssembly
21
{
22
    private static readonly bool IsWin = AppDomain.CurrentDomain.GetAssemblies()
4✔
23
        .Any(static a => a.FullName?.StartsWith("Rdmp.UI", StringComparison.Ordinal) == true);
348!
24

25
    private static readonly string PluginsList = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "rdmpplugins.txt");
4✔
26

27
    internal static readonly List<LoadModuleAssembly> Assemblies = [];
4✔
28
    private readonly FileInfo _file;
29

30
    private LoadModuleAssembly(FileInfo file)
×
31
    {
32
        _file = file;
×
33
    }
×
34

35

36
    private static bool IsReleaseCandidcate(string filename)
37
    {
38
        return filename.Split('.')[^2].Contains("-rc");
×
39
    }
40

41
    private static int GetReleaseCandidateId(string filename)
42
    {
43
        //expects the rc to be something like My.Plugin.3.0.0-rc2
44
        int.TryParse(filename.Split("-rc")[1].Split('.')[0], out var result);
×
45
        return result;
×
46
    }
47

48
    private static Dictionary<string, string> HandlePluginVersioning()
49
    {
50
        var PluginVersionLookup = new Dictionary<string, string>();
×
51
        var PluginPathLookup = new Dictionary<string, string>();
×
52
        var pluginFiles = Directory.EnumerateFiles(AppDomain.CurrentDomain.BaseDirectory, "*.rdmp");
×
53
        foreach (var pluginFile in pluginFiles)
×
54
        {
55
            using FileStream fs = new(pluginFile, FileMode.Open,FileAccess.Read);
×
56
            using ZipFile zip = new(fs);
×
57
            foreach (ZipEntry ze in zip)
×
58
            {
59
                if (ze.Name.Contains(".nuspec"))
×
60
                {
61
                    using StreamReader sr = new StreamReader(zip.GetInputStream(ze.ZipFileIndex));
×
62
                    var content = sr.ReadToEnd();
×
63
                    var xmlDoc = new XmlDocument();
×
64
                    xmlDoc.LoadXml(content);
×
65

66
                    var name = xmlDoc.GetElementsByTagName("id").Item(0).InnerText;
×
67
                    var version = xmlDoc.GetElementsByTagName("version").Item(0).InnerText;
×
68
                    if (PluginVersionLookup.TryGetValue(name, out var currentVersion))
×
69
                    {
70
                        //already found a version
71
                        var result = new Version(currentVersion).CompareTo(new Version(version));
×
72
                        if (result < 0)
×
73
                        {
74
                            //this version is the latest version
75
                            PluginVersionLookup[name] = version;
×
76
                            PluginPathLookup[name] = pluginFile;
×
77
                        }
78
                        if(result == 0)
×
79
                        {
80
                            //check for RC in the name
81
                            var currentIsReleaseCandidate = IsReleaseCandidcate(PluginPathLookup[name]);
×
82
                            var newIsReleaseCandidate = IsReleaseCandidcate(pluginFile);
×
83
                            if(newIsReleaseCandidate && currentIsReleaseCandidate)
×
84
                            {
85
                                var newIsLatest = GetReleaseCandidateId(pluginFile) > GetReleaseCandidateId(PluginPathLookup[name]);
×
86
                                if (newIsLatest)
×
87
                                {
88
                                    PluginVersionLookup[name] = version;
×
89
                                    PluginPathLookup[name] = pluginFile;
×
90
                                }
91
                            } else if (newIsReleaseCandidate)
×
92
                            {
93
                                //new version is the latest
94
                                PluginVersionLookup[name] = version;
×
95
                                PluginPathLookup[name] = pluginFile;
×
96
                            }
97
                        }
98
                    }
99
                    else
100
                    {
101
                        PluginVersionLookup[name] = version;
×
102
                        PluginPathLookup[name] = pluginFile;
×
103
                    }
104
                    break;
×
105
                }
106

107
            }
108

109
        }
110

111
        return PluginPathLookup;
×
112
    }
113

114
    /// <summary>
115
    /// List the plugin files to load
116
    /// </summary>
117
    /// <returns></returns>
118
    internal static IEnumerable<string> PluginFiles()
119
    {
120
        if (!Directory.EnumerateFiles(AppDomain.CurrentDomain.BaseDirectory, "*.rdmp").Any())
16!
121
            return File.Exists(PluginsList)
16!
122
                ? File.ReadAllLines(PluginsList)
16✔
NEW
123
                    .Select(static name => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, name))
×
124
                : Directory.EnumerateFiles(AppDomain.CurrentDomain.BaseDirectory, "*.nupkg");
16✔
125
        //use the modern .RDMP plugins
NEW
126
        var plugins = HandlePluginVersioning();
×
NEW
127
        return plugins.Values.ToArray();
×
128
    }
129

130
    /// <summary>
131
    /// Unpack the plugin DLL files, excluding any Windows UI specific dlls when not running a Windows GUI
132
    /// </summary>
133
    internal static IEnumerable<(string, MemoryStream)> GetContents(string path)
134
    {
135
        var info = new FileInfo(path);
×
136
        if (!info.Exists || info.Length < 100) yield break; // Ignore missing or empty files
×
137

138
        var pluginStream = info.OpenRead();
×
139
        if (!pluginStream.CanSeek)
×
140
            throw new ArgumentException("Seek needed", nameof(path));
×
141

142
        Assemblies.Add(new LoadModuleAssembly(info));
×
143

144
        using var zip = new ZipFile(pluginStream);
×
145
        foreach (var e in zip.Cast<ZipEntry>()
×
146
                     .Where(static e => e.IsFile && e.Name.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) &&
147
                                        (IsWin || !e.Name.Contains("/windows/"))))
148
        {
149
            using var s = zip.GetInputStream(e);
×
150
            using var ms2 = new MemoryStream();
×
151
            s.CopyTo(ms2);
×
152
            ms2.Position = 0;
×
153
            yield return (e.Name, ms2);
×
154
        }
×
155
    }
×
156

157
    /// <summary>
158
    /// Copy the plugin nupkg to the given directory
159
    /// </summary>
160
    /// <param name="downloadDirectory"></param>
161
    public void DownloadAssembly(DirectoryInfo downloadDirectory)
162
    {
163
        if (!downloadDirectory.Exists)
×
164
            downloadDirectory.Create();
×
165
        var targetFile = Path.Combine(downloadDirectory.FullName, _file.Name);
×
166
        _file.CopyTo(targetFile, true);
×
167
    }
×
168

169
    /// <summary>
170
    /// Delete the plugin file from disk, and remove it from rdmpplugins.txt if in use
171
    /// </summary>
172
    public void Delete()
173
    {
174
        _file.Delete();
×
175
        if (!File.Exists(PluginsList)) return;
×
176

177
        var tmp = $"{PluginsList}.tmp";
×
178
        File.WriteAllLines(tmp, File.ReadAllLines(PluginsList).Where(l => !l.Contains(_file.Name)));
×
179
        File.Move(tmp, PluginsList, true);
×
180
    }
×
181

182
    public override string ToString() => _file.Name;
×
183

184
    public static void UploadFile(ICheckNotifier checkNotifier, FileInfo toCommit)
185
    {
186
        try
187
        {
188
            toCommit.CopyTo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, toCommit.Name), true);
×
189
            if (!File.Exists(PluginsList)) return;
×
190

191
            // Now the tricky bit: add this new file, and remove any other versions of the same
192
            // e.g. adding DummyPlugin-1.2.3 should delete DummyPlugin-2.0 and DummyPlugin-1.0 if present
193
            var list = File.ReadAllLines(PluginsList);
×
194
            var tmp = $"{PluginsList}.tmp";
×
195

196
            var versionPos = toCommit.Name.IndexOf('-');
×
197
            if (versionPos != -1)
×
198
            {
199
                var stub = toCommit.Name[..(versionPos + 1)];
×
200
                list = list.Where(l => !l.StartsWith(stub, StringComparison.OrdinalIgnoreCase)).ToArray();
×
201
            }
202

203
            File.WriteAllLines(tmp, list.Union([toCommit.Name]));
×
204
            File.Move(tmp, PluginsList, true);
×
205
        }
×
206
        catch (Exception e)
×
207
        {
208
            checkNotifier.OnCheckPerformed(new CheckEventArgs($"Failed copying plugin {toCommit.Name}",
×
209
                CheckResult.Fail, e));
×
210
            throw;
×
211
        }
212
    }
×
213
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc