internal async Task CreateNewProject() { if (HelpTemplateCommand) { ShowHelp(); } else if (EmptyTemplateCommand) { await DownloadAndPrepareProject(EmptyTemplateUrl); } else if (LibraryTemplateCommand) { await DownloadAndPrepareProject(LibraryTemplateUrl); } else if (CustomTemplateCommand) { if (!TryGetTemplateUri(out var templateUri)) { FlubuSession.LogInfo("Entered uri is not well formed."); return; } await DownloadAndPrepareProject(templateUri); } }
internal async Task DownloadAndPrepareProject(string templateUri) { using (var client = new HttpClient()) { try { FlubuSession.LogInfo($"Creating Flubu template '{Args.MainCommands[1]}'."); await client.DownloadFileAsync(templateUri, TmpZipPath); var rootDir = Path.GetFullPath("."); var files = FlubuSession.Tasks() .UnzipTask(TmpZipPath, rootDir) .WithLogLevel(LogLevel.None) .DoNotLogTaskExecutionInfo() .Execute(FlubuSession); if (!files.Any(x => x.EndsWith(".cs"))) { FlubuSession.LogInfo("Flubu template not found on specified url."); return; } string templateJsonPath = files.FirstOrDefault(x => x.EndsWith(TemplateJsonFileName, StringComparison.OrdinalIgnoreCase)); string templateCsFilePath = files.FirstOrDefault(x => x.EndsWith(TemplateCsFileName, StringComparison.OrdinalIgnoreCase)); TemplateModel templateData = null; if (templateJsonPath != null) { templateData = GetTemplateDataFromJsonFile(templateJsonPath); } else if (templateCsFilePath != null) { var flubuTemplate = await GetTemplateFromCsharpFile(templateCsFilePath); switch (flubuTemplate) { case null: FlubuSession.LogInfo("Template.cs must implement IFlubuTemplate interface."); return; //// ReSharper disable once SuspiciousTypeConversion.Global case IFlubuTemplateTask flubuTask: FlubuTemplateTasksExecutor.AddTaskToExecute(flubuTask); break; } var templateBuilder = new FlubuTemplateBuilder(); flubuTemplate.ConfigureTemplate(templateBuilder); templateData = templateBuilder.Build(); } FlubuTemplateTasksExecutor.SetTasksToExecute(_templateTasksToExecute); FlubuTemplateTasksExecutor.BeforeFileProcessing(templateData, files); foreach (var sourceFilePath in files) { string relativePath = sourceFilePath.Replace(rootDir, string.Empty).TrimStart(Path.DirectorySeparatorChar); relativePath = relativePath .Substring(relativePath.IndexOf(Path.DirectorySeparatorChar)) .TrimStart(Path.DirectorySeparatorChar); if (templateData != null && templateData.SkipFiles.Contains(relativePath)) { continue; } var destinationFilePath = Path.Combine(rootDir, relativePath); var destinationDir = Path.GetDirectoryName(destinationFilePath); if (!string.IsNullOrEmpty(destinationDir)) { Directory.CreateDirectory(destinationDir); } FlubuTemplateTasksExecutor.BeforeFileCopy(sourceFilePath); File.Copy(sourceFilePath, destinationFilePath, true); FlubuTemplateTasksExecutor.AfterFileCopy(destinationFilePath); } FlubuTemplateTasksExecutor.AfterFileProcessing(templateData); var tmp = files[0].Substring(rootDir.Length).TrimStart(Path.DirectorySeparatorChar); var gitDirName = tmp.Substring(0, tmp.IndexOf(Path.DirectorySeparatorChar)); Directory.Delete(gitDirName, true); FlubuSession.LogInfo($"The template '{Args.MainCommands[1]}' was created successfully."); } catch (InvalidDataException) { FlubuSession.LogInfo("Flubu template not found on specified url."); } catch (HttpRequestException) { FlubuSession.LogInfo("Flubu template not found on specified url."); } finally { File.Delete(TmpZipPath); } } }