public async Task <IBuildScript> FindAndCreateBuildScriptInstanceAsync(CommandArguments args) { string buildScriptFilePath = _buildScriptLocator.FindBuildScript(args); var buildScriptAssemblyPath = Path.Combine("bin", Path.GetFileName(buildScriptFilePath)); buildScriptAssemblyPath = Path.ChangeExtension(buildScriptAssemblyPath, "dll"); List <string> code = _file.ReadAllLines(buildScriptFilePath); ScriptAnalyzerResult scriptAnalyzerResult = _scriptAnalyzer.Analyze(code); ProjectFileAnalyzerResult projectFileAnalyzerResult = _projectFileAnalyzer.Analyze(disableAnalysis: scriptAnalyzerResult.ScriptAttributes.Contains(ScriptAttributes.DisableLoadScriptReferencesAutomatically)); bool oldWay = false; #if NET462 oldWay = true; #endif var references = GetBuildScriptReferences(args, projectFileAnalyzerResult, scriptAnalyzerResult, code, oldWay, buildScriptFilePath); if (oldWay) { return(await CreateBuildScriptInstanceOldWay(buildScriptFilePath, references, code, scriptAnalyzerResult)); } var assembly = TryLoadBuildScriptFromAssembly(buildScriptAssemblyPath, buildScriptFilePath); if (assembly != null) { return(CreateBuildScriptInstance(assembly, buildScriptFilePath)); } code.Insert(0, $"#line 1 \"{buildScriptFilePath}\""); assembly = CompileBuildScriptToAssembly(buildScriptAssemblyPath, buildScriptFilePath, references, string.Join("\r\n", code)); return(CreateBuildScriptInstance(assembly, buildScriptFilePath)); }
public bool Process(ScriptAnalyzerResult analyzerResult, string line, int lineIndex) { if (!line.StartsWith("//#nuget", StringComparison.OrdinalIgnoreCase)) { return(false); } int nugetIndex = line.IndexOf(" ", StringComparison.Ordinal); if (nugetIndex < 0) { return(true); } string nugetPackage = line.Substring(nugetIndex); var nugetInfos = nugetPackage.Split(','); if (nugetInfos.Length != 2) { throw new ScriptException("Invalid nuget package directive. Example of valid directive: '//#nuget: FlubuCore, 2.8.0'"); } _logger.LogInformation("#nuget directives are obsolete and will be removed in future versions. Use 'Nuget' attribute instead on build script class."); analyzerResult.NugetPackageReferences.Add(new NugetPackageReference { Id = nugetInfos[0].Trim(), Version = nugetInfos[1].Trim() }); return(true); }
public bool Process(ScriptAnalyzerResult analyzerResult, string line, int lineIndex) { var i = line.IndexOf("class", StringComparison.Ordinal); if (i < 0) { return(false); } var partialIndex = line.IndexOf("partial", StringComparison.Ordinal); if (partialIndex > 0 && partialIndex < i) { analyzerResult.IsPartial = true; } var tmp = line.Substring(i + 6); tmp = tmp.TrimStart(); i = tmp.IndexOf(" ", StringComparison.Ordinal); if (i == -1) { i = tmp.Length; } analyzerResult.ClassName = tmp.Substring(0, i); return(true); }
private void ProcessConfigAttributes(ScriptAnalyzerResult scriptAnalyzerResult) { if (scriptAnalyzerResult.ScriptAttributes.Contains(ScriptConfigAttributes.DisableColoredLogging)) { FlubuConsoleLogger.DisableColloredLogging = true; } }
private void AddOtherCsFilesToBuildScriptCode(ScriptAnalyzerResult analyzerResult, List <AssemblyInfo> assemblyReferenceLocations, List <string> code) { foreach (var file in analyzerResult.CsFiles) { if (_file.Exists(file)) { _log.LogInformation($"File found: {file}"); List <string> additionalCode = _file.ReadAllLines(file); ScriptAnalyzerResult additionalCodeAnalyzerResult = _scriptAnalyzer.Analyze(additionalCode); if (additionalCodeAnalyzerResult.CsFiles.Count > 0) { throw new NotSupportedException("//#imp is only supported in main buildscript .cs file."); } var usings = additionalCode.Where(x => x.StartsWith("using")); assemblyReferenceLocations.AddOrUpdateAssemblyInfo(additionalCodeAnalyzerResult.AssemblyReferences); code.InsertRange(1, usings); code.AddRange(additionalCode.Where(x => !x.StartsWith("using"))); } else { _log.LogInformation($"File was not found: {file}"); } } }
public void AssemblyFromDirectoryAttribute_Succesfull(string line) { AttributesProcessor pr = new AttributesProcessor(_fileWrapper.Object, _pathWrapper.Object, _directory.Object); ScriptAnalyzerResult res = new ScriptAnalyzerResult(); _directory .Setup(x => x.GetFiles("c:\\hello", "*.dll", SearchOption.TopDirectoryOnly)) .Returns(new string[] { "c:\\hello\\test.dll", "c:\\hello\\test2.dll", }); _pathWrapper.Setup(x => x.GetExtension("c:\\hello\\test.dll")).Returns(".dll"); _pathWrapper.Setup(x => x.GetExtension("c:\\hello\\test2.dll")).Returns(".dll"); _fileWrapper.Setup(x => x.Exists("c:\\hello\\test.dll")).Returns(true); _fileWrapper.Setup(x => x.Exists("c:\\hello\\test2.dll")).Returns(true); pr.Process(res, line, 1); Assert.Equal("c:\\hello\\test.dll", res.AssemblyReferences.First().FullPath); Assert.Equal("c:\\hello\\test2.dll", res.AssemblyReferences[1].FullPath); _fileWrapper.VerifyAll(); _pathWrapper.VerifyAll(); _directory.VerifyAll(); }
private void ProcessBaseClass(ScriptAnalyzerResult analyzerResult, string line) { var baseClassIndex = line.IndexOf(":", StringComparison.Ordinal); if (baseClassIndex < 1) { return; } var baseClassName = line.Substring(baseClassIndex + 1); baseClassName = baseClassName.TrimStart(); var baseClassEndIndex = baseClassName.IndexOf(" ", StringComparison.Ordinal); if (baseClassEndIndex != -1) { baseClassName = baseClassName.Substring(0, baseClassEndIndex); } analyzerResult.BaseClassName = baseClassName.TrimEnd(); var baseClassfileName = $"{baseClassName}.cs"; if (baseClassName != nameof(DefaultBuildScript) && _file.Exists(baseClassfileName)) { analyzerResult.CsFiles.Add(_path.GetFullPath(baseClassfileName)); } }
public bool Process(ScriptAnalyzerResult analyzerResult, string line, int lineIndex) { if (!line.StartsWith("//#nuget", StringComparison.OrdinalIgnoreCase)) { return(false); } int nugetIndex = line.IndexOf(" ", StringComparison.Ordinal); if (nugetIndex < 0) { return(true); } string nugetPackage = line.Substring(nugetIndex); var nugetInfos = nugetPackage.Split(','); if (nugetInfos.Length != 2) { throw new ScriptException("Invalid nuget package directive. Example of valid directive: '//#nuget: FlubuCore, 2.8.0'"); } analyzerResult.NugetPackageReferences.Add(new NugetPackageReference { Id = nugetInfos[0].Trim(), Version = nugetInfos[1].Trim() }); return(true); }
/// <summary> /// Installs the tools specified in the build scripts. /// </summary> /// <param name="analyzerResult">The analyzer result.</param> /// <param name="installationRoot">The installation root path.</param> public void InstallTools( ScriptAnalyzerResult analyzerResult, DirectoryPath installationRoot) { if (analyzerResult == null) { throw new ArgumentNullException("analyzerResult"); } if (installationRoot == null) { throw new ArgumentNullException("installationRoot"); } // Make the installation root absolute. installationRoot = installationRoot.MakeAbsolute(_environment); if (analyzerResult.Tools.Count > 0) { _log.Verbose("Installing tools..."); foreach (var addin in analyzerResult.Tools) { var toolExecutables = InstallPackage(addin, installationRoot, GetToolExecutables); if (toolExecutables.Length == 0) { const string format = "Failed to install tool '{0}'."; var message = string.Format(CultureInfo.InvariantCulture, format, addin.PackageId); throw new CakeException(message); } } } }
public ScriptProcessorFixture() { Environment = FakeEnvironment.CreateUnixEnvironment(); FileSystem = new FakeFileSystem(Environment); Log = Substitute.For <ICakeLog>(); Installer = Substitute.For <INuGetPackageInstaller>(); ApplicationRoot = new DirectoryPath("/Working/Bin"); AssembliesLocator = Substitute.For <INuGetPackageAssembliesLocator>(); AssembliesLocator.FindAssemblies(Arg.Any <DirectoryPath>()).Returns( ci => { var dir = FileSystem.GetDirectory(ci.Arg <DirectoryPath>()); return(dir.GetFiles("*.dll", SearchScope.Recursive).ToArray()); }); // Create the script analyzer result. var script1 = new ScriptInformation("/Working/build.cake"); script1.Addins.Add(new NuGetPackage("Addin") { Source = "http://example.com" }); script1.Tools.Add(new NuGetPackage("Tool") { Source = "http://example.com" }); Result = new ScriptAnalyzerResult(script1, new List <string>()); }
private void ProcessAssemblyAttribute(ScriptAnalyzerResult analyzerResult, string line) { string dll = GetSingleParameterFromAttrbiute(line); string pathToDll = Path.GetFullPath(dll); ProcessAssembly(analyzerResult, pathToDll); }
public void ParseCs(string line, string expected) { CsDirectiveProcessor pr = new CsDirectiveProcessor(); ScriptAnalyzerResult res = new ScriptAnalyzerResult(); pr.Process(res, line, 1); Assert.Equal(expected, res.CsFiles.First()); }
public void GetInterfaceNameFromBuildScriptCodeTest(string code, string expectedInterfaceName, bool isPartial) { InterfaceDirectiveProcessor pr = new InterfaceDirectiveProcessor(); ScriptAnalyzerResult res = new ScriptAnalyzerResult(); pr.Process(res, code, 1); Assert.Equal(expectedInterfaceName, res.InterfaceName); }
public void IncludeAttribute_Succesfull(string line) { AttributesProcessor pr = new AttributesProcessor(_fileWrapper.Object, _pathWrapper.Object, _directory.Object); ScriptAnalyzerResult res = new ScriptAnalyzerResult(); pr.Process(res, line, 1); Assert.Contains("\\Test.cs", res.CsFiles[0]); }
private static void ProcessNugetPackageAttribute(ScriptAnalyzerResult analyzerResult, string line) { var nugetInfo = GetMultipleParametersFromAttribute(line); analyzerResult.NugetPackageReferences.Add(new NugetPackageReference { Id = nugetInfo[0].Replace("\"", string.Empty).Trim(), Version = nugetInfo[1].Replace("\"", string.Empty).Trim() }); }
public void GetClassNameFromBuildScriptCodeTest(string code, string expectedClassName) { ClassDirectiveProcessor pr = new ClassDirectiveProcessor(); ScriptAnalyzerResult res = new ScriptAnalyzerResult(); pr.Process(res, code, 1); Assert.Equal(expectedClassName, res.ClassName); }
private static void ProcessReferenceAttribute(ScriptAnalyzerResult analyzerResult, string line) { var reference = GetSingleParameterFromAttrbiute(line); var type = Type.GetType(reference, true); var ass = type.GetTypeInfo().Assembly; analyzerResult.AssemblyReferences.Add(ass.ToAssemblyInfo()); }
private void ProcessAddedCsFilesToBuildScript(ScriptAnalyzerResult analyzerResult, List <string> code) { List <string> csFiles = new List <string>(); foreach (var csDirectory in analyzerResult.CsDirectories) { if (Directory.Exists(csDirectory.Item1.path)) { var searchOption = csDirectory.Item1.includeSubDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; csFiles.AddRange(Directory.GetFiles(csDirectory.Item1.path, "*.cs", searchOption)); } else { _log.LogInformation($"Directory not found: '{csDirectory.Item1.path}'."); } } csFiles.AddRange(analyzerResult.CsFiles); List <string> namespaces = new List <string>(); foreach (var file in csFiles) { if (_file.Exists(file)) { _log.LogInformation($"File found: {file}"); List <string> additionalCode = _file.ReadAllLines(file); ScriptAnalyzerResult additionalCodeAnalyzerResult = _scriptAnalyzer.Analyze(additionalCode); if (additionalCodeAnalyzerResult.CsFiles.Count > 0) { throw new NotSupportedException("//#imp is only supported in main buildscript .cs file."); } namespaces.Add(additionalCodeAnalyzerResult.Namespace); var usings = additionalCode.Where(x => x.StartsWith("using")); analyzerResult.AssemblyReferences.AddRange(additionalCodeAnalyzerResult.AssemblyReferences); code.InsertRange(1, usings); code.AddRange(additionalCode.Where(x => !x.StartsWith("using"))); } else { _log.LogInformation($"File was not found: {file}"); } } namespaces = namespaces.Distinct().ToList(); foreach (var ns in namespaces) { var usng = $"using {ns};"; code.Remove(usng); } }
public void NugetReference(string line, string expectedId, string expectedVersion) { NugetPackageDirectirveProcessor pr = new NugetPackageDirectirveProcessor(new Mock <ILogger <NugetPackageDirectirveProcessor> >().Object); ScriptAnalyzerResult res = new ScriptAnalyzerResult(); pr.Process(res, line, 1); Assert.Equal(expectedId, res.NugetPackageReferences.First().Id); Assert.Equal(expectedVersion, res.NugetPackageReferences.First().Version); }
private static void ProcessPartial(ScriptAnalyzerResult analyzerResult, string line, int i) { var partialIndex = line.IndexOf("partial", StringComparison.Ordinal); if (partialIndex > 0 && partialIndex < i) { analyzerResult.IsPartial = true; } }
public void GetBaseClass_DefaultBuildScript(string code, string expectedBaseClass) { ClassDirectiveProcessor pr = new ClassDirectiveProcessor(_fileWrapper.Object, _pathWrapper.Object); ScriptAnalyzerResult res = new ScriptAnalyzerResult(); pr.Process(res, code, 1); Assert.Equal(expectedBaseClass, res.BaseClassName); Assert.Empty(res.CsFiles); }
public void NugetPackageAttribute_Succesfull(string line) { AttributesProcessor pr = new AttributesProcessor(_fileWrapper.Object, _pathWrapper.Object, _directory.Object); ScriptAnalyzerResult res = new ScriptAnalyzerResult(); pr.Process(res, line, 1); Assert.Equal("FlubuCore", res.NugetPackageReferences.First().Id); Assert.Equal("2.7.0", res.NugetPackageReferences.First().Version); }
public void GetClassNameFromBuildScriptCodeTest(string code, string expectedClassName, bool isPartial) { ClassDirectiveProcessor pr = new ClassDirectiveProcessor(_fileWrapper.Object, _pathWrapper.Object); ScriptAnalyzerResult res = new ScriptAnalyzerResult(); pr.Process(res, code, 1); Assert.Equal(expectedClassName, res.ClassName); Assert.Equal(isPartial, res.IsPartial); }
public void IncludeFromDirectoryAttribute_Succesfull(string line, bool expectedIncludeSubDir) { AttributesProcessor pr = new AttributesProcessor(_fileWrapper.Object, _pathWrapper.Object, _directory.Object); ScriptAnalyzerResult res = new ScriptAnalyzerResult(); pr.Process(res, line, 1); Assert.Contains("\\Test", res.CsDirectories[0].Item1.path); Assert.Equal(expectedIncludeSubDir, res.CsDirectories[0].Item1.includeSubDirectories); }
public void NamespaceProcessor_Succesfull(string line) { NamespaceProcessor pr = new NamespaceProcessor(); ScriptAnalyzerResult res = new ScriptAnalyzerResult(); pr.Process(res, line, 2); Assert.Equal(2, res.NamespaceIndex); Assert.Equal("FlubuCore.Helpers", res.Namespace); }
public bool Process(ScriptAnalyzerResult analyzerResult, string line, int lineIndex) { if (line.StartsWith("namespace")) { analyzerResult.NamespaceIndex = lineIndex; return(false); } return(false); }
public void AssemblyAttribute_Succesfull(string line) { AttributesProcessor pr = new AttributesProcessor(_fileWrapper.Object, _pathWrapper.Object, _directory.Object); ScriptAnalyzerResult res = new ScriptAnalyzerResult(); _pathWrapper.Setup(x => x.GetExtension("c:\\hello.dll")).Returns(".dll"); _fileWrapper.Setup(x => x.Exists("c:\\hello.dll")).Returns(true); pr.Process(res, line, 1); Assert.Equal("c:\\hello.dll", res.AssemblyReferences.First().FullPath); }
public void ParseDll(string line, string expected) { AssemblyDirectiveProcessor pr = new AssemblyDirectiveProcessor(_fileWrapper.Object, _pathWrapper.Object, new Mock <ILogger <AssemblyDirectiveProcessor> >().Object); _pathWrapper.Setup(x => x.GetExtension(expected)).Returns(".dll"); _fileWrapper.Setup(x => x.Exists(expected)).Returns(true); ScriptAnalyzerResult res = new ScriptAnalyzerResult(); pr.Process(res, line, 1); Assert.Equal(expected, res.AssemblyReferences.First().FullPath); }
public void Should_Set_Succeeded_To_True_If_Not_Any_Errors() { // Given var script = new ScriptInformation("./build.cake"); // When var result = new ScriptAnalyzerResult(script, new List <string>(), new List <ScriptAnalyzerError>()); // Then Assert.True(result.Succeeded); }
public void GetBaseClass_NotDefaultBuildScript() { string code = "public class BuildScript : AwesomeBuildScript"; ClassDirectiveProcessor pr = new ClassDirectiveProcessor(_fileWrapper.Object, _pathWrapper.Object); ScriptAnalyzerResult res = new ScriptAnalyzerResult(); _fileWrapper.Setup(x => x.Exists("AwesomeBuildScript.cs")).Returns(true); _pathWrapper.Setup(x => x.GetFullPath("AwesomeBuildScript.cs")).Returns("/AwesomeBuildScript.cs"); pr.Process(res, code, 1); Assert.Equal("AwesomeBuildScript", res.BaseClassName); Assert.Single(res.CsFiles); Assert.Equal("/AwesomeBuildScript.cs", res.CsFiles[0]); }