Skip to content

Commit

Permalink
Improving comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
goergisn committed Jun 10, 2024
1 parent 654e5b9 commit 2deb186
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 48 deletions.
58 changes: 45 additions & 13 deletions Scripts/compare_public_interface_definition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ let old = CommandLine.arguments[1]
let new = CommandLine.arguments[2]
let moduleName = CommandLine.arguments[3]

struct Conformance: Codable, Equatable {
var printedName: String

enum CodingKeys: String, CodingKey {
case printedName
}
}

class Element: Codable, Equatable, CustomDebugStringConvertible {
let kind: String
let name: String
Expand All @@ -24,6 +32,9 @@ class Element: Codable, Equatable, CustomDebugStringConvertible {
let children: [Element]?
let spiGroupNames: [String]?

let declAttributes: [String]?
let conformances: [Conformance]?

var parent: Element?

enum CodingKeys: String, CodingKey {
Expand All @@ -34,6 +45,8 @@ class Element: Codable, Equatable, CustomDebugStringConvertible {
case children
case spiGroupNames = "spi_group_names"
case declKind
case declAttributes
case conformances
}

var debugDescription: String {
Expand All @@ -43,16 +56,24 @@ class Element: Codable, Equatable, CustomDebugStringConvertible {
}
definition += "public "

if declKind == "Var" {
definition += "var "
} else if declKind == "Func" {
definition += "func "
} else if declKind == "Import" {
definition += "import "
if declAttributes?.contains("Final") == true {
definition += "final "
}

if let declKind {
if declKind == "Constructor" {
definition += "func "
} else {
definition += "\(declKind.lowercased()) "
}
}

definition += "\(printedName)"

if let conformanceNames = conformances?.map({ $0.printedName }), !conformanceNames.isEmpty {
definition += " : \(conformanceNames.joined(separator: ", "))"
}

return definition
}

Expand Down Expand Up @@ -101,9 +122,9 @@ struct Change {
var icon: String {
switch self {
case .addition:
return "🐣"
return "❇️"
case .removal:
return "💔"
return "😶‍🌫️"
case .change:
return "🔀"
}
Expand All @@ -122,10 +143,21 @@ func recursiveCompare(element lhs: Element, to rhs: Element, oldFirst: Bool) ->

var changes = [Change]()

if oldFirst, (lhs.printedName != rhs.printedName || lhs.spiGroupNames != rhs.spiGroupNames || lhs.children == rhs.children) {
// TODO: Add check if accessor changed (e.g. changed from get/set to get only...)

if oldFirst, (
lhs.printedName != rhs.printedName ||
lhs.spiGroupNames != rhs.spiGroupNames ||
lhs.conformances != rhs.conformances ||
lhs.declAttributes != rhs.declAttributes
) {
changes += [.init(changeType: .change, parentName: lhs.parentPath, changeDescription: "`\(lhs)` ➡️ `\(rhs)`")]
}

if lhs.children == rhs.children {
return changes
}

changes += lhs.children?.flatMap { lhsElement in
if let rhsChildForName = rhs.children?.first(where: { ($0.mangledName ?? $0.name) == (lhsElement.mangledName ?? lhsElement.name) }) {
return recursiveCompare(element: lhsElement, to: rhsChildForName, oldFirst: oldFirst)
Expand Down Expand Up @@ -163,7 +195,7 @@ func compare() throws {
)

if decodedOldDefinition == decodedNewDefinition {
try persistComparison(fileContent: "## `\(moduleName)`\n✅ Nothing detected")
try persistComparison(fileContent: "## 🫧 `\(moduleName)`\n- No changes detected")
return
}

Expand All @@ -186,12 +218,12 @@ func compare() throws {
groupedChanges[$0.parentName] = (groupedChanges[$0.parentName] ?? []) + [$0]
}

var fileContent: [String] = ["## `\(moduleName)`\n"]
var fileContent = ["## 👀 `\(moduleName)`\n"]

groupedChanges.keys.sorted().forEach { key in
fileContent += ["### \(key)"]
fileContent += ["### \(key)"]
groupedChanges[key]?.sorted(by: { $0.changeDescription < $1.changeDescription }).forEach {
fileContent += ["- \($0.changeType.icon) \($0.changeDescription)"]
fileContent += ["- \($0.changeType.icon) \($0.changeDescription)"]
}
}

Expand Down
76 changes: 41 additions & 35 deletions Scripts/generate_public_interface_definition.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ BRANCH=$1
REPO=$2
TARGET="x86_64-apple-ios17.4-simulator"
SDK="`xcrun --sdk iphonesimulator --show-sdk-path`"
MODULE=$3 #"Adyen"
MODULE_NAMES=("Adyen" "AdyenDropIn" "AdyenActions" "AdyenCard" "AdyenEncryption" "AdyenComponents" "AdyenSession" "AdyenWeChatPay" "AdyenCashAppPay" "AdyenTwint" "AdyenDelegatedAuthentication")
COMPARISON_VERSION_DIR_NAME="comparison_version_$BRANCH"
DERIVED_DATA_PATH=".build"
SDK_DUMP_INPUT_PATH="$DERIVED_DATA_PATH/Build/Products/Debug-iphonesimulator"

echo "Branch: " $BRANCH
echo "Repo: " $REPO
echo "Module: " $MODULE
echo "Target: " $TARGET
echo "Dir: " $COMPARISON_VERSION_DIR_NAME

echo "Modules:"
for MODULE in ${MODULE_NAMES[@]}
do
echo "-" $MODULE
done

rm -rf .build
rm -rf comparison_version

Expand All @@ -22,53 +29,52 @@ function cleanup() {
mv Adyen.xcode_proj Adyen.xcodeproj
}

function setupComparisonRepo() {
rm -rf $COMPARISON_VERSION_DIR_NAME
mkdir $COMPARISON_VERSION_DIR_NAME
cd $COMPARISON_VERSION_DIR_NAME
git clone -b $BRANCH $REPO

cd adyen-ios
mv Adyen.xcodeproj Adyen.xcode_proj
}

trap cleanup EXIT

# TODO: Compile a list of changed modules from the git diff
# TODO: Generate a Package.swift target that contains all modules
# TODO: Build each (updated + comparison) project once
# TODO: Generate an sdk dump from every module that had changes
# TODO: Only generate + compare an sdk dump from any module that had changes

mv Adyen.xcodeproj Adyen.xcode_proj

echo "↘️ Checking out comparison version"

# If the directory already exists we just navigate into it and run the commands
if [ ! -d "$COMPARISON_VERSION_DIR_NAME" ];
then
mkdir $COMPARISON_VERSION_DIR_NAME
cd $COMPARISON_VERSION_DIR_NAME
git clone -b $BRANCH $REPO
COMMIT_HASH=$(git rev-parse origin/$BRANCH)
else
cd $COMPARISON_VERSION_DIR_NAME
COMMIT_HASH=$(git rev-parse origin/$BRANCH)
fi

echo "🗂️ Changed files"

CHANGED_FILEPATHS=$(git diff --name-only $COMMIT_HASH)
for FILE_PATH in $CHANGED_FILEPATHS; do
echo $FILE_PATH
done
setupComparisonRepo # We're now in the comparison repository directory

exit
for MODULE in ${MODULE_NAMES[@]}
do

cd adyen-ios
mv Adyen.xcodeproj Adyen.xcode_proj
echo "👷 [$MODULE] Building comparison project"
xcodebuild -derivedDataPath $DERIVED_DATA_PATH -sdk $SDK -scheme $MODULE -destination "platform=iOS,name=Any iOS Device" -target $TARGET -quiet

echo "👷 Building comparison project"
xcodebuild -derivedDataPath .build -sdk $SDK -scheme $MODULE -destination "platform=iOS,name=Any iOS Device" -target $TARGET -quiet

echo "📋 Generating comparison api_dump"
xcrun swift-api-digester -dump-sdk -module $MODULE -o ../../api_dump_comparison.json -I .build/Build/Products/Debug-iphonesimulator -sdk $SDK -target $TARGET
echo "📋 [$MODULE] Generating comparison api_dump"
xcrun swift-api-digester -dump-sdk -module $MODULE -o ../../api_dump_comparison.json -I $SDK_DUMP_INPUT_PATH -sdk $SDK -target $TARGET

cd ../..

# Building project in `.build` directory
echo "👷 Building updated project"
xcodebuild -derivedDataPath .build -sdk $SDK -scheme $MODULE -destination "platform=iOS,name=Any iOS Device" -target $TARGET -quiet
echo "👷 [$MODULE] Building updated project"
xcodebuild -derivedDataPath $DERIVED_DATA_PATH -sdk $SDK -scheme $MODULE -destination "platform=iOS,name=Any iOS Device" -target $TARGET -quiet

echo "📋 [$MODULE] Generating new api_dump"
xcrun swift-api-digester -dump-sdk -module $MODULE -o api_dump.json -I $SDK_DUMP_INPUT_PATH -sdk $SDK -target $TARGET

echo "🕵️ [$MODULE] Diffing"
./Scripts/compare_public_interface_definition.swift api_dump_comparison.json api_dump.json $MODULE

# Generating api_dump
echo "📋 Generating new api_dump"
xcrun swift-api-digester -dump-sdk -module $MODULE -o api_dump.json -I .build/Build/Products/Debug-iphonesimulator -sdk $SDK -target $TARGET
# Reset and move into comparison dir again for the next iteration
rm api_dump.json
rm api_dump_comparison.json
cd $COMPARISON_VERSION_DIR_NAME/adyen-ios

done

0 comments on commit 2deb186

Please sign in to comment.