コード例 #1
0
ファイル: Scaffolder.cs プロジェクト: jawn/dotnet-script
        public void CreateNewScriptFile(string fileName, string currentDirectory)
        {
            _scriptConsole.WriteNormal($"Creating '{fileName}'");
            if (!Path.HasExtension(fileName))
            {
                fileName = Path.ChangeExtension(fileName, ".csx");
            }
            var pathToScriptFile = Path.Combine(currentDirectory, fileName);

            if (!File.Exists(pathToScriptFile))
            {
                var scriptFileTemplate = TemplateLoader.ReadTemplate("helloworld.csx.template");
                File.WriteAllText(pathToScriptFile, scriptFileTemplate);
                _scriptConsole.WriteSuccess($"...'{pathToScriptFile}' [Created]");
            }
            else
            {
                _scriptConsole.WriteHighlighted($"...'{pathToScriptFile}' already exists [Skipping]");
            }
        }
コード例 #2
0
        public void CreateNewScriptFile(string fileName, string currentDirectory)
        {
            _scriptConsole.WriteNormal($"Creating '{fileName}'");
            if (!Path.HasExtension(fileName))
            {
                fileName = Path.ChangeExtension(fileName, ".csx");
            }
            var pathToScriptFile = Path.Combine(currentDirectory, fileName);

            if (!File.Exists(pathToScriptFile))
            {
                var scriptFileTemplate = TemplateLoader.ReadTemplate("helloworld.csx.template");

                if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ||
                    RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    // add a shebang to set dotnet-script as the interpreter for .csx files
                    // and make sure we are using environment newlines, because shebang won't work with windows cr\lf
                    scriptFileTemplate = $"#!/usr/bin/env dotnet-script" + Environment.NewLine + scriptFileTemplate.Replace("\r\n", Environment.NewLine);
                }

                File.WriteAllText(pathToScriptFile, scriptFileTemplate, new UTF8Encoding(false /* Linux shebang can't handle BOM */));

                if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ||
                    RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    // mark .csx file as executable, this activates the shebang to run dotnet-script as interpreter
                    _commandRunner.Execute($"/bin/chmod", $"+x {pathToScriptFile}");
                }
                _scriptConsole.WriteSuccess($"...'{pathToScriptFile}' [Created]");
            }
            else
            {
                _scriptConsole.WriteHighlighted($"...'{pathToScriptFile}' already exists [Skipping]");
            }
        }
コード例 #3
0
        public virtual ScriptEmitResult Emit <TReturn, THost>(ScriptContext context, string assemblyName)
        {
            var compilationContext = _scriptCompiler.CreateCompilationContext <TReturn, THost>(context);

            foreach (var warning in compilationContext.Warnings)
            {
                _scriptConsole.WriteHighlighted(warning.ToString());
            }

            if (compilationContext.Errors.Any())
            {
                foreach (var diagnostic in compilationContext.Errors)
                {
                    _scriptConsole.WriteError(diagnostic.ToString());
                }

                throw new CompilationErrorException("Script compilation failed due to one or more errors.", compilationContext.Errors.ToImmutableArray());
            }

            var compilation = compilationContext.Script.GetCompilation();

            compilation = compilation.WithAssemblyName(assemblyName);

            var         peStream    = new MemoryStream();
            EmitOptions emitOptions = null;

            if (context.OptimizationLevel == Microsoft.CodeAnalysis.OptimizationLevel.Debug)
            {
                emitOptions = new EmitOptions()
                              .WithDebugInformationFormat(DebugInformationFormat.Embedded);
            }

            var result = compilation.Emit(peStream, options: emitOptions);

            if (result.Success)
            {
                return(new ScriptEmitResult(peStream, compilation.DirectiveReferences, compilationContext.RuntimeDependencies));
            }

            return(ScriptEmitResult.Error(result.Diagnostics));
        }