Example #1
0
        /// <summary>
        /// Analyzes the command line for the AltCmd application, to determine
        /// which store (if any) should be initialized.
        /// </summary>
        /// <param name="branchRef">The reference to the branch (either the path
        /// to a folder that holds an AC file, or the AC file itself).
        /// </param>
        /// <param name="args">The command line arguments.</param>
        /// <returns>The execution context (wraps a reference to the store that
        /// the user has specified)</returns>
        static ExecutionContext GetContext(string branchRef)
        {
            string acSpec = null;

            if (String.IsNullOrEmpty(branchRef))
            {
                // If nothing was specified, look in the current folder
                string curDir = Directory.GetCurrentDirectory();
                acSpec = FileStore.GetAcFilePath(curDir);

                // If nothing in current folder, check last used branch
                if (acSpec == null)
                {
                    string progData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
                    string last     = Path.Combine(progData, "AltCmd", "last");
                    if (File.Exists(last))
                    {
                        string lastSpec = File.ReadAllText(last);
                        if (File.Exists(lastSpec))
                        {
                            acSpec = lastSpec;
                        }
                    }
                }

                if (acSpec == null)
                {
                    throw new ArgumentException("Cannot locate last used command branch");
                }
            }
            else
            {
                if (File.Exists(branchRef))
                {
                    acSpec = branchRef;
                }
                else if (Directory.Exists(branchRef))
                {
                    acSpec = FileStore.GetAcFilePath(branchRef);
                    if (acSpec == null)
                    {
                        throw new ArgumentException("Specified folder does not contain any command data");
                    }
                }
                else
                {
                    throw new ArgumentException("No such file: " + branchRef);
                }
            }

            // Load branch metadata for the store
            var cs = FileStore.Load(acSpec);

            Log.Info($"Opened {cs.Name} (current branch is {cs.Current.Name})");

            // Remember the store we opened as the last one
            cs.SaveCurrent();

            // Load up the command stream as well
            cs.Stream = cs.Current.CreateStream();

            return(new ExecutionContext(cs));
        }
Example #2
0
        /// <summary>
        /// Analyzes the command line for the AltCmd application, to determine
        /// which store (if any) should be initialized.
        /// </summary>
        /// <param name="args">The command line arguments.</param>
        /// <returns>The execution context (wraps a reference to the store that
        /// the user has specified)</returns>
        static ExecutionContext GetContext(string[] args)
        {
            // If the user is trying to initialize (or clone), don't try to determine
            // the initial store
            if (args.Length > 0)
            {
                string verb = args[0];

                if (verb.EqualsIgnoreCase("init") || verb.EqualsIgnoreCase("clone"))
                {
                    return(new ExecutionContext());
                }
            }

            // Check for a SQLite database
            if (args.Length == 1)
            {
                string dbFile = args[0];
                string dbType = Path.GetExtension(dbFile)?.ToLower();

                if (!dbType.IsDefined())
                {
                    dbType  = ".ac-sqlite";
                    dbFile += dbType;
                }

                if (File.Exists(dbFile) && dbType.Contains("sqlite"))
                {
                    // Load branch metadata for the store
                    var ss = SQLiteStore.Load(dbFile);
                    Log.Info($"Opened {ss.Name} (current branch is {ss.Current.Name})");

                    // Load up the command stream as well
                    ss.Stream = ss.Current.CreateStream();
                    return(new ExecutionContext(ss));
                }
            }

            string acSpec = null;
            string curDir = null;

            // If we've been given nothing, start by looking for
            // an AC file in the current folder. If we've been
            // given a specific folder, look in there instead.

            if (args.Length == 0)
            {
                curDir = Directory.GetCurrentDirectory();
            }
            else if (args.Length == 1 && Directory.Exists(args[0]))
            {
                curDir = args[0];
            }

            if (curDir != null)
            {
                acSpec = FileStore.GetAcFilePath(curDir);
            }

            // If we couldn't determine an initial branch based
            // on folder, see if we've been supplied with an AC file

            if (acSpec == null)
            {
                if (args.Length == 1 && File.Exists(args[0]))
                {
                    string fileName = args[0];

                    if (Path.GetExtension(fileName) == ".ac")
                    {
                        acSpec = fileName;
                    }
                    else
                    {
                        throw new ArgumentException("Unrecognized file type");
                    }
                }
            }

            // If that doesn't give us anything, look in C:\ProgramData\AltCmd
            // to obtain the last one we opened

            string progData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
            string last     = Path.Combine(progData, "AltCmd", "last");

            if (acSpec == null && File.Exists(last))
            {
                string lastSpec = File.ReadAllText(last);
                if (File.Exists(lastSpec))
                {
                    acSpec = lastSpec;
                }
            }

            if (acSpec == null)
            {
                return(new ExecutionContext());
            }

            // Load branch metadata for the store
            FileStore fs = FileStore.Load(acSpec);

            Log.Info($"Opened {fs.Name} (current branch is {fs.Current.Name})");

            // Remember the store we opened as the last one
            fs.SaveCurrent();

            // Load up the command stream as well
            fs.Stream = fs.Current.CreateStream();

            return(new ExecutionContext(fs));
        }