/// <summary> /// Executes file at specified path. /// </summary> /// <param name="manager"></param> /// <param name="path">Path to file with fake root.</param> /// <returns></returns> private bool Exec(IManipulatorsManager manager, string path) { var c = manager.GetContext <FileSystemContext>(); var root = c.Roots.Find(GetRoot(path)); if (root == null) { return(false); } var pathOrig = root.ToActualPath(path); if (!File.Exists(pathOrig)) { return(false); } if (!string.IsNullOrWhiteSpace(c.FileSearchPattern)) { // Check if file matches search pattern: var dir = Directory.GetParent(pathOrig); var files = dir.GetFiles(c.FileSearchPattern).Select(x => x.FullName.ToLowerInvariant()); if (!files.Contains(pathOrig.ToLowerInvariant())) { return(false); } } // Check if folders filter allows that file containing folder. if (c.FolderFilter?.Invoke(Directory.GetParent(pathOrig)?.FullName) == false) { return(false); } // Check if file filter allows that file. if (c.FileFilter?.Invoke(pathOrig) == false) { return(false); } try { var process = new Process { StartInfo = new ProcessStartInfo { UseShellExecute = true, FileName = pathOrig } }; process.Start(); return(true); } catch { // ignored } return(false); }
/// <inheritdoc /> public object Execute(IManipulatorsManager manager, string param) { var context = manager.GetContext <T>(); return(context != null && _action.Invoke(context, param)); }
/// <summary> /// Enumerates content of specified directory. /// </summary> /// <param name="manager"></param> /// <param name="param">Path to directory with fake root.</param> /// <returns></returns> private IReadOnlyDictionary <string, IEnumerable <string> > List(IManipulatorsManager manager, string param) { var c = manager.GetContext <FileSystemContext>(); var rv = new Dictionary <string, IEnumerable <string> >(); if (c?.Roots?.Any() != true) { return(null); } if (string.IsNullOrWhiteSpace(param)) { if (c.Roots.Count == 1) { PopulateContent(rv, c, c.Roots[0].Path); } else { rv["folders"] = c.Roots.Select(x => x.Name).ToArray(); } return(rv); } PopulateContent(rv, c, param); return(rv.Any() ? rv : null); }
private static void AddTo(this IEnumerable <IManipulation> manipulations, IManipulatorsManager manager) { foreach (var manipulation in manipulations) { manager.Add(manipulation); } }
public object Execute(IManipulatorsManager manager, string param) { if (_handler == null) { return(default(TResult)); } return(_handler.Invoke(manager, param)); }
public CommandsInformer(IManipulatorsManager manager, Logger logger) { _manager = manager ?? throw new ArgumentNullException(nameof(manager)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _cooldown = new EventCooldown( TimeSpan.FromMilliseconds(250), () => Check(), TimeSpan.FromSeconds(1)); _manager.ItemStateChanged += ManagerOnItemStateChanged; }
public object Execute(IManipulatorsManager manager, string param) { switch (Mode) { case FileSystemManipulationMode.Exec: return(Exec(manager, param)); case FileSystemManipulationMode.List: return(List(manager, param)); default: return(null); } }
private object ExecutePowerShell(IManipulatorsManager manager, string param) { if (!File.Exists(_data)) { return(false); } var dir = DefaultWorkingDirectory; if (!string.IsNullOrWhiteSpace(_workingDirectory) && Directory.Exists(_workingDirectory)) { dir = _workingDirectory; } var arguments = $"-ExecutionPolicy Bypass -File \"{_data}\""; if (_supportParam) { arguments = $"{arguments} \"{param}\""; } Process proc = new Process // https://stackoverflow.com/a/22869734/1763586 { StartInfo = { FileName = @"powershell.exe", Arguments = arguments, WorkingDirectory = dir, UseShellExecute = false, RedirectStandardOutput = true, } }; if (_hidden) { proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; } proc.Start(); var output = proc.StandardOutput.ReadToEnd(); if (_wait) { proc.WaitForExit(); } return(output); }
/// <inheritdoc /> public object Execute(IManipulatorsManager manager, string param) { switch (_type) { case ManipulationCommandType.Code: return(ExecuteCode(manager, param)); case ManipulationCommandType.File: return(ExecuteFile(manager, param)); case ManipulationCommandType.PowerShell: return(ExecutePowerShell(manager, param)); default: throw new ArgumentOutOfRangeException(); } }
private object ExecuteFile(IManipulatorsManager manager, string param) { if (!File.Exists(_data)) { return(false); } var dir = DefaultWorkingDirectory; if (!string.IsNullOrWhiteSpace(_workingDirectory) && Directory.Exists(_workingDirectory)) { dir = _workingDirectory; } Process proc = new Process { StartInfo = { FileName = _data, Arguments = _supportParam && !string.IsNullOrEmpty(param) ? param : string.Empty, WorkingDirectory = dir, UseShellExecute = false, RedirectStandardOutput = true, } }; if (_hidden) { proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; } proc.Start(); var output = proc.StandardOutput.ReadToEnd(); if (_wait) { proc.WaitForExit(); } return(output); }
public CommandsPageViewModel( Dispatcher dispatcher, Options options, CommandsConfig config, IManipulatorsManager manager ) : base(PageName.Commands) { Options = options ?? throw new ArgumentNullException(nameof(options)); Config = config ?? throw new ArgumentNullException(nameof(config)); Manager = manager ?? throw new ArgumentNullException(nameof(manager)); Commands = new ObservableCollection <CommandViewModel> { Dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher)) }; Commands.RegisterHandler(CommandsHandler); CreateCommand = new RelayCommand(CreateCommandHandler); Manager.ItemStateChanged += ManagerOnItemStateChanged; ProceedConfig(); }