Repository navigation
feat(import): import several files or a folder in one run #3745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c10f5de
feat(import): import several files or a folder in one run
datlechin a119a78
chore(l10n): add the strings for importing several files
datlechin 4d32605
docs(changelog): note importing several files
datlechin e8e6537
fix(import): refuse clearing a table two files of a run fill, new or …
datlechin 09248f4
Merge remote-tracking branch 'origin/main' into feat/2499-import-many…
datlechin b67414c
Merge remote-tracking branch 'origin/main' into feat/2499-import-many…
datlechin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
apple/Plugins/FormatPluginTests/RowImportConfigurationTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // | ||
| // RowImportConfigurationTests.swift | ||
| // TableProTests | ||
| // | ||
|
|
||
| import Foundation | ||
| import TableProPluginKit | ||
| import Testing | ||
|
|
||
| /// A run of several files has one transaction, so it reads each format's error handling and transaction options | ||
| /// before it starts, and they have to be the ones the format's own import runs with. | ||
| struct RowImportConfigurationTests { | ||
| @Test("CSV reports the options its import runs with") | ||
| func csvReportsItsOptions() throws { | ||
| let plugin = CSVImportPlugin() | ||
| let saved = plugin.settings | ||
| defer { plugin.settings = saved } | ||
| plugin.settings.errorHandling = .stopAndCommit | ||
| plugin.settings.wrapInTransaction = false | ||
| plugin.settings.deleteExistingRows = true | ||
|
|
||
| let configuration = try #require(plugin.rowImportConfiguration) | ||
|
|
||
| #expect(configuration.errorHandling == .stopAndCommit) | ||
| #expect(configuration.wrapInTransaction == false) | ||
| #expect(configuration.deleteExistingRows == true) | ||
| } | ||
|
|
||
| @Test("JSON reports the options its import runs with") | ||
| func jsonReportsItsOptions() throws { | ||
| let plugin = JSONImportPlugin() | ||
| let saved = plugin.settings | ||
| defer { plugin.settings = saved } | ||
| plugin.settings.errorHandling = .skipAndContinue | ||
|
|
||
| let configuration = try #require(plugin.rowImportConfiguration) | ||
|
|
||
| #expect(configuration.errorHandling == .skipAndContinue) | ||
| #expect(configuration.wrapInTransaction == saved.wrapInTransaction) | ||
| } | ||
|
|
||
| @Test("XLSX reports the options its import runs with") | ||
| func xlsxReportsItsOptions() throws { | ||
| let plugin = XLSXImportPlugin() | ||
| let saved = plugin.settings | ||
| defer { plugin.settings = saved } | ||
| plugin.settings.deleteExistingRows = true | ||
|
|
||
| let configuration = try #require(plugin.rowImportConfiguration) | ||
|
|
||
| #expect(configuration.deleteExistingRows == true) | ||
| #expect(configuration.errorHandling == saved.errorHandling) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
apple/TablePro/Core/Services/Export/ImportBatchFiles.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // | ||
| // ImportBatchFiles.swift | ||
| // TablePro | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| /// The files a choice of several files or folders imports as rows, each with the format its extension names. | ||
| /// | ||
| /// A folder gives the visible files directly inside it, not those of its subfolders, in Finder's order. A file no | ||
| /// row format reads, a SQL dump among them, is left out by name rather than dropped silently: a dump runs its own | ||
| /// statements and is imported on its own. | ||
| internal enum ImportBatchFiles { | ||
| internal struct File: Equatable, Sendable { | ||
| internal let url: URL | ||
| internal let formatId: String | ||
| } | ||
|
|
||
| internal struct Resolution: Equatable, Sendable { | ||
| internal var files: [File] = [] | ||
| internal var leftOut: [URL] = [] | ||
| } | ||
|
|
||
| internal static func resolve( | ||
| _ urls: [URL], | ||
| among options: [ImportFormatOption], | ||
| readsRows: (String) -> Bool | ||
| ) -> Resolution { | ||
| var resolution = Resolution() | ||
| var seen = Set<String>() | ||
| for url in urls { | ||
| let candidates = isDirectory(url) ? contents(of: url) : [url] | ||
| for candidate in candidates { | ||
| let key = candidate.standardizedFileURL.path(percentEncoded: false) | ||
| guard seen.insert(key).inserted else { continue } | ||
| if case .format(let formatId) = ImportFileFormatResolver.match(candidate, among: options), | ||
| readsRows(formatId) { | ||
| resolution.files.append(File(url: candidate, formatId: formatId)) | ||
| } else { | ||
| resolution.leftOut.append(candidate) | ||
| } | ||
| } | ||
| } | ||
| return resolution | ||
| } | ||
|
|
||
| internal static func isDirectory(_ url: URL) -> Bool { | ||
| (try? url.resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true | ||
| } | ||
|
|
||
| private static func contents(of folder: URL) -> [URL] { | ||
| let keys: [URLResourceKey] = [.isRegularFileKey] | ||
| guard let entries = try? FileManager.default.contentsOfDirectory( | ||
| at: folder, | ||
| includingPropertiesForKeys: keys, | ||
| options: [.skipsHiddenFiles] | ||
| ) else { return [] } | ||
| return entries | ||
| .filter { (try? $0.resourceValues(forKeys: Set(keys)))?.isRegularFile == true } | ||
| .sorted { $0.lastPathComponent.localizedStandardCompare($1.lastPathComponent) == .orderedAscending } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rowImportConfiguration and runConfiguration are pasted identically into the CSV, JSON and XLSX plugins, where rowImportConfiguration just returns runConfiguration. A new setting forgotten in one copy silently desynchronises batch policy from a file's own import. Share one definition.