|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Globalization; |
| 4 | +using System.IO.Compression; |
| 5 | +using System.Linq; |
| 6 | +using System.Xml; |
| 7 | +using BuildHelpers; |
| 8 | +using Nuke.Common; |
| 9 | +using Nuke.Common.Execution; |
| 10 | +using Nuke.Common.Git; |
| 11 | +using Nuke.Common.IO; |
| 12 | +using Nuke.Common.ProjectModel; |
| 13 | +using Nuke.Common.Tooling; |
| 14 | +using Nuke.Common.Tools.DotNet; |
| 15 | +using Nuke.Common.Tools.Git; |
| 16 | +using Nuke.Common.Tools.GitVersion; |
| 17 | +using Nuke.Common.Tools.MSBuild; |
| 18 | +using Nuke.Common.Utilities.Collections; |
| 19 | +using static Nuke.Common.EnvironmentInfo; |
| 20 | +using static Nuke.Common.IO.FileSystemTasks; |
| 21 | +using static Nuke.Common.IO.CompressionTasks; |
| 22 | +using static Nuke.Common.IO.PathConstruction; |
| 23 | +using static Nuke.Common.Tools.DotNet.DotNetTasks; |
| 24 | +using static Nuke.Common.Tools.Git.GitTasks; |
| 25 | +using static Nuke.Common.Tools.MSBuild.MSBuildTasks; |
| 26 | + |
| 27 | +[CheckBuildProjectConfigurations] |
| 28 | +class Build : NukeBuild |
| 29 | +{ |
| 30 | + /// Support plugins are available for: |
| 31 | + /// - JetBrains ReSharper https://nuke.build/resharper |
| 32 | + /// - JetBrains Rider https://nuke.build/rider |
| 33 | + /// - Microsoft VisualStudio https://nuke.build/visualstudio |
| 34 | + /// - Microsoft VSCode https://nuke.build/vscode |
| 35 | + |
| 36 | + public static int Main() => Execute<Build>(x => x.Package); |
| 37 | + |
| 38 | + [Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")] |
| 39 | + readonly Configuration Configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release; |
| 40 | + |
| 41 | + [Parameter("Github Token")] |
| 42 | + readonly string GithubToken; |
| 43 | + |
| 44 | + [Solution] readonly Solution Solution; |
| 45 | + [GitRepository] readonly GitRepository GitRepository; |
| 46 | + [GitVersion(Framework = "netcoreapp3.1", UpdateAssemblyInfo = false, NoFetch = true)] readonly GitVersion GitVersion; |
| 47 | + |
| 48 | + |
| 49 | + readonly string ModuleName = "Dnn.Modules.ModuleCreator"; |
| 50 | + readonly IReadOnlyCollection<string> SymbolFiles; |
| 51 | + readonly IReadOnlyCollection<string> InstallFiles; |
| 52 | + readonly IReadOnlyCollection<string> BinaryFiles; |
| 53 | + readonly bool IsInDesktopModules; |
| 54 | + readonly AbsolutePath ArtifactsDirectory; |
| 55 | + readonly AbsolutePath StagingDirectory; |
| 56 | + readonly AbsolutePath DeployDirectory; |
| 57 | + readonly AbsolutePath DnnModuleInstallDirectory; |
| 58 | + string ModuleBranch; |
| 59 | + |
| 60 | + public Build() |
| 61 | + { |
| 62 | + SymbolFiles = GlobFiles(RootDirectory / "bin" / "Release", $"{ModuleName}.pdb"); |
| 63 | + InstallFiles = GlobFiles(RootDirectory, "*.txt", "*.dnn"); |
| 64 | + ArtifactsDirectory = RootDirectory / "Artifacts"; |
| 65 | + StagingDirectory = ArtifactsDirectory / "Staging"; |
| 66 | + BinaryFiles = GlobFiles(RootDirectory / "bin" / Configuration, $"{ModuleName}.dll"); |
| 67 | + IsInDesktopModules = RootDirectory.Parent.ToString().EndsWith("DesktopModules"); |
| 68 | + DeployDirectory = IsInDesktopModules ? RootDirectory.Parent / "Admin" / "ModuleCreator" : null; |
| 69 | + DnnModuleInstallDirectory = RootDirectory.Parent.Parent / "Install" / "Module"; |
| 70 | + } |
| 71 | + |
| 72 | + Target Clean => _ => _ |
| 73 | + .Before(Restore) |
| 74 | + .Before(Package) |
| 75 | + .Executes(() => |
| 76 | + { |
| 77 | + EnsureCleanDirectory(ArtifactsDirectory); |
| 78 | + }); |
| 79 | + |
| 80 | + Target Restore => _ => _ |
| 81 | + .Executes(() => |
| 82 | + { |
| 83 | + var project = Solution.GetProject(ModuleName); |
| 84 | + DotNetRestore(s => s |
| 85 | + .SetProjectFile(project)); |
| 86 | + }); |
| 87 | + |
| 88 | + Target SetManifestVersions => _ => _ |
| 89 | + .Executes(() => |
| 90 | + { |
| 91 | + var manifests = GlobFiles(RootDirectory, "**/*.dnn"); |
| 92 | + manifests.ForEach(manifest => |
| 93 | + { |
| 94 | + var doc = new XmlDocument(); |
| 95 | + doc.Load(manifest); |
| 96 | + var packages = doc.SelectNodes("dotnetnuke/packages/package"); |
| 97 | + foreach (XmlNode package in packages) |
| 98 | + { |
| 99 | + var version = package.Attributes["version"]; |
| 100 | + if (version != null) |
| 101 | + { |
| 102 | + Logger.Normal($"Found package {package.Attributes["name"].Value} with version {version.Value}"); |
| 103 | + version.Value = $"{GitVersion.Major.ToString("00", CultureInfo.InvariantCulture)}.{GitVersion.Minor.ToString("00", CultureInfo.InvariantCulture)}.{GitVersion.Patch.ToString("00", CultureInfo.InvariantCulture)}"; |
| 104 | + Logger.Normal($"Updated packages {package.Attributes["name"].Value} to version {version.Value}"); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + doc.Save(manifest); |
| 109 | + Logger.Normal($"Saved {manifest}"); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + Target TagRelease => _ => _ |
| 114 | + .OnlyWhenDynamic(() => ModuleBranch == "main" || ModuleBranch.StartsWith("release")) |
| 115 | + .OnlyWhenDynamic(() => !string.IsNullOrEmpty(GithubToken)) |
| 116 | + .DependsOn(SetBranch) |
| 117 | + .Executes(() => |
| 118 | + { |
| 119 | + var version = ModuleBranch == "main" ? GitVersion.MajorMinorPatch : GitVersion.SemVer; |
| 120 | + GitLogger = (type, output) => Logger.Info(output); |
| 121 | + Git($"tag v{version}"); |
| 122 | + Git($"push --tags"); |
| 123 | + }); |
| 124 | + |
| 125 | + Target Compile => _ => _ |
| 126 | + .DependsOn(Clean) |
| 127 | + .DependsOn(Restore) |
| 128 | + .DependsOn(SetManifestVersions) |
| 129 | + .DependsOn(TagRelease) |
| 130 | + .DependsOn(SetBranch) |
| 131 | + .Executes(() => |
| 132 | + { |
| 133 | + MSBuild(s => s |
| 134 | + .SetProjectFile(Solution.GetProject(ModuleName)) |
| 135 | + .SetConfiguration(Configuration) |
| 136 | + .SetAssemblyVersion(ModuleBranch == "main" ? GitVersion.MajorMinorPatch : GitVersion.AssemblySemVer) |
| 137 | + .SetFileVersion(ModuleBranch == "main" ? GitVersion.MajorMinorPatch : GitVersion.InformationalVersion)); |
| 138 | + }); |
| 139 | + |
| 140 | + Target SetBranch => _ => _ |
| 141 | + .Executes(() => |
| 142 | + { |
| 143 | + ModuleBranch = GitRepository.Branch.StartsWith("refs/") ? GitRepository.Branch.Substring(11) : GitRepository.Branch; |
| 144 | + Logger.Info($"Set branch name to {ModuleBranch}"); |
| 145 | + }); |
| 146 | + |
| 147 | + /// <summary> |
| 148 | + /// Packages the module for release. |
| 149 | + /// </summary> |
| 150 | + Target Package => _ => _ |
| 151 | + .DependsOn(Clean) |
| 152 | + .DependsOn(SetManifestVersions) |
| 153 | + .DependsOn(Compile) |
| 154 | + .DependsOn(SetBranch) |
| 155 | + .DependsOn(TagRelease) |
| 156 | + .Executes(() => |
| 157 | + { |
| 158 | + EnsureCleanDirectory(StagingDirectory); |
| 159 | + Compress(RootDirectory, StagingDirectory / "Resources.zip", f => |
| 160 | + f.Extension == ".ascx" || |
| 161 | + f.Extension == ".resx" || |
| 162 | + f.Extension == ".js" || |
| 163 | + f.Extension == ".png" || |
| 164 | + f.Extension == ".css" || |
| 165 | + f.Directory.ToString().Contains("Templates")); |
| 166 | + Helpers.AddFilesToZip(StagingDirectory / "Symbols.zip", SymbolFiles); |
| 167 | + InstallFiles.ForEach(i => CopyFileToDirectory(i, StagingDirectory)); |
| 168 | + BinaryFiles.ForEach(b => CopyFileToDirectory(b, StagingDirectory / "bin")); |
| 169 | + var versionString = ModuleBranch == "main" ? GitVersion.MajorMinorPatch : GitVersion.SemVer; |
| 170 | + ZipFile.CreateFromDirectory(StagingDirectory, ArtifactsDirectory / $"{ModuleName}_{versionString}_install.zip"); |
| 171 | + DeleteDirectory(StagingDirectory); |
| 172 | + if (IsWin && IsInDesktopModules) |
| 173 | + { |
| 174 | + var installFile = GlobFiles(ArtifactsDirectory, "*.zip").FirstOrDefault(); |
| 175 | + var previousFiles = GlobFiles(DnnModuleInstallDirectory, $"{ModuleName}*.*"); |
| 176 | + previousFiles.ForEach(f => DeleteFile(f)); |
| 177 | + CopyFileToDirectory(installFile, DnnModuleInstallDirectory); |
| 178 | + } |
| 179 | + }); |
| 180 | + |
| 181 | + Target Deploy => _ => _ |
| 182 | + .OnlyWhenDynamic(() => IsInDesktopModules) |
| 183 | + .DependsOn(Compile) |
| 184 | + .Executes(() => |
| 185 | + { |
| 186 | + var excludedFolders = new string[] |
| 187 | + { |
| 188 | + ".git", |
| 189 | + ".tmp", |
| 190 | + ".vs", |
| 191 | + "Artifacts", |
| 192 | + "bin", |
| 193 | + "build", |
| 194 | + "Components", |
| 195 | + "MigrationBackup", |
| 196 | + "obj", |
| 197 | + "Properties", |
| 198 | + }; |
| 199 | + |
| 200 | + var excludedExtensions = new string[] |
| 201 | + { |
| 202 | + ".gitignore", |
| 203 | + ".nuke", |
| 204 | + ".config", |
| 205 | + ".cmd", |
| 206 | + ".cs", |
| 207 | + ".ps1", |
| 208 | + ".sh", |
| 209 | + ".csproj", |
| 210 | + ".user", |
| 211 | + ".sln", |
| 212 | + ".dnn", |
| 213 | + ".md", |
| 214 | + ".json", |
| 215 | + }; |
| 216 | + |
| 217 | + CopyDirectoryRecursively( |
| 218 | + source: RootDirectory, |
| 219 | + target: DeployDirectory, |
| 220 | + directoryPolicy: DirectoryExistsPolicy.Merge, |
| 221 | + filePolicy: FileExistsPolicy.OverwriteIfNewer, |
| 222 | + excludeDirectory: d => excludedFolders.Contains(d.Name), |
| 223 | + excludeFile: f => excludedExtensions.Contains(f.Extension)); |
| 224 | + }); |
| 225 | +} |
0 commit comments