Skip to content

Commit

Permalink
Configure swiftformat + modify license CI check (#247)
Browse files Browse the repository at this point in the history
* Implement swiftformat to auto-format sample code

* Add format build phase to MobileBuyIntegration
  • Loading branch information
markmur authored Nov 26, 2024
1 parent 56b5761 commit b6774f1
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
runs-on: macos-latest
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- run: ./Scripts/copy_license && git diff --name-only --exit-code
- run: ./Scripts/ensure_license

lint-podspec:
name: CocoaPods
Expand Down
9 changes: 9 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--indent 4
--linebreaks lf
--wraparguments before-first
--wrapcollections before-first
--commas inline
--allman false
--semicolons inline
--trimwhitespace always
--disable redundantReturn
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
isa = PBXNativeTarget;
buildConfigurationList = 4EBBA77B2A5F0CE200193E19 /* Build configuration list for PBXNativeTarget "MobileBuyIntegration" */;
buildPhases = (
6A9124C62CF6114E0076C21C /* Run Swiftformat over sample code */,
4EBBA7632A5F0CE200193E19 /* Sources */,
4EBBA7642A5F0CE200193E19 /* Frameworks */,
4EBBA7652A5F0CE200193E19 /* Resources */,
Expand Down Expand Up @@ -264,6 +265,24 @@
shellPath = /bin/sh;
shellScript = "# Remove .pcm binaries from build\nfind \"${TARGET_BUILD_DIR}/${FULL_PRODUCT_NAME}\" -name \"*.pcm\" -type f -delete\n\n# Remove Swiftlint binaries from build\nfind \"${TARGET_BUILD_DIR}/${FULL_PRODUCT_NAME}\" -iname \"swiftlint*\" -type f -delete\n\n\n\n";
};
6A9124C62CF6114E0076C21C /* Run Swiftformat over sample code */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
);
name = "Run Swiftformat over sample code";
outputFileListPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "export PATH=\"/opt/homebrew/bin:$PATH\"\n\nif which swiftformat >/dev/null; then\n\tswiftformat \"${SRCROOT}\" \nelse \n\techo \"error: SwiftFormat not installed, download using \\\"brew install swiftformat\\\"\"\nfi\n";
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
Expand Down
20 changes: 0 additions & 20 deletions Scripts/copy_license

This file was deleted.

29 changes: 29 additions & 0 deletions Scripts/ensure_license
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env ruby

require 'find'

# Read and normalize the license text by removing extra whitespace
license_text = "/*\n" + File.read('LICENSE') + "*/\n"
normalized_license = license_text.gsub(/\s+/, ' ').strip

Find.find('.') do |path|
next unless File.file?(path) && path.end_with?('.swift') && !path.end_with?('Package.swift')

# Read the current content of the Swift file
content = File.read(path)

# Extract the existing license part from the file, if present
if content =~ /\A\s*\/\*.*?\*\//m
existing_license = $&.gsub(/\s+/, ' ').strip
else
existing_license = ""
end

# Check if the existing license matches the normalized expected license
if existing_license != normalized_license
puts "License missing or incorrect in file: #{path}"
exit 1
else
puts "✔️ #{path}"
end
end

0 comments on commit b6774f1

Please sign in to comment.