Example #1
0
        /// <summary>
        /// Runs the compiler from the command line.
        /// </summary>
        /// <param name="arguments">The command line arguments. Each compiler defines its own.</param>
        /// <returns>0 for success, non-0 for error.</returns>
        private static int Main(string[] arguments)
        {
            CompilerOptions options = new CompilerOptions();
            ICommandLineParser parser = new CommandLineParser(new CommandLineParserSettings(Console.Error) { MutuallyExclusive = true });
            if (!parser.ParseArguments(arguments, options))
            {
                return 1;
            }

            var tzdbCompiler = new TzdbZoneInfoCompiler();
            var tzdb = tzdbCompiler.Compile(options.SourceDirectoryName);
            tzdb.LogCounts();
            if (options.ZoneId != null)
            {
                tzdb.GenerateDateTimeZone(options.ZoneId);
                return 0;
            }
            var windowsZones = CldrWindowsZonesParser.Parse(options.WindowsMappingFile);
            LogWindowsZonesSummary(windowsZones);
            var writer = CreateWriter(options);
            writer.Write(tzdb, windowsZones);

            if (options.OutputFileName != null)
            {
                Console.WriteLine("Reading generated data and validating...");
                var source = Read(options);
                source.Validate();
                if (options.TextDumpFile != null)
                {
                    CreateTextDump(source, options.TextDumpFile);
                }
            }
            return 0;
        }
Example #2
0
        /// <summary>
        /// Loads the best windows zones file based on the options. If the WindowsMapping option is
        /// just a straight file, that's used. If it's a directory, this method loads all the XML files
        /// in the directory (expecting them all to be mapping files) and then picks the best one based
        /// on the version of TZDB we're targeting - basically, the most recent one before or equal to the
        /// target version.
        /// </summary>
        private static WindowsZones LoadWindowsZones(CompilerOptions options, string targetTzdbVersion)
        {
            var mappingPath = options.WindowsMapping;
            if (File.Exists(mappingPath))
            {
                return CldrWindowsZonesParser.Parse(mappingPath);
            }
            if (!Directory.Exists(mappingPath))
            {
                throw new Exception($"{mappingPath} does not exist as either a file or a directory");
            }
            var xmlFiles = Directory.GetFiles(mappingPath, "*.xml");
            if (xmlFiles.Length == 0)
            {
                throw new Exception($"{mappingPath} does not contain any XML files");
            }
            var allFiles = xmlFiles
                .Select(file => CldrWindowsZonesParser.Parse(file))
                .OrderByDescending(zones => zones.TzdbVersion)
                .ToList();

            var versions = string.Join(", ", allFiles.Select(z => z.TzdbVersion).ToArray());

            var bestFile = allFiles
                .Where(zones => StringComparer.Ordinal.Compare(zones.TzdbVersion, targetTzdbVersion) <= 0)
                .FirstOrDefault();

            if (bestFile == null)
            {
                throw new Exception($"No zones files suitable for version {targetTzdbVersion}. Found versions targeting: [{versions}]");
            }
            Console.WriteLine($"Picked Windows Zones with TZDB version {bestFile.TzdbVersion} out of [{versions}] as best match for {targetTzdbVersion}");
            return bestFile;
        }
Example #3
0
        private static TzdbDateTimeZoneSource Read(CompilerOptions options)
        {
#pragma warning disable 0618
            string file = Path.ChangeExtension(options.OutputFileName, "nzd");
            using (var stream = File.OpenRead(file))
            {
                return TzdbDateTimeZoneSource.FromStream(stream);
            }
#pragma warning restore 0618
        }
Example #4
0
 private static TzdbStreamWriter CreateWriter(CompilerOptions options)
 {
     // If we don't have an actual file, just write to an empty stream.
     // That way, while debugging, we still get to see all the data written etc.
     if (options.OutputFileName == null)
     {
         return new TzdbStreamWriter(new MemoryStream());
     }
     string file = Path.ChangeExtension(options.OutputFileName, "nzd");
     return new TzdbStreamWriter(File.Create(file));
 }
Example #5
0
        /// <summary>
        /// Runs the compiler from the command line.
        /// </summary>
        /// <param name="arguments">The command line arguments. Each compiler defines its own.</param>
        /// <returns>0 for success, non-0 for error.</returns>
        private static int Main(string[] arguments)
        {
            CompilerOptions options = new CompilerOptions();
            ICommandLineParser parser = new CommandLineParser(new CommandLineParserSettings(Console.Error) { MutuallyExclusive = true });
            if (!parser.ParseArguments(arguments, options))
            {
                return 1;
            }

            var tzdbCompiler = new TzdbZoneInfoCompiler();
            var tzdb = tzdbCompiler.Compile(options.SourceDirectoryName);
            tzdb.LogCounts();
            if (options.ZoneId != null)
            {
                tzdb.GenerateDateTimeZone(options.ZoneId);
                return 0;
            }
            var windowsZones = LoadWindowsZones(options, tzdb.Version);
            if (options.WindowsOverride != null)
            {
                var overrideFile = CldrWindowsZonesParser.Parse(options.WindowsOverride);
                windowsZones = MergeWindowsZones(windowsZones, overrideFile);
            }
            LogWindowsZonesSummary(windowsZones);
            var writer = new TzdbStreamWriter();
            using (var stream = CreateOutputStream(options))
            {
                writer.Write(tzdb, windowsZones, PclSupport.StandardNameToIdMap, stream);
            }

            if (options.OutputFileName != null)
            {
                Console.WriteLine("Reading generated data and validating...");
                var source = Read(options);
                source.Validate();
            }
            return 0;
        }
Example #6
0
 private static TzdbStreamWriter CreateWriter(CompilerOptions options)
 {
     string file = Path.ChangeExtension(options.OutputFileName, "nzd");
     return new TzdbStreamWriter(File.Create(file));
 }