示例#1
0
文件: Program.cs 项目: yuzs/donetci
        //测试通过.net调用Msbuild
        static void MSBuildAndCompress()
        {
            var rootpath = WorkPath + @"\" + ProjCode + @"\" + BranchName;

            #region 生成build.xml
            var buildxml = rootpath + @"\" + "build.xml";
            if (!File.Exists(buildxml))
            {
                //读取xml模板
                var dir        = System.IO.Directory.GetCurrentDirectory();
                var xmltmp     = dir + @"\" + "build.xml";
                var tmpcontent = string.Empty;
                using (StreamReader sr = new StreamReader(xmltmp))
                {
                    while (sr.Peek() >= 0)
                    {
                        tmpcontent = sr.ReadToEnd();
                    }
                }
                if (!string.IsNullOrEmpty(tmpcontent))
                {
                    tmpcontent = tmpcontent.Replace("{BranchName}", BranchName);

                    if (!string.IsNullOrEmpty(SlnName))
                    {
                        tmpcontent = tmpcontent.Replace("{SlnName}", SlnName);
                    }
                    else
                    {
                        var files = Directory.GetFiles(WorkPath + "\\" + ProjCode + "\\" + BranchName, "*.sln");
                        if (files.Any())
                        {
                            tmpcontent = tmpcontent.Replace("{SlnName}", files[0]);
                        }

                        if (BranchName.ToLower().IndexOf("db_transfer") > -1)
                        {
                            tmpcontent = tmpcontent.Replace("{SlnName}", "db_transfer_new\\DB_Transfer.sln");
                        }
                    }

                    using (StreamWriter writer = new StreamWriter(buildxml, false, new UTF8Encoding(true)))
                    {
                        try
                        {
                            writer.Write(tmpcontent);
                            writer.Flush();
                            writer.Close();
                        }
                        catch (Exception)
                        {
                            writer.Flush();
                            writer.Close();
                        }
                    }
                }
            }
            #endregion

            var xmlpath = WorkPath + "\\" + ProjCode + "\\" + BranchName + "\\build.xml";

            var buildshell = ConfigHelper.GetValue("buildshell").Replace("{logpath}", rootpath);

            var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName        = ConfigHelper.GetValue("MSBuild"), //执行的文件名
                    Arguments       = xmlpath + " " + buildshell,       //需要执行的命令
                    UseShellExecute = false,                            //使用Shell执行
                    WindowStyle     = ProcessWindowStyle.Hidden,        //隐藏窗体
                    CreateNoWindow  = false,                            //不显示窗体
                },
            };


            process.Start();       //开始执行
            process.WaitForExit(); //等待完成并退出

            var path = rootpath;

            if (string.IsNullOrEmpty(PackagePath) && BranchName.ToLower().IndexOf("db_transfer") > -1)
            {
                path += "\\" + ConfigHelper.GetValue("db_transfer_rar");
            }
            else if (string.IsNullOrEmpty(PackagePath) && BranchName.ToLower().IndexOf("jsdx_level_rating") > -1)
            {
                path += "\\" + ConfigHelper.GetValue("jsdx_level_rating");
            }
            else if (!string.IsNullOrEmpty(PackagePath))
            {
                path += "\\" + PackagePath;
            }
            else if (BranchName.ToLower().IndexOf("ccms_v") > -1)
            {
                path += "\\web";
            }
            #region UTMP专用

            // path += "\\" + ConfigHelper.GetValue("UTMP_rar");

            #endregion

            var rarPath = rootpath;
            var rarName = BranchName + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".rar";
            var ignore  = ConfigHelper.GetValue("rar_ignore");

            //重命名*.config
            var configfiles = Directory.GetFiles(path, "*.config");
            var configfile  = configfiles.Any()?configfiles[0]:"";

            if (configfile != "")
            {
                FileHelper.RenameFile(configfile, configfile + ".sample");
            }

            Console.WriteLine("生成打包路径:" + path);
            CompressHelper.CompressRar(path, rarPath, rarName, ignore);
            //重命名*.config.sample
            if (configfile != "")
            {
                FileHelper.RenameFile(configfile + ".sample", configfile);
            }

            Console.WriteLine(BranchName + "发布成功:" + DateTime.Now);
            Console.WriteLine("任务等待中...");
        }