internal int Execute(CommandOption isDebug, CommandOption keepMemory, CommandOption fastWrite) { var dir = Directory.GetCurrentDirectory(); if (!Validate(dir)) { return(1); } var ancient_home = Environment.GetEnvironmentVariable("ANCIENT_HOME", EnvironmentVariableTarget.User); if (ancient_home is null) { throw new InvalidOperationException($"env variable 'ANCIENT_HOME' is not set."); } if (!new DirectoryInfo(ancient_home).Exists) { throw new InvalidOperationException($"Env variable 'ANCIENT_HOME' is invalid."); } var vm_home = Path.Combine(ancient_home, "vm"); var vm_bin = Path.Combine(vm_home, "vm.exe"); if (!new DirectoryInfo(vm_home).Exists || !new FileInfo(vm_bin).Exists) { throw new InvalidOperationException($"Ancient VM is not installed."); } var argBuilder = new List <string>(); var files = Directory.GetFiles(Path.Combine("obj"), "*.*") .Where(x => x.EndsWith(".dlx") || x.EndsWith(".bios")); argBuilder.Add($"\"{Path.Combine("obj", Path.GetFileNameWithoutExtension(files.First()))}\""); var external = new ExternalTools(vm_bin, string.Join(" ", argBuilder)); return(external .WithEnv("VM_ATTACH_DEBUGGER", isDebug.BoolValue.HasValue) .WithEnv("VM_KEEP_MEMORY", keepMemory.BoolValue.HasValue) .WithEnv("VM_MEM_FAST_WRITE", fastWrite.BoolValue.HasValue) .WithEnv("CLI", true) .WithEnv("CLI_WORK_PATH", dir) .Start() .Wait().ExitCode()); }
internal async Task <int> Execute(CommandOption isDebug, CommandOption keepMemory, CommandOption fastWrite, CommandOption isInteractive) { var dir = Directory.GetCurrentDirectory(); if (!this.Validate(dir)) { return(await Fail()); } if (!Dirs.Bin.VM.Exists) { return(await Fail($"VM is not installed. Try 'rune install vm'")); } var vm_bin = Dirs.Bin.VM.FullName; var argBuilder = new List <string>(); if (!Directory.Exists("obj")) { Directory.CreateDirectory("obj"); } var files = Directory.GetFiles(Path.Combine("obj"), "*.*") .Where(x => x.EndsWith(".dlx") || x.EndsWith(".bios")).ToArray(); if (files.Length > 1 && !files.Any(x => x.Contains("entry"))) { return(await Fail($"Cannot find 'entry.dlx'")); } string GetRunArg(Func <string, bool> predicate) { var target = files.First(predicate); var formatted = Path.GetFileNameWithoutExtension(target); return($"\"{Path.Combine("obj", formatted)}\""); } argBuilder.Add(files.Length == 1 ? GetRunArg(_ => true) : GetRunArg(x => x.Contains("entry"))); var external = new ExternalTools(vm_bin, string.Join(" ", argBuilder)); var result = external .WithEnv("VM_ATTACH_DEBUGGER", isDebug.BoolValue.HasValue) .WithEnv("VM_KEEP_MEMORY", keepMemory.BoolValue.HasValue) .WithEnv("VM_MEM_FAST_WRITE", fastWrite.BoolValue.HasValue) .WithEnv("REPL", isInteractive.BoolValue.HasValue) .WithEnv("CLI", true) .WithEnv("CLI_WORK_PATH", dir); try { return(result .Start() .Wait() .ExitCode()); } catch (Win32Exception e) // AccessDenied on linux { WriteLine($"{":x:".Emoji()} {e.Message}"); return(await Fail($"Run [chmod +x \"{vm_bin}\"] for resolve this problem.")); } }