Beispiel #1
0
        public static ScriptEngine ProcessFile(String file)
        {
            var config = Config;

            var sc = new ScriptCode(file);

            //// 加入参数中标明的程序集
            //sc.AddRef(config.Assembly);

            // 使用VisualStudio打开源码文件进行编辑
            if (config.Vs)
            {
                OpenWithVs(sc);
                return(null);
            }

            var dir = Path.GetDirectoryName(file);

            //Environment.CurrentDirectory = dir;
            //PathHelper.BaseDirectory = dir;
            Environment.SetEnvironmentVariable("XScriptFile", file);

            var se = ScriptEngine.Create(sc.ReadCode(true), false);

            if (Config.Debug)
            {
                se.Log = XTrace.Log;
            }
            se.WorkingDirectory = dir;

            // 引用程序集
            if (sc.Refs.Count > 0)
            {
                se.ReferencedAssemblies.AddRange(sc.GetRefArray());
            }

            // 调试状态下输出最终代码
            if (config.Debug)
            {
                var codefile = Path.ChangeExtension(file, "code.cs");
                File.WriteAllText(codefile, se.FinalCode);
            }

            // 从源码读取配置
            config.ParseCode(se.Code);

            // 生成Exe
            if (config.Exe)
            {
                MakeExe(se, file);
            }
            else
            {
                Run(se);
            }

            return(se);
        }
Beispiel #2
0
        /// <summary>使用VisualStudio打开源码文件进行编辑</summary>
        /// <param name="sc"></param>
        static Boolean OpenWithVs(ScriptCode sc)
        {
            // 先读取一次,让其分析Assembly引用
            var code = sc.ReadCode();

            code = sc.GetRefStr();
            if (!String.IsNullOrEmpty(code))
            {
                code += Environment.NewLine;
            }

            var se = ScriptEngine.Create(sc.ReadCode(false), false);

            if (Config.Debug)
            {
                se.Log = XTrace.Log;
            }
            code += se.FinalCode;
            File.WriteAllText(sc.CodeFile, code);

            // 判断项目文件是否存在,若不存在,则根据源码文件生成项目
            var codefile = sc.CodeFile;
            var asm      = Assembly.GetExecutingAssembly();
            var name     = Path.GetFileNameWithoutExtension(codefile) + ".csproj";
            var dir      = codefile.ToLower().MD5();
            var proj     = Path.GetDirectoryName(asm.Location).CombinePath("Projs", dir, name);

            MakeProj(sc, proj);

            // 找到安装VisualStudio地址,暂时还不支持Express
            var root = Registry.ClassesRoot;
            var vs   = "";

            for (int i = 15; i >= 8; i--)
            {
                var reg = root.OpenSubKey(String.Format("VisualStudio.sln.{0}.0", i));
                if (reg != null)
                {
                    reg = reg.OpenSubKey("shell\\Open\\Command");
                    if (reg != null)
                    {
                        vs = reg.GetValue("") + "";
                    }
                    if (!vs.IsNullOrWhiteSpace())
                    {
                        break;
                    }
                }
            }
            if (vs.IsNullOrWhiteSpace())
            {
                XTrace.WriteLine("无法找到VisualStudio!");
                return(false);
            }

            vs = vs.TrimEnd("\"%1\"").Trim().Trim('\"');
            var sln = Path.ChangeExtension(proj, "sln");

            Process.Start(vs, String.Format("\"{0}\"", sln));

            return(true);
        }