Ejemplo n.º 1
0
        /// <summary>
        /// CompileSatelliteAssemblies()
        /// </summary>
        /// <param name="targetCulture"></param>
        /// <param name="ds"></param>
        public void CompileSatelliteAssemblies(string targetCulture, GlobalizationDataSet ds)
        {
            //all columns are required except SourceValue

            string tempPath = Path.GetTempPath();

            applicationPath = applicationPath.Trim().ToLowerInvariant();

            if (applicationPath[applicationPath.Length - 1] != Path.DirectorySeparatorChar)
            {
                applicationPath += Path.DirectorySeparatorChar;
            }

            this.AppendToLog(SharedStrings.INSTALLING_LANGUAGE, targetCulture);

            GlobalizationDataSet.CulturalResourcesDataTable cultureTable = ds.CulturalResources;//.SelectFilter("CultureName='" + targetCulture + "'");

            GlobalizationDataSet.CulturalResourcesDataTable assembliesTable = cultureTable.SelectDistinct("AssemblyName");
            foreach (GlobalizationDataSet.CulturalResourcesRow assemblyRow in assembliesTable)
            {
                List <string> resourceList = new List <string>();

                GlobalizationDataSet.CulturalResourcesDataTable resourceTable = ds.CulturalResources.SelectFilter("AssemblyName='" + assemblyRow.AssemblyName + "'").SelectDistinct("ManifestResourceName");
                foreach (GlobalizationDataSet.CulturalResourcesRow resourceRow in resourceTable)
                {
                    string resourceFilename;
                    resourceFilename = resourceRow.ManifestResourceName.Substring(0, resourceRow.ManifestResourceName.Length - "resources".Length);
                    resourceFilename = Path.Combine(tempPath, resourceFilename + targetCulture + ".resources");

                    try
                    {
                        using (ResourceWriter writer = new ResourceWriter(resourceFilename))
                        {
                            GlobalizationDataSet.CulturalResourcesDataTable itemTable = ds.CulturalResources.SelectFilter("AssemblyName='" + assemblyRow.AssemblyName + "'").SelectFilter("ManifestResourceName='" + resourceRow.ManifestResourceName + "'");
                            foreach (GlobalizationDataSet.CulturalResourcesRow itemRow in itemTable)
                            {
                                writer.AddResource(itemRow.ResourceName, itemRow.ResourceValue);
                            }
                            writer.Close();
                        }

                        resourceList.Add(resourceFilename);
                    }
                    catch
                    {
                        foreach (string tempResourceFile in resourceList)
                        {
                            try
                            {
                                File.Delete(tempResourceFile);
                            }
                            catch
                            {
                                //eat
                            }
                        }

                        // kickout
                        throw;
                    }
                }

                string assemblyFilename = targetCulture + Path.DirectorySeparatorChar + assemblyRow.AssemblyName + ".resources.dll";
                assemblyFilename = Path.Combine(applicationPath, assemblyFilename);

                this.AppendToLog("Compiling " + assemblyRow.AssemblyName + " satellite assembly.");

                if (!Directory.Exists(Path.GetDirectoryName(assemblyFilename)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(assemblyFilename));
                }
                else if (File.Exists(assemblyFilename))
                {
                    File.Delete(assemblyFilename);
                }

                CSharpCodeProvider prov = new CSharpCodeProvider();
                CompilerParameters p    = new CompilerParameters();
                p.EmbeddedResources.AddRange(resourceList.ToArray());
                p.GenerateInMemory = false;
                p.OutputAssembly   = assemblyFilename;

                // setup references
                p.ReferencedAssemblies.Add("System.dll");


                Assembly mainAssembly = Assembly.Load(assemblyRow.AssemblyName);

                string version = GetSatelliteContractVersion(mainAssembly).ToString();
                if (version != assemblyRow.ResourceVersion.Trim())
                {
                    // warning! resource mismatch
                }

                string code = "[assembly: System.Reflection.AssemblyVersion(\"" + version + "\")] " +
                              "[assembly: System.Reflection.AssemblyFileVersion(\"" + version + "\")] " +
                              "[assembly: System.Reflection.AssemblyCulture(\"" + targetCulture + "\")]";
                CompilerResults res = prov.CompileAssemblyFromSource(p, code);

                foreach (string tempResourceFile in resourceList)
                {
                    File.Delete(tempResourceFile);
                }

                if (res.Errors.HasErrors)
                {
                    throw new GeneralException(GetString(res.Output));
                }
            }
        }