示例#1
0
        public CommandResult <List <string> > Alias(
            CommandEvaluationContext context,
            [Parameter(0, "name of the alias", true)] string name,
            [Parameter(1, "text of the alias", true)][OptionRequireParameter("name")] string text,
            [Option("s", "save current aliases to user aliases file")] bool save
            )
        {
            var r = new List <string>();

            if (name == null)
            {
                foreach (var kvp in context.CommandLineProcessor.CommandsAlias.Aliases)
                {
                    Out.Echoln(CommandsAlias.BuildAliasCommand(kvp.Key, kvp.Value));
                }
            }
            else
            {
                if (string.IsNullOrWhiteSpace(text))
                {
                    context.CommandLineProcessor.CommandsAlias.UnsetAlias(context, name);
                }
                else
                {
                    context.CommandLineProcessor.CommandsAlias.AddOrReplaceAlias(context, name, text);
                }
            }
            if (save)
            {
                context.CommandLineProcessor.CommandsAlias.SaveAliases(context);
            }
            return(new CommandResult <List <string> >(r));
        }
 public void Initialize()
 {
     if (_isInitialized)
     {
         return;
     }
     // run user profile
     CommandBatchProcessor.RunBatch(CommandEvaluationContext, UserProfileFilePath);
     // run user aliases
     CommandsAlias.Init(CommandEvaluationContext, AppDataFolderPath, CommandsAliasFileName);
     _isInitialized = true;
 }
        void InitializeCommandProcessor(string[] args, bool printInfo = true, CommandEvaluationContext commandEvaluationContext = null)
        {
            SetArgs(args);

            cons.ForegroundColor = DefaultForeground;
            cons.BackgroundColor = DefaultBackground;

            commandEvaluationContext = commandEvaluationContext ??
                                       new CommandEvaluationContext(
                this,
                Out,
                cons.In,
                Err,
                null
                );
            CommandEvaluationContext = commandEvaluationContext;

            if (printInfo)
            {
                PrintInfo(CommandEvaluationContext);
            }

            // assume the application folder ($env.APPDATA/OrbitalShell) exists and is initialized

            var lbr = false;

            // creates user app data folders
            if (!Directory.Exists(AppDataFolderPath))
            {
                LogAppendAllLinesErrorIsEnabled = false;
                Info(ColorSettings.Log + $"creating user shell folder: '{AppDataFolderPath}' ... ", false);
                try
                {
                    Directory.CreateDirectory(AppDataFolderPath);
                    Success();
                } catch (Exception createAppDataFolderPathException)
                {
                    Fail(createAppDataFolderPathException);
                }
                lbr = true;
            }

            // initialize log file
            if (!File.Exists(LogFilePath))
            {
                Info(ColorSettings.Log + $"creating log file: '{LogFilePath}' ... ", false);
                try
                {
                    var logError = Log($"file created on {System.DateTime.Now}");
                    if (logError == null)
                    {
                        Success();
                    }
                    else
                    {
                        throw logError;
                    }
                }
                catch (Exception createLogFileException)
                {
                    LogAppendAllLinesErrorIsEnabled = false;
                    Fail(createLogFileException);
                }
                lbr = true;
            }

            // initialize user profile
            if (!File.Exists(UserProfileFilePath))
            {
                Info(ColorSettings.Log + $"creating user profile file: '{UserProfileFilePath}' ... ", false);
                try
                {
                    var defaultProfileFilePath = Path.Combine(DefaultsFolderPath, UserProfileFileName);
                    File.Copy(defaultProfileFilePath, UserProfileFilePath);
                    Success();
                }
                catch (Exception createUserProfileFileException)
                {
                    Fail(createUserProfileFileException);
                }
                lbr = true;
            }

            // create/restore commands history
            CmdsHistory = new CommandsHistory();
            var createNewHistoryFile = !File.Exists(HistoryFilePath);

            if (createNewHistoryFile)
            {
                Info(ColorSettings.Log + $"creating user commands history file: '{HistoryFilePath}' ... ", false);
            }
            try
            {
                if (createNewHistoryFile)
#pragma warning disable CS0642 // Possibilité d'instruction vide erronée
                {
                    using (var fs = File.Create(HistoryFilePath));
                }
#pragma warning restore CS0642 // Possibilité d'instruction vide erronée
                CmdsHistory.Init(AppDataFolderPath, HistoryFileName);
                if (createNewHistoryFile)
                {
                    Success();
                }
            }
            catch (Exception createUserProfileFileException)
            {
                Fail(createUserProfileFileException);
            }
            lbr |= createNewHistoryFile;

            // create/restore user aliases
            CommandsAlias = new CommandsAlias();
            var createNewCommandsAliasFile = !File.Exists(CommandsAliasFilePath);
            if (createNewCommandsAliasFile)
            {
                Info(ColorSettings.Log + $"creating user commands aliases file: '{CommandsAliasFilePath}' ... ", false);
            }
            try
            {
                if (createNewCommandsAliasFile)
                {
                    var defaultAliasFilePath = Path.Combine(DefaultsFolderPath, CommandsAliasFileName);
                    File.Copy(defaultAliasFilePath, CommandsAliasFilePath);
                }
                if (createNewCommandsAliasFile)
                {
                    Success();
                }
            }
            catch (Exception createUserProfileFileException)
            {
                Fail(createUserProfileFileException);
            }
            lbr |= createNewHistoryFile;

            // end inits
            if (lbr)
            {
                Out.Echoln();
            }

            // load kernel commands
            RegisterCommandsAssembly(CommandEvaluationContext, Assembly.GetExecutingAssembly());
#if enable_test_commands
            RegisterCommandsClass <TestCommands>();
#endif
        }