Esempio n. 1
0
        public static string BashOrNot(string path)
        {
            var bashPrompt = Environment.GetEnvironmentVariable("PS1");

            if (!string.IsNullOrEmpty(bashPrompt) && bashPrompt.Any(char.IsLetter))
            {
                return(PathExtensions.GetBashPath(path));
            }
            else
            {
                return(path);
            }
        }
Esempio n. 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}");
                }
            }
        }
Esempio n. 3
0
        private static void Main(string[] args)
        {
            try
            {
                var currentGitRoot = GitWorkTreeManager.GetGitRoot(Directory.GetCurrentDirectory());
                var settings       = new Settings(Path.Combine(currentGitRoot, ".ccd", "settings"));

                if (args.Length == 1 && args[0] == "-i")
                {
                    var indexManager = new IndexManager(settings);
                    indexManager.Create();
                }
                else if (args.Any())
                {
                    var path = args[0];
                    if (Directory.Exists(path))
                    {
                        Console.WriteLine($"#!cd {Utils.BashOrNot(path)}");
                    }
                    else if (File.Exists(path))
                    {
                        Console.WriteLine($"#!cd {Utils.BashOrNot(Path.GetDirectoryName(path))}");
                    }
                    else if (path == "gl")
                    {
                        var gitWorktreeInfo = new GitWorkTreeManager(Directory.GetCurrentDirectory());
                        var worktrees       = gitWorktreeInfo.WorkTrees;

                        int count = 0;
                        foreach (var worktree in worktrees)
                        {
                            Console.WriteLine($"{count++} {worktree.BranchName} {worktree.Directory} {worktree.Description}");
                        }
                    }
                    else if (path[0] == 'g' && path.Substring(1).All(char.IsDigit))
                    {
                        var index           = Int32.Parse(path.Substring(1));
                        var gitWorktreeInfo = new GitWorkTreeManager(Directory.GetCurrentDirectory());
                        var worktrees       = gitWorktreeInfo.WorkTrees;
                        if (index > worktrees.Length - 1)
                        {
                            throw new Exception($"index out of range - maximum is {worktrees.Length - 1}");
                        }

                        var relpath       = PathExtensions.GetRelativePath(currentGitRoot, Directory.GetCurrentDirectory());
                        var fullOtherPath = Path.Combine(worktrees[index].Directory, relpath);
                        Console.WriteLine($"#!cd {Utils.BashOrNot(fullOtherPath)}");
                    }
                    else if (path[0] == 'g' && int.TryParse(path.Substring(1, path.Length - 2), out var index) && char.IsLetter(path.Last()))
                    {
                        var gitWorktreeInfo = new GitWorkTreeManager(Directory.GetCurrentDirectory());
                        var worktrees       = gitWorktreeInfo.WorkTrees;
                        if (index > worktrees.Length - 1)
                        {
                            throw new Exception($"index out of range - maximum is {worktrees.Length - 1}");
                        }
                        SubstMappingsHelper.CreateMapping(path.Last(), worktrees[index].Directory, deleteIfExisting: true);
                    }
                    else if (path[0] == 'g' && path.Last() == '-' && char.IsLetter(path[1]))
                    {
                        SubstMappingsHelper.DeleteMapping(path[1]);
                    }
                    else
                    {
                        var pattern      = args.Length > 1 ? args[1] : string.Empty;
                        var indexManager = new IndexManager(settings);
                        indexManager.Lookup(path, pattern);
                    }
                }
            }
            catch (Exception ex)
            {
                var fullname = System.Reflection.Assembly.GetEntryAssembly().Location;
                var progname = Path.GetFileNameWithoutExtension(fullname);
                Console.Error.WriteLine(progname + ": Error: " + ex.Message);
            }
        }