public static bool CheckGcc() { CmdResult result = CmdRunner.CmdRun("gcc"); if (result.error.Contains("no input files")) { return(true); } else { return(false); } }
public static string GetCodePath() { // 已经找到过VScode if (vscPath != NOTFOUND) { return(vscPath); } // 命令行验证是否在PATH路径 CmdResult result = CmdRunner.CmdRun("code --help"); if (result.result.Contains("To read output from another program, append")) { vscPath = string.Empty; return(vscPath); } else { // 若不在,采用注册表查找 RegistryKey machineKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"); RegistryKey userKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall"); string[] machineKeyList = machineKey.GetSubKeyNames(); string[] userKeyList = userKey.GetSubKeyNames(); foreach (string keyName in machineKeyList) { RegistryKey key = machineKey.OpenSubKey(keyName); object name = key.GetValue("DisplayName"); if (name != null && name.ToString().Contains("Microsoft Visual Studio Code")) { vscPath = key.GetValue("InstallLocation").ToString(); return(vscPath); } } foreach (string keyName in userKeyList) { RegistryKey key = userKey.OpenSubKey(keyName); object name = key.GetValue("DisplayName"); if (name != null && name.ToString().Contains("Microsoft Visual Studio Code")) { vscPath = key.GetValue("InstallLocation").ToString(); return(vscPath); } } return(NOTFOUND); } }
/// <summary> /// 开始安装操作 /// </summary> public void StartInstall() { Logger logger = new Logger("debug.log"); logger.Info("Start install"); logger.Info("GccPath: " + gccPath); logger.Info("projectPath: " + projectPath); ChangeProgress("正在检查gcc环境"); bool hasGcc = EnvChecker.CheckGcc(); logger.Info("Found Gcc: " + hasGcc.ToString()); ChangeProgress("正在检查VScode环境"); string codePath = EnvChecker.GetCodePath(); logger.Info("Found code: " + codePath); if (codePath == EnvChecker.NOTFOUND) { MessageBox.Show("没有找到 VScode,将不会为您配置 C/C++ 插件\n若您已经安装了VScode,请在 VScode 插件列表中搜索安装。", "找不到喵", MessageBoxButton.OK, MessageBoxImage.Exclamation); logger.Warn("Cannot Find code!"); } if (!hasGcc) { if (!File.Exists(@"data\MinGW.7z")) { ChangeProgress("正在解析 MinGW 下载链接"); string downloadUrl = LanzouLinkResolutor.Resolve(lanzouUrl); logger.Info("Created Download Link: " + downloadUrl); ChangeProgress("正在下载 MinGW"); DownloadHelper downloadHelper = new DownloadHelper(); downloadHelper.OnProgressChanged += UpdateDownloadProgress; downloadHelper.Download(downloadUrl, "data"); } ChangeProgress("正在解压 MinGW"); ExtractHelper.Extract(@"data\MinGW.7z", gccPath); ChangeProgress("正在修改用户Path路径"); PathAdder.AddInUserPath(gccPath + "\\bin"); } ChangeProgress("正在配置工作区"); ExtractHelper.Extract(@"data\config.7z", projectPath); string launchPath = projectPath + @"\.vscode\launch.json"; logger.Info("Launch File Path: " + launchPath); string launchContent = File.ReadAllText(launchPath); launchContent = launchContent.Replace("%%cPath%%", gccPath.Replace("\\", "/")); logger.Info("New File Content:\n" + launchContent); File.WriteAllText(launchPath, launchContent); string propertyPath = projectPath + @"\.vscode\c_cpp_properties.json"; logger.Info("Property File Path: " + propertyPath); string propertyContent = File.ReadAllText(propertyPath); propertyContent = propertyContent.Replace("%%cPath%%", gccPath.Replace("\\", "/")); logger.Info("New File Content:\n" + propertyContent); File.WriteAllText(propertyPath, propertyContent); if (codePath != EnvChecker.NOTFOUND) { ChangeProgress("正在安装 C/C++插件"); string command = string.Empty; if (codePath != string.Empty) { command += codePath.Substring(0, 2); command += "&"; command += "cd " + codePath + @"\bin" + "&"; } command += "code --install-extension ms-vscode.cpptools&exit"; CmdResult result = CmdRunner.CmdRun(command); if (!result.result.Contains("is already installed") && !result.result.Contains("was successfully installed")) { MessageBox.Show("未能成功安装 C/C++ 插件。请手动安装哦", "安装失败了喵", MessageBoxButton.OK, MessageBoxImage.Exclamation); } } }