Ejemplo n.º 1
0
        /// <summary>
        /// 编译后输出cshtml文件,返回出错信息
        /// </summary>
        /// <param name="sPath">原路径</param>
        /// <param name="tPath">目标路径</param>
        /// <returns></returns>
        private static string OutPutCshtml(string sPath, string tPath)
        {
            Tuple <bool, string> result = JsonCfg.GlobleVar == null?
                                          RazorCshtml.BuildCshtml(sPath) :
                                              RazorCshtml.BuildCshtml(sPath, JsonCfg.GlobleVar);

            if (result.Item1 == false)
            {
                // 编译失败时
                return($"ERR: 发布失败{Environment.NewLine}{result.Item2}");
            }

            // 改成html扩展名
            string targetPath = $"{tPath.Substring(0, tPath.Length - "CSHTML".Length)}html";

            // 选择压缩输出时
            if (new int[] { 1, 3, 5, 7 }.Contains(JsonCfg.MiniOutput))
            {
                string html = Minifier.Html(result.Item2);
                // 如果压缩不成功,直接输出原html
                if (html == null)
                {
                    File.WriteAllText(targetPath, result.Item2);
                    return("INFO: 发布成功,但压缩html失败,请检查html语法错误");
                }
                File.WriteAllText(targetPath, html);
                return("INFO: 发布成功");
            }
            File.WriteAllText(targetPath, result.Item2);
            return("INFO: 发布成功");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 输出文件,如果是js,css,则判断是否需要压缩输出,cshtml文件则编译,其它文件原样输出
        /// </summary>
        /// <param name="sPath">原路径</param>
        /// <param name="tPath">目标路径</param>
        /// <returns></returns>
        private static string OutPutFile(string sPath, string tPath)
        {
            string extName = Path.GetExtension(sPath).ToLower();

            //
            if (extName == ".cshtml")
            {
                return(OutPutCshtml(sPath, tPath));
            }

            string msg = "INFO: 发布成功";

            // js
            if (extName == ".js" && new int[] { 4, 5, 6, 7 }.Contains(JsonCfg.MiniOutput))
            {
                string js = Minifier.Js(File.ReadAllText(sPath));
                if (js == null)
                {
                    File.Copy(sPath, tPath, true);
                    return("INFO: 发布成功,但压缩js失败,请检查js语法错误");
                }
                File.WriteAllText(tPath, js);
                return(msg);
            }
            // css
            if (extName == ".css" && new int[] { 2, 3, 6, 7 }.Contains(JsonCfg.MiniOutput))
            {
                string css = Minifier.Css(File.ReadAllText(sPath));
                if (css == null)
                {
                    File.Copy(sPath, tPath, true);
                    return("INFO: 发布成功,但压缩js失败,请检查js语法错误");
                }
                File.WriteAllText(tPath, css);
                return(msg);
            }
            // html
            if (extName.IndexOf(".html") >= 0 && new int[] { 1, 3, 5, 7 }.Contains(JsonCfg.MiniOutput))
            {
                string html = Minifier.Html(File.ReadAllText(sPath));
                // 如果压缩不成功,直接输出原html
                if (html == null)
                {
                    File.Copy(sPath, tPath, true);
                    return("INFO: 发布成功,但压缩html失败,请检查html语法错误");
                }
                File.WriteAllText(tPath, html);
                return(msg);
            }
            // 其它文件,直接输出
            File.Copy(sPath, tPath, true);
            return(msg);
        }