Skip to content

Commit bec5a6c

Browse files
committed
Moved init and publish into Core
1 parent 6ec895b commit bec5a6c

14 files changed

+348
-65
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Dotnet.Script.Core.Commands
2+
{
3+
public enum Configuration
4+
{
5+
Debug,
6+
Release
7+
}
8+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.IO;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using Dotnet.Script.DependencyModel.Environment;
5+
using Dotnet.Script.DependencyModel.Logging;
6+
using Dotnet.Script.DependencyModel.ProjectSystem;
7+
using Dotnet.Script.DependencyModel.Runtime;
8+
9+
namespace Dotnet.Script.Core.Commands
10+
{
11+
public class FileCommand : IFileCommand
12+
{
13+
private readonly ScriptConsole _scriptConsole;
14+
private readonly LogFactory _logFactory;
15+
16+
public FileCommand(ScriptConsole scriptConsole, LogFactory logFactory)
17+
{
18+
_scriptConsole = scriptConsole;
19+
_logFactory = logFactory;
20+
}
21+
22+
public async Task<TReturn> Run<TReturn, THost>(FileCommandOptions options)
23+
{
24+
var projectFolder = FileUtils.GetPathToTempFolder(options.File.Path);
25+
var publishDirectory = Path.Combine(projectFolder, "publish");
26+
string pathToDll = Path.Combine(publishDirectory, "script.dll");
27+
28+
var compiler = GetScriptCompiler(!options.NoCache, _logFactory);
29+
var runtimeIdentifier = ScriptEnvironment.Default.RuntimeIdentifier;
30+
var scriptEmitter = new ScriptEmitter(ScriptConsole.Default, compiler);
31+
var publisher = new ScriptPublisher(_logFactory, scriptEmitter);
32+
var context = new ScriptContext(options.File.Path.ToSourceText(), publishDirectory, Enumerable.Empty<string>(), options.File.Path, options.OptimizationLevel);
33+
34+
publisher.CreateAssembly<TReturn,THost>(context, _logFactory, Path.GetFileNameWithoutExtension(pathToDll));
35+
36+
var runner = new ScriptRunner(compiler, _logFactory, ScriptConsole.Default);
37+
var result = await runner.Execute<TReturn>(pathToDll, options.Arguments);
38+
return result;
39+
}
40+
41+
private static ScriptCompiler GetScriptCompiler(bool useRestoreCache, LogFactory logFactory)
42+
{
43+
var runtimeDependencyResolver = new RuntimeDependencyResolver(logFactory, useRestoreCache);
44+
var compiler = new ScriptCompiler(logFactory, runtimeDependencyResolver);
45+
return compiler;
46+
}
47+
}
48+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Microsoft.CodeAnalysis;
2+
3+
namespace Dotnet.Script.Core.Commands
4+
{
5+
public class FileCommandOptions
6+
{
7+
public FileCommandOptions(ScriptFile file, string[] arguments, OptimizationLevel optimizationLevel, string[] packageSources, bool noCache)
8+
{
9+
File = file;
10+
Arguments = arguments;
11+
OptimizationLevel = optimizationLevel;
12+
PackageSources = packageSources;
13+
NoCache = noCache;
14+
}
15+
16+
public ScriptFile File { get; }
17+
public string[] Arguments { get; }
18+
public OptimizationLevel OptimizationLevel { get; }
19+
public string[] PackageSources { get; }
20+
public bool NoCache { get; }
21+
}
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Threading.Tasks;
2+
3+
namespace Dotnet.Script.Core.Commands
4+
{
5+
6+
/// <summary>
7+
/// Represents a class that executes a script file located on disk.
8+
/// </summary>
9+
public interface IFileCommand
10+
{
11+
Task<TReturn> Run<TReturn, THost>(FileCommandOptions options);
12+
}
13+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Dotnet.Script.DependencyModel.Logging;
2+
3+
namespace Dotnet.Script.Core.Commands
4+
{
5+
public class InitCommand
6+
{
7+
private readonly Logger _logger;
8+
private readonly LogFactory _logFactory;
9+
10+
public InitCommand(LogFactory logFactory)
11+
{
12+
_logger = logFactory.CreateLogger<InitCommand>();
13+
_logFactory = logFactory;
14+
}
15+
16+
public void Execute(InitCommandOptions options)
17+
{
18+
var scaffolder = new Scaffolder(_logFactory);
19+
scaffolder.InitializerFolder(options.FileName, options.WorkingDirectory);
20+
}
21+
}
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.IO;
2+
3+
namespace Dotnet.Script.Core.Commands
4+
{
5+
public class InitCommandOptions
6+
{
7+
public InitCommandOptions(string fileName, string workingDirectory)
8+
{
9+
FileName = fileName;
10+
WorkingDirectory = workingDirectory ?? Directory.GetCurrentDirectory();
11+
}
12+
13+
public string FileName { get; }
14+
public string WorkingDirectory { get; }
15+
}
16+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System.IO;
2+
using System.Linq;
3+
using Dotnet.Script.DependencyModel.Logging;
4+
using Dotnet.Script.DependencyModel.Runtime;
5+
using Microsoft.CodeAnalysis.Scripting.Hosting;
6+
7+
namespace Dotnet.Script.Core.Commands
8+
{
9+
public class PublishCommand
10+
{
11+
private readonly ScriptConsole _scriptConsole;
12+
private readonly LogFactory _logFactory;
13+
private readonly Logger _logger;
14+
15+
public PublishCommand(ScriptConsole scriptConsole, LogFactory logFactory)
16+
{
17+
_scriptConsole = scriptConsole;
18+
_logFactory = logFactory;
19+
_logger = logFactory.CreateLogger<PublishCommand>();
20+
}
21+
22+
public void Execute(PublishCommandOptions options)
23+
{
24+
var absoluteFilePath = options.File.Path;
25+
26+
// if a publish directory has been specified, then it is used directly, otherwise:
27+
// -- for EXE {current dir}/publish/{runtime ID}
28+
// -- for DLL {current dir}/publish
29+
var publishDirectory = options.OutputDirectory ??
30+
(options.PublishType == PublishType.Library ? Path.Combine(Path.GetDirectoryName(absoluteFilePath), "publish") : Path.Combine(Path.GetDirectoryName(absoluteFilePath), "publish", options.RuntimeIdentifier));
31+
32+
var absolutePublishDirectory = publishDirectory.GetRootedPath();
33+
var compiler = GetScriptCompiler(!options.NoCache, _logFactory);
34+
var scriptEmitter = new ScriptEmitter(ScriptConsole.Default, compiler);
35+
var publisher = new ScriptPublisher(_logFactory, scriptEmitter);
36+
var code = absoluteFilePath.ToSourceText();
37+
var context = new ScriptContext(code, absolutePublishDirectory, Enumerable.Empty<string>(), absoluteFilePath, options.OptimizationLevel);
38+
39+
if (options.PublishType == PublishType.Library)
40+
{
41+
ScriptConsole.Default.WriteHighlighted("Create dll");
42+
publisher.CreateAssembly<int, CommandLineScriptGlobals>(context, _logFactory, options.LibraryName);
43+
}
44+
else
45+
{
46+
ScriptConsole.Default.WriteHighlighted("Create executable");
47+
publisher.CreateExecutable<int, CommandLineScriptGlobals>(context, _logFactory, options.RuntimeIdentifier);
48+
}
49+
}
50+
51+
private static ScriptCompiler GetScriptCompiler(bool useRestoreCache, LogFactory logFactory)
52+
{
53+
var runtimeDependencyResolver = new RuntimeDependencyResolver(logFactory, useRestoreCache);
54+
var compiler = new ScriptCompiler(logFactory, runtimeDependencyResolver);
55+
return compiler;
56+
}
57+
}
58+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Dotnet.Script.DependencyModel.Environment;
2+
using Microsoft.CodeAnalysis;
3+
4+
namespace Dotnet.Script.Core.Commands
5+
{
6+
public class PublishCommandOptions
7+
{
8+
public PublishCommandOptions(ScriptFile file, string outputDirectory, string libraryName, PublishType publishType, OptimizationLevel optimizationLevel,string runtimeIdentifier, bool noCache)
9+
{
10+
File = file;
11+
OutputDirectory = outputDirectory;
12+
LibraryName = libraryName;
13+
PublishType = publishType;
14+
OptimizationLevel = optimizationLevel;
15+
RuntimeIdentifier = runtimeIdentifier ?? ScriptEnvironment.Default.RuntimeIdentifier;
16+
NoCache = noCache;
17+
}
18+
19+
public ScriptFile File { get; }
20+
public string OutputDirectory { get; }
21+
public string LibraryName { get; }
22+
public PublishType PublishType { get; }
23+
public OptimizationLevel OptimizationLevel { get; }
24+
public string RuntimeIdentifier { get; }
25+
public bool NoCache { get; }
26+
}
27+
28+
public enum PublishType
29+
{
30+
Library,
31+
Executable
32+
}
33+
}

src/Dotnet.Script.Core/Extensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.IO;
2+
using Microsoft.CodeAnalysis.Text;
3+
4+
namespace Dotnet.Script.Core
5+
{
6+
public static class Extensions
7+
{
8+
public static string GetRootedPath(this string path) => Path.IsPathRooted(path) ? path : Path.Combine(Directory.GetCurrentDirectory(), path);
9+
10+
public static SourceText ToSourceText(this string absoluteFilePath)
11+
{
12+
using (var filestream = File.OpenRead(absoluteFilePath))
13+
{
14+
return SourceText.From(filestream);
15+
}
16+
}
17+
}
18+
}

src/Dotnet.Script.Core/ScriptFile.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Dotnet.Script.Core
2+
{
3+
public class ScriptFile
4+
{
5+
public ScriptFile(string path)
6+
{
7+
Path = path.GetRootedPath();
8+
Directory = System.IO.Path.GetDirectoryName(Path);
9+
}
10+
11+
public string Path {get;}
12+
13+
public string Directory {get;}
14+
}
15+
}
16+

0 commit comments

Comments
 (0)