-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathProgram.cs
More file actions
126 lines (111 loc) · 4.81 KB
/
Copy pathProgram.cs
File metadata and controls
126 lines (111 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
using WixSharp;
using WixSharp.CommonTasks;
using File = System.IO.File;
namespace QuickMsiBuilder.CLI
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: QuickMsiBuilder.CLI <dll_path> [version] [author] [description] [icon_path] [bg_image_path] [revit_year] [full_class_name]");
return;
}
string dllPath = Path.GetFullPath(args[0]);
string version = args.Length > 1 ? args[1] : "1.0.0";
string author = args.Length > 2 ? args[2] : "Autodesk";
string description = args.Length > 3 ? args[3] : "Revit Add-in";
string iconPath = args.Length > 4 ? args[4] : "";
string bgImagePath = args.Length > 5 ? args[5] : "";
string revitYear = args.Length > 6 ? args[6] : "2024";
string fullClassName = args.Length > 7 ? args[7] : "";
if (!File.Exists(dllPath))
{
Console.WriteLine($"Error: DLL file not found at {dllPath}");
return;
}
try
{
BuildMsi(dllPath, version, author, description, iconPath, bgImagePath, revitYear, fullClassName);
}
catch (Exception ex)
{
Console.WriteLine($"Error building MSI: {ex.Message}");
}
}
static void BuildMsi(string dllPath, string version, string author, string description, string iconPath, string bgImagePath, string revitYear, string fullClassName)
{
string assemblyName = Path.GetFileNameWithoutExtension(dllPath);
string assemblyDir = Path.GetDirectoryName(dllPath);
string outputDir = Path.Combine(assemblyDir, "InstallerOutput");
Directory.CreateDirectory(outputDir);
string addinFilePath = Path.Combine(outputDir, assemblyName + ".addin");
GenerateAddinManifest(addinFilePath, assemblyName, author, description, fullClassName);
string installDir = $@"%AppDataFolder%\Autodesk\Revit\Addins\{revitYear}";
// Stable UpgradeCode based on assembly name
Guid upgradeCode = GenerateGuidFromName(assemblyName);
var project = new Project
{
Name = assemblyName,
OutDir = outputDir,
Platform = Platform.x64,
Description = description,
UI = WUI.WixUI_InstallDir,
Version = new Version(version),
OutFileName = $"{assemblyName}-{version}",
InstallScope = InstallScope.perUser,
MajorUpgrade = MajorUpgrade.Default,
GUID = upgradeCode,
ControlPanelInfo =
{
Manufacturer = author,
Comments = description
},
Dirs = new Dir[]
{
new InstallDir(installDir,
new WixSharp.File(dllPath),
new WixSharp.File(addinFilePath)
)
}
};
if (File.Exists(iconPath)) project.ControlPanelInfo.ProductIcon = iconPath;
if (File.Exists(bgImagePath)) project.BackgroundImage = bgImagePath;
Compiler.BuildMsi(project);
Console.WriteLine("MSI build process completed.");
}
static void GenerateAddinManifest(string filePath, string assemblyName, string author, string description, string fullClassName)
{
if (string.IsNullOrEmpty(fullClassName)) fullClassName = assemblyName + ".Command";
XNamespace ns = "http://www.autodesk.com/revit/2009/addin";
XElement root = new XElement(ns + "RevitAddIns",
new XElement(ns + "AddIn", new XAttribute("Type", "Application"),
new XElement(ns + "Text", assemblyName),
new XElement(ns + "Description", description),
new XElement(ns + "Assembly", assemblyName + ".dll"),
new XElement(ns + "FullClassName", fullClassName),
new XElement(ns + "ClientId", Guid.NewGuid().ToString()),
new XElement(ns + "VendorId", "ADSK"),
new XElement(ns + "VendorDescription", author)
)
);
root.Save(filePath);
}
static Guid GenerateGuidFromName(string name)
{
using (MD5 md5 = MD5.Create())
{
byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(name));
return new Guid(hash);
}
}
}
}