Example #1
0
        /// <summary>
        /// get the aliases from a particular shell source file
        /// </summary>
        /// <param name="rcFile">i.e. ~/.zshrc</param>
        /// <returns>list of all defined aliases</returns>
        public static Dictionary <string, string> Aliases(string rcFile = "~/.zshrc")
        {
            var dict = new Dictionary <string, string>();
            var shrc = new TextFile(rcFile);

            if (shrc.Exists())
            {
                for (int i = 0; i < shrc.Lines.Count; i++)
                {
                    if (shrc.Lines[i].StartsWith("alias"))                     // TODO bashrc syntax
                    {
                        var kvp = shrc.Lines[i].Substring(6).Split(new char[] { '=' });
                        dict.Add(kvp[0], kvp[1]);
                    }
                }
            }
            return(dict);
        }
Example #2
0
        /// <summary>
        /// Used by property Path
        /// </summary>
        /// <param name="rcFile">setup file as used in a source statement for the shell, i.e. "~/.zshrc </param>
        /// <param name="newPath">the directory path to add, i.e. ~/.mlw</param>
        /// <returns>true if Path was added or was there already</returns>
        public static string PATH(string newPath = null, string rcFile = "~/.zshrc")
        {
            var shrc = new TextFile(rcFile);

            if (!shrc.Exists())
            {
                return(null);
            }
            if (!string.IsNullOrEmpty(newPath))
            {
                int pathInserted = -1, exportExists = -1;
                for (int i = 0; i < shrc.Lines.Count; i++)
                {
                    if (shrc.Lines[i].StartsWith("path+="))                     // TODO bashrc syntax
                    {
                        if (shrc.Lines[i] != $"path+={newPath}")
                        {
                            shrc.Insert(i, $"path+={newPath}");
                            // TODO bashrc syntax
                            pathInserted = i++;
                        }
                    }
                    if (shrc.Lines[i].Contains("export PATH"))
                    {
                        exportExists = i;
                    }
                }
                if (pathInserted < 0)
                {
                    if (exportExists < 0)
                    {
                        shrc.Append($"path+={newPath}");
                        shrc.Append("export PATH");
                    }
                }
                else if (exportExists < pathInserted)
                {
                    shrc.Append("export PATH");
                }
                shrc.Save();
            }
            return(EnvironmentVariables["PATH"]);
        }