Example #1
0
        private int Scaffold(
            ScaffoldOptions settings,
            ScaffoldInterceptors?interceptors,
            string provider,
            string?providerLocation,
            string connectionString,
            string?additionalConnectionString,
            string output,
            bool overwrite)
        {
            using var dc  = GetConnection(provider, providerLocation, connectionString, additionalConnectionString, out var secondaryConnection);
            using var sdc = secondaryConnection;
            if (dc == null)
            {
                return(StatusCodes.EXPECTED_ERROR);
            }

            var language = LanguageProviders.CSharp;

            var                  legacyProvider       = new LegacySchemaProvider(dc, settings.Schema, language);
            ISchemaProvider      schemaProvider       = legacyProvider;
            ITypeMappingProvider typeMappingsProvider = legacyProvider;

            if (sdc != null)
            {
                var secondLegacyProvider = new LegacySchemaProvider(sdc, settings.Schema, language);
                schemaProvider       = new MergedAccessSchemaProvider(legacyProvider, secondLegacyProvider);
                typeMappingsProvider = new AggregateTypeMappingsProvider(legacyProvider, secondLegacyProvider);
            }

            var generator  = new Scaffolder(LanguageProviders.CSharp, HumanizerNameConverter.Instance, settings, interceptors);
            var dataModel  = generator.LoadDataModel(schemaProvider, typeMappingsProvider);
            var sqlBuilder = dc.DataProvider.CreateSqlBuilder(dc.MappingSchema);
            var files      = generator.GenerateCodeModel(
                sqlBuilder,
                dataModel,
                MetadataBuilders.GetAttributeBasedMetadataBuilder(generator.Language, sqlBuilder),
                SqlBoolEqualityConverter.Create(generator.Language));
            var sourceCode = generator.GenerateSourceCode(files);

            Directory.CreateDirectory(output);

            for (var i = 0; i < sourceCode.Length; i++)
            {
                // TODO: add file name normalization/deduplication?
                var fileName = Path.Combine(output, sourceCode[i].FileName);
                if (File.Exists(fileName) && !overwrite)
                {
                    Console.WriteLine($"File '{fileName}' already exists. Specify '--overwrite true' option if you want to ovrerwrite existing files");
                    return(StatusCodes.EXPECTED_ERROR);
                }

                File.WriteAllText(fileName, sourceCode[i].Code);
            }

            return(StatusCodes.SUCCESS);
        }
Example #2
0
        private static Scaffolder CreateTestScaffolder(string installLocation)
        {
            var scriptEnvironment    = (ScriptEnvironment)Activator.CreateInstance(typeof(ScriptEnvironment), nonPublic: true);
            var installLocationField = typeof(ScriptEnvironment).GetField("_installLocation", BindingFlags.NonPublic | BindingFlags.Instance);

            installLocationField.SetValue(scriptEnvironment, new Lazy <string>(() => installLocation));
            var scriptConsole = new ScriptConsole(StringWriter.Null, StringReader.Null, StreamWriter.Null);
            var scaffolder    = new Scaffolder(TestOutputHelper.CreateTestLogFactory(), scriptConsole, scriptEnvironment);

            return(scaffolder);
        }
Example #3
0
        public void ShouldCreateUnifiedLaunchFileWhenInstalledAsGlobalTool()
        {
            Scaffolder scaffolder = CreateTestScaffolder("somefolder/.dotnet/tools/dotnet-script");

            using (var scriptFolder = new DisposableFolder())
            {
                scaffolder.InitializerFolder("main.csx", scriptFolder.Path);
                var fileContent = File.ReadAllText(Path.Combine(scriptFolder.Path, ".vscode", "launch.json"));
                Assert.Contains("{env:HOME}/.dotnet/tools/dotnet-script", fileContent);
            }
        }
Example #4
0
        public async Task <object> Get(string connectionString, string databaseName, string workspaceDir, LibType libType)
        {
            await Task.Yield();

            try
            {
                var scaffolder    = new Scaffolder();
                var sourceBuilder = new SourceBuilder();
                var scriptBuilder = new ScriptGenerator();

                var @namespace  = libType == LibType.Assembly ? "GeneratedNamespace" : "System";
                var contextName = $"{databaseName}Context";

                var userFolder = Environment.GetEnvironmentVariable("LocalAppData");
                var appFolder  = $"{userFolder}\\SqlMapper";
                var directory  = new DirectoryInfo(appFolder);
                if (!directory.Exists)
                {
                    directory.Create();
                }
                var libraryPath = libType == LibType.Assembly ? $"{appFolder}\\generatedAssembly.dll" : $"{appFolder}\\generatedAssembly.csx";
                var scriptPath  = $"{workspaceDir}\\main.csx";

                var queryableExtensionsSource = File.ReadAllText($"{AppContext.BaseDirectory}/IQueryableExtensions.cs");
                var scaffolding = scaffolder.ScaffoldDatabase(connectionString, @namespace, contextName);
                scaffolding.AdditionalFiles.Add(queryableExtensionsSource);

                var libraryData            = sourceBuilder.Build(scaffolding.AllFiles, libType);
                var firstDbsetPropertyName = scriptBuilder.GetPropertyName(scaffolding.DbContextSource);
                var script = scriptBuilder.BuildMainScript(@namespace, contextName, libraryPath, firstDbsetPropertyName);

                File.WriteAllBytes(libraryPath, libraryData);
                File.WriteAllText(scriptPath, script);

                return(new { ScriptPath = scriptPath });
            }
#pragma warning disable 168
            catch (Exception ex)
#pragma warning restore 168
            {
                throw;
            }
        }
Example #5
0
 private GennyScaffoldingResult Scaffold(String path)
 {
     return(Scaffolder.Scaffold("Templates/Module/" + path, new ModuleModel(Model, Controller, Area)));
 }
Example #6
0
        private static int Wain(string[] args)
        {
            var app = new CommandLineApplication(throwOnUnexpectedArg: false)
            {
                ExtendedHelpText = "Starting without a path to a CSX file or a command, starts the REPL (interactive) mode."
            };

            var file           = app.Argument("script", "Path to CSX script");
            var interactive    = app.Option("-i | --interactive", "Execute a script and drop into the interactive mode afterwards.", CommandOptionType.NoValue);
            var configuration  = app.Option("-c | --configuration <configuration>", "Configuration to use for running the script [Release/Debug] Default is \"Debug\"", CommandOptionType.SingleValue);
            var packageSources = app.Option("-s | --sources <SOURCE>", "Specifies a NuGet package source to use when resolving NuGet packages.", CommandOptionType.MultipleValue);
            var debugMode      = app.Option(DebugFlagShort + " | " + DebugFlagLong, "Enables debug output.", CommandOptionType.NoValue);
            var verbosity      = app.Option("--verbosity", " Set the verbosity level of the command. Allowed values are t[trace], d[ebug], i[nfo], w[arning], e[rror], and c[ritical].", CommandOptionType.SingleValue);
            var nocache        = app.Option("--no-cache", "Disable caching (Restore and Dll cache)", CommandOptionType.NoValue);
            var infoOption     = app.Option("--info", "Displays environmental information", CommandOptionType.NoValue);

            var argsBeforeDoubleHyphen = args.TakeWhile(a => a != "--").ToArray();
            var argsAfterDoubleHyphen  = args.SkipWhile(a => a != "--").Skip(1).ToArray();

            const string helpOptionTemplate = "-? | -h | --help";

            app.HelpOption(helpOptionTemplate);
            app.VersionOption("-v | --version", () => new VersionProvider().GetCurrentVersion().Version);

            app.Command("eval", c =>
            {
                c.Description = "Execute CSX code.";
                var code      = c.Argument("code", "Code to execute.");
                var cwd       = c.Option("-cwd |--workingdirectory <currentworkingdirectory>", "Working directory for the code compiler. Defaults to current directory.", CommandOptionType.SingleValue);
                c.HelpOption(helpOptionTemplate);
                c.OnExecute(async() =>
                {
                    var source = code.Value;
                    if (string.IsNullOrWhiteSpace(source))
                    {
                        if (Console.IsInputRedirected)
                        {
                            source = await Console.In.ReadToEndAsync();
                        }
                        else
                        {
                            c.ShowHelp();
                            return(0);
                        }
                    }

                    var logFactory = CreateLogFactory(verbosity.Value(), debugMode.HasValue());
                    var options    = new ExecuteCodeCommandOptions(source, cwd.Value(), app.RemainingArguments.Concat(argsAfterDoubleHyphen).ToArray(), configuration.ValueEquals("release", StringComparison.OrdinalIgnoreCase) ? OptimizationLevel.Release : OptimizationLevel.Debug, nocache.HasValue(), packageSources.Values?.ToArray());
                    return(await new ExecuteCodeCommand(ScriptConsole.Default, logFactory).Execute <int>(options));
                });
            });

            app.Command("init", c =>
            {
                c.Description = "Creates a sample script along with the launch.json file needed to launch and debug the script.";
                var fileName  = c.Argument("filename", "(Optional) The name of the sample script file to be created during initialization. Defaults to 'main.csx'");
                var cwd       = c.Option("-cwd |--workingdirectory <currentworkingdirectory>", "Working directory for initialization. Defaults to current directory.", CommandOptionType.SingleValue);
                c.HelpOption(helpOptionTemplate);
                c.OnExecute(() =>
                {
                    var logFactory = CreateLogFactory(verbosity.Value(), debugMode.HasValue());
                    new InitCommand(logFactory).Execute(new InitCommandOptions(fileName.Value, cwd.Value()));
                    return(0);
                });
            });

            app.Command("new", c =>
            {
                c.Description        = "Creates a new script file";
                var fileNameArgument = c.Argument("filename", "The script file name");
                var cwd = c.Option("-cwd |--workingdirectory <currentworkingdirectory>", "Working directory the new script file to be created. Defaults to current directory.", CommandOptionType.SingleValue);
                c.HelpOption(helpOptionTemplate);
                c.OnExecute(() =>
                {
                    var logFactory = CreateLogFactory(verbosity.Value(), debugMode.HasValue());
                    var scaffolder = new Scaffolder(logFactory);
                    if (fileNameArgument.Value == null)
                    {
                        c.ShowHelp();
                        return(0);
                    }
                    scaffolder.CreateNewScriptFile(fileNameArgument.Value, cwd.Value() ?? Directory.GetCurrentDirectory());
                    return(0);
                });
            });

            // windows only
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                // on windows we have command to register .csx files to be executed by dotnet-script
                app.Command("register", c =>
                {
                    c.Description = "Register .csx file handler to enable running scripts directly";
                    c.HelpOption(helpOptionTemplate);
                    c.OnExecute(() =>
                    {
                        var logFactory = CreateLogFactory(verbosity.Value(), debugMode.HasValue());
                        var scaffolder = new Scaffolder(logFactory);
                        scaffolder.RegisterFileHandler();
                    });
                });
            }

            app.Command("publish", c =>
            {
                c.Description              = "Creates a self contained executable or DLL from a script";
                var fileNameArgument       = c.Argument("filename", "The script file name");
                var publishDirectoryOption = c.Option("-o |--output", "Directory where the published executable should be placed.  Defaults to a 'publish' folder in the current directory.", CommandOptionType.SingleValue);
                var dllName       = c.Option("-n |--name", "The name for the generated DLL (executable not supported at this time).  Defaults to the name of the script.", CommandOptionType.SingleValue);
                var dllOption     = c.Option("--dll", "Publish to a .dll instead of an executable.", CommandOptionType.NoValue);
                var commandConfig = c.Option("-c | --configuration <configuration>", "Configuration to use for publishing the script [Release/Debug]. Default is \"Debug\"", CommandOptionType.SingleValue);
                var runtime       = c.Option("-r |--runtime", "The runtime used when publishing the self contained executable. Defaults to your current runtime.", CommandOptionType.SingleValue);
                c.HelpOption(helpOptionTemplate);
                c.OnExecute(() =>
                {
                    if (fileNameArgument.Value == null)
                    {
                        c.ShowHelp();
                        return(0);
                    }

                    var options = new PublishCommandOptions
                                  (
                        new ScriptFile(fileNameArgument.Value),
                        publishDirectoryOption.Value(),
                        dllName.Value(),
                        dllOption.HasValue() ? PublishType.Library : PublishType.Executable,
                        commandConfig.ValueEquals("release", StringComparison.OrdinalIgnoreCase) ? OptimizationLevel.Release : OptimizationLevel.Debug,
                        packageSources.Values?.ToArray(),
                        runtime.Value() ?? ScriptEnvironment.Default.RuntimeIdentifier,
                        nocache.HasValue()
                                  );

                    var logFactory = CreateLogFactory(verbosity.Value(), debugMode.HasValue());
                    new PublishCommand(ScriptConsole.Default, logFactory).Execute(options);
                    return(0);
                });
            });

            app.Command("exec", c =>
            {
                c.Description        = "Run a script from a DLL.";
                var dllPath          = c.Argument("dll", "Path to DLL based script");
                var commandDebugMode = c.Option(DebugFlagShort + " | " + DebugFlagLong, "Enables debug output.", CommandOptionType.NoValue);
                c.HelpOption(helpOptionTemplate);
                c.OnExecute(async() =>
                {
                    if (string.IsNullOrWhiteSpace(dllPath.Value))
                    {
                        c.ShowHelp();
                        return(0);
                    }

                    var options = new ExecuteLibraryCommandOptions
                                  (
                        dllPath.Value,
                        app.RemainingArguments.Concat(argsAfterDoubleHyphen).ToArray(),
                        nocache.HasValue()
                                  );
                    var logFactory = CreateLogFactory(verbosity.Value(), debugMode.HasValue());
                    return(await new ExecuteLibraryCommand(ScriptConsole.Default, logFactory).Execute <int>(options));
                });
            });

            app.OnExecute(async() =>
            {
                int exitCode = 0;

                var scriptFile        = new ScriptFile(file.Value);
                var optimizationLevel = configuration.ValueEquals("release", StringComparison.OrdinalIgnoreCase) ? OptimizationLevel.Release : OptimizationLevel.Debug;
                var scriptArguments   = app.RemainingArguments.Concat(argsAfterDoubleHyphen).ToArray();
                var logFactory        = CreateLogFactory(verbosity.Value(), debugMode.HasValue());
                if (infoOption.HasValue())
                {
                    var environmentReporter = new EnvironmentReporter(logFactory);
                    await environmentReporter.ReportInfo();
                    return(0);
                }

                if (scriptFile.HasValue)
                {
                    if (interactive.HasValue())
                    {
                        return(await RunInteractiveWithSeed(file.Value, logFactory, scriptArguments, packageSources.Values?.ToArray()));
                    }

                    var fileCommandOptions = new ExecuteScriptCommandOptions
                                             (
                        new ScriptFile(file.Value),
                        scriptArguments,
                        optimizationLevel,
                        packageSources.Values?.ToArray(),
                        interactive.HasValue(),
                        nocache.HasValue()
                                             );

                    var fileCommand = new ExecuteScriptCommand(ScriptConsole.Default, logFactory);
                    return(await fileCommand.Run <int, CommandLineScriptGlobals>(fileCommandOptions));
                }
                else
                {
                    await RunInteractive(!nocache.HasValue(), logFactory, packageSources.Values?.ToArray());
                }
                return(exitCode);
            });

            return(app.Execute(argsBeforeDoubleHyphen));
        }
Example #7
0
        private static int Wain(string[] args)
        {
            var app = new CommandLineApplication(throwOnUnexpectedArg: false)
            {
                ExtendedHelpText = "Starting without a path to a CSX file or a command, starts the REPL (interactive) mode."
            };

            var file           = app.Argument("script", "Path to CSX script");
            var interactive    = app.Option("-i | --interactive", "Execute a script and drop into the interactive mode afterwards.", CommandOptionType.NoValue);
            var configuration  = app.Option("-c | --configuration <configuration>", "Configuration to use for running the script [Release/Debug] Default is \"Debug\"", CommandOptionType.SingleValue);
            var packageSources = app.Option("-s | --sources <SOURCE>", "Specifies a NuGet package source to use when resolving NuGet packages.", CommandOptionType.MultipleValue);
            var debugMode      = app.Option(DebugFlagShort + " | " + DebugFlagLong, "Enables debug output.", CommandOptionType.NoValue);
            var verbosity      = app.Option("--verbosity", " Set the verbosity level of the command. Allowed values are t[trace], d[ebug], i[nfo], w[arning], e[rror], and c[ritical].", CommandOptionType.SingleValue);
            var nocache        = app.Option("--nocache", "Disable caching (Restore and Dll cache)", CommandOptionType.NoValue);
            var infoOption     = app.Option("--info", "Displays environmental information", CommandOptionType.NoValue);

            var argsBeforeDoubleHyphen = args.TakeWhile(a => a != "--").ToArray();
            var argsAfterDoubleHyphen  = args.SkipWhile(a => a != "--").Skip(1).ToArray();

            const string helpOptionTemplate = "-? | -h | --help";

            app.HelpOption(helpOptionTemplate);
            app.VersionOption("-v | --version", () => new VersionProvider().GetCurrentVersion().Version);

            var logFactory = CreateLogFactory(verbosity.Value(), debugMode.HasValue());

            app.Command("eval", c =>
            {
                c.Description = "Execute CSX code.";
                var code      = c.Argument("code", "Code to execute.");
                var cwd       = c.Option("-cwd |--workingdirectory <currentworkingdirectory>", "Working directory for the code compiler. Defaults to current directory.", CommandOptionType.SingleValue);
                c.HelpOption(helpOptionTemplate);
                c.OnExecute(async() =>
                {
                    int exitCode = 0;
                    if (!string.IsNullOrWhiteSpace(code.Value))
                    {
                        var optimizationLevel = configuration.ValueEquals("release", StringComparison.OrdinalIgnoreCase) ? OptimizationLevel.Release : OptimizationLevel.Debug;
                        exitCode = await RunCode(code.Value, debugMode.HasValue(), !nocache.HasValue(), logFactory, optimizationLevel, app.RemainingArguments.Concat(argsAfterDoubleHyphen), cwd.Value(), packageSources.Values?.ToArray());
                    }
                    return(exitCode);
                });
            });

            app.Command("init", c =>
            {
                c.Description = "Creates a sample script along with the launch.json file needed to launch and debug the script.";
                var fileName  = c.Argument("filename", "(Optional) The name of the sample script file to be created during initialization. Defaults to 'main.csx'");
                var cwd       = c.Option("-cwd |--workingdirectory <currentworkingdirectory>", "Working directory for initialization. Defaults to current directory.", CommandOptionType.SingleValue);
                c.HelpOption(helpOptionTemplate);
                c.OnExecute(() =>
                {
                    var scaffolder = new Scaffolder(logFactory);
                    scaffolder.InitializerFolder(fileName.Value, cwd.Value() ?? Directory.GetCurrentDirectory());
                    return(0);
                });
            });

            app.Command("new", c =>
            {
                c.Description        = "Creates a new script file";
                var fileNameArgument = c.Argument("filename", "The script file name");
                var cwd = c.Option("-cwd |--workingdirectory <currentworkingdirectory>", "Working directory the new script file to be created. Defaults to current directory.", CommandOptionType.SingleValue);
                c.HelpOption(helpOptionTemplate);
                c.OnExecute(() =>
                {
                    var scaffolder = new Scaffolder(logFactory);
                    if (fileNameArgument.Value == null)
                    {
                        c.ShowHelp();
                        return(0);
                    }
                    scaffolder.CreateNewScriptFile(fileNameArgument.Value, cwd.Value() ?? Directory.GetCurrentDirectory());
                    return(0);
                });
            });

            app.Command("publish", c =>
            {
                c.Description              = "Creates a self contained executable or DLL from a script";
                var fileNameArgument       = c.Argument("filename", "The script file name");
                var publishDirectoryOption = c.Option("-o |--output", "Directory where the published executable should be placed.  Defaults to a 'publish' folder in the current directory.", CommandOptionType.SingleValue);
                var dllName          = c.Option("-n |--name", "The name for the generated DLL (executable not supported at this time).  Defaults to the name of the script.", CommandOptionType.SingleValue);
                var dllOption        = c.Option("--dll", "Publish to a .dll instead of an executable.", CommandOptionType.NoValue);
                var commandConfig    = c.Option("-c | --configuration <configuration>", "Configuration to use for publishing the script [Release/Debug]. Default is \"Debug\"", CommandOptionType.SingleValue);
                var publishDebugMode = c.Option(DebugFlagShort + " | " + DebugFlagLong, "Enables debug output.", CommandOptionType.NoValue);
                var runtime          = c.Option("-r |--runtime", "The runtime used when publishing the self contained executable. Defaults to your current runtime.", CommandOptionType.SingleValue);
                c.HelpOption(helpOptionTemplate);
                c.OnExecute(() =>
                {
                    if (fileNameArgument.Value == null)
                    {
                        c.ShowHelp();
                        return(0);
                    }

                    var optimizationLevel = commandConfig.ValueEquals("release", StringComparison.OrdinalIgnoreCase) ? OptimizationLevel.Release : OptimizationLevel.Debug;
                    var runtimeIdentifier = runtime.Value() ?? ScriptEnvironment.Default.RuntimeIdentifier;
                    var absoluteFilePath  = fileNameArgument.Value.GetRootedPath();

                    // if a publish directory has been specified, then it is used directly, otherwise:
                    // -- for EXE {current dir}/publish/{runtime ID}
                    // -- for DLL {current dir}/publish
                    var publishDirectory = publishDirectoryOption.Value() ??
                                           (dllOption.HasValue() ? Path.Combine(Path.GetDirectoryName(absoluteFilePath), "publish") : Path.Combine(Path.GetDirectoryName(absoluteFilePath), "publish", runtimeIdentifier));

                    var absolutePublishDirectory = publishDirectory.GetRootedPath();
                    var compiler      = GetScriptCompiler(publishDebugMode.HasValue(), !nocache.HasValue(), logFactory);
                    var scriptEmitter = new ScriptEmitter(ScriptConsole.Default, compiler);
                    var publisher     = new ScriptPublisher(logFactory, scriptEmitter);
                    var code          = absoluteFilePath.ToSourceText();
                    var context       = new ScriptContext(code, absolutePublishDirectory, Enumerable.Empty <string>(), absoluteFilePath, optimizationLevel);

                    if (dllOption.HasValue())
                    {
                        publisher.CreateAssembly <int, CommandLineScriptGlobals>(context, logFactory, dllName.Value());
                    }
                    else
                    {
                        publisher.CreateExecutable <int, CommandLineScriptGlobals>(context, logFactory, runtimeIdentifier);
                    }

                    return(0);
                });
            });

            app.Command("exec", c =>
            {
                c.Description        = "Run a script from a DLL.";
                var dllPath          = c.Argument("dll", "Path to DLL based script");
                var commandDebugMode = c.Option(DebugFlagShort + " | " + DebugFlagLong, "Enables debug output.", CommandOptionType.NoValue);
                c.HelpOption(helpOptionTemplate);
                c.OnExecute(async() =>
                {
                    int exitCode = 0;
                    if (!string.IsNullOrWhiteSpace(dllPath.Value))
                    {
                        if (!File.Exists(dllPath.Value))
                        {
                            throw new Exception($"Couldn't find file '{dllPath.Value}'");
                        }

                        var absoluteFilePath = dllPath.Value.GetRootedPath();
                        var compiler         = GetScriptCompiler(commandDebugMode.HasValue(), !nocache.HasValue(), logFactory);
                        var runner           = new ScriptRunner(compiler, logFactory, ScriptConsole.Default);
                        var result           = await runner.Execute <int>(absoluteFilePath, app.RemainingArguments.Concat(argsAfterDoubleHyphen));
                        return(result);
                    }
                    return(exitCode);
                });
            });

            app.OnExecute(async() =>
            {
                int exitCode = 0;

                if (infoOption.HasValue())
                {
                    var environmentReporter = new EnvironmentReporter(logFactory);
                    await environmentReporter.ReportInfo();
                    return(0);
                }

                if (!string.IsNullOrWhiteSpace(file.Value))
                {
                    var optimizationLevel = configuration.ValueEquals("release", StringComparison.OrdinalIgnoreCase) ? OptimizationLevel.Release : OptimizationLevel.Debug;
                    if (Debugger.IsAttached || nocache.HasValue())
                    {
                        exitCode = await RunScript(file.Value, debugMode.HasValue(), !nocache.HasValue(), logFactory, optimizationLevel, app.RemainingArguments.Concat(argsAfterDoubleHyphen), interactive.HasValue(), packageSources.Values?.ToArray());
                    }
                    else
                    {
                        string cacheFolder = Path.Combine(Path.GetTempPath(), "dotnet-scripts");
                        // create unique folder name based on the path
                        string uniqueFolderName = "";
                        using (var sha = SHA256.Create())
                        {
                            uniqueFolderName = Convert.ToBase64String(sha.ComputeHash(Encoding.Unicode.GetBytes(file.Value))).Replace("=", String.Empty).Replace("/", string.Empty);
                        }

                        string publishDirectory = Path.Combine(cacheFolder, uniqueFolderName);
                        if (!Directory.Exists(publishDirectory))
                        {
                            Directory.CreateDirectory(publishDirectory);
                        }

                        string absoluteSourcePath;
                        SourceText code;
                        if (!File.Exists(file.Value))
                        {
                            if (IsHttpUri(file.Value))
                            {
                                var downloader     = new ScriptDownloader();
                                var rawCode        = await downloader.Download(file.Value);
                                absoluteSourcePath = Path.Combine(publishDirectory, "source.csx");
                                File.WriteAllText(absoluteSourcePath, rawCode);
                                code = SourceText.From(rawCode);
                            }
                            else
                            {
                                throw new Exception($"Couldn't find file '{file}'");
                            }
                        }
                        else
                        {
                            absoluteSourcePath = file.Value.GetRootedPath();
                            code = absoluteSourcePath.ToSourceText();
                        }

                        // given the path to a script we create a %temp%\dotnet-scripts\{uniqueFolderName} path
                        string pathToDll = Path.Combine(publishDirectory, Path.GetFileNameWithoutExtension(absoluteSourcePath) + ".dll");

                        // source hash is the checkSum of the code
                        string sourceHash = Convert.ToBase64String(code.GetChecksum().ToArray());

                        // get hash code from previous run
                        string hashCache = Path.Combine(publishDirectory, ".hash");
                        var compiler     = GetScriptCompiler(true, !nocache.HasValue(), logFactory);

                        // if we don't have hash
                        if (!File.Exists(hashCache) ||
                            // or we haven't created a dll
                            !Directory.Exists(publishDirectory) ||
                            // the hashcode has changed (meaning new content)
                            File.ReadAllText(hashCache) != sourceHash)
                        {
                            // then we autopublish into the %temp%\dotnet-scripts\{uniqueFolderName} path
                            var runtimeIdentifier = ScriptEnvironment.Default.RuntimeIdentifier;
                            var scriptEmitter     = new ScriptEmitter(ScriptConsole.Default, compiler);
                            var publisher         = new ScriptPublisher(logFactory, scriptEmitter);
                            var context           = new ScriptContext(code, publishDirectory, Enumerable.Empty <string>(), absoluteSourcePath, optimizationLevel);

                            // create the assembly in our cache folder
                            publisher.CreateAssembly <int, CommandLineScriptGlobals>(context, logFactory, Path.GetFileNameWithoutExtension(pathToDll));

                            // save sourceHash for next time, so we can know it's ok to use the generated dll next time
                            File.WriteAllText(hashCache, sourceHash);
                        }


                        // run the cached %temp%\dotnet-scripts\{uniqueFolderName}/package.dll
                        var runner = new ScriptRunner(compiler, logFactory, ScriptConsole.Default);
                        var result = await runner.Execute <int>(pathToDll, app.RemainingArguments.Concat(argsAfterDoubleHyphen));
                        return(result);
                    }
                }
                else
                {
                    await RunInteractive(debugMode.HasValue(), !nocache.HasValue(), logFactory, packageSources.Values?.ToArray());
                }
                return(exitCode);
            });


            return(app.Execute(argsBeforeDoubleHyphen));
        }
Example #8
0
 private GennyScaffoldingResult Scaffold(String path)
 {
     return(Scaffolder.Scaffold($"Templates/Module/{path}", new ModuleModel(Model !, Controller !, Area)));
 }
Example #9
0
        private static int Wain(string[] args)
        {
            var app = new CommandLineApplication(throwOnUnexpectedArg: false)
            {
                ExtendedHelpText = "Starting without a path to a CSX file or a command, starts the REPL (interactive) mode."
            };
            var file        = app.Argument("script", "Path to CSX script");
            var interactive = app.Option("-i |--interactive", "Execute a script and drop into the interactive mode afterwards.", CommandOptionType.NoValue);

            var debugMode = app.Option(DebugFlagShort + " | " + DebugFlagLong, "Enables debug output.", CommandOptionType.NoValue);

            var argsBeforeDoubleHyphen = args.TakeWhile(a => a != "--").ToArray();
            var argsAfterDoubleHypen   = args.SkipWhile(a => a != "--").Skip(1).ToArray();

            app.HelpOption("-? | -h | --help");

            app.VersionOption("-v | --version", GetVersionInfo);

            app.Command("eval", c =>
            {
                c.Description = "Execute CSX code.";
                var code      = c.Argument("code", "Code to execute.");
                var cwd       = c.Option("-cwd |--workingdirectory <currentworkingdirectory>", "Working directory for the code compiler. Defaults to current directory.", CommandOptionType.SingleValue);

                c.OnExecute(async() =>
                {
                    int exitCode = 0;
                    if (!string.IsNullOrWhiteSpace(code.Value))
                    {
                        exitCode = await RunCode(code.Value, debugMode.HasValue(), app.RemainingArguments.Concat(argsAfterDoubleHypen), cwd.Value());
                    }
                    return(exitCode);
                });
            });

            app.Command("init", c =>
            {
                c.Description = "Creates a sample script along with the launch.json file needed to launch and debug the script.";
                c.OnExecute(() =>
                {
                    var scaffolder = new Scaffolder();
                    scaffolder.InitializerFolder();
                    return(0);
                });
            });

            app.Command("new", c =>
            {
                c.Description        = "Creates a new script file";
                var fileNameArgument = c.Argument("filename", "The script file name");
                c.OnExecute(() =>
                {
                    var scaffolder = new Scaffolder();
                    if (fileNameArgument.Value == null)
                    {
                        c.ShowHelp();
                        return(0);
                    }
                    scaffolder.CreateNewScriptFile(fileNameArgument.Value);
                    return(0);
                });
            });

            app.OnExecute(async() =>
            {
                int exitCode = 0;
                if (!string.IsNullOrWhiteSpace(file.Value))
                {
                    exitCode = await RunScript(file.Value, debugMode.HasValue(), app.RemainingArguments.Concat(argsAfterDoubleHypen), interactive.HasValue());
                }
                else
                {
                    await RunInteractive(debugMode.HasValue());
                }
                return(exitCode);
            });

            return(app.Execute(argsBeforeDoubleHyphen));
        }
Example #10
0
        public void Execute(InitCommandOptions options)
        {
            var scaffolder = new Scaffolder(_logFactory);

            scaffolder.InitializerFolder(options.FileName, options.WorkingDirectory);
        }
Example #11
0
        private static int Wain(string[] args)
        {
            var app       = new CommandLineApplication(throwOnUnexpectedArg: false);
            var file      = app.Argument("script", "Path to CSX script");
            var config    = app.Option("-conf |--configuration <configuration>", "Configuration to use. Defaults to 'Release'", CommandOptionType.SingleValue);
            var debugMode = app.Option(DebugFlagShort + " | " + DebugFlagLong, "Enables debug output.", CommandOptionType.NoValue);

            app.HelpOption("-? | -h | --help");

            app.VersionOption("-v | --version", GetVersionInfo);

            app.Command("eval", c =>
            {
                c.Description = "Execute CSX code.";
                var code      = c.Argument("code", "Code to execute.");
                var cwd       = c.Option("-cwd |--workingdirectory <currentworkingdirectory>", "Working directory for the code compiler. Defaults to current directory.", CommandOptionType.SingleValue);

                c.OnExecute(() =>
                {
                    if (!string.IsNullOrWhiteSpace(code.Value))
                    {
                        RunCode(code.Value, config.HasValue() ? config.Value() : "Release", debugMode.HasValue(), app.RemainingArguments, cwd.Value());
                    }
                    return(0);
                });
            });

            app.OnExecute(() =>
            {
                if (!string.IsNullOrWhiteSpace(file.Value))
                {
                    RunScript(file.Value, config.HasValue() ? config.Value() : "Release", debugMode.HasValue(), app.RemainingArguments);
                }
                else
                {
                    app.ShowHelp();
                }
                return(0);
            });

            app.Command("init", c =>
            {
                c.Description = "Creates a sample script along with the launch.json file needed to launch and debug the script.";
                c.OnExecute(() =>
                {
                    var skaffolder = new Scaffolder();
                    skaffolder.InitializerFolder();
                    return(0);
                });
            });

            app.Command("new", c =>
            {
                c.Description        = "Creates a new script file";
                var fileNameArgument = c.Argument("filename", "The script file name");
                c.OnExecute(() =>
                {
                    var skaffolder = new Scaffolder();
                    if (fileNameArgument.Value == null)
                    {
                        c.ShowHelp();
                        return(0);
                    }
                    skaffolder.CreateNewScriptFile(fileNameArgument.Value);
                    return(0);
                });
            });

            return(app.Execute(args.Except(new[] { "--" }).ToArray()));
        }
Example #12
0
        private static int Wain(string[] args)
        {
            var app = new CommandLineApplication(throwOnUnexpectedArg: false)
            {
                ExtendedHelpText = "Starting without a path to a CSX file or a command, starts the REPL (interactive) mode."
            };
            var file        = app.Argument("script", "Path to CSX script");
            var interactive = app.Option("-i | --interactive", "Execute a script and drop into the interactive mode afterwards.", CommandOptionType.NoValue);

            var configuration = app.Option("-c | --configuration <configuration>", "Configuration to use for running the script [Release/Debug] Default is \"Debug\"", CommandOptionType.SingleValue);

            var packageSources = app.Option("-s | --sources <SOURCE>", "Specifies a NuGet package source to use when resolving NuGet packages.", CommandOptionType.MultipleValue);

            var debugMode = app.Option(DebugFlagShort + " | " + DebugFlagLong, "Enables debug output.", CommandOptionType.NoValue);

            var argsBeforeDoubleHyphen = args.TakeWhile(a => a != "--").ToArray();
            var argsAfterDoubleHypen   = args.SkipWhile(a => a != "--").Skip(1).ToArray();

            app.HelpOption("-? | -h | --help");

            app.VersionOption("-v | --version", GetVersion);

            var infoOption = app.Option("--info", "Displays environmental information", CommandOptionType.NoValue);

            app.Command("eval", c =>
            {
                c.Description = "Execute CSX code.";
                var code      = c.Argument("code", "Code to execute.");
                var cwd       = c.Option("-cwd |--workingdirectory <currentworkingdirectory>", "Working directory for the code compiler. Defaults to current directory.", CommandOptionType.SingleValue);
                c.OnExecute(async() =>
                {
                    int exitCode = 0;
                    if (!string.IsNullOrWhiteSpace(code.Value))
                    {
                        var optimizationLevel = OptimizationLevel.Debug;
                        if (configuration.HasValue() && configuration.Value().ToLower() == "release")
                        {
                            optimizationLevel = OptimizationLevel.Release;
                        }
                        exitCode = await RunCode(code.Value, debugMode.HasValue(), optimizationLevel, app.RemainingArguments.Concat(argsAfterDoubleHypen), cwd.Value(), packageSources.Values?.ToArray());
                    }
                    return(exitCode);
                });
            });

            app.Command("init", c =>
            {
                c.Description = "Creates a sample script along with the launch.json file needed to launch and debug the script.";
                var fileName  = c.Argument("filename", "(Optional) The name of the sample script file to be created during initialization. Defaults to 'main.csx'");
                var cwd       = c.Option("-cwd |--workingdirectory <currentworkingdirectory>", "Working directory for initialization. Defaults to current directory.", CommandOptionType.SingleValue);
                c.OnExecute(() =>
                {
                    var scaffolder = new Scaffolder(new ScriptLogger(ScriptConsole.Default.Error, debugMode.HasValue()));
                    scaffolder.InitializerFolder(fileName.Value, cwd.Value() ?? Directory.GetCurrentDirectory());
                    return(0);
                });
            });

            app.Command("new", c =>
            {
                c.Description        = "Creates a new script file";
                var fileNameArgument = c.Argument("filename", "The script file name");
                var cwd = c.Option("-cwd |--workingdirectory <currentworkingdirectory>", "Working directory the new script file to be created. Defaults to current directory.", CommandOptionType.SingleValue);
                c.OnExecute(() =>
                {
                    var scaffolder = new Scaffolder(new ScriptLogger(ScriptConsole.Default.Error, debugMode.HasValue()));
                    if (fileNameArgument.Value == null)
                    {
                        c.ShowHelp();
                        return(0);
                    }
                    scaffolder.CreateNewScriptFile(fileNameArgument.Value, cwd.Value() ?? Directory.GetCurrentDirectory());
                    return(0);
                });
            });

            app.Command("publish", c =>
            {
                c.Description              = "Creates a self contained executable or DLL from a script";
                var fileNameArgument       = c.Argument("filename", "The script file name");
                var publishDirectoryOption = c.Option("-o |--output", "Directory where the published executable should be placed.  Defaults to a 'publish' folder in the current directory.", CommandOptionType.SingleValue);
                var dllName          = c.Option("-n |--name", "The name for the generated DLL (executable not supported at this time).  Defaults to the name of the script.", CommandOptionType.SingleValue);
                var dllOption        = c.Option("--dll", "Publish to a .dll instead of an executable.", CommandOptionType.NoValue);
                var commandConfig    = c.Option("-c | --configuration <configuration>", "Configuration to use for publishing the script [Release/Debug]. Default is \"Debug\"", CommandOptionType.SingleValue);
                var publishDebugMode = c.Option(DebugFlagShort + " | " + DebugFlagLong, "Enables debug output.", CommandOptionType.NoValue);
                var runtime          = c.Option("-r |--runtime", "The runtime used when publishing the self contained executable. Defaults to your current runtime.", CommandOptionType.SingleValue);

                c.OnExecute(() =>
                {
                    if (fileNameArgument.Value == null)
                    {
                        c.ShowHelp();
                        return(0);
                    }

                    var optimizationLevel = OptimizationLevel.Debug;
                    if (commandConfig.HasValue() && commandConfig.Value().ToLower() == "release")
                    {
                        optimizationLevel = OptimizationLevel.Release;
                    }

                    var runtimeIdentifier = runtime.Value() ?? ScriptEnvironment.Default.RuntimeIdentifier;
                    var absoluteFilePath  = Path.IsPathRooted(fileNameArgument.Value) ? fileNameArgument.Value : Path.Combine(Directory.GetCurrentDirectory(), fileNameArgument.Value);

                    // if a publish directory has been specified, then it is used directly, otherwise:
                    // -- for EXE {current dir}/publish/{runtime ID}
                    // -- for DLL {current dir}/publish
                    var publishDirectory = publishDirectoryOption.Value() ??
                                           (dllOption.HasValue() ? Path.Combine(Path.GetDirectoryName(absoluteFilePath), "publish") : Path.Combine(Path.GetDirectoryName(absoluteFilePath), "publish", runtimeIdentifier));

                    var absolutePublishDirectory = Path.IsPathRooted(publishDirectory) ? publishDirectory : Path.Combine(Directory.GetCurrentDirectory(), publishDirectory);
                    var logFactory    = GetLogFactory();
                    var compiler      = GetScriptCompiler(publishDebugMode.HasValue());
                    var scriptEmmiter = new ScriptEmitter(ScriptConsole.Default, compiler);
                    var publisher     = new ScriptPublisher(logFactory, scriptEmmiter);
                    var code          = SourceText.From(File.ReadAllText(absoluteFilePath));
                    var context       = new ScriptContext(code, absolutePublishDirectory, Enumerable.Empty <string>(), absoluteFilePath, optimizationLevel);

                    if (dllOption.HasValue())
                    {
                        publisher.CreateAssembly <int, CommandLineScriptGlobals>(context, logFactory, dllName.Value());
                    }
                    else
                    {
                        publisher.CreateExecutable <int, CommandLineScriptGlobals>(context, logFactory, runtimeIdentifier);
                    }

                    return(0);

                    LogFactory GetLogFactory()
                    {
                        var logger = new ScriptLogger(ScriptConsole.Default.Error, publishDebugMode.HasValue());
                        return(type => ((level, message) =>
                        {
                            if (level == LogLevel.Debug)
                            {
                                logger.Verbose(message);
                            }
                            if (level == LogLevel.Info)
                            {
                                logger.Log(message);
                            }
                        }));
                    }
                });
            });

            app.Command("exec", c =>
            {
                c.Description        = "Run a script from a DLL.";
                var dllPath          = c.Argument("dll", "Path to DLL based script");
                var commandDebugMode = c.Option(DebugFlagShort + " | " + DebugFlagLong, "Enables debug output.", CommandOptionType.NoValue);
                c.OnExecute(async() =>
                {
                    int exitCode = 0;
                    if (!string.IsNullOrWhiteSpace(dllPath.Value))
                    {
                        if (!File.Exists(dllPath.Value))
                        {
                            throw new Exception($"Couldn't find file '{dllPath.Value}'");
                        }

                        var absoluteFilePath = Path.IsPathRooted(dllPath.Value) ? dllPath.Value : Path.Combine(Directory.GetCurrentDirectory(), dllPath.Value);

                        var compiler = GetScriptCompiler(commandDebugMode.HasValue());
                        var runner   = new ScriptRunner(compiler, compiler.Logger, ScriptConsole.Default);
                        var result   = await runner.Execute <int>(absoluteFilePath);
                        return(result);
                    }
                    return(exitCode);
                });
            });

            app.OnExecute(async() =>
            {
                int exitCode = 0;

                if (infoOption.HasValue())
                {
                    Console.Write(GetEnvironmentInfo());
                    return(0);
                }

                if (!string.IsNullOrWhiteSpace(file.Value))
                {
                    var optimizationLevel = OptimizationLevel.Debug;
                    if (configuration.HasValue() && configuration.Value().ToLower() == "release")
                    {
                        optimizationLevel = OptimizationLevel.Release;
                    }
                    exitCode = await RunScript(file.Value, debugMode.HasValue(), optimizationLevel, app.RemainingArguments.Concat(argsAfterDoubleHypen), interactive.HasValue(), packageSources.Values?.ToArray());
                }
                else
                {
                    await RunInteractive(debugMode.HasValue(), packageSources.Values?.ToArray());
                }
                return(exitCode);
            });

            return(app.Execute(argsBeforeDoubleHyphen));
        }
Example #13
0
        private static int Wain(string[] args)
        {
            var app = new CommandLineApplication(throwOnUnexpectedArg: false)
            {
                ExtendedHelpText = "Starting without a path to a CSX file or a command, starts the REPL (interactive) mode."
            };
            var file        = app.Argument("script", "Path to CSX script");
            var interactive = app.Option("-i | --interactive", "Execute a script and drop into the interactive mode afterwards.", CommandOptionType.NoValue);

            var configuration = app.Option("-c | --configuration <configuration>", "Configuration to use for running the script [Release/Debug] Default is \"Debug\"", CommandOptionType.SingleValue);

            var debugMode = app.Option(DebugFlagShort + " | " + DebugFlagLong, "Enables debug output.", CommandOptionType.NoValue);

            var argsBeforeDoubleHyphen = args.TakeWhile(a => a != "--").ToArray();
            var argsAfterDoubleHypen   = args.SkipWhile(a => a != "--").Skip(1).ToArray();

            app.HelpOption("-? | -h | --help");

            app.VersionOption("-v | --version", GetVersion);

            var infoOption = app.Option("--info", "Displays environmental information", CommandOptionType.NoValue);

            app.Command("eval", c =>
            {
                c.Description = "Execute CSX code.";
                var code      = c.Argument("code", "Code to execute.");
                var cwd       = c.Option("-cwd |--workingdirectory <currentworkingdirectory>", "Working directory for the code compiler. Defaults to current directory.", CommandOptionType.SingleValue);
                c.OnExecute(async() =>
                {
                    int exitCode = 0;
                    if (!string.IsNullOrWhiteSpace(code.Value))
                    {
                        var optimizationLevel = OptimizationLevel.Debug;
                        if (configuration.HasValue() && configuration.Value().ToLower() == "release")
                        {
                            optimizationLevel = OptimizationLevel.Release;
                        }
                        exitCode = await RunCode(code.Value, debugMode.HasValue(), optimizationLevel, app.RemainingArguments.Concat(argsAfterDoubleHypen), cwd.Value());
                    }
                    return(exitCode);
                });
            });

            app.Command("init", c =>
            {
                c.Description = "Creates a sample script along with the launch.json file needed to launch and debug the script.";
                var fileName  = c.Argument("filename", "(Optional) The name of the sample script file to be created during initialization. Defaults to 'main.csx'");
                var cwd       = c.Option("-cwd |--workingdirectory <currentworkingdirectory>", "Working directory for initialization. Defaults to current directory.", CommandOptionType.SingleValue);
                c.OnExecute(() =>
                {
                    var scaffolder = new Scaffolder(new ScriptLogger(ScriptConsole.Default.Error, debugMode.HasValue()));
                    scaffolder.InitializerFolder(fileName.Value, cwd.Value() ?? Directory.GetCurrentDirectory());
                    return(0);
                });
            });

            app.Command("new", c =>
            {
                c.Description        = "Creates a new script file";
                var fileNameArgument = c.Argument("filename", "The script file name");
                var cwd = c.Option("-cwd |--workingdirectory <currentworkingdirectory>", "Working directory the new script file to be created. Defaults to current directory.", CommandOptionType.SingleValue);
                c.OnExecute(() =>
                {
                    var scaffolder = new Scaffolder(new ScriptLogger(ScriptConsole.Default.Error, debugMode.HasValue()));
                    if (fileNameArgument.Value == null)
                    {
                        c.ShowHelp();
                        return(0);
                    }
                    scaffolder.CreateNewScriptFile(fileNameArgument.Value, cwd.Value() ?? Directory.GetCurrentDirectory());
                    return(0);
                });
            });

            app.OnExecute(async() =>
            {
                int exitCode = 0;

                if (infoOption.HasValue())
                {
                    Console.Write(GetEnvironmentInfo());
                    return(0);
                }

                if (!string.IsNullOrWhiteSpace(file.Value))
                {
                    var optimizationLevel = OptimizationLevel.Debug;
                    if (configuration.HasValue() && configuration.Value().ToLower() == "release")
                    {
                        optimizationLevel = OptimizationLevel.Release;
                    }
                    exitCode = await RunScript(file.Value, debugMode.HasValue(), optimizationLevel, app.RemainingArguments.Concat(argsAfterDoubleHypen), interactive.HasValue());
                }
                else
                {
                    await RunInteractive(debugMode.HasValue());
                }
                return(exitCode);
            });

            return(app.Execute(argsBeforeDoubleHyphen));
        }