示例#1
0
            /// <summary>
            /// The action executed when running the lists subcommand.
            /// </summary>
            /// <param name="app">The current console app.</param>
            /// <param name="console">The output console.</param>
            /// <returns></returns>
            private async Task <int> OnExecuteAsync(CommandLineApplication app, IConsole console)
            {
                // Create a new template service.
                var templateService = new TemplateService();

                IEnumerable <string> templates;

                try
                {
                    // Get an available template listing.
                    templates = await templateService.GetAvailableTemplates(SearchPattern, IsRegex);
                }
                catch (FormatException ex)
                {
                    // The search pattern format was incorrect.
                    console.Error.WriteLine($"Failed to search templates: {ex.Message}");
                    console.WriteLine();
                    return(1);
                }
                catch (Exception)
                {
                    // This is an unhandled exception.
                    console.Error.WriteLine("Failed to get a template listing");
                    console.WriteLine();
                    return(1);
                }

                // Write each of the templates to the console.
                foreach (string template in templates)
                {
                    console.WriteLine(Indent + template);
                }

                // Return a success status code.
                return(0);
            }
示例#2
0
        private async Task <int> OnExecuteAsync(CommandLineApplication app, IConsole console)
        {
            // If the version flag was given, print the version and exit.
            if (Version)
            {
                PrintVersion(console);
                return(0);
            }

            string output = Path.GetFullPath(Output ?? Environment.CurrentDirectory);

            if (!Directory.Exists(output))
            {
                if (Force)
                {
                    Directory.CreateDirectory(output);
                }
                else
                {
                    console.Error.WriteLine("The specified output path does not exist.");
                    return(1);
                }
            }

            string outputFile = Path.Combine(output, GitIgnore);

            if (File.Exists(outputFile) && !Force && !Append)
            {
                console.Error.WriteLine($"A .gitignore already exists in the output directory. Add --force to overwrite or --append to append the existing .gitignore.");
                return(1);
            }

            Directory.CreateDirectory(CacheDirectory);

            if (string.IsNullOrWhiteSpace(Template))
            {
                Template = DefaultTemplate;
            }
            else
            {
                if (Template.EndsWith(".gitignore"))
                {
                    Template = Template.Replace(".gitignore", string.Empty);
                }

                var    templateService = new TemplateService();
                string templateName    = (await templateService.GetAvailableTemplates())
                                         .FirstOrDefault(t => string.Equals(t, Template, StringComparison.OrdinalIgnoreCase));

                if (templateName == null)
                {
                    console.Error.WriteLine($"The template '{Template}' was not found.");
                    return(1);
                }

                Template = templateName + ".gitignore";
            }

            string cacheFile    = Path.Combine(CacheDirectory, Template);
            string cacheFileTmp = Path.Combine(CacheDirectory, "_" + Template);

            bool success = false;

            try
            {
                await BaseUrl.AppendPathSegment(Template).DownloadFileAsync(CacheDirectory, "_" + Template);

                success = true;
            }
            catch (System.Net.WebException)
            {
                if (File.Exists(cacheFile))
                {
                    console.WriteLine($"Warn: Unable to download newer version of {GitIgnore}. Do you have an internet connection?");
                    console.WriteLine("      Using cached version.");
                }
                else
                {
                    console.Error.WriteLine($"Failed to download {GitIgnore}. Do you have an internet connection?");
                    return(1);
                }
            }
            catch (System.Net.Http.HttpRequestException)
            {
            }
            catch (System.IO.IOException)
            {
                // We failed to download the file to the cache directory, but we could try to download it directly to the output.
                try
                {
                    console.WriteLine($"Warn: Unable to cache file.");

                    await BaseUrl.AppendPathSegment(Template).DownloadFileAsync(CacheDirectory, GitIgnore);

                    // We don't need to proceed with the later copy of the cache file, because we're here.
                    return(1);
                }
                catch
                {
                    console.Error.WriteLine($"Failed to download {GitIgnore}. Could not write file.");
                    return(1);
                }
            }
            catch (FlurlHttpException ex)
            {
                if (!ex.Call.Completed)
                {
                    if (File.Exists(cacheFile))
                    {
                        console.WriteLine($"Warn: Unable to download newer version of {GitIgnore}. Do you have an internet connection?");
                        console.WriteLine("      Using cached version.");
                    }
                    else
                    {
                        console.Error.WriteLine($"Failed to download {GitIgnore}. Do you have an internet connection?");
                        return(1);
                    }
                }
                else
                {
                    switch (ex?.Call?.Response?.StatusCode ?? HttpStatusCode.InternalServerError)
                    {
                    case HttpStatusCode.NotFound:
                        console.Error.WriteLine($"The template '{Template}' was not found.");
                        return(1);

                    default:
                        console.Error.WriteLine($"Failed to download {GitIgnore}.");
                        return(1);
                    }
                }
            }
            catch (Exception)
            {
                console.Error.WriteLine($"Failed to download {GitIgnore}.");
                Environment.Exit(1);
            }

            if (success)
            {
                try
                {
                    if (File.Exists(cacheFile))
                    {
                        File.Delete(cacheFile);
                    }
                    File.Move(cacheFileTmp, cacheFile);
                }
                catch (Exception)
                {
                }
            }

            try
            {
                if (Append)
                {
                    File.AppendAllLines(outputFile, File.ReadAllLines(cacheFile));
                }
                else
                {
                    File.Copy(cacheFile, outputFile, Force);
                }
            }
            catch (IOException)
            {
                console.Error.WriteLine($"Failed to download {GitIgnore}. Do you have permissions to write to \"{output}\"?");
                return(1);
            }
            catch
            {
                console.Error.WriteLine($"Failed to download {GitIgnore}.");
                return(1);
            }

            return(0);
        }