Beispiel #1
0
        /// <summary>
        /// Credit for this parsing routine goes to mcintyre321
        /// https://github.com/mcintyre321/LINQPadRunner/blob/master/LPRun/Program.cs
        /// </summary>
        private static LinqPadQuery ParseXML(string[] scriptFileContents)
        {
            // XML Lines start with <
            // Code lines do not

            var xml = string.Join("\r\n", scriptFileContents.TakeWhile(l => l.Trim().StartsWith("<")));

            var queryElement = XDocument.Parse(xml).Element("Query");
            var query        = new LinqPadQuery
            {
                Kind               = queryElement.Attribute("Kind").Value,
                Namespaces         = queryElement.Elements("Namespace").Select(n => n.Value).ToList(),
                GACReferences      = queryElement.Elements("GACReference").Select(n => n.Value).ToList(),
                RelativeReferences =
                    queryElement.Elements("Reference").Where(e => e.Attribute("Relative") != null).
                    Select(n => n.Attribute("Relative").Value).ToList(),
                OtherReferences =
                    queryElement.Elements("Reference").Where(e => e.Attribute("Relative") == null).
                    Select(
                        n =>
                        n.Value.Replace("<RuntimeDirectory>",
                                        RuntimeEnvironment.GetRuntimeDirectory())).ToList(),
            };

            var code = string.Join("\r\n", scriptFileContents.SkipWhile(l => l.Trim().StartsWith("<")));

            query.ScriptCode = code;

            return(query);
        }
Beispiel #2
0
        public ExecutableScript MakeScriptExecutable(LinqPadQuery script, string generatedDataContextCode)
        {
            var program = new ExecutableScript();

            // Set Assemblies
            // GAC Reference parsing from: https://github.com/mcintyre321/LINQPadRunner/blob/master/LPRun/Program.cs
            var assemblyGroups = new[]
            {
                script.GACReferences.Select(s => s.Substring(0, s.IndexOf(",")) + ".dll"),
                script.RelativeReferences,
                script.OtherReferences,
                StandardAssemblies,
            };

            program.AssemblyReferences.AddRange(assemblyGroups.SelectMany(group => group));

            // Prepare to Include Namespaces
            var namespaceGroups = new[]
            {
                script.Namespaces,
                StandardNamespaces
            };
            var namespaces = String.Join("\n", from namespaceGroup in namespaceGroups
                                         from nspace in namespaceGroup
                                         select string.Format("using {0};", nspace));

            // Wrap Code in Main() method (if needed)
            var scriptMethodCode = ConvertScriptIntoMethodByQueryKind(script, generatedDataContextCode);

            var code = string.Format(@"

                // Namespaces
                {0}
            
                namespace Generated
                {{
                    public class MyProgram
                    {{
                        {1}
                    }}
                }}
            
            ", namespaces, scriptMethodCode);

            // Add Code and DataContext source to executable script
            program.SourceCodePieces.Add(code);
            program.SourceCodePieces.Add(generatedDataContextCode);

            return(program);
        }
Beispiel #3
0
        private static string ConvertScriptIntoMethodByQueryKind(LinqPadQuery script, string generatedDataContextCode)
        {
            if (script.Kind == LinqPadQueryKind.Program)
            {
                // Make Programs Public
                return(string.Format("public {0}", script.ScriptCode));
            }
            else if (script.Kind == LinqPadQueryKind.Expression)
            {
                // Add Context to Expression
                var expressionWithContext = AddContextToExpression(script.ScriptCode, ContextName, generatedDataContextCode);

                // Hookup Expressions to DataContext
                var code = String.Format(@"

                                public object Main()
                                {{

                                    // Create new DataContext
                                    var {0} = new {1}();

                                    object _q_ = {2};

                                    // Execute Query, if a deferred enumerable
                                    if (_q_ != null && _q_ is IEnumerable)
                                        _q_ = (_q_ as IEnumerable).Cast<object>().ToList();

                                    return _q_;
                                }}

                            ", ContextName, LinqToSQLDataContextGenerator.DataContextName, expressionWithContext);

                return(code);
            }
            else
            {
                return(string.Format(@"public void Main() {{ {0} }}", script.ScriptCode));
            }
        }
        public ExecutableScript MakeScriptExecutable(LinqPadQuery script, string generatedDataContextCode)
        {
            var program = new ExecutableScript();

            // Set Assemblies
            // GAC Reference parsing from: https://github.com/mcintyre321/LINQPadRunner/blob/master/LPRun/Program.cs
            var assemblyGroups = new[]
                                 {
                                     script.GACReferences.Select(s => s.Substring(0, s.IndexOf(",")) + ".dll"),
                                     script.RelativeReferences,
                                     script.OtherReferences,
                                     StandardAssemblies,
                                 };
            program.AssemblyReferences.AddRange(assemblyGroups.SelectMany(group => group));

            // Prepare to Include Namespaces
            var namespaceGroups = new[]
                {
                    script.Namespaces,
                    StandardNamespaces
                };
            var namespaces = String.Join("\n", from namespaceGroup in namespaceGroups
                                               from nspace in namespaceGroup
                                               select string.Format("using {0};", nspace));

            // Wrap Code in Main() method (if needed)
            var scriptMethodCode = ConvertScriptIntoMethodByQueryKind(script, generatedDataContextCode);

            var code = string.Format(@"

                // Namespaces
                {0}

                namespace Generated
                {{
                    public class MyProgram
                    {{
                        {1}
                    }}
                }}

            ", namespaces, scriptMethodCode);

            // Add Code and DataContext source to executable script
            program.SourceCodePieces.Add(code);
            program.SourceCodePieces.Add(generatedDataContextCode);

            return program;
        }
        /// <summary>
        /// Credit for this parsing routine goes to mcintyre321
        /// https://github.com/mcintyre321/LINQPadRunner/blob/master/LPRun/Program.cs
        /// </summary>
        private static LinqPadQuery ParseXML(string[] scriptFileContents)
        {
            // XML Lines start with <
            // Code lines do not

            var xml = string.Join("\r\n", scriptFileContents.TakeWhile(l => l.Trim().StartsWith("<")));

            var queryElement = XDocument.Parse(xml).Element("Query");
            var query = new LinqPadQuery
            {
                Kind = queryElement.Attribute("Kind").Value,
                Namespaces = queryElement.Elements("Namespace").Select(n => n.Value).ToList(),
                GACReferences = queryElement.Elements("GACReference").Select(n => n.Value).ToList(),
                RelativeReferences =
                    queryElement.Elements("Reference").Where(e => e.Attribute("Relative") != null).
                    Select(n => n.Attribute("Relative").Value).ToList(),
                OtherReferences =
                    queryElement.Elements("Reference").Where(e => e.Attribute("Relative") == null).
                    Select(
                        n =>
                        n.Value.Replace("<RuntimeDirectory>",
                                        RuntimeEnvironment.GetRuntimeDirectory())).ToList(),

            };

            var code = string.Join("\r\n", scriptFileContents.SkipWhile(l => l.Trim().StartsWith("<")));
            query.ScriptCode = code;

            return query;
        }
        private static string ConvertScriptIntoMethodByQueryKind(LinqPadQuery script, string generatedDataContextCode)
        {
            if (script.Kind == LinqPadQueryKind.Program)
            {
                // Make Programs Public
                return string.Format("public {0}", script.ScriptCode);
            }
            else if (script.Kind == LinqPadQueryKind.Expression)
            {
                // Add Context to Expression
                var expressionWithContext = AddContextToExpression(script.ScriptCode, ContextName, generatedDataContextCode);

                // Hookup Expressions to DataContext
                var code = String.Format(@"

                                public object Main()
                                {{

                                    // Create new DataContext
                                    var {0} = new {1}();

                                    object _q_ = {2};

                                    // Execute Query, if a deferred enumerable
                                    if (_q_ != null && _q_ is IEnumerable)
                                        _q_ = (_q_ as IEnumerable).Cast<object>().ToList();

                                    return _q_;
                                }}

                            ", ContextName, LinqToSQLDataContextGenerator.DataContextName, expressionWithContext);

                return code;
            }
            else
                return string.Format(@"public void Main() {{ {0} }}", script.ScriptCode);
        }