コード例 #1
0
        private async Task SetupDebuggerAsync(HttpSelfHostConfiguration config)
        {
            if (Debugger == DebuggerType.Vs)
            {
                using (var client = new HttpClient {
                    BaseAddress = config.BaseAddress
                })
                {
                    await DebuggerHelper.AttachManagedAsync(client);
                }
            }
            else if (Debugger == DebuggerType.VsCode)
            {
                var nodeDebugger = await DebuggerHelper.TrySetupNodeDebuggerAsync();

                if (nodeDebugger == NodeDebuggerStatus.Error)
                {
                    ColoredConsole
                    .Error
                    .WriteLine(ErrorColor("Unable to configure node debugger. Check your launch.json."));
                    return;
                }
                else
                {
                    ColoredConsole
                    .WriteLine("launch.json for VSCode configured.");
                }
            }
        }
コード例 #2
0
        public override async Task RunAsync()
        {
            if (SourceControl != SourceControl.Git)
            {
                throw new Exception("Only Git is supported right now for vsc");
            }

            if (!string.IsNullOrEmpty(FolderName))
            {
                var folderPath = Path.Combine(Environment.CurrentDirectory, FolderName);
                FileSystemHelpers.EnsureDirectory(folderPath);
                Environment.CurrentDirectory = folderPath;
            }

            foreach (var pair in fileToContentMap)
            {
                if (!FileSystemHelpers.FileExists(pair.Key.Value))
                {
                    ColoredConsole.WriteLine($"Writing {pair.Key}");
                    await FileSystemHelpers.WriteAllTextToFileAsync(pair.Key.Value, pair.Value);
                }
                else
                {
                    ColoredConsole.WriteLine($"{pair.Key} already exists. Skipped!");
                }
            }

            var setupNodeDebugResult = await DebuggerHelper.TrySetupNodeDebuggerAsync();

            if (setupNodeDebugResult == NodeDebuggerStatus.Created)
            {
                ColoredConsole.WriteLine("Created launch.json");
            }
            else if (setupNodeDebugResult == NodeDebuggerStatus.Updated)
            {
                ColoredConsole.WriteLine("Added Azure Functions attach target to existing launch.json");
            }
            else if (setupNodeDebugResult == NodeDebuggerStatus.AlreadyCreated)
            {
                ColoredConsole.WriteLine("launch.json already configured. Skipped!");
            }
            else if (setupNodeDebugResult == NodeDebuggerStatus.Error)
            {
                ColoredConsole.Error.WriteLine(ErrorColor("Unable to configure launch.json. Check the file for more info"));
            }

            if (InitSourceControl)
            {
                try
                {
                    var checkGitRepoExe = new Executable("git", "rev-parse --git-dir");
                    var result          = await checkGitRepoExe.RunAsync();

                    if (result != 0)
                    {
                        var exe = new Executable("git", $"init");
                        await exe.RunAsync(l => ColoredConsole.WriteLine(l), l => ColoredConsole.Error.WriteLine(l));
                    }
                    else
                    {
                        ColoredConsole.WriteLine("Directory already a git repository.");
                    }
                }
                catch (FileNotFoundException)
                {
                    ColoredConsole.WriteLine(WarningColor("unable to find git on the path"));
                }
            }
        }
コード例 #3
0
        public override async Task RunAsync()
        {
            using (var client = await _scriptServer.ConnectAsync(Timeout, NoInteractive))
            {
                var hostStatusResponse = await client.GetAsync("admin/host/status");

                var functionStatusResponse = await client.GetAsync($"admin/functions/{FunctionName}/status");

                if (!hostStatusResponse.IsSuccessStatusCode)
                {
                    ColoredConsole
                    .Error
                    .WriteLine(ErrorColor($"Error calling the functions host: {hostStatusResponse.StatusCode}"));
                    return;
                }
                else if (!functionStatusResponse.IsSuccessStatusCode)
                {
                    ColoredConsole
                    .Error
                    .WriteLine(ErrorColor($"Error calling function {FunctionName}: {functionStatusResponse.StatusCode}"));
                    return;
                }


                var functionMetadata = ScriptHostHelpers.GetFunctionMetadata(FunctionName);
                var hostStatus       = await hostStatusResponse.Content.ReadAsAsync <HostStatus>();

                Func <IEnumerable <string>, string, bool> printError = (errors, title) =>
                {
                    if (errors?.Any() == true)
                    {
                        ColoredConsole
                        .Error
                        .WriteLine(ErrorColor(title));

                        foreach (var error in errors)
                        {
                            ColoredConsole
                            .Error
                            .WriteLine(ErrorColor($"\t{error}"));
                        }
                        return(true);
                    }
                    return(false);
                };

                if (printError(hostStatus.Errors, "The function host has the following errors:") ||
                    printError(hostStatus.Errors, $"Function {FunctionName} has the following errors:"))
                {
                    return;
                }

                if (Debug)
                {
                    var scriptType = functionMetadata.ScriptType;
                    if (scriptType != ScriptType.CSharp && scriptType != ScriptType.Javascript)
                    {
                        ColoredConsole
                        .Error
                        .WriteLine(ErrorColor($"Only C# and Javascript functions are currently supported for debugging."));
                        return;
                    }

                    if (scriptType == ScriptType.CSharp)
                    {
                        ColoredConsole
                        .WriteLine("Debugger launching...")
                        .WriteLine("Setup your break points, and hit continue!");
                        await DebuggerHelper.AttachManagedAsync(client);
                    }
                    else if (scriptType == ScriptType.Javascript)
                    {
                        var nodeDebugger = await DebuggerHelper.TrySetupNodeDebuggerAsync();

                        if (nodeDebugger == NodeDebuggerStatus.Error)
                        {
                            ColoredConsole
                            .Error
                            .WriteLine(ErrorColor("Unable to configure node debugger. Check your launch.json."));
                            return;
                        }
                        else if (!NoInteractive)
                        {
                            ColoredConsole.WriteLine("launch.json configured.");
                        }
                        else
                        {
                            ColoredConsole
                            .Write("launch.json configured. Setup your break points, launch debugger (F5), and press any key to continue...");
                            Console.ReadKey();
                        }
                    }
                }

                var invocation = string.IsNullOrEmpty(FileName)
                    ? Content
                    : await FileSystemHelpers.ReadAllTextFromFileAsync(FileName);

                invocation = invocation ?? string.Empty;

                var adminInvocation = JsonConvert.SerializeObject(new FunctionInvocation {
                    Input = invocation
                });

                if (functionMetadata.IsHttpFunction())
                {
                    ColoredConsole.WriteLine(WarningColor("NOTE: the 'func run' command only supports POST for HTTP triggers. For other verbs, consider a REST client like cURL or Postman."));
                }

                var response = functionMetadata.IsHttpFunction()
                    ? await client.PostAsync($"api/{FunctionName}", new StringContent(invocation, Encoding.UTF8, invocation.IsJson() ? "application/json" : "plain/text"))
                    : await client.PostAsync($"admin/functions/{FunctionName}", new StringContent(adminInvocation, Encoding.UTF8, "application/json"));

                ColoredConsole.WriteLine($"{TitleColor($"Response Status Code:")} {response.StatusCode}");
                var contentTask = response.Content?.ReadAsStringAsync();
                if (contentTask != null)
                {
                    var content = await contentTask;
                    if (!response.IsSuccessStatusCode)
                    {
                        try
                        {
                            var exception = JsonConvert.DeserializeObject <JObject>(content);
                            if (exception?["InnerException"]?["ExceptionMessage"]?.ToString() == "Script compilation failed.")
                            {
                                ColoredConsole.Error.WriteLine(ErrorColor("Script compilation failed."));
                                return;
                            }
                        }
                        catch { }
                    }
                    ColoredConsole.WriteLine(await contentTask);
                }
            }
        }