-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add Snippet support with SwiftBuild #9089
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
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
9 changes: 9 additions & 0 deletions
9
Fixtures/Miscellaneous/Plugins/PluginsAndSnippets/Snippets/ContainsMain.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,9 @@ | ||
// The Swift Programming Language | ||
// https://docs.swift.org/swift-book | ||
|
||
@main | ||
struct foo { | ||
static func main() { | ||
print("hello, snippets. File: \(#file)") | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Fixtures/Miscellaneous/Plugins/PluginsAndSnippets/Snippets/ImportsProductTarget.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,3 @@ | ||
import MyLib | ||
|
||
libraryCall() |
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 @@ | ||
print("hello, snippets. File: \(#file)") |
1 change: 1 addition & 0 deletions
1
Fixtures/Miscellaneous/Plugins/PluginsAndSnippets/Snippets/SubDirectory/main.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 @@ | ||
print("hello, snippets! File: \(#file)") |
4 changes: 4 additions & 0 deletions
4
Fixtures/Miscellaneous/Plugins/PluginsAndSnippets/Sources/MyLib/MyLib.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,4 @@ | ||
public func libraryCall() { | ||
print("From library") | ||
print("hello, snippets. File: \(#file)") | ||
} |
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
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
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,118 @@ | ||
import Foundation | ||
import Basics | ||
|
||
func getFiles(atPath path: String, matchingExtension fileExtension: String) -> [URL] { | ||
let fileManager = FileManager.default | ||
var matchingFiles: [URL] = [] | ||
|
||
guard | ||
let enumerator = fileManager.enumerator( | ||
at: URL(fileURLWithPath: path), | ||
includingPropertiesForKeys: [.isRegularFileKey], | ||
options: [.skipsHiddenFiles, .skipsPackageDescendants] | ||
) | ||
else { | ||
print("Error: Could not create enumerator for path: \(path)") | ||
return [] | ||
} | ||
|
||
for case let fileURL as URL in enumerator { | ||
do { | ||
let resourceValues = try fileURL.resourceValues(forKeys: [.isRegularFileKey]) | ||
if let isRegularFile = resourceValues.isRegularFile, isRegularFile { | ||
if fileURL.pathExtension.lowercased() == fileExtension.lowercased() { | ||
matchingFiles.append(fileURL) | ||
} | ||
} | ||
} catch { | ||
print("Error retrieving resource values for \(fileURL.lastPathComponent): \(error.localizedDescription)") | ||
} | ||
} | ||
return matchingFiles | ||
} | ||
|
||
/// Returns all files that match the given extension in the specified directory. | ||
/// | ||
/// - Parameters: | ||
/// - directory: The directory to search in (AbsolutePath) | ||
/// - extension: The file extension to match (without the leading dot) | ||
/// - recursive: Whether to search subdirectories recursively (default: true) | ||
/// - fileSystem: The file system to use for operations (defaults to localFileSystem) | ||
/// - Returns: An array of AbsolutePath objects | ||
/// - Throws: FileSystemError if the directory cannot be accessed or enumerated | ||
public func getFiles( | ||
in directory: AbsolutePath, | ||
matchingExtension extension: String, | ||
recursive: Bool = true, | ||
fileSystem: FileSystem = localFileSystem | ||
) throws -> [AbsolutePath] { | ||
var matchingFiles: [AbsolutePath] = [] | ||
let normalizedExtension = `extension`.lowercased() | ||
|
||
guard fileSystem.exists(directory) else { | ||
throw StringError("Directory does not exist: \(directory)") | ||
} | ||
|
||
guard fileSystem.isDirectory(directory) else { | ||
throw StringError("Path is not a directory: \(directory)") | ||
} | ||
|
||
if recursive { | ||
try fileSystem.enumerate(directory: directory) { filePath in | ||
if fileSystem.isFile(filePath) { | ||
if let fileExtension = filePath.extension?.lowercased(), | ||
fileExtension == normalizedExtension { | ||
matchingFiles.append(filePath) | ||
} | ||
} | ||
} | ||
} else { | ||
// Non-recursive: only check direct children | ||
let contents = try fileSystem.getDirectoryContents(directory) | ||
for item in contents { | ||
let itemPath = directory.appending(component: item) | ||
if fileSystem.isFile(itemPath) { | ||
if let fileExtension = itemPath.extension?.lowercased(), | ||
fileExtension == normalizedExtension { | ||
matchingFiles.append(itemPath) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return matchingFiles | ||
} | ||
|
||
/// Returns all files that match the given extension in the specified directory. | ||
/// | ||
/// - Parameters: | ||
/// - directory: The directory to search in (RelativePath) | ||
/// - extension: The file extension to match (without the leading dot) | ||
/// - recursive: Whether to search subdirectories recursively (default: true) | ||
/// - fileSystem: The file system to use for operations (defaults to localFileSystem) | ||
/// - Returns: An array of RelativePath objects | ||
/// - Throws: FileSystemError if the directory cannot be accessed or enumerated | ||
public func getFiles( | ||
in directory: RelativePath, | ||
matchingExtension extension: String, | ||
recursive: Bool = true, | ||
fileSystem: FileSystem = localFileSystem | ||
) throws -> [RelativePath] { | ||
// Convert RelativePath to AbsolutePath for enumeration | ||
guard let currentWorkingDirectory = fileSystem.currentWorkingDirectory else { | ||
throw StringError("Cannot determine current working directory") | ||
} | ||
|
||
let absoluteDirectory = currentWorkingDirectory.appending(directory) | ||
let absoluteResults = try getFiles( | ||
in: absoluteDirectory, | ||
matchingExtension: `extension`, | ||
recursive: recursive, | ||
fileSystem: fileSystem | ||
) | ||
|
||
// Convert results back to RelativePath | ||
return absoluteResults.map { absolutePath in | ||
absolutePath.relative(to: currentWorkingDirectory) | ||
} | ||
} |
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
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.
Just taking a quick look, and I see it's still in draft. The "Build" target is the native build system. We shouldn't have SwiftBuildSupport depending on it since it'll be removed eventually. Common things should go in SPMBuildCore.