|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using System.Xml.Linq; |
| 5 | +using System.Diagnostics; |
| 6 | + |
| 7 | +namespace QuickMsiBuilder.CLI |
| 8 | +{ |
| 9 | + class Program |
| 10 | + { |
| 11 | + static void Main(string[] args) |
| 12 | + { |
| 13 | + if (args.Length < 1) |
| 14 | + { |
| 15 | + Console.WriteLine("Usage: QuickMsiBuilder.CLI <dll_path> [version] [author] [description] [icon_path] [bg_image_path] [revit_year]"); |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + string dllPath = Path.GetFullPath(args[0]); |
| 20 | + string version = args.Length > 1 ? args[1] : "1.0.0"; |
| 21 | + string author = args.Length > 2 ? args[2] : "Autodesk"; |
| 22 | + string description = args.Length > 3 ? args[3] : "Revit Add-in"; |
| 23 | + string iconPath = args.Length > 4 ? args[4] : ""; |
| 24 | + string bgImagePath = args.Length > 5 ? args[5] : ""; |
| 25 | + string revitYear = args.Length > 6 ? args[6] : "2024"; |
| 26 | + |
| 27 | + if (!File.Exists(dllPath)) |
| 28 | + { |
| 29 | + Console.WriteLine($"Error: DLL file not found at {dllPath}"); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + try |
| 34 | + { |
| 35 | + BuildMsi(dllPath, version, author, description, iconPath, bgImagePath, revitYear); |
| 36 | + } |
| 37 | + catch (Exception ex) |
| 38 | + { |
| 39 | + Console.WriteLine($"Error building MSI: {ex.Message}"); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + static void BuildMsi(string dllPath, string version, string author, string description, string iconPath, string bgImagePath, string revitYear) |
| 44 | + { |
| 45 | + string assemblyName = Path.GetFileNameWithoutExtension(dllPath); |
| 46 | + string assemblyDir = Path.GetDirectoryName(dllPath); |
| 47 | + string outputDir = Path.Combine(assemblyDir, "InstallerOutput"); |
| 48 | + Directory.CreateDirectory(outputDir); |
| 49 | + |
| 50 | + string addinFilePath = Path.Combine(outputDir, assemblyName + ".addin"); |
| 51 | + GenerateAddinManifest(addinFilePath, assemblyName, author, description); |
| 52 | + |
| 53 | + Console.WriteLine("Generating Wix Source..."); |
| 54 | + string wxsPath = Path.Combine(outputDir, assemblyName + ".wxs"); |
| 55 | + string installDir = $@"AppDataFolder\Autodesk\Revit\Addins\{revitYear}"; |
| 56 | + |
| 57 | + string wxsContent = $@"<?xml version='1.0' encoding='UTF-8'?> |
| 58 | +<Wix xmlns='http://schemas.microsoft.com/wix/2006/wi'> |
| 59 | + <Product Id='*' Name='{assemblyName}' Language='1033' Version='{version}' Manufacturer='{author}' UpgradeCode='{Guid.NewGuid()}'> |
| 60 | + <Package InstallerVersion='200' Compressed='yes' InstallScope='perUser' /> |
| 61 | + <MajorUpgrade DowngradeErrorMessage='A newer version of [ProductName] is already installed.' /> |
| 62 | + <MediaTemplate EmbedCab='yes' /> |
| 63 | +
|
| 64 | + <Feature Id='ProductFeature' Title='{assemblyName}' Level='1'> |
| 65 | + <ComponentGroupRef Id='ProductComponents' /> |
| 66 | + </Feature> |
| 67 | +
|
| 68 | + <Icon Id='ProductIcon' SourceFile='{(File.Exists(iconPath) ? iconPath : "")}' /> |
| 69 | + <Property Id='ARPPRODUCTICON' Value='ProductIcon' /> |
| 70 | + </Product> |
| 71 | +
|
| 72 | + <Fragment> |
| 73 | + <Directory Id='TARGETDIR' Name='SourceDir'> |
| 74 | + <Directory Id='AppDataFolder'> |
| 75 | + <Directory Id='AutodeskDir' Name='Autodesk'> |
| 76 | + <Directory Id='RevitDir' Name='Revit'> |
| 77 | + <Directory Id='AddinsDir' Name='Addins'> |
| 78 | + <Directory Id='INSTALLFOLDER' Name='{revitYear}' /> |
| 79 | + </Directory> |
| 80 | + </Directory> |
| 81 | + </Directory> |
| 82 | + </Directory> |
| 83 | + </Directory> |
| 84 | + </Fragment> |
| 85 | +
|
| 86 | + <Fragment> |
| 87 | + <ComponentGroup Id='ProductComponents' Directory='INSTALLFOLDER'> |
| 88 | + <Component Id='MainDll' Guid='{Guid.NewGuid()}'> |
| 89 | + <File Source='{dllPath}' KeyPath='yes' /> |
| 90 | + </Component> |
| 91 | + <Component Id='AddinManifest' Guid='{Guid.NewGuid()}'> |
| 92 | + <File Source='{addinFilePath}' KeyPath='yes' /> |
| 93 | + </Component> |
| 94 | + </ComponentGroup> |
| 95 | + </Fragment> |
| 96 | +</Wix>"; |
| 97 | + File.WriteAllText(wxsPath, wxsContent); |
| 98 | + |
| 99 | + Console.WriteLine($"Wxs generated: {wxsPath}"); |
| 100 | + Console.WriteLine("In a production environment, candle.exe and light.exe would be called here."); |
| 101 | + } |
| 102 | + |
| 103 | + static void GenerateAddinManifest(string filePath, string assemblyName, string author, string description) |
| 104 | + { |
| 105 | + XNamespace ns = "http://www.autodesk.com/revit/2009/addin"; |
| 106 | + XElement root = new XElement(ns + "RevitAddIns", |
| 107 | + new XElement(ns + "AddIn", new XAttribute("Type", "Command"), |
| 108 | + new XElement(ns + "Text", assemblyName), |
| 109 | + new XElement(ns + "Description", description), |
| 110 | + new XElement(ns + "Assembly", assemblyName + ".dll"), |
| 111 | + new XElement(ns + "FullClassName", assemblyName + ".Command"), |
| 112 | + new XElement(ns + "ClientId", Guid.NewGuid().ToString()), |
| 113 | + new XElement(ns + "VendorId", "ADSK"), |
| 114 | + new XElement(ns + "VendorDescription", author) |
| 115 | + ) |
| 116 | + ); |
| 117 | + |
| 118 | + root.Save(filePath); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments