Skip to content

Commit

Permalink
feat: support export compilation out to folder
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi committed Mar 1, 2024
1 parent e1f383f commit 2bb169d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
12 changes: 10 additions & 2 deletions src/dotnet-exec/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,18 @@ public async Task<int> Execute(ExecOptions options)
{
Directory.CreateDirectory(dir);
}
var outputPath = options.CompileOutput;
if (string.IsNullOrEmpty(Path.GetFileName(outputPath)))
{
outputPath = Path.Combine(dir ?? Environment.CurrentDirectory, $"{compileResult.Data!.Compilation.AssemblyName}.dll");
}

var originalPosition = compileResult.Data!.Stream.Position;
compileResult.Data.Stream.Seek(0, SeekOrigin.Begin);
using var fs = File.Create(options.CompileOutput);
await compileResult.Data!.Stream.CopyToAsync(fs);

using var fs = File.Create(outputPath);
await compileResult.Data.Stream.CopyToAsync(fs);

compileResult.Data!.Stream.Position = originalPosition;
}

Expand Down
12 changes: 5 additions & 7 deletions src/dotnet-exec/Services/SimpleCodeCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ ICompilationOptionsPipeline compilationOptionsPipeline
public async Task<Result<CompileResult>> Compile(ExecOptions options, string? code = null)
{
ArgumentNullException.ThrowIfNull(options);
var assemblyName = $"{Helper.ApplicationName}_{Guid.NewGuid():N}.dll";
var assemblyName = $"{Helper.ApplicationName}_{Guid.NewGuid():N}";
var globalUsingCode = Helper.GetGlobalUsingsCodeText(options);
var parseOptions = new CSharpParseOptions(options.GetLanguageVersion());
parseOptions = parseOptionsPipeline.Configure(parseOptions, options);
var globalUsingSyntaxTree = CSharpSyntaxTree.ParseText(globalUsingCode, parseOptions, "__GlobalUsings.cs",
cancellationToken: options.CancellationToken);
parseOptions = parseOptionsPipeline.Configure(parseOptions, options);
var path = "__Script.cs";

if (File.Exists(options.Script))
{
path = Path.GetFullPath(options.Script);
Expand All @@ -40,8 +37,9 @@ public async Task<Result<CompileResult>> Compile(ExecOptions options, string? co
throw new InvalidOperationException("Code to compile can not be empty");
}

var scriptSyntaxTree =
CSharpSyntaxTree.ParseText(code, parseOptions, path, cancellationToken: options.CancellationToken);
var scriptSyntaxTree = CSharpSyntaxTree.ParseText(code, parseOptions, path, cancellationToken: options.CancellationToken);
var globalUsingSyntaxTree = CSharpSyntaxTree.ParseText(globalUsingCode, parseOptions, "__GlobalUsings.cs",
cancellationToken: options.CancellationToken);
var syntaxTreeList = new List<SyntaxTree>() { globalUsingSyntaxTree, scriptSyntaxTree, };
if (options.AdditionalScripts.HasValue())
{
Expand Down
17 changes: 8 additions & 9 deletions src/dotnet-exec/Services/WorkspaceCodeCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,14 @@ public async Task<Result<CompileResult>> Compile(ExecOptions options, string? co
{
ArgumentNullException.ThrowIfNull(options);
var projectName = $"{Helper.ApplicationName}_{Guid.NewGuid():N}";
var assemblyName = $"{projectName}.dll";
var assemblyName = projectName;
var projectInfo = ProjectInfo.Create(
ProjectId.CreateNewId(),
VersionStamp.Default,
projectName,
assemblyName,
LanguageNames.CSharp);

var globalUsingCode = Helper.GetGlobalUsingsCodeText(options);
var globalUsingDocument = DocumentInfo.Create(
DocumentId.CreateNewId(projectInfo.Id, "__GlobalUsings"),
"__GlobalUsings",
loader: new PlainTextLoader(globalUsingCode),
filePath: "__GlobalUsings.cs",
isGenerated: true);

var scriptDocument = DocumentInfo.Create(DocumentId.CreateNewId(projectInfo.Id, "__Script"), "__Script.cs");
scriptDocument = string.IsNullOrEmpty(code)
? scriptDocument.WithFilePath(options.Script)
Expand All @@ -45,6 +37,13 @@ public async Task<Result<CompileResult>> Compile(ExecOptions options, string? co
scriptDocument = scriptDocument.WithFilePath(fullPath);
}

var globalUsingCode = Helper.GetGlobalUsingsCodeText(options);
var globalUsingDocument = DocumentInfo.Create(
DocumentId.CreateNewId(projectInfo.Id, "__GlobalUsings"),
"__GlobalUsings",
loader: new PlainTextLoader(globalUsingCode),
filePath: "__GlobalUsings.cs",
isGenerated: true);
var documents = new List<DocumentInfo>() { globalUsingDocument, scriptDocument };
if (options.AdditionalScripts.HasValue())
{
Expand Down

0 comments on commit 2bb169d

Please sign in to comment.