public async Task ImportAsync(string path, SyncOptions options, ISession session) { using (var fs = await FileSystems.CreateAsync(path, session.WorkingDirectory)) { var selectedSynchronizers = GetSynchronizers(options.Targets); var selectedCount = selectedSynchronizers.Count; WriteSummary(fs, session, "<-", selectedSynchronizers); var sync = new SyncService(fs, session); await selectedSynchronizers.Foreach(async (synchronizer, step) => { log.WriteLine(); log.WriteLine("--------------------------------------------------------"); log.WriteLine("* STEP {0} of {1}: Importing {2} started", step, selectedCount, synchronizer.Name); log.WriteLine(); await synchronizer.ImportAsync(sync, options, session); log.WriteLine(); log.WriteLine("* STEP {0} of {1}: Importing {2} completed", step, selectedCount, synchronizer.Name); log.WriteLine("--------------------------------------------------------"); }); } }
public async Task Export(ImportArguments arguments) { var session = configuration.StartSession(arguments.App); var assets = session.Assets; using (var fs = await FileSystems.CreateAsync(arguments.Path, session.WorkingDirectory)) { var folderTree = new FolderTree(session); var folderNames = new HashSet <string>(); var parentId = await folderTree.GetIdAsync(arguments.TargetFolder); var downloadPipeline = new DownloadPipeline(session, log, fs) { FilePathProviderAsync = async asset => { var assetFolder = await folderTree.GetPathAsync(asset.ParentId); var assetPath = asset.FileName; if (!string.IsNullOrWhiteSpace(assetFolder)) { assetPath = Path.Combine(assetFolder, assetPath); } if (!folderNames.Add(assetPath)) { assetPath = Path.Combine(assetFolder !, $"{asset.Id}_{asset.FileName}"); } return(FilePath.Create(assetPath)); } }; try { await assets.GetAllByQueryAsync(session.App, async asset => { await downloadPipeline.DownloadAsync(asset); }, new AssetQuery { ParentId = parentId }); } finally { await downloadPipeline.CompleteAsync(); } log.WriteLine("> Export completed"); } }
public async Task Describe(string path, ISession session) { using (var fs = await FileSystems.CreateAsync(path, session.WorkingDirectory)) { if (!fs.CanWrite) { Console.WriteLine("ERROR: Cannot write to the file system."); return; } var sync = new SyncService(fs, session); var readme = fs.GetFile(new FilePath("README.md")); await using (var file = readme.OpenWrite()) { await using (var textWriter = new StreamWriter(file)) { var markdown = new MarkdownWriter(textWriter); markdown.H1($"Export {DateTime.UtcNow}"); markdown.Paragraph("--- Describe your export here ---"); markdown.Paragraph("Usage"); markdown.Code( "// Add a config to your app.", "sq config add <APP> <CLIENT_ID> <CLIENT_SECRET>", string.Empty, "// Switch to your app.", "sq config use <APP>", string.Empty, "// Import this folder", "sq sync in <PATH_TO_THIS_FOLDER"); foreach (var synchronizer in synchronizers) { markdown.H2(synchronizer.Name); await synchronizer.DescribeAsync(sync, markdown); } } } } }
public async Task GenerateTemplateAsync(string path, ISession session) { using (var fs = await FileSystems.CreateAsync(path, session.WorkingDirectory)) { if (!fs.CanWrite) { Console.WriteLine("ERROR: Cannot write to the file system."); return; } var sync = new SyncService(fs, session); foreach (var synchronizer in GetSynchronizers()) { await synchronizer.GenerateSchemaAsync(sync); } } }
public async Task ExportAsync(string path, SyncOptions options, ISession session) { using (var fs = await FileSystems.CreateAsync(path, session.WorkingDirectory)) { if (!fs.CanWrite) { Console.WriteLine("ERROR: Cannot write to the file system."); return; } var selectedSynchronizers = GetSynchronizers(options.Targets); var selectedCount = selectedSynchronizers.Count; WriteSummary(fs, session, "->", selectedSynchronizers); var sync = new SyncService(fs, session); foreach (var synchronizer in selectedSynchronizers) { await synchronizer.GenerateSchemaAsync(sync); } await selectedSynchronizers.Foreach(async (synchronizer, step) => { log.WriteLine(); log.WriteLine("--------------------------------------------------------"); log.WriteLine("* STEP {0} of {1}: Exporting {2} started", step, selectedCount, synchronizer.Name); log.WriteLine(); await synchronizer.CleanupAsync(fs); await synchronizer.ExportAsync(sync, options, session); log.WriteLine(); log.WriteLine("* STEP {0} of {1}: Exporting {2} completed", step, selectedSynchronizers.Count, synchronizer.Name); log.WriteLine("--------------------------------------------------------"); }); } }
private static async Task <ISyncService> CreateSyncServiceAsync(string repository, ISession session) { var fs = await FileSystems.CreateAsync(repository, session.WorkingDirectory); return(new SyncService(fs, session)); }
public async Task Import(ImportArguments arguments) { var session = configuration.StartSession(arguments.App); var assets = session.Assets; using (var fs = await FileSystems.CreateAsync(arguments.Path, session.WorkingDirectory)) { var folders = new FolderTree(session); var assetQuery = new AssetQuery(); foreach (var file in fs.GetFiles(FilePath.Root, ".*")) { var targetFolder = file.LocalFolderPath(); if (!string.IsNullOrWhiteSpace(arguments.TargetFolder)) { targetFolder = Path.Combine(arguments.TargetFolder, targetFolder); } assetQuery.ParentId = await folders.GetIdAsync(targetFolder); assetQuery.Filter = $"fileName eq '{file.Name}'"; var existings = await assets.GetAssetsAsync(session.App, assetQuery); var existing = existings.Items.FirstOrDefault(); var fileHash = file.GetFileHash(); try { var fileParameter = new FileParameter(file.OpenRead(), file.Name, MimeTypesMap.GetMimeType(file.Name)); log.WriteLine($"Uploading: {file.FullName}"); if (existings.Items.Any(x => string.Equals(x.FileHash, fileHash, StringComparison.Ordinal))) { log.StepSkipped("Same hash."); } else if (existings.Items.Count > 1) { log.StepSkipped("Multiple candidates found."); } else if (existing != null) { await assets.PutAssetContentAsync(session.App, existing.Id, fileParameter); log.StepSuccess("Existing Asset"); } else { var result = await assets.PostAssetAsync(session.App, assetQuery.ParentId, null, arguments.Duplicate, fileParameter); if (result._meta?.IsDuplicate == "true") { log.StepSkipped("duplicate."); } else { log.StepSuccess("New Asset"); } } } catch (Exception ex) { LogExtensions.HandleException(ex, error => log.WriteLine("Error: {0}", error)); } finally { log.WriteLine(); } } log.WriteLine("> Import completed"); } }