示例#1
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 override bool Process(IScriptProcessor processor, ScriptProcessorContext context, FilePath currentScriptPath, string line)
        {
            if (processor == null)
            {
                throw new ArgumentNullException("processor");
            }

            var tokens = Split(line);

            if (tokens.Length <= 0)
            {
                return(false);
            }

            if (!tokens[0].Equals("#l", StringComparison.Ordinal))
            {
                return(false);
            }

            var directoryPath = GetAbsoluteDirectory(currentScriptPath);
            var scriptPath    = new FilePath(tokens[1].UnQuote()).MakeAbsolute(directoryPath);

            processor.Process(scriptPath, context);

            return(true);
        }
示例#2
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());
        }
示例#3
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);
        }
示例#4
0
        public void Run(CakeOptions options)
        {
            // Initialize the script session factory.
            _sessionFactory.Initialize();

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

            // Create and prepare the session.
            var session = _sessionFactory.CreateSession(_host);

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

            _processor.Process(options.Script, context);

            // Set the working directory.
            _environment.WorkingDirectory = options.Script.MakeAbsolute(_environment).GetDirectory();

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

            assemblies.AddRange(GetDefaultAssemblies());
            foreach (var reference in context.References)
            {
                if (_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());
        }