Skip to content
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

Embedding third-party dependencies #379

Merged
merged 10 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/cron-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ jobs:
if: failure()
run: |
brew install chargepoint/xcparse/xcparse
xcparse logs fastlane/test_output/StreamChatUITestsApp.xcresult fastlane/test_output/logs/
xcparse logs fastlane/test_output/StreamChatSwiftUITestsApp.xcresult fastlane/test_output/logs/
- uses: actions/upload-artifact@v3
if: failure()
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/smoke-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ jobs:
if: failure()
run: |
brew install chargepoint/xcparse/xcparse
xcparse logs fastlane/test_output/StreamChatUITestsApp.xcresult fastlane/test_output/logs/
xcparse logs fastlane/test_output/StreamChatSwiftUITestsApp.xcresult fastlane/test_output/logs/
- uses: actions/upload-artifact@v3
if: failure()
with:
Expand Down
2 changes: 2 additions & 0 deletions .slather.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ ignore:
- "**/*_Mock.swift"
- "**/*_Vendor.swift"
- "**/Generated/*.swift"
- "Sources/StreamChatSwiftUI/StreamNuke"
- "Sources/StreamChatSwiftUI/StreamSwiftyGif"
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

# Upcoming

### ⚠️ Important

- Dependencies are no longer exposed (this includes Nuke and SwiftyGif). If you were using those dependencies we were exposing, you would need to import them manually. If you encounter any SPM-related problems, be sure to reset the package caches.

### ✅ Added
- Add message preview with attachments in channel list

Expand Down
6 changes: 3 additions & 3 deletions DemoAppSwiftUI/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,20 @@ class AppDelegate: NSObject, UIApplicationDelegate {
streamChat = StreamChat(chatClient: chatClient, utils: utils)

let credentials = UnsecureRepository.shared.loadCurrentUser()
if let credentials, let token = try? Token(rawValue: credentials.token) {
if let credentials, let token = try? Token(rawValue: credentials.token) {
chatClient.connectUser(
userInfo: .init(
id: credentials.id,
name: credentials.name,
imageURL: credentials.avatarURL
),
token: token
token: token
)
}

DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
withAnimation {
if AppState.shared.userState == .launchAnimation {
if AppState.shared.userState == .launchAnimation {
AppState.shared.userState = credentials == nil ? .notLoggedIn : .loggedIn
}
}
Expand Down
4 changes: 2 additions & 2 deletions DemoAppSwiftUI/UserRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Copyright © 2023 Stream.io Inc. All rights reserved.
//

import StreamChat
import Foundation
import StreamChat

protocol UserRepository {

Expand All @@ -14,7 +14,7 @@ protocol UserRepository {
func removeCurrentUser()
}

//NOTE: This is just for simplicity. User data shouldn't be kept in `UserDefaults`.
// NOTE: This is just for simplicity. User data shouldn't be kept in `UserDefaults`.
final class UnsecureRepository: UserRepository {
enum Key: String, CaseIterable {
case user = "stream.chat.user"
Expand Down
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MAKEFLAGS += --silent

update_dependencies:
echo "👉 Updating Nuke"
make update_nuke version=11.3.1
echo "👉 Updating SwiftyGif"
make update_swiftygif version=5.4.2

update_nuke: check_version_parameter
./Scripts/updateDependency.sh $(version) Dependencies/Nuke Sources/StreamChatSwiftUI/StreamNuke Sources
./Scripts/removePublicDeclarations.sh Sources/StreamChatSwiftUI/StreamNuke

update_swiftygif: check_version_parameter
./Scripts/updateDependency.sh $(version) Dependencies/SwiftyGif Sources/StreamChatSwiftUI/StreamSwiftyGif SwiftyGif
./Scripts/removePublicDeclarations.sh Sources/StreamChatSwiftUI/StreamSwiftyGif

check_version_parameter:
@if [ "$(version)" = "" ]; then\
echo "❌ Missing version parameter"; \
exit 1;\
fi
5 changes: 2 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ let package = Package(
)
],
dependencies: [
.package(url: "https://github.com/GetStream/stream-chat-swift.git", from: "4.39.0"),
.package(url: "https://github.com/kean/Nuke.git", .exact("11.3.1"))
.package(url: "https://github.com/GetStream/stream-chat-swift.git", from: "4.39.0"),
],
targets: [
.target(
name: "StreamChatSwiftUI",
dependencies: [.product(name: "StreamChat", package: "stream-chat-swift"), "Nuke", .product(name: "NukeUI", package: "Nuke")],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still see Difference, Swifter and the tools. Can we also get rid of those? (not in this PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those come from StreamChat. They will be gone as soon as #376 is merged

dependencies: [.product(name: "StreamChat", package: "stream-chat-swift")],
exclude: ["README.md", "Info.plist", "Generated/L10n_template.stencil"],
resources: [.process("Resources")]
)
Expand Down
66 changes: 66 additions & 0 deletions Scripts/removePublicDeclarations.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env bash
#
# Usage: ./removePublicDeclarations.sh Sources/StreamNuke
#
# This script would iterate over the files on a particular directory, and perform basic replacement operations.
# It heavily relies on 'sed':
# sed -i '<backup-file-extension>' -e 's/<original-string>/<replacement>/g' <file>
# ^
# Passing empty string prevents the creation of backup files

args=("$@")
directory=$1

replaceDeclaration() {
original=$1
replacement=$2
file=$3
`sed -i '' -e "s/$original/$replacement/g" $file`
}

files=`find $directory -name "*.swift"`
for f in $files
do
replaceDeclaration 'public internal(set) ' '' $f
replaceDeclaration 'open ' '' $f
replaceDeclaration 'public ' '' $f

# Nuke
if [[ $directory == *"Nuke"* ]]; then
replaceDeclaration 'var log' 'var nukeLog' $f
replaceDeclaration 'log =' 'nukeLog =' $f
replaceDeclaration 'log: log' 'log: nukeLog' $f
replaceDeclaration 'signpost(log' 'signpost(nukeLog' $f
replaceDeclaration ' Cache(' ' NukeCache(' $f
replaceDeclaration ' Cache<' ' NukeCache<' $f
replaceDeclaration ' Image?' ' NukeImage?' $f
replaceDeclaration ' Image(' ' NukeImage(' $f
replaceDeclaration 'struct Image:' 'struct NukeImage:' $f
replaceDeclaration 'extension Image {' 'extension NukeImage {' $f
replaceDeclaration 'Content == Image' 'Content == NukeImage' $f
replaceDeclaration ' VideoPlayerView' ' NukeVideoPlayerView' $f
replaceDeclaration 'typealias Color' 'typealias NukeColor' $f
replaceDeclaration 'extension Color' 'extension NukeColor' $f
replaceDeclaration 'AssetType' 'NukeAssetType' $f
replaceDeclaration 'typealias ImageRequest = Nuke.ImageRequest' '' $f
replaceDeclaration 'typealias ImageResponse = Nuke.ImageResponse' '' $f
replaceDeclaration 'typealias ImagePipeline = Nuke.ImagePipeline' '' $f
replaceDeclaration 'typealias ImageContainer = Nuke.ImageContainer' '' $f
replaceDeclaration 'open class ' '' $f
replaceDeclaration 'import Nuke' '' $f

# Remove Cancellable interface duplicate
if [[ $f == *"DataLoader"* && `head -10 $f` == *"protocol Cancellable"* ]]; then
`sed -i '' -e '7,11d' $f`
fi

# Rename files
if [[ $f == *"Caching/Cache.swift" ]]; then
new_f="${f/Cache.swift/NukeCache.swift}"
mv "$f" "$new_f"
elif [[ $f == *"NukeUI/VideoPlayerView.swift" ]]; then
new_f="${f/VideoPlayerView.swift/NukeVideoPlayerView.swift}"
mv "$f" "$new_f"
fi
fi
done
51 changes: 51 additions & 0 deletions Scripts/removeUnneededSymbols.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash
#
# Usage: ./removeUnneededSymbols.sh StreamChatSwiftUI ./Products
#
# Creating an xcframework for StreamChatSwiftUI generates a .bcsymbolmap file for itself, and one for
# each of its dependencies too (eg. StreamChat). That means that we will end up having something like:
#
# -> StreamChatSwiftUI/BCSymbolMaps/
# <UUID-StreamChatSwiftUI>.bcsymbolmap
# <UUID-StreamChat>.bcsymbolmap
#
# When adding both StreamChat and StreamChatSwiftUI to an app, it will throw an error when trying to compile
# saying that there are multiple executions producing the same file (<UUID-StreamChat>.bcsymbolmap).
#
# This script will remove duplicated .bcsymbolmap in the generated xcframeworks.
# If we countinue with the same example, it will leave it as follows:
#
# -> StreamChatSwiftUI/BCSymbolMaps/
# <UUID-StreamChatSwiftUI>.bcsymbolmap
#
# Each xcframework only contains its symbols now.

args=("$@")
library=$1
output_directory=$2

function removeUnneededSymbols() {
arch=$1
path="$output_directory/$library.xcframework/$arch/BCSymbolMaps"
cd $path

# Looking for [...]/DerivedSources/[LIBRARY-NAME]_vers.c
regex="(\/DerivedSources\/)([a-zA-Z_]*)(_vers.c)"
files="*.bcsymbolmap"
for f in $files
do
text=`head -10 $f`
[[ $text =~ $regex ]]
library_match="${BASH_REMATCH[2]}"
if [[ $library_match != $library ]]
then
echo "→ Removing uneeded 'bcsymbolmap' from $library-$arch: $library_match - $f"
rm $f
fi
done

cd - >/dev/null
}

removeUnneededSymbols "ios-arm64"
removeUnneededSymbols "ios-arm64_x86_64-simulator"
2 changes: 1 addition & 1 deletion Scripts/run-linter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -euo pipefail
echo -e "👉 Running SwiftFormat Linting"

echo -e "👉 Linting Sources..."
mint run swiftformat --lint --config .swiftformat Sources --exclude **/Generated
mint run swiftformat --lint --config .swiftformat Sources --exclude **/Generated,Sources/StreamChatSwiftUI/StreamNuke,Sources/StreamChatSwiftUI/StreamSwiftyGif
echo -e "👉 Linting Tests..."
mint run swiftformat --lint --config .swiftformat StreamChatSwiftUITests
echo -e "👉 Linting DemoApp..."
Expand Down
60 changes: 60 additions & 0 deletions Scripts/updateDependency.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env bash
#
# Usage: ./updateDependency.sh 10.3.3 Dependencies/Nuke Sources/StreamNuke Sources
#
# This script gets the source code of a dependency of a given library, and copies it to our codebase

ensure_clean_git () {
if !(git diff-index --quiet HEAD)
then
echo "→ Seems like git is not clean in $dependency_directory. Please make sure it is clean, and run it again"
exit 1
fi
}

args=("$@")
version=$1
dependency_directory=$2
output_directory=$3
sources_directory=$4

dependency_url=""

if [[ $dependency_directory == *"Nuke"* ]]; then
dependency_url="[email protected]:kean/Nuke.git"
elif [[ $dependency_directory == *"SwiftyGif"* ]]; then
dependency_url="[email protected]:kirualex/SwiftyGif.git"
else
echo "→ Unknown dependency at $dependency_directory"
exit 1
fi

if ! [[ -d "$dependency_directory" ]]; then
echo "→ $dependency_directory does not exist in your filesystem. Cloning the repo"
git clone $dependency_url $dependency_directory
fi

cd $dependency_directory

ensure_clean_git

git fetch --tags
git checkout $version

ensure_clean_git

cd -

echo "→ Copying source files"
rm -rf $output_directory
mkdir $output_directory
cp -r "$dependency_directory/$sources_directory/." $output_directory


for f in `find $output_directory -type f \( -iname \*.h -o -iname \*.plist \)`
do
echo "→ Removing $f"
rm $f
done

rm -rf $dependency_directory
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//

import Combine
import Nuke
import StreamChat
import SwiftUI

Expand Down Expand Up @@ -163,7 +162,7 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {

@objc
private func didReceiveMemoryWarning() {
Nuke.ImageCache.shared.removeAll()
ImageCache.shared.removeAll()
messageCachingUtils.clearCache()
}

Expand Down Expand Up @@ -511,7 +510,7 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
messageCachingUtils.clearCache()
if messageController == nil {
utils.channelControllerFactory.clearCurrentController()
Nuke.ImageCache.shared.trim(toCost: utils.messageListConfig.cacheSizeOnChatDismiss)
ImageCache.shared.trim(toCost: utils.messageListConfig.cacheSizeOnChatDismiss)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ private struct ZoomableScrollViewImpl<Content: View>: UIViewControllerRepresenta
let coordinator: Coordinator
let scrollView = UIScrollView()

var doubleTapCancellable: Cancellable?
var updateConstraintsCancellable: Cancellable?
var doubleTapCancellable: Combine.Cancellable?
var updateConstraintsCancellable: Combine.Cancellable?

private var hostedView: UIView { coordinator.hostingController.view! }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Copyright © 2023 Stream.io Inc. All rights reserved.
//

import Nuke
import NukeUI
import StreamChat
import SwiftUI

Expand Down Expand Up @@ -106,7 +104,7 @@ struct LazyGiphyView: View {
var body: some View {
LazyImage(imageURL: source) { state in
if let imageContainer = state.imageContainer {
Image(imageContainer)
NukeImage(imageContainer)
} else if state.error != nil {
Color(.secondarySystemBackground)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Copyright © 2023 Stream.io Inc. All rights reserved.
//

import Nuke
import NukeUI
import StreamChat
import SwiftUI

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Copyright © 2023 Stream.io Inc. All rights reserved.
//

import Nuke
import NukeUI
import StreamChat
import SwiftUI

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Copyright © 2023 Stream.io Inc. All rights reserved.
//

import NukeUI
import StreamChat
import SwiftUI

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
//

import AVKit
import Nuke
import NukeUI
import StreamChat
import SwiftUI

Expand Down
Loading
Loading