Skip to content

Commit

Permalink
Improve Coding Style (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion authored Feb 4, 2025
1 parent ee48f66 commit 10e2a40
Show file tree
Hide file tree
Showing 773 changed files with 2,991 additions and 2,742 deletions.
19 changes: 9 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ DerivedData/
timeline.xctimeline
playground.xcworkspace

# Swift Package Manager
Swift Package Manager
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
# Package.resolved
# *.xcodeproj
*.xcodeproj
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
# hence it is not needed unless you have added a package configuration file to your project
# .swiftpm
.swiftpm

.build/

Expand Down Expand Up @@ -124,13 +124,12 @@ iOSInjectionProject/

### Xcode Patch ###
*.xcodeproj/*
!*.xcodeproj/project.pbxproj
!*.xcodeproj/xcshareddata/
!*.xcodeproj/project.xcworkspace/
!*.xcworkspace/contents.xcworkspacedata
/*.gcno
**/xcshareddata/WorkspaceSettings.xcsettings
# !*.xcodeproj/project.pbxproj
# !*.xcodeproj/xcshareddata/
# !*.xcodeproj/project.xcworkspace/
# !*.xcworkspace/contents.xcworkspacedata
# /*.gcno
# **/xcshareddata/WorkspaceSettings.xcsettings

# End of https://www.toptal.com/developers/gitignore/api/swift,xcode,macos
.swiftpm
.mint
15 changes: 9 additions & 6 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ opt_in_rules:
- empty_xctest_method
- enum_case_associated_values_count
- expiring_todo
- explicit_acl
- explicit_init
- explicit_top_level_acl
- fallthrough
- fatal_error_message
- file_name
Expand Down Expand Up @@ -58,7 +55,6 @@ opt_in_rules:
- optional_enum_case_matching
- overridden_super_call
- override_in_extension
- pattern_matching_keywords
- prefer_self_type_over_type_of_self
- prefer_zero_over_explicit_init
- private_action
Expand All @@ -78,7 +74,6 @@ opt_in_rules:
- static_operator
- strict_fileprivate
- strong_iboutlet
- switch_case_on_newline
- toggle_bool
- trailing_closure
- type_contents_order
Expand All @@ -99,19 +94,27 @@ file_length:
- 200
- 550
function_body_length:
- 20
- 30
- 40
function_parameter_count: 8
line_length:
- 90
- 90
identifier_name:
excluded:
- id
- n
excluded:
- Tests
- DerivedData
- .build
- Mint
indentation_width:
indentation_width: 2
type_name:
min_length: 1
max_length: 100
file_name:
excluded: ["PackageDescription.swift"]
disabled_rules:
- switch_case_alignment
64 changes: 33 additions & 31 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
// swift-tools-version: 5.10
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "PackageDSL",
platforms: [.macOS(.v12)],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "PackageDSL",
targets: ["PackageDSL"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-package-manager.git", branch: "swift-5.10-RELEASE"),
.package(url: "https://github.com/apple/swift-format.git", from: "510.1.0"),
.package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.4.0")
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "PackageDSL",
dependencies: [.product(name: "PackageDescription", package: "swift-package-manager")],
swiftSettings: [
.define("USE_IMPL_ONLY_IMPORTS"),
.unsafeFlags(["-package-description-version", "999.0"]),
.unsafeFlags(["-enable-library-evolution"])
]
),
.testTarget(
name: "PackageDSLTests",
dependencies: ["PackageDSL"])
]
name: "PackageDSL",
platforms: [.macOS(.v12)],
products: [
.library(
name: "PackageDSL",
targets: ["PackageDSL"]
)
],
dependencies: [
.package(
url: "https://github.com/apple/swift-package-manager.git",
branch: "swift-5.10-RELEASE"
),
.package(url: "https://github.com/apple/swift-format.git", from: "510.1.0"),
.package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.4.0")
],
targets: [
.target(
name: "PackageDSL",
dependencies: [
.product(name: "PackageDescription", package: "swift-package-manager")
],
swiftSettings: [
.define("USE_IMPL_ONLY_IMPORTS"),
.unsafeFlags(["-package-description-version", "999.0"]),
.unsafeFlags(["-enable-library-evolution"])
]
),
.testTarget(
name: "PackageDSLTests",
dependencies: ["PackageDSL"]
)
]
)
4 changes: 3 additions & 1 deletion Scripts/soundness/features.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ create_feature_file() {
')

# Create the Swift file with documentation
echo "///" > "$output_directory/$feature_state_dir/${feature_name}.swift"
echo "// swiftlint:disable line_length" > "$output_directory/$feature_state_dir/${feature_name}.swift"
echo "///" >> "$output_directory/$feature_state_dir/${feature_name}.swift"
# Format each line of documentation with ///
echo "$documentation" | sed '/^$/d' | while IFS= read -r line; do
echo "/// $line" >> "$output_directory/$feature_state_dir/${feature_name}.swift"
Expand All @@ -48,6 +49,7 @@ create_feature_file() {
echo "/// - SeeAlso: [$proposal_title ($proposal_number)]($html_url)" >> "$output_directory/$feature_state_dir/${feature_name}.swift"
echo "///" >> "$output_directory/$feature_state_dir/${feature_name}.swift"
echo "public struct $feature_name : SwiftSettingFeature {" >> "$output_directory/$feature_state_dir/${feature_name}.swift"
echo " // swiftlint:enable line_length" >> "$output_directory/$feature_state_dir/${feature_name}.swift"
echo " /// The current state of the feature." >> "$output_directory/$feature_state_dir/${feature_name}.swift"
echo " public var featureState : FeatureState {" >> "$output_directory/$feature_state_dir/${feature_name}.swift"
echo " return .$feature_state" >> "$output_directory/$feature_state_dir/${feature_name}.swift"
Expand Down
23 changes: 11 additions & 12 deletions Scripts/soundness/unsafeflags.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ process_flags() {
else
flag_for_camel="$flag_name"
fi
camel_case_flag=$(echo "$flag_for_camel" | awk '{gsub("-", " "); print $0}' | awk '{for (i=1; i<=NF; i++) $i=toupper(substr($i,1,1)) tolower(substr($i,2)); print}' | sed 's/ //g')
camel_case_flag=$(echo "$flag_for_camel" | awk '{gsub(/[-_]/, " "); print $0}' | awk '{for (i=1; i<=NF; i++) $i=toupper(substr($i,1,1)) tolower(substr($i,2)); print}' | sed 's/ //g')

# Handle single-letter flags
if [ ${#camel_case_flag} -eq 1 ]; then
Expand Down Expand Up @@ -135,7 +135,7 @@ process_flags() {
# Use parameter name for the value property, taking only the first part if it contains punctuation
# Convert to camelCase and remove dashes
value_property_name=$(echo "$param_for_type" | cut -d'=' -f1 | cut -d',' -f1 | cut -d':' -f1 | cut -d'#' -f1 | \
awk '{gsub("-", " "); print $0}' | \
awk '{gsub(/[-_]/, " "); print $0}' | \
awk '{for(i=1;i<=NF;i++)if(i==1){$i=tolower($i)}else{$i=toupper(substr($i,1,1)) tolower(substr($i,2))};}1' | \
sed 's/ //g' | tr '[:upper:]' '[:lower:]')
fi
Expand All @@ -161,25 +161,24 @@ process_flags() {
{
echo "/// Passes the flag \`$original_flag\`"
if [ ! -z "$description" ]; then
# Split description into lines of max 80 characters (accounting for "/// " prefix)
capitalized_desc=$(echo "$description" | sed 's/^[[:lower:]]/\U&/')
echo "/// $capitalized_desc"
echo "$capitalized_desc" | fold -s -w 80 | while read -r line; do
echo "/// $line"
done
fi

if [ ! -z "$parameter" ]; then
echo "public struct $camel_case_flag: $protocol_name {"
echo " public let $value_property_name: $param_type"
echo ""
echo " public init(_ $value_property_name: $param_type) {"
echo " self.$value_property_name = $value_property_name"
echo " }"
echo ""
echo " public var $property_name: [String] {"
if [[ "$original_flag" == *"="* ]]; then
echo " [\"\(name.camelToSnakeCaseFlag())=\($value_property_name)\"]"
else
echo " [\"\(name.camelToSnakeCaseFlag())\", \"\($value_property_name)\"]"
fi
echo " [\"\(name.camelToSnakeCaseFlag())\", \"\($value_property_name)\"]"
echo " }"
echo ""
echo " public init(_ $value_property_name: $param_type) {"
echo " self.$value_property_name = $value_property_name"
echo " }"
echo "}"
else
echo "public struct $camel_case_flag: $protocol_name { }"
Expand Down
2 changes: 1 addition & 1 deletion Sources/PackageDSL/Array+Depedencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extension Array: Dependencies where Element == Dependency {
/// Appends the provided `Dependencies` to the current `Array` of `Dependency` elements.
///
/// - Parameter dependencies: The `Dependencies` to be appended.
/// - Returns: A new `Array` of `Dependency` elements containing the original elements and the appended `Dependencies`.
/// - Returns: A new array containing the original elements and the appended items.
public func appending(_ dependencies: any Dependencies) -> [Dependency] {
self + dependencies
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/PackageDSL/Array+TestTargets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ extension Array: TestTargets where Element == any TestTarget {
/// Appends the given `TestTargets` to the current array.
///
/// - Parameter testTargets: The `TestTargets` to append to the array.
/// - Returns: A new array containing the elements of the current array plus the elements of the `testTargets`.
/// - Returns: A new array containing the elements of the current array
/// plus the elements of the `testTargets`.
public func appending(_ testTargets: any TestTargets) -> [any TestTarget] {
self + testTargets
}
Expand Down
11 changes: 6 additions & 5 deletions Sources/PackageDSL/Dependencies.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/// A protocol that represents a collection of `Dependency` elements.
public protocol Dependencies: Sequence where Element == Dependency {
/// Initializes a `Dependencies` instance from a sequence of `Dependency` elements.
/// - Parameter s: A sequence of `Dependency` elements.
init<S>(_ s: S) where S.Element == Dependency, S: Sequence
/// - Parameter sequence: A sequence of `Dependency` elements.
init<S>(_ sequence: S) where S.Element == Dependency, S: Sequence

/// Creates a new `Dependencies` instance by appending the provided `Dependencies` to the existing `Dependencies`.
/// - Parameter dependencies: The `Dependencies` to append.
/// - Returns: A new `Dependencies` instance that includes the existing `Dependencies` and the provided `Dependencies`.
/// Appends the provided `Dependencies` to the current `Array` of `Dependency` elements.
///
/// - Parameter dependencies: The `Dependencies` to be appended.
/// - Returns: A new array containing the original elements and the appended items.
func appending(_ dependencies: any Dependencies) -> Self
}
12 changes: 8 additions & 4 deletions Sources/PackageDSL/DependencyBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ public enum DependencyBuilder {
[first]
}

/// Builds a partial block of dependencies, adding a new `Dependency` to an existing collection.
/// Builds a partial block of dependencies,
/// adding a new `Dependency` to an existing collection.
/// - Parameter accumulated: The existing collection of dependencies.
/// - Parameter next: The new `Dependency` to add to the collection.
/// - Returns: A new collection of dependencies, containing the accumulated dependencies and the `next` dependency.
public static func buildPartialBlock(accumulated: any Dependencies, next: Dependency)
-> any Dependencies {
/// - Returns: A new collection of dependencies,
/// containing the accumulated dependencies and the `next` dependency.
public static func buildPartialBlock(
accumulated: any Dependencies,
next: Dependency
) -> any Dependencies {
accumulated + [next]
}
}
7 changes: 2 additions & 5 deletions Sources/PackageDSL/FeatureState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ extension FeatureState {
/// - Returns: A `SwiftSetting` that enables the feature based on its state.
public func swiftSetting(name: String) -> SwiftSetting {
switch self {
case .experimental:
return .enableExperimentalFeature(name)

case .upcoming:
return .enableUpcomingFeature(name)
case .experimental: .enableExperimentalFeature(name)
case .upcoming: .enableUpcomingFeature(name)
}
}
}
20 changes: 20 additions & 0 deletions Sources/PackageDSL/FrontendFlag.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// FrontendFlag.swift
// PackageDSL
//
// Created by Leo Dion on 2/3/25.
//

public protocol FrontendFlag: UnsafeFlag, _Named {
var flagArguments: [String] { get }
}

extension FrontendFlag {
public var flagArguments: [String] {
[name.camelToSnakeCaseFlag()]
}

public var unsafeFlagArguments: [String] {
["-Xfrontend", flagArguments.joined(separator: "=")]
}
}
6 changes: 3 additions & 3 deletions Sources/PackageDSL/Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import Foundation

/// A group that can be used with the `@GroupBuilder` to create a collection of elements.
public struct Group<T> {
/// The name of the group, or `nil` if the group has no name.
public let name: String?

/// Creates a new group with an optional name.
/// - Parameter name: The name of the group, or `nil` if the group has no name.
internal init(_ name: String? = nil) {
self.name = name
}

/// The name of the group, or `nil` if the group has no name.
public let name: String?

/// Executes the content closure and returns the resulting array of elements.
/// - Parameter content: A closure that returns an array of elements of type `T`.
/// - Returns: The array of elements returned by the content closure.
Expand Down
16 changes: 12 additions & 4 deletions Sources/PackageDSL/GroupBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
/// A builder for creating groups of elements.
@resultBuilder
public enum GroupBuilder<U> {
/// Builds a partial block of the group by combining the accumulated elements with the output of the next `GroupBuildable` element.
/// Builds a partial block of the group
/// by combining the accumulated elements
/// with the output of the next `GroupBuildable` element.
/// - Parameters:
/// - accumulated: The accumulated elements in the group.
/// - next: The next `GroupBuildable` element to add to the group.
/// - Returns: The updated group of elements.
public static func buildPartialBlock<T: GroupBuildable>(accumulated: [U], next: T) -> [U]
public static func buildPartialBlock<T: GroupBuildable>(
accumulated: [U],
next: T
) -> [U]
where T.Output == U {
accumulated + T.output(from: [next])
}

/// Builds a partial block of the group with the output of the first `GroupBuildable` element.
/// Builds a partial block of the group
/// with the output of the first `GroupBuildable` element.
/// - Parameter first: The first `GroupBuildable` element to add to the group.
/// - Returns: The group of elements.
public static func buildPartialBlock<T: GroupBuildable>(first: T) -> [U] where T.Output == U {
public static func buildPartialBlock<T: GroupBuildable>(
first: T
) -> [U] where T.Output == U {
T.output(from: [first])
}
}
Loading

0 comments on commit 10e2a40

Please sign in to comment.