Exemplo n.º 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);
        }
Exemplo n.º 2
0
        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);
            }
        }