public static int Execute(params string[] args) { // Trigger plugin manager before anything else. if (ExecutorClient.IsRunningIsolated) { // TODO: This is not needed right now, but might be again when we fix the TODO in tap.exe //PluginManager.DirectoriesToSearch.Clear(); //PluginManager.DirectoriesToSearch.Add(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)); using (var tpmClient = new ExecutorClient()) { tpmClient.MessageServer("delete " + Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)); } } // Set TapMutex to ensure any installers know about running OpenTAP processes. ReflectionHelper.SetTapMutex(); try { // Turn off the default system behavior when CTRL+C is pressed. // When Console.TreatControlCAsInput is false, CTRL+C is treated as an interrupt instead of as input. Console.TreatControlCAsInput = false; } catch { } try { var execThread = TapThread.Current; Console.CancelKeyPress += (s, e) => { e.Cancel = true; execThread.Abort(); }; } catch { } CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; // Find the called action if (!TypeData.GetDerivedTypes <ICliAction>().Any()) { Console.WriteLine("No commands found. Please try reinstalling OpenTAP."); return(1); } try { // setup logging to be relative to the executing assembly. // at this point SessionLogs.Initialize has already been called (PluginManager.Load). // so the log is already being saved at a different location. var logpath = EngineSettings.Current.SessionLogPath.Expand(date: Process.GetCurrentProcess().StartTime); bool isPathRooted = Path.IsPathRooted(logpath); if (isPathRooted == false) { var dir = Path.GetDirectoryName(typeof(SessionLogs).Assembly.Location); if (ExecutorClient.IsRunningIsolated) { // redirect the isolated log path to the non-isolated path. dir = ExecutorClient.ExeDir; } logpath = Path.Combine(dir, logpath); } SessionLogs.Rename(logpath); } catch (Exception e) { log.Error("Path defined in Engine settings contains invalid characters: {0}", EngineSettings.Current.SessionLogPath); log.Debug(e); } ITypeData selectedCommand = null; // Find selected command var actionTree = new CliActionTree(); var selectedcmd = actionTree.GetSubCommand(args); if (selectedcmd?.Type != null && selectedcmd?.SubCommands.Any() != true) { selectedCommand = selectedcmd.Type; } void print_command(CliActionTree cmd, int level, int descriptionStart) { if (cmd.IsBrowsable) { int relativePadding = descriptionStart - (level * LevelPadding); // Calculate amount of characters to pad right before description start to ensure description alignments. Console.Write($"{"".PadRight(level * LevelPadding)}{cmd.Name.PadRight(relativePadding)}"); if (cmd.Type?.IsBrowsable() ?? false) { Console.WriteLine($"{cmd.Type.GetDisplayAttribute().Description}"); } else { Console.WriteLine(); } if (cmd.IsGroup) { foreach (var subCmd in cmd.SubCommands) { print_command(subCmd, level + 1, descriptionStart); } } } } // Print default info if (selectedCommand == null) { Console.WriteLine("OpenTAP Command Line Interface ({0})", Assembly.GetExecutingAssembly().GetSemanticVersion().ToString(4)); Console.WriteLine("Usage: tap <command> [<subcommand(s)>] [<args>]\n"); if (selectedcmd == null) { Console.WriteLine("Valid commands are:"); foreach (var cmd in actionTree.SubCommands) { print_command(cmd, 0, actionTree.GetMaxCommandTreeLength(LevelPadding) + LevelPadding); } } else { Console.Write("Valid subcommands of "); print_command(selectedcmd, 0, actionTree.GetMaxCommandTreeLength(LevelPadding) + LevelPadding); } Console.WriteLine($"\nRun \"{(OperatingSystem.Current == OperatingSystem.Windows ? "tap.exe" : "tap")} " + "<command> [<subcommand>] -h\" to get additional help for a specific command.\n"); if (args.Length == 0 || args.Any(s => s.ToLower() == "--help" || s.ToLower() == "-h")) { return(0); } else { return(-1); } } if (selectedCommand != TypeData.FromType(typeof(RunCliAction)) && UserInput.Interface == null) // RunCliAction has --non-interactive flag and custom platform interaction handling. { CliUserInputInterface.Load(); } ICliAction packageAction = null; try{ packageAction = (ICliAction)selectedCommand.CreateInstance(); }catch (TargetInvocationException e1) when(e1.InnerException is System.ComponentModel.LicenseException e) { Console.Error.WriteLine("Unable to load CLI Action '{0}'", selectedCommand.GetDisplayAttribute().GetFullName()); Console.Error.WriteLine(e.Message); return(-4); } if (packageAction == null) { Console.WriteLine("Error instantiating command {0}", selectedCommand.Name); return(-3); } try { int skip = selectedCommand.GetDisplayAttribute().Group.Length + 1; // If the selected command has a group, it takes two arguments to use the command. E.g. "package create". If not, it only takes 1 argument, E.g. "restapi". return(packageAction.Execute(args.Skip(skip).ToArray())); } catch (ExitCodeException ec) { log.Error(ec.Message); return(ec.ExitCode); } catch (ArgumentException ae) { // ArgumentException usually contains several lines. // Only print the first line as an error message. // Example message: // "Directory is not a git repository. // Parameter name: repositoryDir" var lines = ae.Message.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); log.Error(lines.First()); for (int i = 1; i < lines.Length; i++) { log.Debug(lines[i]); } return(-1); } catch (OperationCanceledException ex) { log.Error(ex.Message); return(1); } catch (Exception ex) { log.Error(ex.Message); log.Debug(ex); return(-1); } finally { Log.Flush(); } }
internal static void RunIsolated(string application = null, string target = null, IsolatedPackageAction isolatedAction = null) { using (var tpmClient = new ExecutorClient()) { var packages = new Installation(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)).GetPackages(); PackageDef findPackageWithFile(string file) { foreach (var package in packages) { foreach (var pkgfile in package.Files) { var filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), pkgfile.FileName); if (string.Equals(NormalizePath(filePath), NormalizePath(file), StringComparison.OrdinalIgnoreCase)) { return(package); } } } return(null); } var exec = application ?? Assembly.GetEntryAssembly().Location; PackageDef pkg = findPackageWithFile(Path.GetFullPath(exec)); if (pkg == null) { throw new InvalidOperationException($"{Path.GetFileName(exec)} was not installed through a package."); } var dependencies = pkg.Dependencies.ToList(); if (isolatedAction != null) { // If the executing IsolatedPackageAction does not origin from OpenTAP package, we need to include it when we copy and run isolated var actionAsm = isolatedAction.GetType().Assembly.Location; PackageDef isolatedActionPackage = findPackageWithFile(Path.GetFullPath(actionAsm)); if (isolatedActionPackage == null) { throw new InvalidOperationException($"{Path.GetFileName(actionAsm)} was not installed through a package."); } if (pkg.Name != isolatedActionPackage.Name) { if (!dependencies.Any(p => p.Name == isolatedActionPackage.Name)) { dependencies.Add(new PackageDependency(isolatedActionPackage.Name, new VersionSpecifier(isolatedActionPackage.Version, VersionMatchBehavior.Compatible))); } } } // when installing/uninstalling packages we might need to use custom package actions as well. var extraDependencies = PluginManager.GetPlugins <ICustomPackageAction>().Select(t => t.Assembly.Location).Distinct().ToList(); foreach (var exDep in extraDependencies) { var package = findPackageWithFile(exDep); if (package != null && !dependencies.Any(p => p.Name == package.Name)) { dependencies.Add(new PackageDependency(package.Name, new VersionSpecifier(package.Version, VersionMatchBehavior.Compatible))); } } var deps = OpenTap.Utils.FlattenHeirarchy(dependencies, dep => (IEnumerable <PackageDependency>)packages.FirstOrDefault(x => x.Name == dep.Name)?.Dependencies ?? Array.Empty <PackageDependency>(), distinct: true); if (false == deps.Any(x => x.Name == pkg.Name)) { deps.Add(new PackageDependency(pkg.Name, new VersionSpecifier(pkg.Version, VersionMatchBehavior.Compatible))); } bool force = isolatedAction?.Force ?? false; List <string> allFiles = new List <string>(); foreach (var d in deps) { var availPackages = packages.Where(p => p.Name == d.Name); var package = availPackages.FirstOrDefault(p => d.Version.IsCompatible(p.Version)); if (package == null) { package = availPackages.FirstOrDefault(); if (!force) { if (package != null) { throw new Exception($"Cannot find compatible dependency '{d.Name}' {d.Version}. Version {package.Version} is installed."); } throw new Exception($"Cannot find needed dependency '{d.Name}'."); } log.Warning("Unable to find compatible package, using {0} v{1} instead.", package.Name, package.Version); } var defPath = String.Join("/", PackageDef.PackageDefDirectory, package.Name, PackageDef.PackageDefFileName); if (File.Exists(defPath)) { allFiles.Add(defPath); } var fs = package.Files; var brokenPackages = new HashSet <string>(); foreach (var file in fs) { string loc = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), file.FileName); if (!File.Exists(loc)) { brokenPackages.Add(package.Name); log.Debug($"Could not find file '{loc}' part of package '{package.Name}'."); continue; } allFiles.Add(file.FileName); } foreach (var name in brokenPackages) { log.Warning($"Package '{name}' has missing files and is broken."); } } string tempFolder = Path.GetFullPath(FileSystemHelper.CreateTempDirectory()); foreach (var _loc in allFiles.Distinct()) { string loc = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), _loc); var newloc = Path.Combine(tempFolder, _loc); OpenTap.FileSystemHelper.EnsureDirectory(newloc); if (File.Exists(newloc)) { continue; } File.Copy(loc, newloc); } { // tell TPM Server to start new app. var loc = application ?? Assembly.GetEntryAssembly().Location; if (string.Equals(".dll", Path.GetExtension(loc), StringComparison.OrdinalIgnoreCase)) { //.netcore wierdness. loc = Path.ChangeExtension(loc, "exe"); if (File.Exists(loc) == false) { loc = loc.Substring(0, loc.Length - ".exe".Length); } } var newname = Path.Combine(tempFolder, Path.GetFileName(loc)); newname = $"\"{newname}\""; // there could be whitespace in the name. // now that we start from a different dir, we need to supply a --target argument if (target != null) { newname = $"{newname} --target \"{target}\""; } tpmClient.MessageServer("run " + newname); tpmClient.Dispose(); } } }