예제 #1
0
        public byte[] CreateRibbon(Target element)
        {
            CleanupFiles.Clear();

            try
            {
                VerifyTemplateBat();

                // use the following format to specify localization info {Resource:key}
                string localizedRibbonXmlFilename = LocalizeRibbon(element);

                // create the ribbon dll
                string ribbonDll = ConvertXmlToDll(localizedRibbonXmlFilename);

                // return the content of the ribbon dll
                var result = File.ReadAllBytes(ribbonDll);

                Util.LogMessage("Manager.CreateRibbon returns {0} bytes for file '{1}'", result.Length, ribbonDll);
                return(result);
            }
            catch (Exception ex)
            {
                Util.LogError(ex);
                throw ex;
            }
            finally
            {
                Cleanup();
            }
        }
예제 #2
0
        public byte[] CreateRibbon(Target element, string resourceName)
        {
            string localizedRibbonXmlFilename = null;

            CleanupFiles.Clear();

            RibbonCompiler compiler = new RibbonCompiler(this);

            try
            {
                // use the following format to specify localization info {Resource:key}
                localizedRibbonXmlFilename = LocalizeRibbon(element);
                string ribbonDll = null;
                byte[] result;

                RibbonCompileResult compileResult = compiler.Compile(localizedRibbonXmlFilename, resourceName);
                if (compileResult == RibbonCompileResult.Ok)
                {
                    ribbonDll = compiler.RibbonDll;
                    result    = File.ReadAllBytes(ribbonDll);
                }
                else
                {
                    Util.LogMessage("Error: " + compileResult.ToString());
                    result = new byte[0];
                }
                for (int i = 0; i < compiler.Messages.Count; i++)
                {
                    if (!string.IsNullOrEmpty(compiler.Messages[i]))
                    {
                        _output.WriteLine(compiler.Messages[i]);
                    }
                }
                Util.LogMessage("Manager.CreateRibbon returns {0} bytes for file '{1}'", result.Length, ribbonDll);
                return(result);
            }
            catch (Exception ex)
            {
                for (int i = 0; i < compiler.Messages.Count; i++)
                {
                    if (!string.IsNullOrEmpty(compiler.Messages[i]))
                    {
                        _output.WriteLine(compiler.Messages[i]);
                    }
                }
                Util.LogError(ex);
                throw;
            }
            finally
            {
                Cleanup();
                if (localizedRibbonXmlFilename != null)
                {
                    string firstExtensionExcluded = Path.GetFileNameWithoutExtension(localizedRibbonXmlFilename);
                    string dotCulture             = Path.GetExtension(firstExtensionExcluded);
                    if (!string.IsNullOrEmpty(dotCulture) && dotCulture.Equals(".default", StringComparison.OrdinalIgnoreCase))
                    {
                        string defaultH = Path.ChangeExtension(localizedRibbonXmlFilename, ".h");
                        if (File.Exists(defaultH))
                        {
                            string neutralHFile = Path.Combine(Path.GetDirectoryName(localizedRibbonXmlFilename), Path.ChangeExtension(firstExtensionExcluded, ".h"));
                            File.Delete(neutralHFile);
                            File.Move(defaultH, neutralHFile); // rename *.default.h to *.h
                        }
                        string defaultRes = Path.ChangeExtension(localizedRibbonXmlFilename, ".res");
                        if (File.Exists(defaultRes))
                        {
                            string neutralResFile = Path.Combine(Path.GetDirectoryName(localizedRibbonXmlFilename), Path.ChangeExtension(firstExtensionExcluded, ".res"));
                            File.Delete(neutralResFile);
                            File.Move(defaultRes, neutralResFile); // rename *.default.res to *.res
                        }
                    }
                }
            }
        }