Ejemplo n.º 1
0
        public ScriptProcessorContext Process()
        {
            var context = new ScriptProcessorContext();

            CreateProcessor().Process(ScriptPath, context);
            return(context);
        }
Ejemplo n.º 2
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 (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var tokens = Split(line);

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

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

            var @namespace = tokens[1].TrimEnd(';');

            context.AddNamespace(@namespace);

            return(true);
        }
Ejemplo n.º 3
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 (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var tokens = Split(line);

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

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

            var referencePath = new FilePath(tokens[1].UnQuote());

            var directoryPath         = GetAbsoluteDirectory(currentScriptPath);
            var absoluteReferencePath = referencePath.MakeAbsolute(directoryPath);

            context.AddReference(_fileSystem.Exist(absoluteReferencePath)
                ? absoluteReferencePath.FullPath : referencePath.FullPath);

            return(true);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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 (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var tokens = Split(line);

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

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

            // Using block?
            var @namespace = tokens[1].TrimEnd(';');

            if (@namespace.StartsWith("("))
            {
                return(false);
            }

            // Using alias directive?
            if (tokens.Any(t => t == "="))
            {
                context.AddUsingAliasDirective(string.Join(" ", tokens));
                return(true);
            }

            // Namespace
            context.AddNamespace(@namespace);
            return(true);
        }
Ejemplo n.º 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 override bool Process(IScriptProcessor processor, ScriptProcessorContext context, FilePath currentScriptPath, string line)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var tokens    = Split(line);
            var directive = tokens.FirstOrDefault();

            if (string.IsNullOrWhiteSpace(directive))
            {
                return(false);
            }

            if (!directive.Equals("#tool", StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            // Fetch the tool NuGet ID.
            var toolId = tokens
                         .Select(value => value.UnQuote())
                         .Skip(1).FirstOrDefault();

            if (string.IsNullOrWhiteSpace(toolId))
            {
                return(false);
            }

            // Fetch optional NuGet source.
            var source = tokens
                         .Skip(2)
                         .Select(value => value.UnQuote())
                         .FirstOrDefault();

            // Get the directory path to Cake.
            var applicationRoot = _environment.WorkingDirectory;

            // Get the tool directory.
            var toolsRootDirectoryPath = applicationRoot
                                         .Combine(".\\tools")
                                         .Collapse()
                                         .MakeAbsolute(_environment);

            var toolDirectoryPath  = toolsRootDirectoryPath.Combine(toolId);
            var toolsRootDirectory = _fileSystem.GetDirectory(toolsRootDirectoryPath);

            // Create the tool directory if it doesn't exist.
            if (!toolsRootDirectory.Exists)
            {
                _log.Verbose("Creating tool directory {0}", toolsRootDirectoryPath.FullPath);
                toolsRootDirectory.Create();
            }

            // Fetch available tool executables.
            var toolExecutables = GetToolExecutables(toolDirectoryPath);

            // If no executables were found, try install tool from NuGet.
            if (toolExecutables.Length == 0)
            {
                InstallTool(toolId, toolsRootDirectory, source);
                toolExecutables = GetToolExecutables(toolDirectoryPath);
            }

            // Validate found assemblies.
            if (toolExecutables.Length == 0)
            {
                throw new CakeException("Failed to find tool executables.");
            }

            _log.Debug(logAction =>
            {
                foreach (var toolExecutable in toolExecutables)
                {
                    logAction("Found tool executable: {0}.", toolExecutable.Path);
                }
            });

            return(true);
        }
Ejemplo n.º 7
0
 public string GetActualSource(ScriptProcessorContext context)
 {
     return(string.Join("\r\n", context.Lines));
 }
Ejemplo n.º 8
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 (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var tokens    = Split(line);
            var directive = tokens.FirstOrDefault();

            if (string.IsNullOrWhiteSpace(directive))
            {
                return(false);
            }

            if (!directive.Equals("#addin", StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            // Fetch the addin NuGet ID.
            var addInId = tokens
                          .Select(value => value.UnQuote())
                          .Skip(1).FirstOrDefault();

            if (string.IsNullOrWhiteSpace(addInId))
            {
                return(false);
            }

            // Fetch optional NuGet source.
            var source = tokens
                         .Skip(2)
                         .Select(value => value.UnQuote())
                         .FirstOrDefault();

            // Get the directory path to Cake.
            var applicationRoot = _environment.GetApplicationRoot();

            // Get the addin directory.
            var addInRootDirectoryPath = applicationRoot
                                         .Combine("..\\Addins")
                                         .Collapse()
                                         .MakeAbsolute(_environment);

            var addInDirectoryPath = addInRootDirectoryPath.Combine(addInId);
            var addInRootDirectory = _fileSystem.GetDirectory(addInRootDirectoryPath);

            // Create the addin directory if it doesn't exist.
            if (!addInRootDirectory.Exists)
            {
                _log.Verbose("Creating addin directory {0}", addInRootDirectoryPath.FullPath);
                addInRootDirectory.Create();
            }

            // Fetch available addin assemblies.
            var addInAssemblies = GetAddInAssemblies(addInDirectoryPath);

            // If no assemblies were found, try install addin from NuGet.
            if (addInAssemblies.Length == 0)
            {
                InstallAddin(addInId, addInRootDirectory, source);
                addInAssemblies = GetAddInAssemblies(addInDirectoryPath);
            }

            // Validate found assemblies.
            if (addInAssemblies.Length == 0)
            {
                throw new CakeException("Failed to find AddIn assemblies");
            }

            // Reference found assemblies.
            foreach (var assemblyPath in addInAssemblies.Select(assembly => assembly.Path.FullPath))
            {
                _log.Verbose("Addin: {0}, adding Reference {1}", addInId, assemblyPath);
                context.AddReference(assemblyPath);
            }

            return(true);
        }
Ejemplo n.º 9
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());
        }
Ejemplo n.º 10
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)
 {
     // Remove all shebang lines that we encounter.
     return(line.StartsWith("#!", StringComparison.OrdinalIgnoreCase));
 }