<commit_before>﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Net.Http;
using System.Threading.Tasks;
using System.IO.Compression;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Pomelo.NetCore.Node.Models;
using Pomelo.NetCore.Node.Common;

namespace Pomelo.NetCore.Node.Hubs
{
    public partial class PomeloHub
    {
        public static List<Process> ProcessPool = new List<Process>();

        public object GetProjectInfo(string projectName)
        {
            try
            {
                string path = Path.Combine(Config.RootPath, projectName);
                if (!Directory.Exists(path))
                    return new {isSucceeded = false, msg = $"项目\"{projectName}\"不存在"};

                var info = Dotnet.GetProjectInfo(path);
                return new { isSucceeded = true, projects = info };
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.StackTrace);
                return new { isSucceeded = false, msg = ex.Message };
            }
        }

        public object RunBash()
        {
            var proc = new Process();
            try
            {
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardInput = true;
                proc.StartInfo.CreateNoWindow = true;
                proc.StartInfo.FileName = CodeComb.Package.OS.Current == CodeComb.Package.OSType.Windows ? "cmd" : "bash";
                proc.OutputDataReceived += Proc_OutputDataReceived;
                proc.ErrorDataReceived += Proc_ErrorDataReceived;
                proc.Start();
                proc.StandardInput.WriteLine("chcp 65001");
                proc.BeginErrorReadLine();
                proc.BeginOutputReadLine();
            }
            catch (Exception ex)
            {
                return new { isSucceeded = false, msg = ex.ToString() };
            }

            Task.Factory.StartNew(() =>
            {
                proc.WaitForExit();
                ProcessPool.Remove(proc);

                // 推送进程退出消息
                Clients.Group("process-" + proc.Id).OnOutputDataReceived(proc.Id, proc.OutputSequence++, $"Process has exited with code {proc.ExitCode}.");
                Clients.Group("process-" + proc.Id).OnProcessExited(proc.Id, proc.ExitCode);

                proc.Dispose();
            });

            ProcessPool.Add(proc);

            // 将Caller加入process-id广播组
            Groups.Add(Context.ConnectionId, "process-" + proc.Id);

            return new { isSucceeded = true, pid = proc.Id };
        }

        private void Proc_ErrorDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
        {
            var content = e.Data;
            Clients.Group("process-" + ((Process)sender).Id).OnOutputDataReceived(((Process)sender).Id, ((Process)sender).OutputSequence++, e.Data + "\r\n");
        }

        private void Proc_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
        {
            var content = e.Data;
            Clients.Group("process-" + ((Process)sender).Id).OnOutputDataReceived(((Process)sender).Id, ((Process)sender).OutputSequence++, e.Data + "\r\n");
        }

        public object RunCommand(string projectName, string args, string projectPath)
        {
            var proc = new Process();
            try
            {
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardInput = true;<commit_msg>Fix Node<commit_after>﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Net.Http;
using System.Threading.Tasks;
using System.IO.Compression;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Pomelo.NetCore.Node.Models;
using Pomelo.NetCore.Node.Common;

namespace Pomelo.NetCore.Node.Hubs
{
    public partial class PomeloHub
    {
        public static List<Process> ProcessPool = new List<Process>();

        public object GetProjectInfo(string projectName)
        {
            try
            {
                string path = Path.Combine(Config.RootPath, projectName);
                if (!Directory.Exists(path))
                    return new {isSucceeded = false, msg = $"项目\"{projectName}\"不存在"};

                var info = Dotnet.GetProjectInfo(path);
                return new { isSucceeded = true, projects = info };
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.StackTrace);
                return new { isSucceeded = false, msg = ex.Message };
            }
        }

        public object RunBash()
        {
            var proc = new Process();
            try
            {
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardInput = true;
                proc.StartInfo.CreateNoWindow = true;
                proc.StartInfo.FileName = CodeComb.Package.OS.Current == CodeComb.Package.OSType.Windows ? "cmd" : "bash";
                proc.OutputDataReceived += Proc_OutputDataReceived;
                proc.ErrorDataReceived += Proc_ErrorDataReceived;
                proc.Start();
                proc.StandardInput.WriteLine("chcp 65001");
                proc.BeginErrorReadLine();
                proc.BeginOutputReadLine();
            }
            catch (Exception ex)
            {
                return new { isSucceeded = false, msg = ex.ToString() };
            }

            Task.Factory.StartNew(() =>
            {
                proc.WaitForExit();
                ProcessPool.Remove(proc);

                // 推送进程退出消息
                Clients.Group("process-" + proc.Id).OnOutputDataReceived(proc.Id, proc.OutputSequence++, $"Process has exited with code {proc.ExitCode}.");
                Clients.Group("process-" + proc.Id).OnProcessExited(proc.Id, proc.ExitCode);

                proc.Dispose();
            });

            ProcessPool.Add(proc);

            // 将Caller加入process-id广播组
            Groups.Add(Context.ConnectionId, "process-" + proc.Id);

            return new { isSucceeded = true, pid = proc.Id };
        }

        private void Proc_ErrorDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
        {
            var content = e.Data;
            Clients.Group("process-" + ((Process)sender).Id).OnOutputDataReceived(((Process)sender).Id, ((Process)sender).OutputSequence++, e.Data + "\r\n");
        }

        private void Proc_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
        {
            var content = e.Data;
            Clients.Group("process-" + ((Process)sender).Id).OnOutputDataReceived(((Process)sender).Id, ((Process)sender).OutputSequence++, e.Data + "\r\n");
        }

        public object RunCommand(string projectName, string args, string projectPath)
        {
            var proc = new Process();
            var restore = new Process();
            try
            {
                restore.StartInfo.UseShellExecute = false;
                restore.StartInfo.RedirectStandardError = true;
                restore.StartInfo.RedirectStandardOutput = true;
                restore.StartInfo.RedirectStandardInput = true;
                restore.StartInfo.CreateNoWindow = true;
                restore.StartInfo.WorkingDirectory = Path.Combine(Config.RootPath, projectName, projectPath);
                restore.StartInfo.FileName = "dotnet";
                restore.StartInfo.Arguments = "restore";
                restore.OutputDataReceived += Proc_OutputDataReceived;
                restore.ErrorDataReceived += Proc_ErrorDataReceived;
                restore.Start();
                restore.BeginErrorReadLine();
                restore.BeginOutputReadLine();
                restore.WaitForExit();
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardInput = true;
{'fraction_non_alphanumeric': 0.10342401500938087, 'fraction_numerical': 0.0011726078799249532, 'mean_word_length': 1.878501518731016, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '241093', 'n_tokens_mistral': 2267, 'n_tokens_neox': 2136, 'n_words': 410}