コード例 #1
0
        /// <summary>
        /// Main running method for the application.
        /// </summary>
        /// <param name="args">Commandline arguments to the application.</param>
        /// <returns>Returns the application error code.</returns>
        private int Run(string[] args)
        {
            Linker            linker     = null;
            Localizer         localizer  = null;
            SectionCollection sections   = new SectionCollection();
            ArrayList         transforms = new ArrayList();

            try
            {
                // parse the command line
                this.ParseCommandLine(args);

                // load any extensions
                List <WixExtension> loadedExtensionList = new List <WixExtension>();
                foreach (string extension in this.extensionList)
                {
                    WixExtension wixExtension = WixExtension.Load(extension);
                    loadedExtensionList.Add(wixExtension);

                    // If the extension provides a binder, use that now if it
                    // matches the class from the command line.
                    if (null != wixExtension.CustomBinder && null != this.binderClass && wixExtension.CustomBinder.GetType().Name.Equals(this.binderClass, StringComparison.Ordinal))
                    {
                        this.binder = wixExtension.CustomBinder;
                    }
                }

                // If a binder was specified, but not found then show an error.
                if (!String.IsNullOrEmpty(this.binderClass) && null == this.binder)
                {
                    throw new WixException(WixErrors.SpecifiedBinderNotFound(this.binderClass));
                }

                // create the linker, binder, and validator
                linker = new Linker();
                if (null == this.binder)
                {
                    this.binder = new Microsoft.Tools.WindowsInstallerXml.Binder();
                }

                // have the binder parse the command line arguments light did not recognize
                string[] unparsedArgsArray = new string[this.unparsedArgs.Count];
                this.unparsedArgs.CopyTo(unparsedArgsArray, 0);
                StringCollection remainingArgs = this.binder.ParseCommandLine(unparsedArgsArray, this.messageHandler);

                // Loop through the extensions to give them a shot at processing the remaining command-line args.
                foreach (WixExtension wixExtension in loadedExtensionList)
                {
                    if (0 == remainingArgs.Count)
                    {
                        break;
                    }

                    remainingArgs = wixExtension.ParseCommandLine(remainingArgs, this.messageHandler);
                }

                this.ParseCommandLinePassTwo(remainingArgs);

                // exit if there was an error parsing the command line (otherwise the logo appears after error messages)
                if (this.messageHandler.EncounteredError)
                {
                    return(this.messageHandler.LastErrorNumber);
                }

                foreach (string parameter in this.invalidArgs)
                {
                    this.messageHandler.Display(this, WixWarnings.UnsupportedCommandLineArgument(parameter));
                }

                this.invalidArgs = null;

                // exit if there was an error parsing the command line (otherwise the logo appears after error messages)
                if (this.messageHandler.EncounteredError)
                {
                    return(this.messageHandler.LastErrorNumber);
                }

                if (0 == this.inputFiles.Count)
                {
                    this.showHelp = true;
                }
                else if (null == this.outputFile)
                {
                    if (1 < this.inputFiles.Count)
                    {
                        throw new WixException(WixErrors.MustSpecifyOutputWithMoreThanOneInput());
                    }

                    this.outputFile = Path.ChangeExtension(Path.GetFileName(this.inputFiles[0]), ".wix"); // we'll let the linker change the extension later
                }

                this.binder.OutputFile = this.outputFile;
                this.binder.PostParseCommandLine();

                if (this.showLogo)
                {
                    AppCommon.DisplayToolHeader();
                }

                if (this.showHelp)
                {
                    this.PrintHelp();
                    AppCommon.DisplayToolFooter();
                    return(this.messageHandler.LastErrorNumber);
                }

                linker.AllowIdenticalRows        = this.allowIdenticalRows;
                linker.AllowUnresolvedReferences = this.allowUnresolvedReferences;
                linker.Cultures = this.cultures;
                linker.UnreferencedSymbolsFile  = this.unreferencedSymbolsFile;
                linker.ShowPedanticMessages     = this.showPedanticMessages;
                linker.DropUnrealTables         = this.dropUnrealTables;
                linker.SuppressLocalization     = this.suppressLocalization;
                linker.SuppressMsiAssemblyTable = this.suppressMsiAssemblyTable;
                linker.WixVariableResolver      = this.wixVariableResolver;

                // set the sequence suppression options
                linker.SuppressAdminSequence     = this.suppressAdminSequence;
                linker.SuppressAdvertiseSequence = this.suppressAdvertiseSequence;
                linker.SuppressUISequence        = this.suppressUISequence;

                linker.SectionIdOnRows = this.sectionIdOnRows;

                this.binder.TempFilesLocation   = Environment.GetEnvironmentVariable("WIX_TEMP");
                this.binder.WixVariableResolver = this.wixVariableResolver;

                if (null != this.bindPaths)
                {
                    foreach (string bindPath in this.bindPaths)
                    {
                        if (-1 == bindPath.IndexOf('='))
                        {
                            this.sourcePaths.Add(bindPath);
                        }
                    }
                }

                // instantiate the localizer and load any localization files
                if (!this.suppressLocalization || 0 < this.localizationFiles.Count || null != this.cultures || !this.outputXml)
                {
                    List <Localization> localizations = new List <Localization>();
                    localizer = new Localizer();

                    localizer.Message += new MessageEventHandler(this.messageHandler.Display);

                    // load each localization file
                    foreach (string localizationFile in this.localizationFiles)
                    {
                        Localization localization = Localization.Load(localizationFile, linker.TableDefinitions, this.suppressSchema);
                        localizations.Add(localization);
                    }

                    if (null != this.cultures)
                    {
                        // add localizations in order specified in cultures
                        foreach (string culture in this.cultures)
                        {
                            foreach (Localization localization in localizations)
                            {
                                if (culture.Equals(localization.Culture, StringComparison.OrdinalIgnoreCase))
                                {
                                    localizer.AddLocalization(localization);
                                }
                            }
                        }
                    }
                    else
                    {
                        bool neutralFound = false;
                        foreach (Localization localization in localizations)
                        {
                            if (0 == localization.Culture.Length)
                            {
                                // if a neutral wxl was provided use it
                                localizer.AddLocalization(localization);
                                neutralFound = true;
                            }
                        }

                        if (!neutralFound)
                        {
                            // cultures wasn't specified and no neutral wxl are available, include all of the files
                            foreach (Localization localization in localizations)
                            {
                                localizer.AddLocalization(localization);
                            }
                        }
                    }

                    // immediately stop processing if any errors were found
                    if (this.messageHandler.EncounteredError)
                    {
                        return(this.messageHandler.LastErrorNumber);
                    }

                    // tell all of the objects about the localizer
                    linker.Localizer      = localizer;
                    this.binder.Localizer = localizer;
                    this.wixVariableResolver.Localizer = localizer;
                }

                // process loaded extensions
                foreach (WixExtension wixExtension in loadedExtensionList)
                {
                    linker.AddExtension(wixExtension);
                    this.binder.AddExtension(wixExtension);

                    // load the extension's localizations
                    Library library = wixExtension.GetLibrary(linker.TableDefinitions);
                    if (null != library)
                    {
                        // load the extension's default culture if it provides one and we don't specify any cultures
                        string[] extensionCultures = this.cultures;
                        if (null == extensionCultures && null != wixExtension.DefaultCulture)
                        {
                            extensionCultures = new string[] { wixExtension.DefaultCulture };
                        }

                        library.GetLocalizations(extensionCultures, localizer);
                    }
                }

                this.binder.ProcessExtensions(loadedExtensionList.ToArray());

                // set the message handlers
                linker.Message += new MessageEventHandler(this.messageHandler.Display);
                this.binder.AddMessageEventHandler(new MessageEventHandler(this.messageHandler.Display));

                Output output = null;

                // loop through all the believed object files
                foreach (string inputFile in this.inputFiles)
                {
                    string dirName           = Path.GetDirectoryName(inputFile);
                    string inputFileFullPath = Path.GetFullPath(inputFile);

                    if (!this.sourcePaths.Contains(dirName))
                    {
                        this.sourcePaths.Add(dirName);
                    }

                    // try loading as an object file
                    try
                    {
                        Intermediate intermediate = Intermediate.Load(inputFileFullPath, linker.TableDefinitions, this.suppressVersionCheck, this.suppressSchema);
                        sections.AddRange(intermediate.Sections);
                        continue; // next file
                    }
                    catch (WixNotIntermediateException)
                    {
                        // try another format
                    }

                    // try loading as a library file
                    try
                    {
                        Library library = Library.Load(inputFileFullPath, linker.TableDefinitions, this.suppressVersionCheck, this.suppressSchema);
                        library.GetLocalizations(this.cultures, localizer);
                        sections.AddRange(library.Sections);
                        continue; // next file
                    }
                    catch (WixNotLibraryException)
                    {
                        // try another format
                    }

                    // try loading as an output file
                    output = Output.Load(inputFileFullPath, this.suppressVersionCheck, this.suppressSchema);
                }

                // immediately stop processing if any errors were found
                if (this.messageHandler.EncounteredError)
                {
                    return(this.messageHandler.LastErrorNumber);
                }

                // set the binder file manager information
                foreach (string bindPath in this.bindPaths)
                {
                    //Checking as IndexOf will return 0 if the string value is String.Empty.
                    if (String.IsNullOrEmpty(bindPath))
                    {
                        continue;
                    }

                    if (-1 == bindPath.IndexOf('='))
                    {
                        this.binder.FileManager.BindPaths.Add(bindPath);
                    }
                    else
                    {
                        string[] namedPair = bindPath.Split('=');

                        //It is ok to have duplicate key.
                        this.binder.FileManager.NamedBindPaths.Add(namedPair[0], namedPair[1]);
                    }
                }

                foreach (string sourcePath in this.sourcePaths)
                {
                    this.binder.FileManager.SourcePaths.Add(sourcePath);
                }

                // and now for the fun part
                if (null == output)
                {
                    OutputType expectedOutputType = OutputType.Unknown;
                    if (this.outputFile != null)
                    {
                        expectedOutputType = Output.GetOutputType(Path.GetExtension(this.outputFile));
                    }

                    output = linker.Link(sections, transforms, expectedOutputType);

                    // if an error occurred during linking, stop processing
                    if (null == output)
                    {
                        return(this.messageHandler.LastErrorNumber);
                    }
                }
                else if (0 != sections.Count)
                {
                    throw new InvalidOperationException(LightStrings.EXP_CannotLinkObjFilesWithOutpuFile);
                }

                // Now that the output object is either linked or loaded, tell the binder file manager about it.
                this.binder.FileManager.Output = output;

                // only output the xml if its a patch build or user specfied to only output wixout
                if (this.outputXml || OutputType.Patch == output.Type)
                {
                    string outputExtension = Path.GetExtension(this.outputFile);
                    if (null == outputExtension || 0 == outputExtension.Length || ".wix" == outputExtension)
                    {
                        if (OutputType.Patch == output.Type)
                        {
                            this.outputFile = Path.ChangeExtension(this.outputFile, ".wixmsp");
                        }
                        else
                        {
                            this.outputFile = Path.ChangeExtension(this.outputFile, ".wixout");
                        }
                    }

                    output.Save(this.outputFile, (this.bindFiles ? this.binder.FileManager : null), this.wixVariableResolver, this.binder.TempFilesLocation);
                }
                else // finish creating the MSI/MSM
                {
                    string outputExtension = Path.GetExtension(this.outputFile);
                    if (null == outputExtension || 0 == outputExtension.Length || ".wix" == outputExtension)
                    {
                        outputExtension = Output.GetExtension(output.Type);
                        this.outputFile = Path.ChangeExtension(this.outputFile, outputExtension);
                    }

                    this.binder.Bind(output, this.outputFile);
                }
            }
            catch (WixException we)
            {
                if (we is WixInvalidIdtException)
                {
                    // make sure the IDT files stay around
                    this.tidy = false;
                }

                this.messageHandler.Display(this, we.Error);
            }
            catch (Exception e)
            {
                // make sure the files stay around for debugging
                this.tidy = false;

                this.messageHandler.Display(this, WixErrors.UnexpectedException(e.Message, e.GetType().ToString(), e.StackTrace));
                if (e is NullReferenceException || e is SEHException)
                {
                    throw;
                }
            }
            finally
            {
                if (null != binder)
                {
                    this.binder.Cleanup(this.tidy);
                }
            }

            return(this.messageHandler.LastErrorNumber);
        }
コード例 #2
0
ファイル: light.cs プロジェクト: Jeremiahf/wix3
        /// <summary>
        /// Main running method for the application.
        /// </summary>
        /// <param name="args">Commandline arguments to the application.</param>
        /// <returns>Returns the application error code.</returns>
        private int Run(string[] args)
        {
            Linker linker = null;
            Localizer localizer = null;
            SectionCollection sections = new SectionCollection();
            ArrayList transforms = new ArrayList();

            try
            {
                // parse the command line
                this.ParseCommandLine(args);

                // load any extensions
                List<WixExtension> loadedExtensionList = new List<WixExtension>();
                foreach (string extension in this.extensionList)
                {
                    WixExtension wixExtension = WixExtension.Load(extension);
                    loadedExtensionList.Add(wixExtension);

                    // If the extension provides a binder, use that now if it
                    // matches the class from the command line.
                    if (null != wixExtension.CustomBinder && null != this.binderClass && wixExtension.CustomBinder.GetType().Name.Equals(this.binderClass, StringComparison.Ordinal))
                    {
                        this.binder = wixExtension.CustomBinder;
                    }
                }

                // If a binder was specified, but not found then show an error.
                if (!String.IsNullOrEmpty(this.binderClass) && null == this.binder)
                {
                    throw new WixException(WixErrors.SpecifiedBinderNotFound(this.binderClass));
                }

                // create the linker, binder, and validator
                linker = new Linker();
                if (null == this.binder)
                {
                    this.binder = new Microsoft.Tools.WindowsInstallerXml.Binder();
                }

                // have the binder parse the command line arguments light did not recognize
                string[] unparsedArgsArray = new string[this.unparsedArgs.Count];
                this.unparsedArgs.CopyTo(unparsedArgsArray, 0);
                StringCollection remainingArgs = this.binder.ParseCommandLine(unparsedArgsArray, this.messageHandler);

                // Loop through the extensions to give them a shot at processing the remaining command-line args.
                foreach (WixExtension wixExtension in loadedExtensionList)
                {
                    if (0 == remainingArgs.Count)
                    {
                        break;
                    }

                    remainingArgs = wixExtension.ParseCommandLine(remainingArgs, this.messageHandler);
                }

                this.ParseCommandLinePassTwo(remainingArgs);

                // exit if there was an error parsing the command line (otherwise the logo appears after error messages)
                if (this.messageHandler.EncounteredError)
                {
                    return this.messageHandler.LastErrorNumber;
                }

                foreach (string parameter in this.invalidArgs)
                {
                    this.messageHandler.Display(this, WixWarnings.UnsupportedCommandLineArgument(parameter));
                }

                this.invalidArgs = null;

                // exit if there was an error parsing the command line (otherwise the logo appears after error messages)
                if (this.messageHandler.EncounteredError)
                {
                    return this.messageHandler.LastErrorNumber;
                }

                if (0 == this.inputFiles.Count)
                {
                    this.showHelp = true;
                }
                else if (null == this.outputFile)
                {
                    if (1 < this.inputFiles.Count)
                    {
                        throw new WixException(WixErrors.MustSpecifyOutputWithMoreThanOneInput());
                    }

                    this.outputFile = Path.ChangeExtension(Path.GetFileName(this.inputFiles[0]), ".wix"); // we'll let the linker change the extension later
                }

                this.binder.OutputFile = this.outputFile;
                this.binder.PostParseCommandLine();

                if (this.showLogo)
                {
                    AppCommon.DisplayToolHeader();
                }

                if (this.showHelp)
                {
                    this.PrintHelp();
                    AppCommon.DisplayToolFooter();
                    return this.messageHandler.LastErrorNumber;
                }

                linker.AllowIdenticalRows = this.allowIdenticalRows;
                linker.AllowUnresolvedReferences = this.allowUnresolvedReferences;
                linker.Cultures = this.cultures;
                linker.UnreferencedSymbolsFile = this.unreferencedSymbolsFile;
                linker.ShowPedanticMessages = this.showPedanticMessages;
                linker.DropUnrealTables = this.dropUnrealTables;
                linker.SuppressLocalization = this.suppressLocalization;
                linker.SuppressMsiAssemblyTable = this.suppressMsiAssemblyTable;
                linker.WixVariableResolver = this.wixVariableResolver;

                // set the sequence suppression options
                linker.SuppressAdminSequence = this.suppressAdminSequence;
                linker.SuppressAdvertiseSequence = this.suppressAdvertiseSequence;
                linker.SuppressUISequence = this.suppressUISequence;

                linker.SectionIdOnRows = this.sectionIdOnRows;

                this.binder.TempFilesLocation = Environment.GetEnvironmentVariable("WIX_TEMP");
                this.binder.WixVariableResolver = this.wixVariableResolver;

                if (null != this.bindPaths)
                {
                    foreach (string bindPath in this.bindPaths)
                    {
                        if (-1 == bindPath.IndexOf('='))
                        {
                            this.sourcePaths.Add(bindPath);
                        }
                    }
                }

                // instantiate the localizer and load any localization files
                if (!this.suppressLocalization || 0 < this.localizationFiles.Count || null != this.cultures || !this.outputXml)
                {
                    List<Localization> localizations = new List<Localization>();
                    localizer = new Localizer();

                    localizer.Message += new MessageEventHandler(this.messageHandler.Display);

                    // load each localization file
                    foreach (string localizationFile in this.localizationFiles)
                    {
                        Localization localization = Localization.Load(localizationFile, linker.TableDefinitions, this.suppressSchema);
                        localizations.Add(localization);
                    }

                    if (null != this.cultures)
                    {
                        // add localizations in order specified in cultures
                        foreach (string culture in this.cultures)
                        {
                            foreach (Localization localization in localizations)
                            {
                                if (culture.Equals(localization.Culture, StringComparison.OrdinalIgnoreCase))
                                {
                                    localizer.AddLocalization(localization);
                                }
                            }
                        }
                    }
                    else 
                    {
                        bool neutralFound = false;
                        foreach (Localization localization in localizations)
                        {
                            if (0 == localization.Culture.Length)
                            {
                                // if a neutral wxl was provided use it
                                localizer.AddLocalization(localization);
                                neutralFound = true;
                            }
                        }

                        if (!neutralFound)
                        {
                            // cultures wasn't specified and no neutral wxl are available, include all of the files
                            foreach (Localization localization in localizations)
                            {
                                localizer.AddLocalization(localization);
                            }
                        }
                    }

                    // immediately stop processing if any errors were found
                    if (this.messageHandler.EncounteredError)
                    {
                        return this.messageHandler.LastErrorNumber;
                    }

                    // tell all of the objects about the localizer
                    linker.Localizer = localizer;
                    this.binder.Localizer = localizer;
                    this.wixVariableResolver.Localizer = localizer;
                }

                // process loaded extensions
                foreach (WixExtension wixExtension in loadedExtensionList)
                {
                    linker.AddExtension(wixExtension);
                    this.binder.AddExtension(wixExtension);

                    // load the extension's localizations
                    Library library = wixExtension.GetLibrary(linker.TableDefinitions);
                    if (null != library)
                    {
                        // load the extension's default culture if it provides one and we don't specify any cultures
                        string[] extensionCultures = this.cultures;
                        if (null == extensionCultures && null != wixExtension.DefaultCulture)
                        {
                            extensionCultures = new string[] { wixExtension.DefaultCulture };
                        }

                        library.GetLocalizations(extensionCultures, localizer);
                    }
                }

                this.binder.ProcessExtensions(loadedExtensionList.ToArray());

                // set the message handlers
                linker.Message += new MessageEventHandler(this.messageHandler.Display);
                this.binder.AddMessageEventHandler(new MessageEventHandler(this.messageHandler.Display));

                Output output = null;

                // loop through all the believed object files
                foreach (string inputFile in this.inputFiles)
                {
                    string dirName = Path.GetDirectoryName(inputFile);
                    string inputFileFullPath = Path.GetFullPath(inputFile);

                    if (!this.sourcePaths.Contains(dirName))
                    {
                        this.sourcePaths.Add(dirName);
                    }

                    // try loading as an object file
                    try
                    {
                        Intermediate intermediate = Intermediate.Load(inputFileFullPath, linker.TableDefinitions, this.suppressVersionCheck, this.suppressSchema);
                        sections.AddRange(intermediate.Sections);
                        continue; // next file
                    }
                    catch (WixNotIntermediateException)
                    {
                        // try another format
                    }

                    // try loading as a library file
                    try
                    {
                        Library library = Library.Load(inputFileFullPath, linker.TableDefinitions, this.suppressVersionCheck, this.suppressSchema);
                        library.GetLocalizations(this.cultures, localizer);
                        sections.AddRange(library.Sections);
                        continue; // next file
                    }
                    catch (WixNotLibraryException)
                    {
                        // try another format
                    }

                    // try loading as an output file
                    output = Output.Load(inputFileFullPath, this.suppressVersionCheck, this.suppressSchema);
                }

                // immediately stop processing if any errors were found
                if (this.messageHandler.EncounteredError)
                {
                    return this.messageHandler.LastErrorNumber;
                }

                // set the binder file manager information
                foreach (string bindPath in this.bindPaths)
                {
                    //Checking as IndexOf will return 0 if the string value is String.Empty.
                    if (String.IsNullOrEmpty(bindPath))
                    {
                        continue;
                    }

                    if (-1 == bindPath.IndexOf('='))
                    {
                        this.binder.FileManager.BindPaths.Add(bindPath);
                    }
                    else
                    {
                        string[] namedPair = bindPath.Split('=');

                        //It is ok to have duplicate key.
                        this.binder.FileManager.NamedBindPaths.Add(namedPair[0], namedPair[1]);
                    }
                }

                foreach (string sourcePath in this.sourcePaths)
                {
                    this.binder.FileManager.SourcePaths.Add(sourcePath);
                }

                // and now for the fun part
                if (null == output)
                {
                    OutputType expectedOutputType = OutputType.Unknown;
                    if (this.outputFile != null)
                    {
                        expectedOutputType = Output.GetOutputType(Path.GetExtension(this.outputFile));
                    }

                    output = linker.Link(sections, transforms, expectedOutputType);

                    // if an error occurred during linking, stop processing
                    if (null == output)
                    {
                        return this.messageHandler.LastErrorNumber;
                    }
                }
                else if (0 != sections.Count)
                {
                    throw new InvalidOperationException(LightStrings.EXP_CannotLinkObjFilesWithOutpuFile);
                }

                // Now that the output object is either linked or loaded, tell the binder file manager about it.
                this.binder.FileManager.Output = output;

                // only output the xml if its a patch build or user specfied to only output wixout
                if (this.outputXml || OutputType.Patch == output.Type)
                {
                    string outputExtension = Path.GetExtension(this.outputFile);
                    if (null == outputExtension || 0 == outputExtension.Length || ".wix" == outputExtension)
                    {
                        if (OutputType.Patch == output.Type)
                        {
                            this.outputFile = Path.ChangeExtension(this.outputFile, ".wixmsp");
                        }
                        else
                        {
                            this.outputFile = Path.ChangeExtension(this.outputFile, ".wixout");
                        }
                    }

                    output.Save(this.outputFile, (this.bindFiles ? this.binder.FileManager : null), this.wixVariableResolver, this.binder.TempFilesLocation);
                }
                else // finish creating the MSI/MSM
                {
                    string outputExtension = Path.GetExtension(this.outputFile);
                    if (null == outputExtension || 0 == outputExtension.Length || ".wix" == outputExtension)
                    {
                        outputExtension = Output.GetExtension(output.Type);
                        this.outputFile = Path.ChangeExtension(this.outputFile, outputExtension);
                    }

                    this.binder.Bind(output, this.outputFile);
                }
            }
            catch (WixException we)
            {
                if (we is WixInvalidIdtException)
                {
                    // make sure the IDT files stay around
                    this.tidy = false;
                }

                this.messageHandler.Display(this, we.Error);
            }
            catch (Exception e)
            {
                // make sure the files stay around for debugging
                this.tidy = false;

                this.messageHandler.Display(this, WixErrors.UnexpectedException(e.Message, e.GetType().ToString(), e.StackTrace));
                if (e is NullReferenceException || e is SEHException)
                {
                    throw;
                }
            }
            finally
            {
                if (null != binder)
                {
                    this.binder.Cleanup(this.tidy);
                }
            }

            return this.messageHandler.LastErrorNumber;
        }