private string genAssemblyInfo(ScriptContext context, Script script, GeneratorOptions options) { if ((options & GeneratorOptions.CreateMain) == 0) return string.Empty; var d = new Dictionary<string, string>() { {"[assembly: System.Reflection.AssemblyTitle('{}')]", script.VersionInfo.Title}, {"[assembly: System.Reflection.AssemblyDescription('{}')]", script.VersionInfo.Value}, {"[assembly: System.Reflection.AssemblyCompany('{}')]", script.VersionInfo.Company}, {"[assembly: System.Reflection.AssemblyProduct('{}')]", script.VersionInfo.Product}, {"[assembly: System.Reflection.AssemblyCopyright('{}')]", script.VersionInfo.Copyright}, {"[assembly: System.Reflection.AssemblyVersion('{}')]", script.VersionInfo.Version}, {"[assembly: System.Reflection.AssemblyFileVersion('{}')]", script.VersionInfo.Version} }; StringBuilder sb=new StringBuilder(); foreach (var pair in d) { var tr = context.TransformStr(pair.Value??string.Empty, script.VersionInfo.Transform | TransformRules.Trim ).Replace("\"","\"\""); if (!string.IsNullOrEmpty(tr)) sb.AppendLine(pair.Key.Replace("'{}'", "@\"" + tr + "\"")); } if (sb.Length==0) return string.Empty; ScriptContext sv=new ScriptContext(); sv["guid"] = ToValidName(Guid.NewGuid().ToString()); sv["code"] = sb.ToString(); string s = @"#region -- Assembly attributes -- ${code} // C# compiler bug may treat assembly attributes as applying to namespace class C${guid} {} #endregion"; return sv.ExpandStr(s); }
private string genMain() { StringBuilder appDomainLoader = extractContents("XSharper.Embedded.Source.AppDomainLoader"); // Make it private appDomainLoader.Replace("public class", "class"); // Read main program ScriptContext ctx=new ScriptContext(); ctx["GENERATED_CLASS_PROGRAM"] = Class + "Program"; ctx["GENERATED_CLASS"] = Class; ctx["GENERATED_NAMESPACE"] = Namespace; ctx["INSERT_APPDOMAIN_LOADER"] = appDomainLoader.ToString(); return ctx.ExpandStr(extractContents("XSharper.Embedded.Source.GeneratedProgram")); }
public void Generate(ScriptContext context, TextWriter codeStream, Script script, GeneratorOptions options) { Compiler.AddHeaders("using XS = " + typeof(Script).Namespace + ";"); ScriptContext sv = new ScriptContext(); sv["main"]= ((options & GeneratorOptions.CreateMain) != 0) ? genMain() : string.Empty; sv["assembly"] = genAssemblyInfo(context, script, options); sv["namespace"] = Namespace; sv["class"] = Class; sv["src"] = script.Location; sv["date"] = DateTime.Now.ToString(); sv["version"] = context.CoreVersion.ToString(); sv["script"] = Compiler.GetTypeName(script.GetType()); sv["callIsolation"] = Compiler.GetTypeName(typeof (CallIsolation)); sv["iscriptaction"] = Compiler.GetTypeName(typeof (IScriptAction)); sv["usings"] = Compiler.GenerateFileHeader(true,false,false); sv["headers"] = Compiler.GenerateFileHeader(false,true,false); sv["context"] = Compiler.GetTypeName(context.GetType()); using (StringWriter swCode = new StringWriter()) using (StringWriter swSnippets = new StringWriter()) using (new ScriptContextScope(context)) { Generator c = new Generator(Compiler, swCode, swSnippets, options); c.GenerateObjectCode(script, "_script", 3); sv["snippets-code"] = swSnippets.ToString(); sv["script-code"] = swCode.ToString(); } using (MemoryStream ms = AppDomainLoader.TryLoadResourceStream("XSharper.Embedded.Source.SourceTemplate")) using (var sr = new StreamReader(ms)) codeStream.Write(sv.ExpandStr(sr.ReadToEnd())); }