예제 #1
0
파일: Script.cs 프로젝트: zzmzaizai/XScript
        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);
        }
예제 #2
0
파일: Script.cs 프로젝트: zzmzaizai/XScript
        static void MakeProj(ScriptCode sc, String proj)
        {
            if (!File.Exists(proj))
            {
                XTrace.WriteLine("释放csproj模版到:{0}", proj);
                FileSource.ReleaseFile(null, "tmpCmd.csproj", proj, true);
            }

            var doc = new XmlDocument();

            doc.Load(proj);

            var uri   = "http://schemas.microsoft.com/developer/msbuild/2003";
            var nsmgr = new XmlNamespaceManager(doc.NameTable);

            nsmgr.AddNamespace("ns", uri);

            var group = doc.SelectSingleNode("//ns:PropertyGroup", nsmgr);
            // Guid
            var node = group.SelectSingleNode("ns:ProjectGuid", nsmgr);

            if (node == null)
            {
                node = doc.CreateElement("ProjectGuid", uri);
                group.AppendChild(node);
                node.InnerText = "{" + Guid.NewGuid().ToString().ToUpper() + "}";
            }
            var guid = node.InnerText.Trim('{', '}');

            // 版本
            node = group.SelectSingleNode("ns:TargetFrameworkVersion", nsmgr);
#if NET4
            node.InnerText = "v4.0";
#else
            node.InnerText = "v2.0";
#endif
            // 程序集名称
            node = group.SelectSingleNode("ns:AssemblyName", nsmgr);
            if (node.InnerText.IsNullOrWhiteSpace())
            {
                node.InnerText = Path.GetFileNameWithoutExtension(proj);
            }

            // 输出目录
            node = group.SelectSingleNode("ns:OutputPath", nsmgr);
            if (node.InnerText.IsNullOrWhiteSpace())
            {
                node.InnerText = Path.GetDirectoryName(proj);
            }

            var items = doc.SelectSingleNode("//ns:ItemGroup", nsmgr);
            // 设定源码文件
            node = items.SelectSingleNode("ns:Compile", nsmgr);
            if (node == null)
            {
                node = doc.CreateElement("Compile", uri);
                items.AppendChild(node);
            }
            //node.InnerText = null;
            var att = node.Attributes["Include"];
            if (att == null)
            {
                att = doc.CreateAttribute("Include");
                node.Attributes.Append(att);
            }
            att.Value = sc.CodeFile;

            // 引用DLL
            foreach (var item in sc.GetRefArray())
            {
                var name = Path.GetFileNameWithoutExtension(item);
                node = items.SelectSingleNode("ns:Reference[@Include='" + name + "']", nsmgr);
                if (node == null)
                {
                    node = doc.CreateElement("Reference", uri);
                    items.AppendChild(node);

                    var node2 = doc.CreateElement("HintPath", uri);
                    node.AppendChild(node2);
                    node2.InnerText = Path.GetDirectoryName(sc.CodeFile).CombinePath(item).GetFullPath();
                }
                att = node.Attributes["Include"];
                if (att == null)
                {
                    att = doc.CreateAttribute("Include");
                    node.Attributes.Append(att);
                }
                att.Value = name;
            }

            doc.Save(proj);

            var sln = Path.ChangeExtension(proj, "sln");
            if (!File.Exists(sln))
            {
                XTrace.WriteLine("释放sln模版到:{0}", sln);
                FileSource.ReleaseFile(null, "tmpCmd.sln", sln, true);

                var txt = File.ReadAllText(sln);
                txt = txt.Replace("{$SlnGUID}", Guid.NewGuid().ToString().ToUpper());
                txt = txt.Replace("{$GUID}", guid);
                txt = txt.Replace("{$Name}", Path.GetFileNameWithoutExtension(proj));
                File.WriteAllText(sln, txt, Encoding.UTF8);
            }
        }
예제 #3
0
파일: Script.cs 프로젝트: zzmzaizai/XScript
        /// <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);
        }