Esempio n. 1
0
        public static string GetBasicPath(this Track track, string pathFormat, Dictionary <string, object> vars)
        {
            // Hacky method to clean the file path
            var formatStrComponents = pathFormat.Split(Path.DirectorySeparatorChar);
            var newFormat           = String.Join("\0", formatStrComponents);
            var finalPath           = Named.Format(newFormat, vars);

            return(PathHelpers.CleanPath(finalPath));
        }
Esempio n. 2
0
        public static string FormatFilePath(object obj, string pathFormat)
        {
            // Hacky method to clean the file path
            var formatStrComponents = Split(pathFormat);
            var newFormat           = string.Join(NullSeparatorChar.ToString(), formatStrComponents);
            var vars = Dictify.ObjectToDictionary(obj);
            // vars["ServiceName"] = title;
            var finalPath = CleanPath(Named.Format(newFormat, vars));

            return(finalPath);
        }
Esempio n. 3
0
        public static void Test_02()
        {
            var keyValues = new Dictionary <string, object> {
                { "date", DateTime.Now.ToUniversalTime().ToString("o") }
            };
            string format    = "{{ 'data.LoadFromWebDate': {{ $gt: ISODate('{date}') }} }}";
            var    formatted = Named.Format(format, keyValues);

            Trace.WriteLine($"format    : \"{format}\"");
            Trace.WriteLine($"formatted : \"{formatted}\"");
        }
Esempio n. 4
0
        public static void Test_01()
        {
            // from https://github.com/kekyo/CenterCLR.NamingFormatter
            var keyValues = new Dictionary <string, object> {
                { "lastName", "Matsui" }, { "firstName", "Kouji" }
            };
            string format    = "FirstName:{firstName}, LastName:{lastName}";
            var    formatted = Named.Format(format, keyValues);

            Trace.WriteLine($"format    : \"{format}\"");
            Trace.WriteLine($"formatted : \"{formatted}\"");
        }
Esempio n. 5
0
 public static string GetBasicPath(this Track track, string pathFormat, IMediaCollection collection)
 {
     // Hacky method to clean the file path
     var formatStrComponents = pathFormat.Split(Path.DirectorySeparatorChar);
     var newFormat = String.Join("\0", formatStrComponents);
     var vars = Dictify.ObjectToDictionary(track);
     vars["PlaylistName"] = collection.Title;
     vars["CollectionName"] = collection.Title;
     vars["ServiceName"] = collection.Title;
     var finalPath = PathHelpers.CleanPath(Named.Format(newFormat, vars));
     return finalPath;
 }
Esempio n. 6
0
        public void Write(Level level, string moduleTag, string message)
        {
            EnsureLog();
            var vars = new Dictionary <string, object>
            {
                { "Date", DateTime.Now.ToString("O") },
                { "Level", level },
                { "Tag", moduleTag ?? "Global" },
                { "Message", message }
            };
            var formattedMessage = Named.Format(LineFormat, vars);

            writer.WriteLine(formattedMessage);
        }
Esempio n. 7
0
        public void FormatIdentityTraversePropertyNotFoundTest()
        {
            var now       = DateTime.Now;
            var keyValues = new Dictionary <string, object>()
            {
                { "abc", 123 },
                { "defgh", now },
                { "ijkl", "XYZ" }
            };

            var actual = Named.Format(
                "{defgh.TimeOfDa.TotalMilliseconds}",
                keyValues);

            Assert.AreEqual(string.Empty, actual);
        }
Esempio n. 8
0
        public void EndFormatIdentityTest()
        {
            var now       = DateTime.Now;
            var keyValues = new Dictionary <string, object>()
            {
                { "abc", 123 },
                { "defgh", now },
                { "ijkl", "XYZ" }
            };

            var actual = Named.Format(
                "AAA{defgh}",
                keyValues);

            Assert.AreEqual("AAA" + now, actual);
        }
Esempio n. 9
0
        public void DoubleBracketEmptyKeyTest()
        {
            var now       = DateTime.Now;
            var keyValues = new Dictionary <string, object>()
            {
                { "abc", 123 },
                { "defgh", now },
                { "ijkl", "XYZ" }
            };

            var actual = Named.Format(
                "{{}}",
                keyValues);

            Assert.AreEqual("{}", actual);
        }
Esempio n. 10
0
        public void EmptyKeyIdentityWithEmptyOptionTest()
        {
            var now       = DateTime.Now;
            var keyValues = new Dictionary <string, object>()
            {
                { "abc", 123 },
                { "", now },
                { "ijkl", "XYZ" }
            };

            var actual = Named.Format(
                "{:}",
                keyValues);

            Assert.AreEqual(now.ToString(), actual);
        }
Esempio n. 11
0
        public void FormatIdentityWithOptionTest()
        {
            var now       = DateTime.Now;
            var keyValues = new Dictionary <string, object>()
            {
                { "abc", 123 },
                { "defgh", now },
                { "ijkl", "XYZ" }
            };

            var actual = Named.Format(
                "{defgh:yyyyMMddHHmmssfff}",
                keyValues);

            Assert.AreEqual(now.ToString("yyyyMMddHHmmssfff"), actual);
        }
Esempio n. 12
0
        public void FormatIdentityWithAlignmentTest()
        {
            var now       = DateTime.Now;
            var keyValues = new Dictionary <string, object>()
            {
                { "abc", 123 },
                { "defgh", now },
                { "ijkl", "XYZ" }
            };

            var actual = Named.Format(
                "{abc,10}",
                keyValues);

            Assert.AreEqual("       123", actual);
        }
Esempio n. 13
0
        public void IReadOnlyDictionaryOverloadTest()
        {
            var now       = DateTime.Now;
            var keyValues = new Dictionary <string, object>()
            {
                { "abc", 123 },
                { "defgh", now },
                { "ijkl", "XYZ" }
            };

            var actual = Named.Format <IReadOnlyDictionary <string, object> >(
                "AAA{defgh}BBB{abc}CCC{ijkl}DDD",
                keyValues);

            Assert.AreEqual("AAA" + now + "BBB123CCCXYZDDD", actual);
        }
Esempio n. 14
0
        public void EnumerableOverloadWithArrayTest()
        {
            var now = DateTime.Now;
            IEnumerable <KeyValuePair <string, object> > keyValues = new[]
            {
                new KeyValuePair <string, object>("abc", 123),
                new KeyValuePair <string, object>("defgh", now),
                new KeyValuePair <string, object>("ijkl", "XYZ"),
            };

            var actual = Named.Format(
                "AAA{defgh}BBB{abc}CCC{ijkl}DDD",
                keyValues);

            Assert.AreEqual("AAA" + now + "BBB123CCCXYZDDD", actual);
        }
Esempio n. 15
0
        public void FormatIdentityTraversePropertyTest()
        {
            var now       = DateTime.Now;
            var keyValues = new Dictionary <string, object>()
            {
                { "abc", 123 },
                { "defgh", now },
                { "ijkl", "XYZ" }
            };

            var actual = Named.Format(
                "{defgh.Year}",
                keyValues);

            Assert.AreEqual(now.Year.ToString(), actual);
        }
Esempio n. 16
0
        public void EnumerableOverloadWithComparerTest()
        {
            var now = DateTime.Now;
            IEnumerable <KeyValuePair <string, object> > keyValues = new[]
            {
                new KeyValuePair <string, object>("aBc", 123),
                new KeyValuePair <string, object>("deFgH", now),
                new KeyValuePair <string, object>("iJKl", "XYZ"),
            };

            var actual = Named.Format(
                "AAA{Defgh}BBB{abC}CCC{IjkL}DDD",
                StringComparer.InvariantCultureIgnoreCase,
                keyValues);

            Assert.AreEqual("AAA" + now + "BBB123CCCXYZDDD", actual);
        }
        public void EnumerableOverloadTest()
        {
            var now = DateTime.Now;
            IEnumerable <KeyValuePair <string, object> > keyValues = new Dictionary <string, object>()
            {
                { "abc", 123 },
                { "defgh", now },
                { "ijkl", "XYZ" }
            };

            var actual = Named.Format(
                formatProvider_,
                "AAA{defgh}BBB{abc}CCC{ijkl}DDD",
                keyValues);

            Assert.AreEqual("AAA" + now.ToString(formatProvider_) + "BBB123CCCXYZDDD", actual);
        }
Esempio n. 18
0
        public void DictionaryWithComparerOverloadTest()
        {
            var now = DateTime.Now;
            IDictionary <string, object> keyValues = new Dictionary <string, object>(
                StringComparer.InvariantCultureIgnoreCase)
            {
                { "aBc", 123 },
                { "dEFgh", now },
                { "ijKl", "XYZ" }
            };

            var actual = Named.Format(
                "AAA{Defgh}BBB{abC}CCC{IjkL}DDD",
                keyValues);

            Assert.AreEqual("AAA" + now + "BBB123CCCXYZDDD", actual);
        }
        public void EnumerableOverloadWithNoListTest()
        {
            var now       = DateTime.Now;
            var keyValues = new[]
            {
                Tuple.Create("abc", (object)123),
                Tuple.Create("defgh", (object)now),
                Tuple.Create("ijkl", (object)"XYZ")
            }.
            Select(entry => new KeyValuePair <string, object>(entry.Item1, entry.Item2));

            var actual = Named.Format(
                formatProvider_,
                "AAA{defgh}BBB{abc}CCC{ijkl}DDD",
                keyValues);

            Assert.AreEqual("AAA" + now.ToString(formatProvider_) + "BBB123CCCXYZDDD", actual);
        }
        // IServerManager method
        //public virtual LoadNewDocumentsResult LoadNewDocuments(int maxNbDocumentsLoadedFromStore = 5, int startPage = 1, int maxPage = 20, WebImageRequest webImageRequest = null)
        //{
        //    return _headerDetailManager.LoadNewDocuments(maxDocumentsLoadedFromStore: maxNbDocumentsLoadedFromStore, startPage: startPage, maxPage: maxPage, webImageRequest: webImageRequest);
        //}

        // IServerManager method
        public virtual IEnumerable <IPostToDownload> FindFromDateTime(DateTime datetime)
        {
            //throw new PBException("FindFromDateTime(DateTime dateTime) not implemented");
            if (_findFromDateTimeQuery == null)
            {
                throw new PBException("FindFromDateTime query undefined");
            }
            string query = Named.Format(_findFromDateTimeQuery, new Dictionary <string, object> {
                { "date", datetime.ToUniversalTime().ToString("o") }
            });
            string sort = _findFromDateTimeSort;

            if (sort == null)
            {
                sort = _detailDataManager.DataStore.DefaultSort;
            }
            return((IEnumerable <IPostToDownload>)_detailDataManager.Find(query, sort: sort, loadImage: false));
        }
Esempio n. 21
0
        private void EnsureLog()
        {
            var temp = new Dictionary <string, object> {
                { "FileDate", DateTime.Now.ToString("yyyyMMdd") }
            };

            formattedName = Named.Format(FilenameFormat, temp);
            if (logFileName != formattedName)
            {
                logFile?.Dispose();
                writer?.Dispose();
                logFile     = File.Open(Path.Combine(logDirectory, formattedName), FileMode.Append, FileAccess.Write);
                logFileName = formattedName;
                writer      = new StreamWriter(logFile)
                {
                    AutoFlush = true
                };
            }
        }
Esempio n. 22
0
        public void Write(
            ProcessorContext context,
            Dictionary <string, object> keyValues,
            DateTimeOffset generated,
            IEnumerable <Rule> ruleSet,
            IEnumerable <string> importSet)
        {
            Debug.Assert(string.IsNullOrWhiteSpace(context.OutputPath) == false);
            Debug.Assert(keyValues != null);
            Debug.Assert(ruleSet != null);
            Debug.Assert(importSet != null);

            var targetFolder = Path.GetDirectoryName(context.OutputPath);

            if (!string.IsNullOrWhiteSpace(targetFolder) && !Directory.Exists(targetFolder))
            {
                try
                {
                    // Construct sub folders (ex: obj\Debug).
                    // May fail if parallel-building on MSBuild, ignoring exceptions.
                    Directory.CreateDirectory(targetFolder);
                }
                catch
                {
                }
            }

            using (var ftw = File.CreateText(context.OutputPath))
            {
                var tw = new SourceCodeWriter(ftw, context);

                this.WriteComment(tw,
                                  $"This is auto-generated version information attributes by RelaxVersioner.{this.GetType().Assembly.GetName().Version}, Do not edit.");
                this.WriteComment(tw,
                                  $"Generated date: {generated:R}");
                tw.WriteLine();

                this.WriteBeforeBody(tw);

                foreach (var importNamespace in importSet)
                {
                    this.WriteImport(tw, importNamespace);
                }
                tw.WriteLine();

                foreach (var rule in ruleSet)
                {
                    var formattedValue = Named.Format(rule.Format, keyValues, key => string.Empty);
                    if (!string.IsNullOrWhiteSpace(rule.Key))
                    {
                        this.WriteAttributeWithArguments(tw, rule.Name, rule.Key, formattedValue);
                    }
                    else
                    {
                        this.WriteAttributeWithArguments(tw, rule.Name, formattedValue);
                    }
                }
                tw.WriteLine();

                if (context.GenerateStatic)
                {
                    this.WriteBeforeLiteralBody(tw);

                    foreach (var g in ruleSet.GroupBy(rule => rule.Name))
                    {
                        var rules = g.ToArray();

                        if (rules.Length >= 2)
                        {
                            this.WriteBeforeNestedLiteralBody(tw, rules[0].Name);
                        }

                        foreach (var rule in rules)
                        {
                            var formattedValue = Named.Format(rule.Format, keyValues, key => string.Empty);
                            if (!string.IsNullOrWhiteSpace(rule.Key))
                            {
                                this.WriteLiteralWithArgument(tw, rule.Key, formattedValue);
                            }
                            else
                            {
                                this.WriteLiteralWithArgument(tw, rule.Name, formattedValue);
                            }
                        }

                        if (rules.Length >= 2)
                        {
                            this.WriteAfterNestedLiteralBody(tw);
                        }
                    }

                    this.WriteAfterLiteralBody(tw);
                    tw.WriteLine();
                }

                this.WriteAfterBody(tw);

                tw.Flush();
            }
        }
        public virtual void Write(
            string targetPath,
            Branch branch,
            Dictionary <string, IEnumerable <Tag> > tags,
            Dictionary <string, IEnumerable <Branch> > branches,
            bool requireMetadataAttribute,
            DateTimeOffset generated,
            ICollection <Rule> ruleSet)
        {
            Debug.Assert(string.IsNullOrWhiteSpace(targetPath) == false);
            Debug.Assert(tags != null);
            Debug.Assert(branches != null);
            Debug.Assert(ruleSet != null);

            var unknownBranch = new UnknownBranch(generated);

            var altBranch = branch ?? unknownBranch;
            var commit    = altBranch.Commits.FirstOrDefault() ?? unknownBranch.Commits.First();

            var targetFolder = Path.GetDirectoryName(targetPath);

            if (Directory.Exists(targetFolder) == false)
            {
                try
                {
                    // Construct sub folders (ex: obj\Debug).
                    // May fail if parallel-building on MSBuild, ignoring exceptions.
                    Directory.CreateDirectory(targetFolder);
                }
                catch
                {
                }
            }

            using (var tw = File.CreateText(targetPath))
            {
                this.WriteComment(tw, "This is auto-generated version information attributes by CenterCLR.RelaxVersioner.{0}",
                                  this.GetType().Assembly.GetName().Version);
                this.WriteComment(tw, "Do not edit.");
                this.WriteComment(tw, "Generated date: {0:R}", generated);
                tw.WriteLine();

                this.WriteBeforeBody(tw, requireMetadataAttribute);

                var namespaces = Utilities.AggregateNamespacesFromRuleSet(ruleSet);
                foreach (var namespaceName in namespaces)
                {
                    this.WriteImport(tw, namespaceName);
                }
                tw.WriteLine();

                var commitId  = commit.Sha;
                var author    = commit.Author;
                var committer = commit.Committer;

                var altBranches = string.Join(
                    ",",
                    branches.GetValue(commitId, Enumerable.Empty <Branch>()).
                    Select(b => b.Name));
                var altTags = string.Join(
                    ",",
                    tags.GetValue(commitId, Enumerable.Empty <Tag>()).
                    Select(b => b.Name));

                var safeVersion = Utilities.GetSafeVersionFromDate(committer.When);
                var gitLabel    = Utilities.GetLabelWithFallback(altBranch, tags, branches) ?? safeVersion;

                var keyValues = new Dictionary <string, object>(StringComparer.InvariantCultureIgnoreCase)
                {
                    { "generated", generated },
                    { "branch", altBranch },
                    { "branches", altBranches },
                    { "tags", altTags },
                    { "commit", commit },
                    { "author", author },
                    { "committer", committer },
                    { "commitId", commitId },
                    { "gitLabel", gitLabel },
                    { "safeVersion", safeVersion }
                };

                foreach (var rule in ruleSet)
                {
                    var formattedValue = Named.Format(rule.Format, keyValues);
                    if (!string.IsNullOrWhiteSpace(rule.Key))
                    {
                        this.WriteAttributeWithArguments(tw, rule.Name, rule.Key, formattedValue);
                    }
                    else
                    {
                        this.WriteAttributeWithArguments(tw, rule.Name, formattedValue);
                    }
                }
                tw.WriteLine();

                this.WriteAfterBody(tw, requireMetadataAttribute);

                tw.Flush();
            }
        }