/// <inheritdoc/> public IReadOnlyList <Assembly> GetDefaultAssemblies(DirectoryPath root) { // Prepare the default assemblies. var result = new HashSet <Assembly>(new SimpleAssemblyComparer()); result.Add(typeof(Action).GetTypeInfo().Assembly); // mscorlib or System.Private.Core result.Add(typeof(IQueryable).GetTypeInfo().Assembly); // System.Core or System.Linq.Expressions result.Add(typeof(Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo).Assembly); // Dynamic support result.AddRange(_referenceAssemblyResolver.GetReferenceAssemblies()); // Load other Cake-related assemblies that we need. var cakeAssemblies = LoadCakeAssemblies(root); result.AddRange(cakeAssemblies); // Load all referenced assemblies. foreach (var cakeAssembly in cakeAssemblies) { foreach (var reference in cakeAssembly.GetReferencedAssemblies()) { result.Add(_loader.Load(reference)); } } // Return the assemblies. return(result.ToArray()); }
public CakeScript Generate(FileChange fileChange) { if (fileChange == null) { throw new ArgumentNullException(nameof(fileChange)); } // Make the script path absolute. var scriptPath = new FilePath(fileChange.FileName).MakeAbsolute(_environment); // Prepare the file changes _log.Verbose("Handling file change..."); HandleFileChange(scriptPath, fileChange); // Analyze the script file. _log.Verbose("Analyzing build script..."); var result = _analyzer.Analyze(scriptPath, new ScriptAnalyzerSettings { Mode = ScriptAnalyzerMode.Everything, }); // Install addins. foreach (var addin in result.Addins) { try { _log.Verbose("Installing addins..."); var addinReferences = _processor.InstallAddins(new[] { addin }, _addinRoot); foreach (var addinReference in addinReferences) { result.References.Add(addinReference.FullPath); } } catch (Exception e) { // Log and continue if it fails _log.Error(e); } } // Load all references. _log.Verbose("Adding references..."); var references = new HashSet <FilePath>( _scriptConventions .GetDefaultAssemblies(_environment.ApplicationRoot) .Union(_referenceAssemblyResolver.GetReferenceAssemblies()) .Select(a => FilePath.FromString(a.Location))); references.AddRange(result.References.Select(r => new FilePath(r))); // Find aliases _log.Verbose("Finding aliases..."); var aliases = new List <CakeScriptAlias>(); foreach (var reference in references) { if (_fileSystem.Exist(reference)) { aliases.AddRange(_aliasFinder.FindAliases(reference)); } } // Import all namespaces. _log.Verbose("Importing namespaces..."); var namespaces = new HashSet <string>(result.Namespaces, StringComparer.Ordinal); namespaces.AddRange(_scriptConventions.GetDefaultNamespaces()); namespaces.AddRange(aliases.SelectMany(alias => alias.Namespaces)); // Create the response. // ReSharper disable once UseObjectOrCollectionInitializer _log.Verbose("Creating response..."); var response = new CakeScript(); response.Host.TypeName = _hostObject.TypeName; response.Host.AssemblyPath = _hostObject.AssemblyPath; response.Source = string.Join("\n", result.Defines) + string.Join("\n", result.UsingAliases) + string.Join("\n", result.UsingStaticDirectives) + GenerateSource(aliases) + string.Join("\n", result.Lines); response.Usings.AddRange(namespaces); response.References.AddRange(references.Select(r => r.FullPath)); // Return the response. return(response); }