private void AnalyzeCallback(IScriptAnalyzerContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } var path = context.Current.Path; // Read the source. _log.Debug("Analyzing {0}...", path.FullPath); var lines = ReadLines(path); // Iterate all lines in the script. foreach (var line in lines) { string replacement = null; if (!_defaultProcessors.Any(p => p.Process(context, _environment.ExpandEnvironmentVariables(line), out replacement))) { context.AddScriptLine(line); } else { // Add replacement or comment out processed lines to keep line data. context.AddScriptLine(replacement ?? string.Concat("// ", line)); } } }
/// <summary> /// Load the given reference /// </summary> /// <returns>The load.</returns> /// <param name="context">The context.</param> /// <param name="reference">The load reference.</param> public void Load(IScriptAnalyzerContext context, LoadReference reference) { var urlsPath = GetUrlsPath(context.Root.GetDirectory()); var actualUrl = reference.Address; var referenceHash = HashMd5(actualUrl.ToString()); var urlFile = urlsPath.CombineWithFilePath(referenceHash + ".cake").MakeAbsolute(_environment); if (!System.IO.File.Exists(urlFile.FullPath)) { var urlFileDir = urlFile.GetDirectory().FullPath; // Make sure the directory exists we want to save to if (!System.IO.Directory.Exists(urlFileDir)) { System.IO.Directory.CreateDirectory(urlFileDir); } var http = new System.Net.Http.HttpClient(); using (var httpStream = http.GetStreamAsync(actualUrl).Result) using (var cacheStream = System.IO.File.Create(urlFile.MakeAbsolute(_environment).FullPath)) { httpStream.CopyTo(cacheStream); } } context.Analyze(urlFile); }
public override bool Process(IScriptAnalyzerContext processor, string line, out string replacement) { replacement = null; // Remove all shebang lines that we encounter. return(line.StartsWith("#!", StringComparison.OrdinalIgnoreCase)); }
public sealed override bool Process(IScriptAnalyzerContext context, string line) { if (context == null) { throw new ArgumentNullException("context"); } var tokens = Split(line); var directive = tokens.FirstOrDefault(); if (!string.IsNullOrWhiteSpace(directive)) { if (directive.Equals(GetDirectiveName(), StringComparison.OrdinalIgnoreCase)) { if (tokens.Length >= 2) { // Try to parse an URI. var uri = ParseUriFromTokens(tokens); if (uri != null) { // Add the URI to the context. AddToContext(context, uri); // Return success. return(true); } } } } return(false); }
public override bool Process(IScriptAnalyzerContext context, string line, out string replacement) { if (context == null) { throw new ArgumentNullException(nameof(context)); } replacement = null; 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 = context.Current.Path.MakeAbsolute(_environment).GetDirectory(); var absoluteReferencePath = referencePath.MakeAbsolute(directoryPath); context.Current.References.Add(_fileSystem.Exist(absoluteReferencePath) ? absoluteReferencePath.FullPath : referencePath.FullPath); return(true); }
public void Load(IScriptAnalyzerContext context, LoadReference reference) { #if NETCORE throw new NotSupportedException("The NuGet provider for #load is not supported on .NET Core."); #else // Create a package reference from our load reference. // The package should contain the necessary include parameters to make sure // that .cake files are included as part of the result. var separator = reference.OriginalString.Contains("?") ? "&" : "?"; var uri = string.Concat(reference.OriginalString, $"{separator}include=./**/*.cake"); var package = new PackageReference(uri); // Find the tool folder. var toolPath = GetToolPath(context.Root.GetDirectory()); // Install the NuGet package. var files = _installer.Install(package, PackageType.Tool, toolPath); if (files.Count == 0) { // No files found. _log.Warning("No scripts found in NuGet package {0}.", package.Package); return; } foreach (var file in files) { var extension = file.Path.GetExtension(); if (extension != null && extension.Equals(".cake", StringComparison.OrdinalIgnoreCase)) { context.Analyze(file.Path); } } #endif }
private void AnalyzeCallback(IScriptAnalyzerContext context) { if (context == null) { throw new ArgumentNullException("context"); } var path = context.Script.Path; // Read the source. _log.Debug("Analyzing {0}...", path.FullPath); var lines = ReadLines(path); // Iterate all lines in the script. foreach (var line in lines) { if (!_lineProcessors.Any(p => p.Process(context, line))) { context.AddScriptLine(line); } else { // Comment out processed lines to keep line data. context.AddScriptLine(string.Concat("// ", line)); } } }
public override bool Process(IScriptAnalyzerContext context, string line, out string replacement) { if (context == null) { throw new ArgumentNullException(nameof(context)); } replacement = null; var tokens = Split(line); if (tokens.Length <= 0) { return(false); } if (!tokens[0].Equals("#l", StringComparison.Ordinal) && !tokens[0].Equals("#load", StringComparison.Ordinal)) { return(false); } var directoryPath = GetAbsoluteDirectory(context.Script.Path); var scriptPath = new FilePath(tokens[1].UnQuote()).MakeAbsolute(directoryPath); context.Analyze(scriptPath); return(true); }
public void Load(IScriptAnalyzerContext context, LoadReference reference) { FilePath path = null; if (reference.Parameters.ContainsKey("path")) { if (reference.Parameters["path"].Count == 1) { path = reference.Parameters["path"].FirstOrDefault(); } else if (reference.Parameters["path"].Count > 1) { throw new CakeException("Query string for #load contains more than one parameter 'path'."); } } if (path == null) { throw new CakeException("Query string for #load is missing parameter 'path'."); } // URL decode the path. path = new FilePath(WebUtility.UrlDecode(path.FullPath)); // Get the current script path. var current = context.Current.Path.GetDirectory(); path = path.MakeAbsolute(current); // Analyze the script. context.Analyze(path); }
private void AnalyzeCallback(IScriptAnalyzerContext context) { if (context == null) { throw new ArgumentNullException("context"); } var path = context.Script.Path; // Read the source. _log.Debug("Analyzing {0}...", path.FullPath); var lines = ReadLines(path); // Iterate all lines in the script. var firstLine = true; foreach (var line in lines) { if (!_lineProcessors.Any(p => p.Process(context, line))) { if (firstLine) { // Append the line directive for the script. var scriptFullPath = path.MakeAbsolute(_environment); context.AddScriptLine(string.Format(CultureInfo.InvariantCulture, "#line 1 \"{0}\"", scriptFullPath.FullPath)); firstLine = false; } context.AddScriptLine(line); } } }
public override bool Process(IScriptAnalyzerContext context, string line, out string replacement) { if (context == null) { throw new ArgumentNullException(nameof(context)); } replacement = null; var tokens = Split(line); if (tokens.Length <= 1) { return(false); } if (!tokens[0].Equals("using", StringComparison.Ordinal)) { return(false); } // Using disposable block? var @namespace = tokens[1].TrimEnd(';'); if (@namespace.StartsWith("(")) { return(false); } // Using disposable statement? const int usingLength = 5; int openParentheses = line.IndexOf('(', usingLength), closeParentheses = openParentheses < usingLength ? -1 : line.IndexOf(')', openParentheses); if (closeParentheses > openParentheses) { return(false); } // Using alias directive? if (tokens.Any(t => t == "=")) { context.Current.UsingAliases.Add(string.Join(" ", tokens)); return(true); } // Using static directive? if (tokens.Length == 3 && tokens[1].Equals("static", StringComparison.Ordinal)) { context.Current.UsingStaticDirectives.Add(string.Join(" ", tokens)); return(true); } // Namespace context.Current.Namespaces.Add(@namespace); return(true); }
protected override void AddToContext(IScriptAnalyzerContext context, Uri uri) { var reference = new LoadReference(uri); foreach (var provider in _providers) { if (provider.CanLoad(context, reference)) { provider.Load(context, reference); } } }
public void Load(IScriptAnalyzerContext context, LoadReference reference) { FilePath path = null; if (reference.Parameters.ContainsKey("path")) { if (reference.Parameters["path"].Count == 1) { path = reference.Parameters["path"].FirstOrDefault(); } else if (reference.Parameters["path"].Count > 1) { throw new CakeException("Query string for #load contains more than one parameter 'path'."); } } if (path == null) { throw new CakeException("Query string for #load is missing parameter 'path'."); } // URL decode the path. path = new FilePath(WebUtility.UrlDecode(path.FullPath)); // Get the current script path. var current = context.Current.Path.GetDirectory(); path = path.MakeAbsolute(current); var expectedExtension = path.HasExtension ? path.GetExtension() : ".cake"; var files = _globber .GetFiles(path.FullPath) .Where(file => { var extension = file.GetExtension(); return(extension != null && (extension.Equals(".cake", StringComparison.OrdinalIgnoreCase) || extension.Equals(expectedExtension, StringComparison.OrdinalIgnoreCase))); }) .ToArray(); if (files.Length == 0) { // No scripts found. _log.Warning("No scripts found at {0}.", path); return; } foreach (var file in files) { context.Analyze(file); } }
public override bool Process(IScriptAnalyzerContext context, string line, out string replacement) { if (context == null) { throw new ArgumentNullException(nameof(context)); } replacement = null; if (!line.StartsWith("#define", StringComparison.Ordinal)) { return(false); } context.Current.Defines.Add(line); return(true); }
public override bool Process(IScriptAnalyzerContext context, string line, out string replacement) { if (context == null) { throw new ArgumentNullException("context"); } replacement = null; if (!line.Trim().Equals("#break", StringComparison.Ordinal)) { return(false); } replacement = @"if (System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debugger.Break(); }"; return(true); }
public override bool Process(IScriptAnalyzerContext context, 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(); // Add the package definition for the addin. context.Script.Addins.Add(new NuGetPackage(addInId) { Source = source }); // Return success. return(true); }
public void Load(IScriptAnalyzerContext context, LoadReference reference) { // Create a package reference from our load reference. // If not specified in script, the package should contain the necessary include // parameters to make sure that .cake files are included as part of the result. var uri = new Uri(reference.OriginalString); var parameters = uri.GetQueryString(); const string includeParameterName = "include"; if (!parameters.ContainsKey(includeParameterName)) { var separator = parameters.Count > 0 ? "&" : "?"; uri = new Uri(string.Concat(reference.OriginalString, $"{separator}include=./**/*.cake")); } var package = new PackageReference(uri); // Find the tool folder. var toolPath = GetToolPath(_environment.WorkingDirectory); // Install the NuGet package. var files = _installer .Install(package, PackageType.Tool, toolPath) .Where(file => { var extension = file.Path.GetExtension(); return(extension != null && extension.Equals(".cake", StringComparison.OrdinalIgnoreCase)); }) .ToArray(); if (files.Length == 0) { // No scripts found. _log.Warning("No scripts found in NuGet package {0}.", package.Package); return; } foreach (var file in files) { context.Analyze(file.Path); } }
public sealed override bool Process(IScriptAnalyzerContext context, string line, out string replacement) { if (context == null) { throw new ArgumentNullException(nameof(context)); } replacement = null; var tokens = Split(line); var directive = tokens.FirstOrDefault(); if (!string.IsNullOrWhiteSpace(directive)) { if (GetDirectiveNames().Any(n => directive.Equals(n, StringComparison.OrdinalIgnoreCase))) { if (tokens.Length >= 2) { // Try to parse an URI. var uri = ParseUriFromTokens(tokens); if (uri != null) { try { // Add the URI to the context. AddToContext(context, uri); } catch (Exception e) { // Add any errors to context context.AddScriptError(e.Message); } // Return success. return(true); } } } } return(false); }
private void ModuleAnalyzeCallback(IScriptAnalyzerContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } // Iterate all lines in the script. var lines = ReadLines(context.Current.Path); foreach (var line in lines) { foreach (var processor in _defaultProcessors) { if (processor.Process(context, _environment.ExpandEnvironmentVariables(line), out var _)) { break; } } } }
public override bool Process(IScriptAnalyzerContext context, string line, out string replacement) { if (context == null) { throw new ArgumentNullException("context"); } replacement = null; 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.Script.UsingAliases.Add(string.Join(" ", tokens)); return(true); } // Namespace context.Script.Namespaces.Add(@namespace); return(true); }
public void Load(IScriptAnalyzerContext context, LoadReference reference) { if (!_cache.TryGetValue(reference, out IReadOnlyList <FilePath> result)) { var provider = _loadDirectiveProviders.FirstOrDefault(x => x.CanLoad(context, reference)); if (provider == null) { return; } var interceptor = new InterceptingScriptAnalyzerContext(context); provider.Load(interceptor, reference); result = interceptor.FilePaths; _cache.Add(reference, result); } foreach (var file in result ?? Enumerable.Empty <FilePath>()) { context.Analyze(file); } }
public InterceptingScriptAnalyzerContext(IScriptAnalyzerContext context) => _context = context;
public bool CanLoad(IScriptAnalyzerContext context, LoadReference reference) { return(_loadDirectiveProviders.Any(x => x.CanLoad(context, reference))); }
/// <summary> /// Processes the specified line. /// </summary> /// <param name="analyzer">The analyzer.</param> /// <param name="line">The line.</param> /// <param name="replacement">The replacement for line, null if no replacement.</param> /// <returns><c>true</c> if the line was processed /// by this processor; otherwise <c>false</c>.</returns> public abstract bool Process(IScriptAnalyzerContext analyzer, string line, out string replacement);
/// <summary> /// Processes the specified line. /// </summary> /// <param name="analyzer">The analyzer.</param> /// <param name="line">The line.</param> /// <returns><c>true</c> if the line was processed /// by this processor; otherwise <c>false</c>.</returns> public abstract bool Process(IScriptAnalyzerContext analyzer, string line);
protected override void AddToContext(IScriptAnalyzerContext context, Uri uri) { var package = new PackageReference(uri); context.Current.Tools.Add(package); }
protected abstract void AddToContext(IScriptAnalyzerContext context, Uri uri);
public bool CanLoad(IScriptAnalyzerContext context, LoadReference reference) { return(reference.Scheme != null && reference.Scheme.Equals("local", StringComparison.OrdinalIgnoreCase)); }