Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to compile a cs files without a project #958

Merged
merged 4 commits into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 58 additions & 20 deletions src/Neo.Compiler.CSharp/CompilationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Akka.Util.Internal;
using BigInteger = System.Numerics.BigInteger;

namespace Neo.Compiler
Expand Down Expand Up @@ -60,13 +59,44 @@ public List<CompilationContext> Compile(IEnumerable<string> sourceFiles, IEnumer
return CompileProjectContracts(Compilation);
}

public List<CompilationContext> CompileSources(string[] sourceFiles)
public List<CompilationContext> CompileSources(params string[] sourceFiles)
{
List<MetadataReference> references = new(CommonReferences)
{
MetadataReference.CreateFromFile(typeof(scfx.Neo.SmartContract.Framework.SmartContract).Assembly.Location)
};
return Compile(sourceFiles, references);
// Generate a dummy csproj

var version = typeof(scfx.Neo.SmartContract.Framework.SmartContract).Assembly.GetName().Version!.ToString();
var csproj = $@"
<Project Sdk=""Microsoft.NET.Sdk"">

<PropertyGroup>
<TargetFramework>{AppContext.TargetFrameworkName!}</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<!-- Remove all Compile items from compilation -->
<ItemGroup>
<Compile Remove=""*.cs"" />
</ItemGroup>

<!-- Add specific files for compilation -->
<ItemGroup>
{string.Join(Environment.NewLine, sourceFiles.Select(u => $"<Compile Include=\"{Path.GetFullPath(u)}\" />"))}
</ItemGroup>

<ItemGroup>
<PackageReference Include=""Neo.SmartContract.Framework"" Version=""{version}"" />
</ItemGroup>

</Project>";
shargon marked this conversation as resolved.
Show resolved Hide resolved

// Write and compile

var path = Path.GetTempFileName();
File.WriteAllText(path, csproj);

try { return CompileProject(path); }
catch { throw; }
finally { File.Delete(path); }
}

public List<CompilationContext> CompileProject(string csproj)
Expand Down Expand Up @@ -176,26 +206,34 @@ static bool IsDerivedFromSmartContract(INamedTypeSymbol classSymbol, string smar

public Compilation GetCompilation(string csproj)
{
// Restore project

string folder = Path.GetDirectoryName(csproj)!;
string obj = Path.Combine(folder, "obj");
string binSc = Path.Combine(Path.Combine(folder, "bin"), "sc");
HashSet<string> sourceFiles = Directory.EnumerateFiles(folder, "*.cs", SearchOption.AllDirectories)
.Where(p => !p.StartsWith(obj) && !p.StartsWith(binSc))
.GroupBy(Path.GetFileName)
.Select(g => g.First())
.ToHashSet(StringComparer.OrdinalIgnoreCase);
List<MetadataReference> references = new(CommonReferences);
CSharpCompilationOptions compilationOptions = new(OutputKind.DynamicallyLinkedLibrary, deterministic: true, nullableContextOptions: Options.Nullable);
XDocument document = XDocument.Load(csproj);
sourceFiles.UnionWith(document.Root!.Elements("ItemGroup").Elements("Compile").Attributes("Include").Select(p => Path.GetFullPath(p.Value, folder)));
Process.Start(new ProcessStartInfo
{
FileName = "dotnet",
Arguments = $"restore \"{csproj}\"",
WorkingDirectory = folder
})!.WaitForExit();
string assetsPath = Path.Combine(folder, "obj", "project.assets.json");
JObject assets = (JObject)JToken.Parse(File.ReadAllBytes(assetsPath))!;

// Get sources

XDocument document = XDocument.Load(csproj);
var remove = document.Root!.Elements("ItemGroup").Elements("Compile").Attributes("Remove").Select(p => p.Value).ToArray();
var obj = Path.Combine(folder, "obj");
var binSc = Path.Combine(Path.Combine(folder, "bin"), "sc");
var sourceFiles =
remove.Contains("*.cs") ? new HashSet<string>(StringComparer.OrdinalIgnoreCase) :
Directory.EnumerateFiles(folder, "*.cs", SearchOption.AllDirectories)
.Where(p => !p.StartsWith(obj) && !p.StartsWith(binSc))
.GroupBy(Path.GetFileName)
.Select(g => g.First())
.ToHashSet(StringComparer.OrdinalIgnoreCase);
sourceFiles.UnionWith(document.Root!.Elements("ItemGroup").Elements("Compile").Attributes("Include").Select(p => Path.GetFullPath(p.Value, folder)));
var assetsPath = Path.Combine(folder, "obj", "project.assets.json");
var assets = (JObject)JToken.Parse(File.ReadAllBytes(assetsPath))!;
List<MetadataReference> references = new(CommonReferences);
CSharpCompilationOptions compilationOptions = new(OutputKind.DynamicallyLinkedLibrary, deterministic: true, nullableContextOptions: Options.Nullable);
foreach (var (name, package) in ((JObject)assets["targets"]![0]!).Properties)
{
MetadataReference? reference = GetReference(name, (JObject)package!, assets, folder, Options, compilationOptions);
Expand Down
Loading