Exemple #1
0
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="sorceFile"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public bool GenCsharp(string sorceFile, EnumGrpcGenType genType, out string errorMsg, ServiceModel serviceModel = null)
        {
            #region 逻辑判断
            errorMsg = string.Empty;
            if (string.IsNullOrWhiteSpace(sorceFile))
            {
                errorMsg = "请输入源文件地址";
                return(false);
            }

            if (!System.IO.File.Exists(sorceFile))
            {
                errorMsg = "您输入的地址文件不存在";
                return(false);
            }

            string ext = System.IO.Path.GetExtension(sorceFile).ToLower();
            if (ext != ".proto")
            {
                errorMsg = "只支持proto文件";
                return(false);
            }
            #endregion

            try
            {
                var resultDic   = string.Empty;
                var csNamespace = string.Empty;
                var result      = false;
                var tempKey     = Guid.NewGuid().ToString().Substring(0, 6);
                switch (genType)
                {
                case EnumGrpcGenType.Origin:
                    result = GenExecutor.Execute(sorceFile, EnumGrpcGenType.Origin, tempKey, out csNamespace, out errorMsg, out resultDic);
                    break;

                case EnumGrpcGenType.GenDll:
                    result = GenExecutor.ExecuteDll(sorceFile, tempKey, out csNamespace, out errorMsg, out resultDic);
                    break;

                case EnumGrpcGenType.GenNuget:
                    result = GenExecutor.ExcuteNuget(sorceFile, serviceModel, tempKey, out csNamespace, out errorMsg, out resultDic);
                    break;

                default:
                    errorMsg = "命令错误...";
                    break;
                }

                if (result)
                {
                    // 打开当前目录
                    Process.Start(resultDic);
                }
                return(result);
            }
            catch (Exception ex)
            {
                errorMsg = ex.Message;
                return(false);
            }
        }
Exemple #2
0
        /// <summary>
        /// 生成异步或者通过的代码文件
        /// </summary>
        /// <param name="fullFilePathList"></param>
        public static bool Execute(string filePath, EnumGrpcGenType genType, string tempKey, out string csNamespace, out string errorMsg, out string resultDic)
        {
            csNamespace = string.Empty;
            errorMsg    = string.Empty;
            resultDic   = string.Empty;
            if (!Utility.CheckFilePath(filePath))
            {
                errorMsg = "没有文件可以生成";
                return(false);
            }

            var csFiles = GenUtilityOrigin.GenGrpc(filePath, out errorMsg);

            if (!string.IsNullOrEmpty(errorMsg))
            {
                return(false);
            }

            if (csFiles == null || csFiles.Length == 0)
            {
                errorMsg = "proto 协议文件有异常";
                return(false);
            }
            resultDic = csFiles[0].Directory.FullName;

            // 生成配置文件
            var fmConfigs = GenUtilityOrigin.MakeFmConfig();

            if (fmConfigs != null && fmConfigs.Count > 0)
            {
                var fmConfigDic = Path.Combine(resultDic, "fmconfigs");
                Utility.MakeDir(fmConfigDic);

                foreach (var configPair in fmConfigs)
                {
                    var dllconfigFileName = Path.Combine(fmConfigDic, configPair.Key);
                    Utility.WriteNewFile(dllconfigFileName, configPair.Value);
                }
            }

            var coreConfigs = GenUtilityOrigin.MakeCoreConfig();

            if (fmConfigs != null && fmConfigs.Count > 0)
            {
                var coreConfigDic = Path.Combine(resultDic, "coreconfigs");
                Utility.MakeDir(coreConfigDic);

                foreach (var configPair in coreConfigs)
                {
                    var dllconfigFileName = Path.Combine(coreConfigDic, configPair.Key);
                    Utility.WriteNewFile(dllconfigFileName, configPair.Value);
                }
            }


            //如果只需要生成原生的,就直接返回
            if (genType == EnumGrpcGenType.Origin)
            {
                return(true);
            }

            #region 第一次编译
            // 每次生成都只能生成一次文件夹路径
            var fileName    = new FileInfo(filePath).Name;
            var projDicPath = GrpcGlobal.GenProjDic(fileName);
            if (string.IsNullOrEmpty(projDicPath))
            {
                errorMsg = "Create gen temp folder error,may u have already open it!";
                return(false);
            }

            // 组织这些文件成为一个project.xml文件
            var projXml     = BuildGrpcProject.MakeProj(csFiles, "grpcProj");
            var projXmlPath = Path.Combine(projDicPath, "grpcProj.csproj");
            Utility.WriteNewFile(projXmlPath, projXml);

            // 拷贝json文件
            GrpcGlobal.MoveProjectAsset(fileName);

            // 调用MSBuild生成这个项目
            var msbuildPath = GrpcGlobal.MsBuildPath();
            if (string.IsNullOrEmpty(msbuildPath))
            {
                errorMsg = "VS2017安装目录中找不到MSBuild.exe,或者安装的VS2017目录没有2017标识,或者VS2017从未打开过, 请检查注册表 \r\n Software\\Classes\\Local Settings\\Software\\Microsoft\\Windows\\Shell\\MuiCache";
                return(false);
            }

            // 调用restore
            Utility.RunCmd("dotnet", "restore", projDicPath, out errorMsg);

            // 为防止有的客户端路径中包含空格影响参数的设置
            // 设置运行的目录在客户端当前目录调用MSBuild
            Utility.RunProcess(msbuildPath, projDicPath, "grpcProj.csproj");
            #endregion

            #region  制到专门的路径
            var net46Fold   = Path.Combine(projDicPath, @"bin\Debug\net46");
            var assemblyDic = GrpcGlobal.GetLoadAssemblyDic(fileName, tempKey);
            Utility.Copy(net46Fold, assemblyDic);
            #endregion

            // 获取命名空间
            var grpcDllPath = Path.Combine(assemblyDic, "net46", "grpcProj.dll");

            // 创建一个ClientManager
            var proxyCode     = GenUtilityOrigin.GenAsyncProxyCs(grpcDllPath, out csNamespace);
            var proxyFilePath = Path.Combine(resultDic, "ClientManager.cs");
            Utility.WriteNewFile(proxyFilePath, proxyCode);

            #region 第二次编译
            // 每次生成都只能生成一次文件夹路径
            fileName    = new FileInfo(filePath).Name;
            projDicPath = GrpcGlobal.GenProjDic(fileName);
            if (string.IsNullOrEmpty(projDicPath))
            {
                errorMsg = "Create gen temp folder error,may u have already open it!";
                return(false);
            }

            // 组织这些文件成为一个project.xml文件
            csFiles     = new DirectoryInfo(resultDic).GetFiles("*.cs");
            projXml     = BuildGrpcProject.MakeProj(csFiles, csNamespace);
            projXmlPath = Path.Combine(projDicPath, csNamespace + ".csproj");
            Utility.WriteNewFile(projXmlPath, projXml);

            // 拷贝json文件
            GrpcGlobal.MoveProjectAsset(fileName);

            // 调用MSBuild生成这个项目
            msbuildPath = GrpcGlobal.MsBuildPath();
            if (string.IsNullOrEmpty(msbuildPath))
            {
                errorMsg = "VS2017安装目录中找不到MSBuild.exe,或者安装的VS2017目录没有2017标识,或者VS2017从未打开过, 请检查注册表 \r\n Software\\Classes\\Local Settings\\Software\\Microsoft\\Windows\\Shell\\MuiCache";
                return(false);
            }

            // 调用restore
            Utility.RunCmd("dotnet", "restore", projDicPath, out errorMsg);


            // 为防止有的客户端路径中包含空格影响参数的设置
            // 设置运行的目录在客户端当前目录调用MSBuild
            Utility.RunProcess(msbuildPath, projDicPath, csNamespace + ".csproj");
            #endregion

            grpcDllPath = Path.Combine(projDicPath, @"bin\Debug\net46", csNamespace + ".dll");
            resultDic   = GenUtilityOrigin.ToResultDic(fileName, grpcDllPath, csNamespace, out errorMsg);
            if (!string.IsNullOrEmpty(errorMsg))
            {
                return(false);
            }
            return(true);
        }