|
1 | | -// Licensed to the .NET Foundation under one or more agreements. |
2 | | -// The .NET Foundation licenses this file to you under the MIT license. |
3 | | -// See the LICENSE file in the project root for more information |
4 | | - |
5 | | -namespace DotNetNuke.Build.Tasks; |
6 | | - |
7 | | -using System.IO; |
8 | | -using System.Linq; |
9 | | -using System.Xml; |
10 | | - |
11 | | -using Cake.Common.Diagnostics; |
12 | | -using Cake.Common.IO; |
13 | | -using Cake.Core.IO; |
14 | | -using Cake.Frosting; |
15 | | -using Dnn.CakeUtils; |
16 | | - |
17 | | -/// <summary>Provides the base functionality for packaging a folder inside Components.</summary> |
18 | | -public abstract class PackageComponentTask : FrostingTask<Context> |
19 | | -{ |
20 | | - private static readonly string[] AllFiles = ["*",]; |
21 | | - private static readonly string[] ManifestFiles = ["*.dnn",]; |
22 | | - |
23 | | - /// <summary>Initializes a new instance of the <see cref="PackageComponentTask"/> class.</summary> |
24 | | - /// <param name="componentName">The name of the component.</param> |
25 | | - /// <param name="primaryAssemblyName">The name of the primary assembly.</param> |
26 | | - /// <param name="componentFolderName">The name of the folder in <c>DNN Platform/Components/</c>.</param> |
27 | | - protected PackageComponentTask(string componentName, FilePath primaryAssemblyName = null, DirectoryPath componentFolderName = null) |
28 | | - { |
29 | | - this.ComponentName = componentName; |
30 | | - this.ComponentFolderName = componentFolderName ?? componentName; |
31 | | - this.PrimaryAssemblyName = primaryAssemblyName ?? $"{componentName}.dll"; |
32 | | - } |
33 | | - |
34 | | - /// <summary>Gets the name of the component.</summary> |
35 | | - public string ComponentName { get; } |
36 | | - |
37 | | - /// <summary>Gets the name of the folder in <c>DNN Platform/Components/</c> where the component files are.</summary> |
38 | | - public DirectoryPath ComponentFolderName { get; } |
39 | | - |
40 | | - /// <summary>Gets the name of the primary assembly.</summary> |
41 | | - public FilePath PrimaryAssemblyName { get; } |
42 | | - |
43 | | - /// <inheritdoc /> |
44 | | - public override void Run(Context context) |
45 | | - { |
46 | | - var binDir = context.WebsiteDir.Path.Combine("bin"); |
47 | | - var mainAssemblyPath = binDir.CombineWithFilePath(this.PrimaryAssemblyName); |
48 | | - var packageVersion = context.GetAssemblyFileVersion(mainAssemblyPath); |
49 | | - |
50 | | - var packageZip = context.WebsiteDir.Path.CombineWithFilePath($"Install/Library/{this.ComponentName}_{packageVersion}_Install.zip"); |
51 | | - var packageDir = context.Directory($"DNN Platform/Components/{this.ComponentFolderName}"); |
52 | | - |
53 | | - context.Information($"Creating {packageZip}"); |
54 | | - context.Zip( |
55 | | - packageDir.ToString(), |
56 | | - packageZip, |
57 | | - context.GetFilesByPatterns(packageDir, AllFiles, ManifestFiles)); |
58 | | - |
59 | | - var manifestPath = context.GetFiles(packageDir.Path.CombineWithFilePath("*.dnn").ToString()).Single(); |
60 | | - context.Information($"Reading manifest from {manifestPath}"); |
61 | | - var manifest = new XmlDocument(); |
62 | | - using (var manifestReader = XmlReader.Create(new StringReader(context.ReadFile(manifestPath)), new XmlReaderSettings { XmlResolver = null, })) |
63 | | - { |
64 | | - manifest.Load(manifestReader); |
65 | | - } |
66 | | - |
67 | | - var assemblies = |
68 | | - from XmlNode assemblyNode in manifest.SelectNodes("//assembly") |
69 | | - from XmlNode childNode in assemblyNode.ChildNodes |
70 | | - where childNode.LocalName.Equals("name", System.StringComparison.Ordinal) |
71 | | - select childNode; |
72 | | - |
73 | | - foreach (var assemblyNameNode in assemblies) |
74 | | - { |
75 | | - var assemblyPath = binDir.CombineWithFilePath(assemblyNameNode.InnerText); |
76 | | - context.Information($"Adding {assemblyPath} to {packageZip}"); |
77 | | - context.AddFilesToZip( |
78 | | - packageZip, |
79 | | - context.MakeAbsolute(context.WebsiteDir.Path), |
80 | | - context.GetFiles(assemblyPath.ToString()), |
81 | | - append: true); |
82 | | - |
83 | | - var versionNode = assemblyNameNode.ParentNode?.ChildNodes.Cast<XmlNode>() |
84 | | - .SingleOrDefault(childNode => childNode.LocalName.Equals("version", System.StringComparison.Ordinal)); |
85 | | - if (versionNode != null) |
86 | | - { |
87 | | - versionNode.InnerText = context.GetAssemblyFileVersion(assemblyPath); |
88 | | - context.Information($"Set {assemblyPath} version to {versionNode.InnerText}"); |
89 | | - } |
90 | | - } |
91 | | - |
92 | | - manifest.SelectSingleNode("//package[@version]").Attributes["version"].Value = packageVersion; |
93 | | - |
94 | | - context.AddXmlFileToZip(packageZip, manifest, manifestPath.GetFilename().ToString(), append: true); |
95 | | - } |
96 | | -} |
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +namespace DotNetNuke.Build.Tasks; |
| 6 | + |
| 7 | +using System.IO; |
| 8 | +using System.Linq; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using System.Xml; |
| 11 | + |
| 12 | +using Cake.Common.Diagnostics; |
| 13 | +using Cake.Common.IO; |
| 14 | +using Cake.Core.IO; |
| 15 | +using Cake.Frosting; |
| 16 | +using Dnn.CakeUtils; |
| 17 | + |
| 18 | +/// <summary>Provides the base functionality for packaging a folder inside Components.</summary> |
| 19 | +public abstract class PackageComponentTaskBase : AsyncFrostingTask<Context> |
| 20 | +{ |
| 21 | + private static readonly string[] AllFiles = ["*",]; |
| 22 | + private static readonly string[] ManifestFiles = ["*.dnn",]; |
| 23 | + |
| 24 | + /// <summary>Initializes a new instance of the <see cref="PackageComponentTaskBase"/> class.</summary> |
| 25 | + /// <param name="componentName">The name of the component.</param> |
| 26 | + /// <param name="primaryAssemblyName">The name of the primary assembly.</param> |
| 27 | + /// <param name="componentFolderName">The name of the folder in <c>DNN Platform/Components/</c>.</param> |
| 28 | + protected PackageComponentTaskBase(string componentName, FilePath primaryAssemblyName = null, DirectoryPath componentFolderName = null) |
| 29 | + { |
| 30 | + this.ComponentName = componentName; |
| 31 | + this.ComponentFolderName = componentFolderName ?? componentName; |
| 32 | + this.PrimaryAssemblyName = primaryAssemblyName ?? $"{componentName}.dll"; |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary>Gets the name of the component.</summary> |
| 36 | + public string ComponentName { get; } |
| 37 | + |
| 38 | + /// <summary>Gets the name of the folder in <c>DNN Platform/Components/</c> where the component files are.</summary> |
| 39 | + public DirectoryPath ComponentFolderName { get; } |
| 40 | + |
| 41 | + /// <summary>Gets the name of the primary assembly.</summary> |
| 42 | + public FilePath PrimaryAssemblyName { get; } |
| 43 | + |
| 44 | + /// <inheritdoc /> |
| 45 | + public override Task RunAsync(Context context) |
| 46 | + { |
| 47 | + var binDir = GetBinDir(context); |
| 48 | + var packageZip = this.GetPackageZipPath(context, binDir); |
| 49 | + var packageDir = context.Directory($"DNN Platform/Components/{this.ComponentFolderName}"); |
| 50 | + |
| 51 | + context.Information($"Creating {packageZip}"); |
| 52 | + context.Zip( |
| 53 | + packageDir.ToString(), |
| 54 | + packageZip, |
| 55 | + context.GetFilesByPatterns(packageDir, AllFiles, ManifestFiles)); |
| 56 | + |
| 57 | + var manifestPath = context.GetFiles(packageDir.Path.CombineWithFilePath("*.dnn").ToString()).Single(); |
| 58 | + context.Information($"Reading manifest from {manifestPath}"); |
| 59 | + var manifest = new XmlDocument(); |
| 60 | + using (var manifestReader = XmlReader.Create(new StringReader(context.ReadFile(manifestPath)), new XmlReaderSettings { XmlResolver = null, })) |
| 61 | + { |
| 62 | + manifest.Load(manifestReader); |
| 63 | + } |
| 64 | + |
| 65 | + var assemblies = |
| 66 | + from XmlNode assemblyNode in manifest.SelectNodes("//assembly") |
| 67 | + where assemblyNode.Attributes?["action"]?.Value != "UnRegister" |
| 68 | + from XmlNode childNode in assemblyNode.ChildNodes |
| 69 | + where childNode.LocalName.Equals("name", System.StringComparison.Ordinal) |
| 70 | + select childNode; |
| 71 | + |
| 72 | + foreach (var assemblyNameNode in assemblies) |
| 73 | + { |
| 74 | + var assemblyPath = binDir.CombineWithFilePath(assemblyNameNode.InnerText); |
| 75 | + context.Information($"Adding {assemblyPath} to {packageZip}"); |
| 76 | + context.AddFilesToZip( |
| 77 | + packageZip, |
| 78 | + context.MakeAbsolute(context.WebsiteDir.Path), |
| 79 | + context.GetFiles(assemblyPath.ToString()), |
| 80 | + append: true); |
| 81 | + |
| 82 | + var versionNode = assemblyNameNode.ParentNode?.ChildNodes.Cast<XmlNode>() |
| 83 | + .SingleOrDefault(childNode => childNode.LocalName.Equals("version", System.StringComparison.Ordinal)); |
| 84 | + if (versionNode != null) |
| 85 | + { |
| 86 | + versionNode.InnerText = context.GetAssemblyFileVersion(assemblyPath); |
| 87 | + context.Information($"Set {assemblyPath} version to {versionNode.InnerText}"); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + manifest.SelectSingleNode("//package[@version]").Attributes["version"].Value = this.GetPackageVersion(context, binDir); |
| 92 | + |
| 93 | + context.AddXmlFileToZip(packageZip, manifest, manifestPath.GetFilename().ToString(), append: true); |
| 94 | + |
| 95 | + return Task.CompletedTask; |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary>Get the path to the <c>bin</c> directory, from which to get DLLs.</summary> |
| 99 | + /// <param name="context">The context.</param> |
| 100 | + /// <returns>The path.</returns> |
| 101 | + protected static DirectoryPath GetBinDir(Context context) |
| 102 | + { |
| 103 | + return context.WebsiteDir.Path.Combine("bin"); |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary>Gets the path to the <c>zip</c> file generated by this task.</summary> |
| 107 | + /// <param name="context">The context.</param> |
| 108 | + /// <param name="binDir">The path to the <c>bin</c> directory (from <see cref="GetBinDir"/>).</param> |
| 109 | + /// <returns>The file path.</returns> |
| 110 | + protected FilePath GetPackageZipPath(Context context, DirectoryPath binDir) |
| 111 | + { |
| 112 | + return context.WebsiteDir.Path.CombineWithFilePath($"Install/Library/{this.ComponentName}_{this.GetPackageVersion(context, binDir)}_Install.zip"); |
| 113 | + } |
| 114 | + |
| 115 | + private string GetPackageVersion(Context context, DirectoryPath binDir) |
| 116 | + { |
| 117 | + var mainAssemblyPath = binDir.CombineWithFilePath(this.PrimaryAssemblyName); |
| 118 | + var packageVersion = context.GetAssemblyFileVersion(mainAssemblyPath); |
| 119 | + return packageVersion; |
| 120 | + } |
| 121 | +} |
0 commit comments