Exemple #1
0
        private void InitFile(FileInfo file)
        {
            var frontMatter = PostMeta.FromFile(file);

            if (frontMatter == null)
            {
                OutputOn("    没有 YAML 元数据。", ConsoleColor.Yellow);
                return;
            }

            var dateString        = frontMatter.Date;
            var publishDateString = frontMatter.PublishDate ?? dateString;

            if (!string.IsNullOrWhiteSpace(dateString))
            {
                var date        = DateTimeOffset.Parse(dateString);
                var publishDate = DateTimeOffset.Parse(publishDateString);

                Console.WriteLine($"    创建于 {publishDate},更新于 {date}。");
                FixFileDate(file, publishDate, date);
            }
            else
            {
                OutputOn("    YAML 元数据中没有发现时间。", ConsoleColor.Yellow);
            }
        }
        private bool UpdateFile(FileInfo file)
        {
            var frontMatter = PostMeta.FromFile(file);

            if (frontMatter == null)
            {
                return(false);
            }

            var dateString = frontMatter.Date;

            if (!string.IsNullOrWhiteSpace(dateString))
            {
                var date = DateTimeOffset.Parse(dateString);

                var writeTime      = file.LastWriteTimeUtc.ToUniversalTime();
                var roundWriteTime = new DateTimeOffset(
                    writeTime.Year, writeTime.Month, writeTime.Day,
                    writeTime.Hour, writeTime.Minute, writeTime.Second, TimeSpan.Zero);
                if (roundWriteTime != date.ToUniversalTime())
                {
                    var splitted = UpdateMetaTime(file, frontMatter, writeTime);
                    // 更新文件的最近写入时间,在此前的时间上额外添加 10ms,以便编辑器或其他软件能够识别到文件变更。
                    var fileLastWriteTime = writeTime + TimeSpan.FromMilliseconds(10);
                    if (splitted)
                    {
                        var fileCreationTime = DateTimeOffset.Parse(
                            frontMatter.PublishDate ?? frontMatter.Date).UtcDateTime;
                        file.CreationTimeUtc  = fileCreationTime;
                        file.LastWriteTimeUtc = fileLastWriteTime;
                    }
                    else
                    {
                        file.CreationTimeUtc  = fileLastWriteTime;
                        file.LastWriteTimeUtc = fileLastWriteTime;
                    }

                    return(true);
                }
            }

            return(false);
        }