public IOfficeInstallReturn Generate(IOfficeInstallProperties installProperties, string remoteLogPath = "")
        {
            var msiPath = installProperties.ExecutablePath;
            var exePath = Path.GetDirectoryName(installProperties.ExecutablePath) + @"\InstallOfficeProPlus.exe";
            try
            {
                var tmpDir = Environment.ExpandEnvironmentVariables(@"%temp%");

                var wixDirectory = tmpDir + @"\wixTools";
                var wixZip = ZipExtractor.AssemblyDirectory + @"\wixTools.zip";
                if (!File.Exists(wixZip))
                {
                    var projectPath = Directory.GetCurrentDirectory() + @"\Project\wixTools.zip";
                    if (File.Exists(projectPath))
                    {
                        wixZip = projectPath;
                    }
                }
                
                if (!Directory.Exists(wixDirectory))
                {
                    ZipExtractor.Extract(wixZip, tmpDir);
                }

                var exeGenerator = new OfficeInstallExecutableGenerator();
                installProperties.ExecutablePath = exePath;

                string version = null;
                if (installProperties.Version != null)
                {
                    version = installProperties.Version.ToString();
                }

                var exeReturn = exeGenerator.Generate(installProperties, remoteLogPath);
                var exeFilePath = exeReturn.GeneratedFilePath;

                var msiCreatePath = Regex.Replace(msiPath, ".msi$", "", RegexOptions.IgnoreCase);

                var msiInstallProperties = new MsiGeneratorProperties()
                {
                    MsiPath = msiCreatePath,
                    ExecutablePath = exePath,
                    Manufacturer = "Microsoft Corporation",
                    Name = installProperties.ProductName,
                    ProgramFilesPath = installProperties.ProgramFilesPath,
                    ProgramFiles = new List<string>()
                    {
                        installProperties.ConfigurationXmlPath
                    },
                    ProductId = new Guid(installProperties.ProductId),
                    WixToolsPath = wixDirectory,
                    Version = installProperties.Version,
                    UpgradeCode = new Guid(installProperties.UpgradeCode),
                    Language = installProperties.Language,
                    SourceFilePath = installProperties.SourceFilePath
                };
                var msiGenerator = new MsiGenerator();
                msiGenerator.Generate(msiInstallProperties);

                var installDirectory = new OfficeInstallReturn
                {
                    GeneratedFilePath = msiPath
                };

                return installDirectory;
            }
            finally
            {
                try
                {
                    if (File.Exists(exePath))
                    {
                        File.Delete(exePath);
                    }
                }
                catch { }
            }
        }
Exemplo n.º 2
0
        public IOfficeInstallReturn Generate(IOfficeInstallProperties installProperties, string remoteLogPath = "")
        {
            var currentDirectory = Directory.GetCurrentDirectory();
            var embededExeFiles  = new List <string>();

            try
            {
                if (Directory.Exists(currentDirectory + @"\Project"))
                {
                    currentDirectory = currentDirectory + @"\Project";
                }

                var codeProvider = new CSharpCodeProvider();
                var icc          = codeProvider.CreateCompiler();

                var tmpPath = Environment.ExpandEnvironmentVariables("%temp%");
                var output  = currentDirectory + @"\InstallOffice365ProPlus.exe";

                if (!string.IsNullOrEmpty(installProperties.ExecutablePath))
                {
                    output = installProperties.ExecutablePath;
                }

                var parameters = new CompilerParameters
                {
                    GenerateExecutable = true,
                    OutputAssembly     = output
                };
                parameters.ReferencedAssemblies.Add("System.dll");
                parameters.ReferencedAssemblies.Add("System.Xml.dll");
                parameters.ReferencedAssemblies.Add("System.Core.dll");
                parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
                parameters.ReferencedAssemblies.Add("Microsoft.CSharp.dll");
                parameters.ReferencedAssemblies.Add("System.Management.dll");

                embededExeFiles = EmbeddedResources.GetEmbeddedItems(currentDirectory, @"\.exe$");

                File.Copy(installProperties.ConfigurationXmlPath, tmpPath + @"\configuration.xml", true);

                var productIdPath = tmpPath + @"\productid.txt";
                var remoteLog     = tmpPath + @"\RemoteLog.txt";
                File.WriteAllText(productIdPath, installProperties.ProductId);
                File.WriteAllText(remoteLog, remoteLogPath);

                parameters.EmbeddedResources.Add(tmpPath + @"\configuration.xml");
                parameters.EmbeddedResources.Add(productIdPath);
                parameters.EmbeddedResources.Add(remoteLog);

                // parameters.EmbeddedResources.Add(@"\tools\");

                var office2013Setup = DirectoryHelper.GetCurrentDirectoryFilePath("Office2013Setup.exe");
                var office2016Setup = DirectoryHelper.GetCurrentDirectoryFilePath("Office2016Setup.exe");

                parameters.EmbeddedResources.Add(installProperties.OfficeVersion == OfficeVersion.Office2013
                    ? office2013Setup
                    : office2016Setup);

                var installOfficeFp = DirectoryHelper.GetCurrentDirectoryFilePath("InstallOffice.cs");

                var fileContents = File.ReadAllText(installOfficeFp);
                fileContents = fileContents.Replace("public static void Main1(string[] args)",
                                                    "public static void Main(string[] args)");

                var configXml = new ConfigXmlParser(tmpPath + @"\configuration.xml");
                var addNode   = configXml.ConfigurationXml.Add;
                if (addNode != null && addNode.Version != null)
                {
                    fileContents = fileContents.Replace("//[assembly: AssemblyVersion(\"\")]",
                                                        "[assembly: AssemblyVersion(\"" + addNode.Version + "\")]");
                }

                if (configXml.ConfigurationXml.Logging == null)
                {
                    configXml.ConfigurationXml.Logging = new ODTLogging();
                }

                configXml.ConfigurationXml.Logging.Level = LoggingLevel.Standard;
                configXml.ConfigurationXml.Logging.Path  = "%temp%";

                if (installProperties.SourceFilePath != null)
                {
                    if (!Directory.Exists(installProperties.SourceFilePath + @"\Office"))
                    {
                        throw (new DirectoryNotFoundException("Invalid Source Path: " + installProperties.SourceFilePath));
                    }

                    //commenting out, trying to go a different path with this, copy out the source files to the same dir as exe file
                    //possibly only copy out if contents of source file greater than 1.5 GB
                    long embeddedFileSize = CalcSize(parameters, installProperties.SourceFilePath + @"\Office", installProperties.BuildVersion, installProperties.OfficeClientEdition);

                    //find file size of embedded files, if less than 1.5 GB, then embed, if greater, copy out as separate folder in same dir as the MSI or exe file
                    if (embeddedFileSize < 1900000000)
                    {
                        EmbedSourceFiles(parameters, installProperties.SourceFilePath + @"\Office",
                                         installProperties.BuildVersion, installProperties.OfficeClientEdition);
                    }
                    else
                    {
                        CopyFolder(new DirectoryInfo(installProperties.SourceFilePath), new DirectoryInfo(installProperties.ExecutablePath.Substring(0, installProperties.ExecutablePath.LastIndexOf(@"\"))));
                    }
                }

                if (installProperties.OfficeVersion == OfficeVersion.Office2013)
                {
                    fileContents = fileContents.Replace("//[assembly: AssemblyTitle(\"\")]",
                                                        "[assembly: AssemblyTitle(\"" + "Office 365 ProPlus (2013)" + "\")]");
                    fileContents = fileContents.Replace("//[assembly: AssemblyDescription(\"\")]",
                                                        "[assembly: AssemblyDescription(\"" + "Office 365 ProPlus (2013)" + "\")]");
                }

                if (installProperties.OfficeVersion == OfficeVersion.Office2016)
                {
                    fileContents = fileContents.Replace("//[assembly: AssemblyTitle(\"\")]",
                                                        "[assembly: AssemblyTitle(\"" + "Office 365 ProPlus (2016)" + "\")]");
                    fileContents = fileContents.Replace("//[assembly: AssemblyDescription(\"\")]",
                                                        "[assembly: AssemblyDescription(\"" + "Office 365 ProPlus (2016)" + "\")]");
                }

                var results = icc.CompileAssemblyFromSource(parameters, fileContents);
                //delete temp files
                foreach (var fileMarkedForDelete in filesMarkedForDelete)
                {
                    fileMarkedForDelete.Delete();
                }

                if (results.Errors.Count > 0)
                {
                    var strBuilder = new StringBuilder();
                    foreach (CompilerError CompErr in results.Errors)
                    {
                        var errorText = "Line number " + CompErr.Line +
                                        ", Error Number: " + CompErr.ErrorNumber +
                                        ", '" + CompErr.ErrorText + ";" +
                                        Environment.NewLine + Environment.NewLine;
                        strBuilder.AppendLine(errorText);
                    }
                    throw (new Exception(strBuilder.ToString()));
                }

                return(new OfficeInstallReturn()
                {
                    GeneratedFilePath = output
                });
            }
            finally
            {
                foreach (var fileName in embededExeFiles)
                {
                    if (File.Exists(currentDirectory + @"\" + fileName))
                    {
                        File.Delete(currentDirectory + @"\" + fileName);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public IOfficeInstallReturn Generate(IOfficeInstallProperties installProperties, string remoteLogPath = "")
        {
            var msiPath = installProperties.ExecutablePath;
            var exePath = Path.GetDirectoryName(installProperties.ExecutablePath) + @"\InstallOfficeProPlus.exe";

            try
            {
                var tmpDir = Environment.ExpandEnvironmentVariables(@"%temp%");

                var wixDirectory = tmpDir + @"\wixTools";
                var wixZip       = ZipExtractor.AssemblyDirectory + @"\wixTools.zip";
                if (!File.Exists(wixZip))
                {
                    var projectPath = Directory.GetCurrentDirectory() + @"\Project\wixTools.zip";
                    if (File.Exists(projectPath))
                    {
                        wixZip = projectPath;
                    }
                }

                if (!Directory.Exists(wixDirectory))
                {
                    ZipExtractor.Extract(wixZip, tmpDir);
                }

                var exeGenerator = new OfficeInstallExecutableGenerator();
                installProperties.ExecutablePath = exePath;

                string version = null;
                if (installProperties.Version != null)
                {
                    version = installProperties.Version.ToString();
                }

                var exeReturn   = exeGenerator.Generate(installProperties, remoteLogPath);
                var exeFilePath = exeReturn.GeneratedFilePath;

                var msiCreatePath = Regex.Replace(msiPath, ".msi$", "", RegexOptions.IgnoreCase);

                var msiInstallProperties = new MsiGeneratorProperties()
                {
                    MsiPath          = msiCreatePath,
                    ExecutablePath   = exePath,
                    Manufacturer     = "Microsoft Corporation",
                    Name             = installProperties.ProductName,
                    ProgramFilesPath = installProperties.ProgramFilesPath,
                    ProgramFiles     = new List <string>()
                    {
                        installProperties.ConfigurationXmlPath
                    },
                    ProductId      = new Guid(installProperties.ProductId),
                    WixToolsPath   = wixDirectory,
                    Version        = installProperties.Version,
                    UpgradeCode    = new Guid(installProperties.UpgradeCode),
                    Language       = installProperties.Language,
                    SourceFilePath = installProperties.SourceFilePath
                };
                var msiGenerator = new MsiGenerator();
                msiGenerator.Generate(msiInstallProperties);

                var installDirectory = new OfficeInstallReturn
                {
                    GeneratedFilePath = msiPath
                };

                return(installDirectory);
            }
            finally
            {
                try
                {
                    if (File.Exists(exePath))
                    {
                        File.Delete(exePath);
                    }
                }
                catch { }
            }
        }
        public IOfficeInstallReturn Generate(IOfficeInstallProperties installProperties, string remoteLogPath = "")
        {
            var currentDirectory = Directory.GetCurrentDirectory();
            var embededExeFiles = new List<string>();
            try
            {
                if (Directory.Exists(currentDirectory + @"\Project"))
                {
                    currentDirectory = currentDirectory + @"\Project";
                }

                var codeProvider = new CSharpCodeProvider();
                var icc = codeProvider.CreateCompiler();

                var tmpPath = Environment.ExpandEnvironmentVariables("%temp%");
                var output = currentDirectory + @"\InstallOffice365ProPlus.exe";

                if (!string.IsNullOrEmpty(installProperties.ExecutablePath))
                {
                    output = installProperties.ExecutablePath;
                }

                var parameters = new CompilerParameters
                {
                    GenerateExecutable = true,
                    OutputAssembly = output
                };
                parameters.ReferencedAssemblies.Add("System.dll");
                parameters.ReferencedAssemblies.Add("System.Xml.dll");
                parameters.ReferencedAssemblies.Add("System.Core.dll");
                parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
                parameters.ReferencedAssemblies.Add("Microsoft.CSharp.dll");
                parameters.ReferencedAssemblies.Add("System.Management.dll");

                embededExeFiles = EmbeddedResources.GetEmbeddedItems(currentDirectory, @"\.exe$");

                File.Copy(installProperties.ConfigurationXmlPath, tmpPath + @"\configuration.xml", true);

                var productIdPath = tmpPath + @"\productid.txt";
                var remoteLog = tmpPath + @"\RemoteLog.txt";
                File.WriteAllText(productIdPath, installProperties.ProductId);
                File.WriteAllText(remoteLog, remoteLogPath);

                parameters.EmbeddedResources.Add(tmpPath + @"\configuration.xml");
                parameters.EmbeddedResources.Add(productIdPath);
                parameters.EmbeddedResources.Add(remoteLog);

                // parameters.EmbeddedResources.Add(@"\tools\");

                var office2013Setup = DirectoryHelper.GetCurrentDirectoryFilePath("Office2013Setup.exe");
                var office2016Setup = DirectoryHelper.GetCurrentDirectoryFilePath("Office2016Setup.exe");

                parameters.EmbeddedResources.Add(installProperties.OfficeVersion == OfficeVersion.Office2013
                    ? office2013Setup
                    : office2016Setup);

                var installOfficeFp = DirectoryHelper.GetCurrentDirectoryFilePath("InstallOffice.cs");

                var fileContents = File.ReadAllText(installOfficeFp);
                fileContents = fileContents.Replace("public static void Main1(string[] args)",
                    "public static void Main(string[] args)");

                var configXml = new ConfigXmlParser(tmpPath + @"\configuration.xml");
                var addNode = configXml.ConfigurationXml.Add;
                if (addNode != null && addNode.Version != null)
                {
                    fileContents = fileContents.Replace("//[assembly: AssemblyVersion(\"\")]",
                         "[assembly: AssemblyVersion(\"" + addNode.Version + "\")]");
                }

                if (configXml.ConfigurationXml.Logging == null)
                {
                    configXml.ConfigurationXml.Logging = new ODTLogging();
                }

                configXml.ConfigurationXml.Logging.Level = LoggingLevel.Standard;
                configXml.ConfigurationXml.Logging.Path = "%temp%";

                if (installProperties.SourceFilePath != null)
                {
                    if (!Directory.Exists(installProperties.SourceFilePath + @"\Office"))
                    {
                        throw (new DirectoryNotFoundException("Invalid Source Path: " + installProperties.SourceFilePath));
                    }

                    //commenting out, trying to go a different path with this, copy out the source files to the same dir as exe file
                    //possibly only copy out if contents of source file greater than 1.5 GB
                    long embeddedFileSize = CalcSize(parameters, installProperties.SourceFilePath + @"\Office", installProperties.BuildVersion, installProperties.OfficeClientEdition);
                    
                    //find file size of embedded files, if less than 1.5 GB, then embed, if greater, copy out as separate folder in same dir as the MSI or exe file
                    if (embeddedFileSize < 1900000000)
                    {
                        EmbedSourceFiles(parameters, installProperties.SourceFilePath + @"\Office",
                            installProperties.BuildVersion, installProperties.OfficeClientEdition);
                    }
                    else
                    {
                        CopyFolder(new DirectoryInfo(installProperties.SourceFilePath), new DirectoryInfo(installProperties.ExecutablePath.Substring(0, installProperties.ExecutablePath.LastIndexOf(@"\"))));
                    }
                    
                }

                if (installProperties.OfficeVersion == OfficeVersion.Office2013)
                {
                    fileContents = fileContents.Replace("//[assembly: AssemblyTitle(\"\")]",
                        "[assembly: AssemblyTitle(\"" + "Office 365 ProPlus (2013)" + "\")]");
                    fileContents = fileContents.Replace("//[assembly: AssemblyDescription(\"\")]",
                        "[assembly: AssemblyDescription(\"" + "Office 365 ProPlus (2013)" + "\")]");
                }

                if (installProperties.OfficeVersion == OfficeVersion.Office2016)
                {
                    fileContents = fileContents.Replace("//[assembly: AssemblyTitle(\"\")]",
                        "[assembly: AssemblyTitle(\"" + "Office 365 ProPlus (2016)" + "\")]");
                    fileContents = fileContents.Replace("//[assembly: AssemblyDescription(\"\")]",
                        "[assembly: AssemblyDescription(\"" + "Office 365 ProPlus (2016)" + "\")]");
                }
                
                var results = icc.CompileAssemblyFromSource(parameters, fileContents);
                //delete temp files
                foreach (var fileMarkedForDelete in filesMarkedForDelete)
                {
                    fileMarkedForDelete.Delete();
                }

                if (results.Errors.Count > 0)
                {
                    var strBuilder = new StringBuilder();
                    foreach (CompilerError CompErr in results.Errors)
                    {
                        var errorText = "Line number " + CompErr.Line +
                                        ", Error Number: " + CompErr.ErrorNumber +
                                        ", '" + CompErr.ErrorText + ";" +
                                        Environment.NewLine + Environment.NewLine;
                        strBuilder.AppendLine(errorText);
                    }
                    throw (new Exception(strBuilder.ToString()));
                }

                return new OfficeInstallReturn()
                {
                    GeneratedFilePath = output
                };
            }
            finally
            {
                foreach (var fileName in embededExeFiles)
                {
                    if (File.Exists(currentDirectory + @"\" + fileName))
                    {
                        File.Delete(currentDirectory + @"\" + fileName);
                    }
                }
            }
        }