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

HicServices / RDMP / 8389446117

22 Mar 2024 11:14AM UTC coverage: 56.872%. First build
8389446117

push

github

JFriel
add .rdmp plugin handling

10818 of 20495 branches covered (52.78%)

Branch coverage included in aggregate %.

1 of 26 new or added lines in 1 file covered. (3.85%)

30816 of 52711 relevant lines covered (58.46%)

7376.78 hits per line

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

9.82
/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);
354!
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 Dictionary<string, string> HandlePluginVersioning()
37
    {
NEW
38
        var PluginVersionLookup = new Dictionary<string, string>();
×
NEW
39
        var PluginPathLookup = new Dictionary<string, string>();
×
NEW
40
        var pluginFiles = Directory.EnumerateFiles(AppDomain.CurrentDomain.BaseDirectory, "*.rdmp");
×
NEW
41
        foreach (var pluginFile in pluginFiles)
×
42
        {
NEW
43
            using (FileStream fs = new FileStream(pluginFile, FileMode.Open))
×
NEW
44
            using (ZipFile zip = new ZipFile(fs))
×
45
            {
NEW
46
               foreach(ZipEntry ze in zip) {
×
NEW
47
                    if (ze.Name.Contains(".nuspec"))
×
48
                    {
NEW
49
                        using (StreamReader sr = new StreamReader(zip.GetInputStream(ze.ZipFileIndex)))
×
50
                        {
NEW
51
                            var content = sr.ReadToEnd();
×
NEW
52
                            var xmlDoc = new XmlDocument();
×
NEW
53
                            xmlDoc.LoadXml(content);
×
54

NEW
55
                            var name = xmlDoc.GetElementsByTagName("id").Item(0).InnerText;
×
NEW
56
                            var version = xmlDoc.GetElementsByTagName("version").Item(0).InnerText;
×
NEW
57
                            if (PluginVersionLookup.TryGetValue(name, out var currentVersion))
×
58
                            {
59
                                //already found a version
NEW
60
                                var result = new Version(currentVersion).CompareTo(new Version(version));
×
NEW
61
                                if (result < 0)
×
62
                                {
63
                                    //this version is the latest version
NEW
64
                                    PluginVersionLookup[name] = version;
×
NEW
65
                                    PluginPathLookup[name] = pluginFile;
×
66
                                }
67
                            }
68
                            else
69
                            {
NEW
70
                                PluginVersionLookup[name] = version;
×
NEW
71
                                PluginPathLookup[name] = pluginFile;
×
72
                            }
NEW
73
                        }
×
74
                        break;
75
                    }
76
                    
77
                }
78
            }
79

80
        }
81

NEW
82
        return PluginPathLookup;
×
83
    }
84

85
    /// <summary>
86
    /// List the plugin files to load
87
    /// </summary>
88
    /// <returns></returns>
89
    internal static IEnumerable<string> PluginFiles()
90
    {
91
        if (Directory.EnumerateFiles(AppDomain.CurrentDomain.BaseDirectory, "*.rdmp").Count() > 0)
16!
92
        {
93
            //use the modern .RDMP plugins
NEW
94
            var plugins = HandlePluginVersioning();
×
NEW
95
            return plugins.Values.ToArray();
×
96

97
        }
98
        return File.Exists(PluginsList)
16!
99
            ? File.ReadAllLines(PluginsList)
16✔
100
                .Select(static name => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, name))
×
101
            : Directory.EnumerateFiles(AppDomain.CurrentDomain.BaseDirectory, "*.nupkg");
16✔
102
    }
103

104
    /// <summary>
105
    /// Unpack the plugin DLL files, excluding any Windows UI specific dlls when not running a Windows GUI
106
    /// </summary>
107
    internal static IEnumerable<(string, MemoryStream)> GetContents(string path)
108
    {
109
        var info = new FileInfo(path);
×
110
        if (!info.Exists || info.Length < 100) yield break; // Ignore missing or empty files
×
111

112
        var pluginStream = info.OpenRead();
×
113
        if (!pluginStream.CanSeek)
×
114
            throw new ArgumentException("Seek needed", nameof(path));
×
115

116
        Assemblies.Add(new LoadModuleAssembly(info));
×
117

118
        using var zip = new ZipFile(pluginStream);
×
119
        foreach (var e in zip.Cast<ZipEntry>()
×
120
                     .Where(static e => e.IsFile && e.Name.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) &&
121
                                        (IsWin || !e.Name.Contains("/windows/"))))
122
        {
123
            using var s = zip.GetInputStream(e);
×
124
            using var ms2 = new MemoryStream();
×
125
            s.CopyTo(ms2);
×
126
            ms2.Position = 0;
×
127
            yield return (e.Name, ms2);
×
128
        }
×
129
    }
×
130

131
    /// <summary>
132
    /// Copy the plugin nupkg to the given directory
133
    /// </summary>
134
    /// <param name="downloadDirectory"></param>
135
    public void DownloadAssembly(DirectoryInfo downloadDirectory)
136
    {
137
        if (!downloadDirectory.Exists)
×
138
            downloadDirectory.Create();
×
139
        var targetFile = Path.Combine(downloadDirectory.FullName, _file.Name);
×
140
        _file.CopyTo(targetFile, true);
×
141
    }
×
142

143
    /// <summary>
144
    /// Delete the plugin file from disk, and remove it from rdmpplugins.txt if in use
145
    /// </summary>
146
    public void Delete()
147
    {
148
        _file.Delete();
×
149
        if (!File.Exists(PluginsList)) return;
×
150

151
        var tmp = $"{PluginsList}.tmp";
×
152
        File.WriteAllLines(tmp, File.ReadAllLines(PluginsList).Where(l => !l.Contains(_file.Name)));
×
153
        File.Move(tmp, PluginsList, true);
×
154
    }
×
155

156
    public override string ToString() => _file.Name;
×
157

158
    public static void UploadFile(ICheckNotifier checkNotifier, FileInfo toCommit)
159
    {
160
        try
161
        {
162
            toCommit.CopyTo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, toCommit.Name), true);
×
163
            if (!File.Exists(PluginsList)) return;
×
164

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

170
            var versionPos = toCommit.Name.IndexOf('-');
×
171
            if (versionPos != -1)
×
172
            {
173
                var stub = toCommit.Name[..(versionPos + 1)];
×
174
                list = list.Where(l => !l.StartsWith(stub, StringComparison.OrdinalIgnoreCase)).ToArray();
×
175
            }
176

177
            File.WriteAllLines(tmp, list.Union([toCommit.Name]));
×
178
            File.Move(tmp, PluginsList, true);
×
179
        }
×
180
        catch (Exception e)
×
181
        {
182
            checkNotifier.OnCheckPerformed(new CheckEventArgs($"Failed copying plugin {toCommit.Name}",
×
183
                CheckResult.Fail, e));
×
184
            throw;
×
185
        }
186
    }
×
187
}
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

© 2026 Coveralls, Inc