示例#1
0
        /// <summary>
        /// Processes the specified script.
        /// </summary>
        /// <param name="path">The script path.</param>
        /// <param name="context">The context.</param>
        public void Process(FilePath path, ScriptProcessorContext context)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            // Already processed this script?
            if (context.HasScriptBeenProcessed(path.FullPath))
            {
                _log.Debug("Skipping {0} since it's already been processed.", path.GetFilename().FullPath);
                return;
            }

            // Add the script.
            context.MarkScriptAsProcessed(path.MakeAbsolute(_environment).FullPath);

            // Read the source.
            _log.Debug("Processing {0}...", path.GetFilename().FullPath);
            var lines = ReadSource(path);

            // Iterate all lines in the script.
            foreach (var line in lines)
            {
                if (!_lineProcessors.Any(p => p.Process(this, context, path, line)))
                {
                    context.AppendScriptLine(line);
                }
            }
        }
示例#2
0
        /// <summary>
        /// Generates script aliases and adds them to the specified session.
        /// </summary>
        /// <param name="context">The script context to add generated code to.</param>
        /// <param name="assemblies">The assemblies to find script aliases in.</param>
        public void GenerateScriptAliases(ScriptProcessorContext context, IEnumerable <Assembly> assemblies)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            foreach (var method in ScriptAliasFinder.FindAliases(assemblies))
            {
                try
                {
                    // Import the method's namespace to the session.
                    var @namespace = method.GetNamespace();
                    context.AddNamespace(@namespace);

                    // Find out if the method want us to import more namespaces.
                    var namespaceAttributes = method.GetCustomAttributes <CakeNamespaceImportAttribute>();
                    foreach (var namespaceAttribute in namespaceAttributes)
                    {
                        context.AddNamespace(namespaceAttribute.Namespace);
                    }

                    // Find out if the class contains any more namespaces.
                    namespaceAttributes = method.DeclaringType.GetCustomAttributes <CakeNamespaceImportAttribute>();
                    foreach (var namespaceAttribute in namespaceAttributes)
                    {
                        context.AddNamespace(namespaceAttribute.Namespace);
                    }

                    // Generate the code.
                    var code = GenerateCode(method);

                    // Add the generated code to the context.
                    context.AddScriptAliasCode(code);
                }
                catch (Exception)
                {
                    // Log this error.
                    const string format = "An error occured while generating code for alias {0}.";
                    _log.Error(format, method.GetSignature(false));

                    // Rethrow the original exception.
                    throw;
                }
            }
        }
示例#3
0
        /// <summary>
        /// Processes the specified script.
        /// </summary>
        /// <param name="path">The script path.</param>
        /// <param name="context">The context.</param>
        public void Process(FilePath path, ScriptProcessorContext context)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            // Already processed this script?
            if (context.HasScriptBeenProcessed(path.FullPath))
            {
                _log.Debug("Skipping {0} since it's already been processed.", path.GetFilename().FullPath);
                return;
            }

            // Add the script.
            context.MarkScriptAsProcessed(path.MakeAbsolute(_environment).FullPath);

            // Read the source.
            _log.Debug("Processing {0}...", path.GetFilename().FullPath);
            var lines = ReadSource(path);

            // Iterate all lines in the script.
            var firstLine = true;

            foreach (var line in lines)
            {
                if (!_lineProcessors.Any(p => p.Process(this, context, path, line)))
                {
                    if (firstLine)
                    {
                        // Append the line directive for the script.
                        var scriptFullPath = path.MakeAbsolute(_environment);
                        context.AppendScriptLine(string.Format(CultureInfo.InvariantCulture, "#line 1 \"{0}\"", scriptFullPath.FullPath));
                        firstLine = false;
                    }

                    context.AppendScriptLine(line);
                }
            }
        }
示例#4
0
        /// <summary>
        /// Runs the script using the specified script host.
        /// </summary>
        /// <param name="host">The script host.</param>
        /// <param name="script">The script.</param>
        /// <param name="arguments">The arguments.</param>
        public void Run(IScriptHost host, FilePath script, IDictionary <string, string> arguments)
        {
            if (host == null)
            {
                throw new ArgumentNullException("host");
            }
            if (script == null)
            {
                throw new ArgumentNullException("script");
            }
            if (arguments == null)
            {
                throw new ArgumentNullException("arguments");
            }

            // Copy the arguments from the options.
            host.Context.Arguments.SetArguments(arguments);

            // Set the working directory.
            host.Context.Environment.WorkingDirectory
                = script.MakeAbsolute(host.Context.Environment).GetDirectory();

            // Make sure that any directories are stripped from the script path.
            script = script.GetFilename();

            // Create and prepare the session.
            var session = _sessionFactory.CreateSession(host, arguments);

            // Process the script.
            var context = new ScriptProcessorContext();

            _scriptProcessor.Process(script, context);

            // Load all references.
            var assemblies = new List <Assembly>();

            assemblies.AddRange(GetDefaultAssemblies(host.Context.FileSystem));
            foreach (var reference in context.References)
            {
                if (host.Context.FileSystem.Exist((FilePath)reference))
                {
                    var assembly = Assembly.LoadFile(reference);
                    assemblies.Add(assembly);
                }
                else
                {
                    // Add a reference to the session.
                    session.AddReferencePath(reference);
                }
            }

            // Got any assemblies?
            if (assemblies.Count > 0)
            {
                // Find all extension methods and generate proxy methods.
                _aliasGenerator.GenerateScriptAliases(context, assemblies);

                // Add assembly references to the session.
                foreach (var assembly in assemblies)
                {
                    session.AddReference(assembly);
                }
            }

            // Import all namespaces.
            var namespaces = new List <string>(context.Namespaces);

            namespaces.AddRange(GetDefaultNamespaces());
            foreach (var @namespace in namespaces.OrderBy(ns => ns))
            {
                session.ImportNamespace(@namespace);
            }

            // Execute the script.
            session.Execute(context.GetScriptCode());
        }
示例#5
0
        /// <summary>
        /// Runs the script using the specified script host.
        /// </summary>
        /// <param name="host">The script host.</param>
        /// <param name="scriptPath">The script.</param>
        /// <param name="arguments">The arguments.</param>
        public void Run(IScriptHost host, FilePath scriptPath, IDictionary <string, string> arguments)
        {
            if (host == null)
            {
                throw new ArgumentNullException("host");
            }
            if (scriptPath == null)
            {
                throw new ArgumentNullException("scriptPath");
            }
            if (arguments == null)
            {
                throw new ArgumentNullException("arguments");
            }

            // Copy the arguments from the options.
            host.Context.Arguments.SetArguments(arguments);

            // Set the working directory.
            host.Context.Environment.WorkingDirectory = scriptPath.MakeAbsolute(host.Context.Environment).GetDirectory();

            // Process the script.
            var context = new ScriptProcessorContext();

            _scriptProcessor.Process(scriptPath.GetFilename(), context);

            // Create and prepare the session.
            var session = _engine.CreateSession(host, arguments);

            // Load all references.
            var assemblies = new HashSet <Assembly>();

            assemblies.AddRange(GetDefaultAssemblies(host.Context.FileSystem));
            foreach (var reference in context.References)
            {
                if (host.Context.FileSystem.Exist((FilePath)reference))
                {
                    var assembly = Assembly.LoadFile(reference);
                    assemblies.Add(assembly);
                }
                else
                {
                    // Add a reference to the session.
                    session.AddReference(reference);
                }
            }

            var aliases = new List <ScriptAlias>();

            // Got any assemblies?
            if (assemblies.Count > 0)
            {
                // Find all script aliases.
                var foundAliases = _aliasFinder.FindAliases(assemblies);
                if (foundAliases.Length > 0)
                {
                    aliases.AddRange(foundAliases);
                }

                // Add assembly references to the session.
                foreach (var assembly in assemblies)
                {
                    session.AddReference(assembly);
                }
            }

            // Import all namespaces.
            var namespaces = new HashSet <string>(context.Namespaces, StringComparer.Ordinal);

            namespaces.AddRange(GetDefaultNamespaces());
            namespaces.AddRange(aliases.SelectMany(alias => alias.Namespaces));
            foreach (var @namespace in namespaces.OrderBy(ns => ns))
            {
                session.ImportNamespace(@namespace);
            }

            // Execute the script.
            var script = new Script(context.Namespaces, context.Lines, aliases);

            session.Execute(script);
        }
示例#6
0
 /// <summary>
 /// Processes the specified line.
 /// </summary>
 /// <param name="processor">The script processor.</param>
 /// <param name="context">The script processor context.</param>
 /// <param name="currentScriptPath">The current script path.</param>
 /// <param name="line">The line to process.</param>
 /// <returns>
 ///   <c>true</c> if the processor handled the line; otherwise <c>false</c>.
 /// </returns>
 public abstract bool Process(IScriptProcessor processor, ScriptProcessorContext context, FilePath currentScriptPath, string line);