Пример #1
0
        // Dictionary<string, string> singleSettings = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
        public Settings(string settingsFile)
        {
            if (!File.Exists(settingsFile))
            {
                var directory = Path.GetDirectoryName(settingsFile);
                Directory.CreateDirectory(directory);
                File.WriteAllLines(settingsFile, new[] {
                    "# Place repo-specific settings here. Comments start with #",
                    "# include=dir to include a directory",
                    "# By default all directories in the repo are included but if there are any includes then nothing else will be included",
                    "# exclude=dir to exclude a directory. Exclude filters are applied after all the include filters."
                });
            }
            else
            {
                foreach (var line in File.ReadLines(settingsFile).Select(x => x.Trim()))
                {
                    if (string.IsNullOrEmpty(line) || line.StartsWith("#"))
                    {
                        continue;
                    }

                    if (line.Count(x => x == '=') == 1)
                    {
                        var segments = line.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray();
                        if (segments[0].Equals("include", StringComparison.OrdinalIgnoreCase))
                        {
                            DictUtils.AddEntryToList(listSettings, "include", segments[1]);
                        }
                        if (segments[0].Equals("exclude", StringComparison.OrdinalIgnoreCase))
                        {
                            DictUtils.AddEntryToList(listSettings, "exclude", segments[1]);
                        }
                    }
                }
            }
        }
Пример #2
0
        public void Create()
        {
            _gitRootDir = GitWorkTreeManager.GetGitRoot(Directory.GetCurrentDirectory());
            if (string.IsNullOrEmpty(_gitRootDir))
            {
                throw new DirectoryNotFoundException($"Cannot find a directory containing a .git directory or file.");
            }
            var resultingIndex = new SortedDictionary <string, List <string> >(StringComparer.OrdinalIgnoreCase);

            var includes = _settings.GetList("include");
            var excludes = _settings.GetList("exclude");

            var dirStack = new Stack <string>();

            dirStack.Push(_gitRootDir);

            while (dirStack.Any())
            {
                var dir     = dirStack.Pop();
                var subDirs = Directory.GetDirectories(dir);
                foreach (var subDir in subDirs)
                {
                    var name = new DirectoryInfo(subDir).Name;

                    if (name.StartsWith(".") || name == "obj" || name.StartsWith("Deploy", StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    dirStack.Push(subDir);

                    var relativeDir = PathExtensions.GetRelativePath(_gitRootDir, subDir);

                    // if we have includes, then use them, otherwise include everything:
                    if (includes.Any() && !includes.Any(x => relativeDir.StartsWith(x, StringComparison.OrdinalIgnoreCase)))
                    {
                        continue;
                    }

                    // split names with dots and spaces:
                    if (name.Contains("."))
                    {
                        var segments = name.Split('.');
                        foreach (var segment in segments)
                        {
                            DictUtils.AddEntryToList(resultingIndex, segment, relativeDir);
                        }
                    }
                    if (name.Contains(" "))
                    {
                        var segments = name.Split(' ');
                        foreach (var segment in segments)
                        {
                            DictUtils.AddEntryToList(resultingIndex, segment, relativeDir);
                        }
                    }

                    DictUtils.AddEntryToList(resultingIndex, name, relativeDir);
                }
            }

            var ccdPath = Path.Combine(_gitRootDir, CcdDirectory);

            Directory.CreateDirectory(ccdPath);
            var indexPath = Path.Combine(ccdPath, IndexFilename);

            using (var writer = new StreamWriter(indexPath))
            {
                foreach (var entry in resultingIndex)
                {
                    var matchingPaths = string.Join("|", entry.Value);
                    writer.WriteLine($"{entry.Key}|{matchingPaths}");
                }
            }
        }