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); AnalyserResult analyserResult = _analyser.Analyze(code); bool oldWay = false; #if NET462 oldWay = true; #endif var references = GetBuildScriptReferences(args, analyserResult, code, oldWay, buildScriptFilePath); if (oldWay) { return(await CreateBuildScriptInstanceOldWay(buildScriptFilePath, references, code, analyserResult)); } 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 void Analyse() { List <string> lines = new List <string>() { "//#ass", "//", "//#ass hello.dll", "//#imp test.cs", "public class MyScript" }; var res = _analyser.Analyze(lines); Assert.Equal("MyScript", res.ClassName); Assert.Single(res.References); Assert.Single(res.CsFiles); Assert.Equal(2, lines.Count); }
public void Analyse() { List <string> lines = new List <string>() { "//#ass", "//", "//#ass hello.dll", "//#nuget Package, 2.0.0", "//#imp test.cs", "public class MyScript" }; _pathWrapper.Setup(x => x.GetExtension(It.IsAny <string>())).Returns(".dll"); _fileWrapper.Setup(x => x.Exists(It.IsAny <string>())).Returns(true); var res = _analyser.Analyze(lines); Assert.Equal("MyScript", res.ClassName); Assert.Single(res.References); Assert.Single(res.NugetPackages); Assert.Single(res.CsFiles); Assert.Equal(2, lines.Count); }
public async Task <IBuildScript> FindAndCreateBuildScriptInstanceAsync(CommandArguments args) { var coreDir = Path.GetDirectoryName(typeof(object).GetTypeInfo().Assembly.Location); var flubuPath = typeof(DefaultBuildScript).GetTypeInfo().Assembly.Location; List <string> assemblyReferenceLocations = new List <string> { Path.Combine(coreDir, "mscorlib.dll"), typeof(object).GetTypeInfo().Assembly.Location, flubuPath, typeof(File).GetTypeInfo().Assembly.Location, typeof(ILookup <string, string>).GetTypeInfo().Assembly.Location, typeof(Expression).GetTypeInfo().Assembly.Location, }; List <MetadataReference> references = new List <MetadataReference>(); #if NETSTANDARD2_0 assemblyReferenceLocations.Add(typeof(Console).GetTypeInfo().Assembly.Location); #endif // Enumerate all assemblies referenced by this executing assembly // and provide them as references to the build script we're about to // compile. AssemblyName[] referencedAssemblies = Assembly.GetEntryAssembly().GetReferencedAssemblies(); foreach (var referencedAssembly in referencedAssemblies) { Assembly loadedAssembly = Assembly.Load(referencedAssembly); if (string.IsNullOrEmpty(loadedAssembly.Location)) { continue; } assemblyReferenceLocations.Add(loadedAssembly.Location); } string fileName = _buildScriptLocator.FindBuildScript(args); List <string> code = _file.ReadAllLines(fileName); AnalyserResult analyserResult = _analyser.Analyze(code); assemblyReferenceLocations.AddRange(analyserResult.References); foreach (var file in analyserResult.CsFiles) { if (_file.Exists(file)) { _log.LogInformation($"File found: {file}"); List <string> additionalCode = _file.ReadAllLines(file); AnalyserResult additionalCodeAnalyserResult = _analyser.Analyze(additionalCode); if (additionalCodeAnalyserResult.CsFiles.Count > 0) { throw new NotSupportedException("//#imp is only supported in main buildscript .cs file."); } var usings = additionalCode.Where(x => x.StartsWith("using")); assemblyReferenceLocations.AddRange(additionalCodeAnalyserResult.References); code.InsertRange(0, usings); code.AddRange(additionalCode.Where(x => !x.StartsWith("using"))); } else { _log.LogInformation($"File was not found: {file}"); } } assemblyReferenceLocations.AddRange(FindAssemblyReferencesInDirectories(args.AssemblyDirectories)); assemblyReferenceLocations = assemblyReferenceLocations.Distinct().ToList(); references.AddRange(assemblyReferenceLocations.Select(i => MetadataReference.CreateFromFile(i))); var opts = ScriptOptions.Default .WithReferences(references); Script script = CSharpScript .Create(string.Join("\r\n", code), opts) .ContinueWith(string.Format("var sc = new {0}();", analyserResult.ClassName)); try { ScriptState result = await script.RunAsync(); var buildScript = result.Variables[0].Value as IBuildScript; if (buildScript == null) { throw new ScriptLoaderExcetpion($"Class in file: {fileName} must inherit from DefaultBuildScript or implement IBuildScipt interface. See getting started on https://github.com/flubu-core/flubu.core/wiki"); } return(buildScript); } catch (CompilationErrorException e) { if (e.Message.Contains("CS0234")) { throw new ScriptLoaderExcetpion($"Csharp source code file: {fileName} has some compilation errors. {e.Message}. If u are using flubu script correctly you have to add assembly reference with #ref directive in build script. See build script fundamentals section 'Referencing other assemblies in build script' in https://github.com/flubu-core/flubu.core/wiki for more details.Otherwise if u think u are not using flubu correctly see Getting started section in wiki.", e); } throw new ScriptLoaderExcetpion($"Csharp source code file: {fileName} has some compilation errors. {e.Message}. See getting started and build script fundamentals in https://github.com/flubu-core/flubu.core/wiki", e); } }