Skip to content

Commit

Permalink
Merge branch 'release/0.8.0'
Browse files Browse the repository at this point in the history
* release/0.8.0:
  Add GitHub Actions & Remove Bitrise Badges
  Update src/Cake.Scripting/Cake.Scripting.csproj
  (GH-148) Also use Basic.Reference.Assemblies * fixes #148
  (GH-148) Use Cake Scripting Conventions
  • Loading branch information
gep13 committed Nov 24, 2021
2 parents 6562edd + 7c290ea commit 1b01113
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 52 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# Bakery

[![NuGet](https://img.shields.io/nuget/v/Cake.Bakery.svg)](https://www.nuget.org/packages/Cake.Bakery)
[![MyGet](https://img.shields.io/myget/cake/vpre/Cake.Bakery.svg?label=myget)](https://www.myget.org/gallery/cake)

Cake server for code generation and script analysis
Cake server for code generation and script analysis, used for i.e. intellisense support in Visual Studio Code.

## Build Status

|Build server|Platform|Develop|Master|
|:--:|:--:|:--:|:--:|
|AppVeyor|Windows|[![Build status](https://ci.appveyor.com/api/projects/status/ayjo26v8rbhp2eqt/branch/develop?svg=true)](https://ci.appveyor.com/project/cakebuild/bakery/branch/develop)|[![Build status](https://ci.appveyor.com/api/projects/status/ayjo26v8rbhp2eqt/branch/develop?svg=true)](https://ci.appveyor.com/project/cakebuild/bakery/branch/master)|
|Bitrise|OS X|[![Build Status](https://www.bitrise.io/app/000857f04ffa750e/status.svg?token=B72wRhe84cgWLCI5k_dgNw&branch=develop)](https://www.bitrise.io/app/000857f04ffa750e)|[![Build Status](https://www.bitrise.io/app/000857f04ffa750e/status.svg?token=B72wRhe84cgWLCI5k_dgNw&branch=master)](https://www.bitrise.io/app/000857f04ffa750e)|
|Bitrise|Linux|[![Build Status](https://www.bitrise.io/app/704fc45fd74ac20f/status.svg?token=TnnmPBUFK5KISi5WhME5MQ&branch=develop)](https://www.bitrise.io/app/704fc45fd74ac20f)|[![Build Status](https://www.bitrise.io/app/704fc45fd74ac20f/status.svg?token=TnnmPBUFK5KISi5WhME5MQ&branch=master)](https://www.bitrise.io/app/704fc45fd74ac20f)|
| Build server | Platform | Develop| Master |
|-------------------|:-------------:|:------:|:------:|
| AppVeyor | Windows | [![Build status](https://ci.appveyor.com/api/projects/status/ayjo26v8rbhp2eqt/branch/develop?svg=true)](https://ci.appveyor.com/project/cakebuild/bakery/branch/develop) | [![Build status](https://ci.appveyor.com/api/projects/status/ayjo26v8rbhp2eqt/branch/develop?svg=true)](https://ci.appveyor.com/project/cakebuild/bakery/branch/master)|
| GitHub Actions | Windows | [![Build status](https://github.com/cake-build/bakery/actions/workflows/build.yml/badge.svg?branch=develop)](https://github.com/cake-build/bakery/actions/workflows/build.yml) | [![Build status](https://github.com/cake-build/bakery/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/cake-build/bakery/actions/workflows/build.yml) |
| GitHub Actions | macOS / Linux | [![Posix Build status](https://github.com/cake-build/bakery/actions/workflows/posixbuild.yml/badge.svg?branch=develop)](https://github.com/cake-build/bakery/actions/workflows/posixbuild.yml) | [![Posix Build status](https://github.com/cake-build/bakery/actions/workflows/posixbuild.yml/badge.svg?branch=master)](https://github.com/cake-build/bakery/actions/workflows/posixbuild.yml) |

## Code of Conduct

Expand Down
1 change: 1 addition & 0 deletions src/Cake.Scripting/Cake.Scripting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageReference Include="Cake.Core" Version="1.3.0" />
<PackageReference Include="Cake.Common" Version="1.3.0" />
<PackageReference Include="Mono.Cecil" Version="0.10.1" />
<PackageReference Include="Basic.Reference.Assemblies.Net60" Version="1.2.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Cake.Scripting.Abstractions\Cake.Scripting.Abstractions.csproj" />
Expand Down
59 changes: 13 additions & 46 deletions src/Cake.Scripting/CodeGen/CakeScriptGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ public sealed class CakeScriptGenerator : IScriptGenerationService
private readonly IBufferedFileSystem _fileSystem;
private readonly IScriptAliasFinder _aliasFinder;
private readonly ICakeAliasGenerator _aliasGenerator;
private readonly IScriptConventions _scriptConventions;
private readonly IReferenceAssemblyResolver _referenceAssemblyResolver;
private readonly DirectoryPath _addinRoot;
private readonly ScriptHost _hostObject;
private readonly Lazy<ISet<FilePath>> _defaultReferences;

public CakeScriptGenerator(
IBufferedFileSystem fileSystem,
Expand All @@ -52,6 +53,8 @@ public CakeScriptGenerator(
IScriptAliasFinder aliasFinder,
ICakeAliasGenerator aliasGenerator,
ICakeLog log,
IScriptConventions scriptConventions,
IReferenceAssemblyResolver referenceAssemblyResolver,
IEnumerable<ILoadDirectiveProvider> loadDirectiveProviders = null)
{
_fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
Expand All @@ -63,10 +66,11 @@ public CakeScriptGenerator(
_aliasFinder = aliasFinder ?? throw new ArgumentNullException(nameof(aliasFinder));
_aliasGenerator = aliasGenerator ?? throw new ArgumentNullException(nameof(aliasGenerator));
_analyzer = new ScriptAnalyzer(_fileSystem, _environment, _log, loadDirectiveProviders);
_scriptConventions = scriptConventions ?? throw new ArgumentNullException(nameof(scriptConventions));
_referenceAssemblyResolver = referenceAssemblyResolver ?? throw new ArgumentNullException(nameof(referenceAssemblyResolver));

_addinRoot = GetAddinPath(_environment.WorkingDirectory);
_hostObject = GetHostObject();
_defaultReferences = new Lazy<ISet<FilePath>>(GetDefaultReferences);
}

public CakeScript Generate(FileChange fileChange)
Expand Down Expand Up @@ -111,7 +115,12 @@ public CakeScript Generate(FileChange fileChange)

// Load all references.
_log.Verbose("Adding references...");
var references = new HashSet<FilePath>(_defaultReferences.Value);
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
Expand All @@ -128,7 +137,7 @@ public CakeScript Generate(FileChange fileChange)
// Import all namespaces.
_log.Verbose("Importing namespaces...");
var namespaces = new HashSet<string>(result.Namespaces, StringComparer.Ordinal);
namespaces.AddRange(GetDefaultNamespaces());
namespaces.AddRange(_scriptConventions.GetDefaultNamespaces());
namespaces.AddRange(aliases.SelectMany(alias => alias.Namespaces));

// Create the response.
Expand Down Expand Up @@ -165,48 +174,6 @@ private void HandleFileChange(FilePath path, FileChange fileChange)
_fileSystem.UpdateFileBuffer(path, fileChange.Buffer);
}

// TODO: Move to conventions
private IEnumerable<string> GetDefaultNamespaces()
{
return new List<string>
{
"System",
"System.Collections.Generic",
"System.Linq",
"System.Text",
"System.Threading.Tasks",
"System.IO",
"Cake.Core",
"Cake.Core.IO",
"Cake.Core.Scripting",
"Cake.Core.Diagnostics"
};
}

private ISet<FilePath> GetDefaultReferences()
{
// Prepare the default assemblies.
var references = new HashSet<FilePath>();
references.Add(typeof(Action).GetTypeInfo().Assembly.Location); // mscorlib or System.Private.Core
references.Add(typeof(IQueryable).GetTypeInfo().Assembly.Location); // System.Core or System.Linq.Expressions

references.Add(typeof(IScriptHost).Assembly.Location); // Cake.Core
references.Add(typeof(EnvironmentAliases).Assembly.Location); // Cake.Common

references.Add(typeof(Uri).GetTypeInfo().Assembly.Location); // System
references.Add(typeof(Enumerable).GetTypeInfo().Assembly.Location); // System.Linq
references.Add(typeof(XmlReader).GetTypeInfo().Assembly.Location); // System.Xml
references.Add(typeof(XDocument).GetTypeInfo().Assembly.Location); // System.Xml.Linq
references.Add(typeof(DataTable).GetTypeInfo().Assembly.Location); // System.Data
references.Add(typeof(ZipArchive).GetTypeInfo().Assembly.Location); // System.IO.Compression
references.Add(typeof(ZipFile).GetTypeInfo().Assembly.Location); // System.IO.Compression.FileSystem
references.Add(typeof(HttpClient).GetTypeInfo().Assembly.Location); // System.Net.Http
references.Add(typeof(DataContractJsonSerializer).GetTypeInfo().Assembly.Location); // System.Runtime.Serialization

// Return the assemblies.
return references;
}

private static ScriptHost GetHostObject()
{
return new ScriptHost
Expand Down
19 changes: 19 additions & 0 deletions src/Cake.Scripting/Polyfill/IReferenceAssemblyResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Reflection;

#pragma warning disable IDE0130
// Temporary polyfill see https://github.com/cake-build/cake/pull/3715
namespace Cake.Core.Scripting
#pragma warning restore IDE0130
{
/// <summary>
/// Represents a framework reference assembly resolver.
/// </summary>
public interface IReferenceAssemblyResolver
{
/// <summary>
/// Finds framwork reference assemblies.
/// </summary>
/// <returns>The resolved reference assemblies.</returns>
Assembly[] GetReferenceAssemblies();
}
}
43 changes: 43 additions & 0 deletions src/Cake.Scripting/Reflection/ReferenceAssemblyResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Cake.Core.Diagnostics;
using Cake.Core.Scripting;

namespace Cake.Scripting.Reflection
{
public sealed class ReferenceAssemblyResolver : IReferenceAssemblyResolver
{
private readonly ICakeLog _log;

public ReferenceAssemblyResolver(ICakeLog log)
{
_log = log;
}

public Assembly[] GetReferenceAssemblies()
{
IEnumerable<Assembly> TryGetReferenceAssemblies()
{
foreach (var reference in Basic.Reference.Assemblies.Net60.All)
{
Assembly name;
try
{
name = Assembly.Load(System.IO.Path.GetFileNameWithoutExtension(reference.FilePath));
}
catch (Exception ex)
{
_log.Debug(log => log("Failed to load {0}\r\n{1}", reference.FilePath, ex));
continue;
}

yield return name;
}
}

return TryGetReferenceAssemblies().ToArray();
}
}
}
2 changes: 2 additions & 0 deletions src/Cake.Scripting/ScriptingModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Cake.Scripting.CodeGen;
using Cake.Scripting.CodeGen.Generators;
using Cake.Scripting.IO;
using Cake.Scripting.Reflection;

namespace Cake.Scripting
{
Expand Down Expand Up @@ -41,6 +42,7 @@ public void Register(ICakeContainerRegistrar registrar)
registrar.RegisterType<CakeScriptAliasFinder>().As<IScriptAliasFinder>().Singleton();
registrar.RegisterType<CakeScriptGenerator>().As<IScriptGenerationService>().Singleton();
registrar.RegisterType<CakeAliasGenerator>().As<ICakeAliasGenerator>().Singleton();
registrar.RegisterType<ReferenceAssemblyResolver>().As<Core.Scripting.IReferenceAssemblyResolver>().Singleton();
}
}
}

0 comments on commit 1b01113

Please sign in to comment.