更新gem源,若是系統中沒有安裝gem,請先安裝gem環境ios
$ sudo gem update --system
Latest version already installed. Done.
複製代碼
CocoaPods依賴ruby環境,在項目開始前先查看一下本地ruby環境,通常Mac電腦都自帶了ruby環境。git
$ gem sources -l
*** CURRENT SOURCES ***
https://rubygems.org/
複製代碼
因爲ruby官方源國內被牆,須要修改ruby源。github
$ gem sources --remove https://rubygems.org/
$ gem sources --add https://gems.ruby-china.com/
複製代碼
查看ruby鏡像是否已經切換shell
$ gem sources -l
*** CURRENT SOURCES ***
https://gems.ruby-china.com/
複製代碼
查看本地CocoaPods版本xcode
$ pod --version
1.10.1
複製代碼
若是沒有安裝CocoaPods,先安裝CocoaPods安全
$ sudo gem install -n /usr/local/bin cocoapods
複製代碼
再次查看CocoaPods版本ruby
$ pod --version
1.10.1
複製代碼
工欲善其事必先利其器!bash
建立遠程私有倉庫。因爲示例中倉庫名稱已經存在,Gitee會有相應的錯誤提示,但不影響繼續往下進行。markdown
將遠程倉庫添加至本地pod repo中架構
$ pod repo add ishadoo-specs https://gitee.com/ishadoo/Specs.git
複製代碼
查看本地倉庫
$ pod repo list
ishadoo-specs
- Type: git (master)
- URL: https://gitee.com/ishadoo/Specs.git
- Path: /Users/wangchuanhai/.cocoapods/repos/ishadoo-specs
master
- Type: git (master)
- URL: https://github.com/CocoaPods/Specs.git
- Path: /Users/wangchuanhai/.cocoapods/repos/master
trunk
- Type: CDN
- URL: https://cdn.cocoapods.org/
- Path: /Users/wangchuanhai/.cocoapods/repos/trunk
複製代碼
Gitee倉庫地址:gitee.com/ishadoo/CHM…
初始化項目,將iOS工程與遠程git倉庫進行關聯。
#!/bin/bash
Cyan='\033[0;36m'
Default='\033[0;m'
projectName=""
httpsRepo=""
sshRepo=""
homePage=""
confirmed="n"
getProjectName() {
read -p "Enter Project Name: " projectName
if test -z "$projectName"; then
getProjectName
fi
}
getHTTPSRepo() {
read -p "Enter HTTPS Repo URL: " httpsRepo
if test -z "$httpsRepo"; then
getHTTPSRepo
fi
}
getSSHRepo() {
read -p "Enter SSH Repo URL: " sshRepo
if test -z "$sshRepo"; then
getSSHRepo
fi
}
getHomePage() {
read -p "Enter Home Page URL: " homePage
if test -z "$homePage"; then
getHomePage
fi
}
getInfomation() {
getProjectName
getHTTPSRepo
getSSHRepo
getHomePage
echo -e "\n${Default}================================================"
echo -e " Project Name : ${Cyan}${projectName}${Default}"
echo -e " HTTPS Repo : ${Cyan}${httpsRepo}${Default}"
echo -e " SSH Repo : ${Cyan}${sshRepo}${Default}"
echo -e " Home Page URL : ${Cyan}${homePage}${Default}"
echo -e "================================================\n"
}
echo -e "\n"
while [ "$confirmed" != "y" -a "$confirmed" != "Y" ]
do
if [ "$confirmed" == "n" -o "$confirmed" == "N" ]; then
getInfomation
fi
read -p "confirm? (y/n):" confirmed
done
mkdir -p "../${projectName}/${projectName}"
licenseFilePath="../${projectName}/FILE_LICENSE"
gitignoreFilePath="../${projectName}/.gitignore"
specFilePath="../${projectName}/${projectName}.podspec"
readmeFilePath="../${projectName}/readme.md"
uploadFilePath="../${projectName}/upload.sh"
podfilePath="../${projectName}/Podfile"
echo "copy to $licenseFilePath"
cp -f ./templates/FILE_LICENSE "$licenseFilePath"
echo "copy to $gitignoreFilePath"
cp -f ./templates/gitignore "$gitignoreFilePath"
echo "copy to $specFilePath"
cp -f ./templates/pod.podspec "$specFilePath"
echo "copy to $readmeFilePath"
cp -f ./templates/readme.md "$readmeFilePath"
echo "copy to $uploadFilePath"
cp -f ./templates/upload.sh "$uploadFilePath"
echo "copy to $podfilePath"
cp -f ./templates/Podfile "$podfilePath"
echo "editing..."
sed -i "" "s%__ProjectName__%${projectName}%g" "$gitignoreFilePath"
sed -i "" "s%__ProjectName__%${projectName}%g" "$readmeFilePath"
sed -i "" "s%__ProjectName__%${projectName}%g" "$uploadFilePath"
sed -i "" "s%__ProjectName__%${projectName}%g" "$podfilePath"
sed -i "" "s%__ProjectName__%${projectName}%g" "$specFilePath"
sed -i "" "s%__HomePage__%${homePage}%g" "$specFilePath"
sed -i "" "s%__HTTPSRepo__%${httpsRepo}%g" "$specFilePath"
echo "edit finished"
echo "cleaning..."
cd ../$projectName
git init
git remote add origin $httpsRepo &> /dev/null
git rm -rf --cached ./Pods/ &> /dev/null
git rm --cached Podfile.lock &> /dev/null
git rm --cached .DS_Store &> /dev/null
git rm -rf --cached $projectName.xcworkspace/ &> /dev/null
git rm -rf --cached $projectName.xcodeproj/xcuserdata/`whoami`.xcuserdatad/xcschemes/$projectName.xcscheme &> /dev/null
git rm -rf --cached $projectName.xcodeproj/project.xcworkspace/xcuserdata/ &> /dev/null
git add . &> /dev/null
git commit -m "first commit" &> /dev/null
git push -u origin master &> /dev/null
echo "clean finished"
say "finished"
echo "finished"
複製代碼
提供初始化上傳到CocoaPods倉庫模塊工程的配置文件模板。
該模板提供了最基礎的配置信息。
初始化私有庫模塊的podspec文件,爲後續模塊上傳到私有倉庫初始化基本配置。
Pod::Spec.new do |s|
s.name = "__ProjectName__"
s.version = "1.0.0"
s.summary = "__ProjectName__."
s.description = <<-DESC this is __ProjectName__ DESC
s.homepage = "__HomePage__"
s.license = { :type => "MIT", :file => "FILE_LICENSE" }
s.author = { "王傳海" => "ishadoo@163.com" }
s.platform = :ios, "10.0"
s.source = { :git => "__HTTPSRepo__", :tag => s.version }
s.source_files = "__ProjectName__/__ProjectName__/**/*.{h,m}"
s.requires_arc = true
end
複製代碼
初始項目的Podfile文件
# Uncomment this line to define a global platform for your project
platform :ios, '10.0'
source 'https://gitee.com/ishadoo/Specs.git'
source 'https://github.com/CocoaPods/Specs.git'
target '__ProjectName__' do
end
複製代碼
初始化模塊上傳腳本,須要根據本地repo修改文件中對應的repo名稱和私有倉庫地址
pod repo push ishadoo-specs __ProjectName__.podspec --verbose --allow-warnings --use-libraries --sources='https://gitee.com/ishadoo/Specs.git,https://github.com/CocoaPods/Specs.git'
複製代碼
因爲公司git倉庫的隱私性以及安全性方面的考慮,本教程以gitee倉庫爲例,手摸手教你們如何從零搭建一個iOS模塊化架構。
個人項目路徑是:~/code/private/CHShare.
$ cd ~/code/private/CHShare
$ git clone https://gitee.com/ishadoo/CHModuleConfig.git
複製代碼
遠程倉庫根據本身的項目環境去選擇,教程中我用的是gitee.
工程名要與遠端工程名稱相同,建立時直接複製遠端工程名便可,且路徑與ModuleConfig平級
此時項目目錄結構中多了咱們剛建立的CHShareThird工程
這一步的主要任務是,將本地工程初始化爲由CocoaPods管理的工程。
同時將該項目同步到git遠程倉庫。
終端下cd到CHModuleConfig根目錄,執行初始化配置腳本 config.sh。
~ » cd /Users/wangchuanhai/code/private/CHShare/CHModuleConfig
---------------------------------------------------------------------------------------------------------------------------------
~/code/private/CHShare/CHModuleConfig(master*) » ll
total 32
-rw-r--r-- 1 wangchuanhai staff 1.0K 3 7 14:24 LICENSE
-rw-r--r-- 1 wangchuanhai staff 839B 3 7 14:24 README.en.md
-rw-r--r-- 1 wangchuanhai staff 928B 3 7 14:24 README.md
-rwxr-xr-x 1 wangchuanhai staff 3.2K 3 7 14:04 config.sh
drwxr-xr-x 6 wangchuanhai staff 192B 3 7 14:06 templates
---------------------------------------------------------------------------------------------------------------------------------
~/code/private/CHShare/CHModuleConfig(master*) » ./config.sh
Enter Project Name: CHShareThird
Enter HTTPS Repo URL: https://gitee.com/ishadoo/CHShareThird.git
Enter SSH Repo URL: git@gitee.com:ishadoo/CHShareThird.git
Enter Home Page URL: https://gitee.com/ishadoo/CHShareThird
================================================
Project Name : CHShareThird
HTTPS Repo : https://gitee.com/ishadoo/CHShareThird.git
SSH Repo : git@gitee.com:ishadoo/CHShareThird.git
Home Page URL : https://gitee.com/ishadoo/CHShareThird
================================================
confirm? (y/n):y
copy to ../CHShareThird/FILE_LICENSE
cp: ./templates/FILE_LICENSE: No such file or directory
copy to ../CHShareThird/.gitignore
cp: ./templates/gitignore: No such file or directory
copy to ../CHShareThird/CHShareThird.podspec
copy to ../CHShareThird/readme.md
cp: ./templates/readme.md: No such file or directory
copy to ../CHShareThird/upload.sh
copy to ../CHShareThird/Podfile
editing...
sed: ../CHShareThird/.gitignore: No such file or directory
sed: ../CHShareThird/readme.md: No such file or directory
edit finished
cleaning...
Initialized empty Git repository in /Users/wangchuanhai/code/private/CHShare/CHShareThird/.git/
clean finished
finished
複製代碼
切換到CHShareThird工程,發現多了三個文件
~/code/private/CHShare/CHModuleConfig(master*) » cd ..
---------------------------------------------------------------------------------------------------------------------------------
~/code/private/CHShare » ll
total 0
drwxr-xr-x 9 wangchuanhai staff 288B 3 7 14:25 CHModuleConfig
drwxr-xr-x 18 wangchuanhai staff 576B 3 9 17:36 CHShareDesk
drwxr-xr-x 18 wangchuanhai staff 576B 3 10 00:39 CHShareHome
drwxr-xr-x 16 wangchuanhai staff 512B 3 8 00:25 CHShareMaster
drwxr-xr-x 18 wangchuanhai staff 576B 3 9 15:15 CHShareMine
drwxr-xr-x 8 wangchuanhai staff 256B 3 10 11:43 CHShareThird
---------------------------------------------------------------------------------------------------------------------------------
~/code/private/CHShare » cd CHShareThird
---------------------------------------------------------------------------------------------------------------------------------
~/code/private/CHShare/CHShareThird(master) » ll
total 24
drwxr-xr-x 12 wangchuanhai staff 384B 3 10 11:32 CHShareThird
-rw-r--r-- 1 wangchuanhai staff 643B 3 10 11:43 CHShareThird.podspec
drwxr-xr-x@ 5 wangchuanhai staff 160B 3 10 11:32 CHShareThird.xcodeproj
-rw-r--r-- 1 wangchuanhai staff 213B 3 10 11:43 Podfile
-rw-r--r-- 1 wangchuanhai staff 178B 3 10 11:43 upload.sh
---------------------------------------------------------------------------------------------------------------------------------
複製代碼
cd 到項目根目錄,執行pod install
$ pod install
複製代碼
模塊化工程的源碼以SDK的形式提供給宿主工程(也就是模塊工程自己)和主App以及其餘須要依賴的工程。
選擇初始化好的工程,用Xcode打開,File -> New -> Target -> Framework
Product Name: ${projectName} + SDK
Resource bundle做爲單獨的Target爲SDK提供靜態資源等的支持。
File -> New -> Target -> 選擇macOS模塊下的Bundle。因爲Xcode只支持在macOS下建立bundle,故選擇macOS模塊下的Bundle選項。
Product Name: ${projectName} + Bundle
在剛建立好的CHShareThirdBundle文件夾下新建Assets.xcassets文件,做爲圖片等靜態資源的容器。
另外,要將CHShareThirdBundle Targets的Base SDK屬性修改爲iOS支持。
需特別注意的,resource bundle這個SDK中須要將info.plist文件中的Executable file選項移除,否則會出現獲取不到文件的狀況。
將編譯好的framework文件和bundle文件從模擬器的沙盒目錄拷貝至工程的根路徑,以方便CocoaPods上傳到私有倉庫。
File -> New -> Target -> 選擇Other下的Aggregate。
添加執行腳本
#!/bin/sh
#要build的target名
TARGET_NAME=${PROJECT_NAME}
if [[ $1 ]]
then
TARGET_NAME=$1
fi
UNIVERSAL_OUTPUT_FOLDER="${SRCROOT}/${PROJECT_NAME}Upload/"
#建立輸出目錄,並刪除以前的framework文件
rm -rf "${UNIVERSAL_OUTPUT_FOLDER}"
mkdir -p "${UNIVERSAL_OUTPUT_FOLDER}"
#編譯模擬器的Framework
xcodebuild -target "${TARGET_NAME}SDK" ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphonesimulator BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build
#拷貝framework到univer目錄
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${TARGET_NAME}SDK.framework" "${UNIVERSAL_OUTPUT_FOLDER}"
#拷貝bundle到univer目錄
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${TARGET_NAME}Bundle.bundle" "${UNIVERSAL_OUTPUT_FOLDER}"
#打開合併後的文件夾
open "${UNIVERSAL_OUTPUT_FOLDER}"
複製代碼
到此一個模塊的基本配置工做就算完成,接下來須要調整一下資源的依賴關係,從系統層面不難理解,Demo工程(也就是模塊項目自己)依賴SDK工程,SDK依賴Bundle資源。
接下來,就能夠開開心心地擼代碼了。
修改version版本號,要與最終提交到git上的代碼tag保持一致
增長vendored_frameworks指向工程根目錄中script腳本拷貝的framework和bundle資源
添加工程中所依賴的第三方庫和系統庫等
Pod::Spec.new do |s|
s.name = "CHShareThird"
s.version = "1.0.20210310"
s.summary = "CHShareThird."
s.description = <<-DESC
this is CHShareThird
DESC
s.homepage = "https://gitee.com/ishadoo/CHShareThird"
s.license = { :type => "MIT", :file => "FILE_LICENSE" }
s.author = { "王傳海" => "ishadoo@163.com" }
s.platform = :ios, "10.0"
s.source = { :git => "https://gitee.com/ishadoo/CHShareThird.git", :tag => s.version }
## 源碼形式集成
# s.source_files = "CHShareThird/CHShareThird/**/*.{h,m}"
## 是否支持ARC
s.requires_arc = true
## 構建的模塊類型
s.vendored_frameworks = "CHShareThirdUpload/CHShareThirdSDK.framework"
s.resources = "CHShareThirdUpload/CHShareThirdBundle.bundle"
## 依賴的第三方庫以及framework資源
s.dependency "Masonry"
s.framework = "UIKit"
end
複製代碼
以下圖所示,項目中添加一個UIViewController做爲NavigationControler根視圖,現將該模塊封板上傳到CocoaPods私有庫。
編譯經過後,先執行framework 拷貝腳本,而後執行該腳本構建到遠端CocoaPods庫.
腳本執行後在項目的根目錄多了一個CHShareThirdUpload文件夾,其中包含CHShareThirdSDK.framework和CHShareThirdBundle.bundle這兩個文件
代碼封板,將代碼上傳到遠程倉庫,同時封版打tag,tag號要與CHShareThird.podspec中的version相一致。
此時準備工做完成。
執行upload.sh
~/code/private/CHShare/CHShareThird(master) » ll
total 56
drwxr-xr-x 8 wangchuanhai staff 256B 3 10 16:39 CHShareThird
-rw-r--r-- 1 wangchuanhai staff 979B 3 10 17:13 CHShareThird.podspec
drwxr-xr-x@ 5 wangchuanhai staff 160B 3 10 16:41 CHShareThird.xcodeproj
drwxr-xr-x@ 5 wangchuanhai staff 160B 3 10 15:05 CHShareThird.xcworkspace
drwxr-xr-x 4 wangchuanhai staff 128B 3 10 15:29 CHShareThirdBundle
drwxr-xr-x 6 wangchuanhai staff 192B 3 10 16:25 CHShareThirdSDK
-rw-r--r--@ 1 wangchuanhai staff 1.1K 2 24 15:18 FILE_LICENSE
-rw-r--r--@ 1 wangchuanhai staff 232B 3 10 16:33 Podfile
-rw-r--r-- 1 wangchuanhai staff 270B 3 10 16:34 Podfile.lock
drwxr-xr-x 8 wangchuanhai staff 256B 3 10 16:34 Pods
-rw-r--r-- 1 wangchuanhai staff 956B 3 10 14:53 README.en.md
-rw-r--r-- 1 wangchuanhai staff 1.3K 3 10 14:53 README.md
-rw-r--r-- 1 wangchuanhai staff 178B 3 10 14:50 upload.sh
複製代碼
執行腳本有時會出現執行權限問題,此時須要對upload.sh腳本受權
~/code/private/CHShare/CHShareThird(master) » ./upload.sh
zsh: permission denied: ./upload.sh
複製代碼
受權
~/code/private/CHShare/CHShareThird(master) » chmod +x upload.sh
複製代碼
再次執行腳本
~/code/private/CHShare/CHShareThird(master*) » ./upload.sh
複製代碼
通過漫長的編譯,會收到成功上傳的信息,部分編譯信息以下,
** BUILD SUCCEEDED **
Testing with `xcodebuild`.
-> CHShareThird (1.0.2021031001)
- NOTE | xcodebuild: note: Using new build system
- NOTE | xcodebuild: note: Building targets in parallel
- NOTE | xcodebuild: note: Using codesigning identity override: -
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Constructing build description
- NOTE | [iOS] xcodebuild: warning: The iOS Simulator deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 6.0, but the range of supported deployment target versions is 9.0 to 14.4.99. (in target 'Masonry' from project 'Pods')
- NOTE | [iOS] xcodebuild: warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
- NOTE | [iOS] xcodebuild: ld: warning: ignoring file CHShareThird/CHShareThirdUpload/CHShareThirdSDK.framework/CHShareThirdSDK, building for iOS Simulator-i386 but attempting to link with file built for iOS Simulator-x86_64
- NOTE | [iOS] xcodebuild: ld: warning: ignoring file CHShareThird/CHShareThirdUpload/CHShareThirdSDK.framework/CHShareThirdSDK, building for iOS Simulator-arm64 but attempting to link with file built for iOS Simulator-x86_64
Updating the `ishadoo-specs' repo
$ /usr/bin/git -C /Users/wangchuanhai/.cocoapods/repos/ishadoo-specs pull
Already up to date.
Adding the spec to the `ishadoo-specs' repo
$ /usr/bin/git -C /Users/wangchuanhai/.cocoapods/repos/ishadoo-specs status --porcelain
?? CHShareThird/
- [Add] CHShareThird (1.0.2021031001)
$ /usr/bin/git -C /Users/wangchuanhai/.cocoapods/repos/ishadoo-specs add CHShareThird
$ /usr/bin/git -C /Users/wangchuanhai/.cocoapods/repos/ishadoo-specs commit --no-verify -m [Add] CHShareThird (1.0.2021031001)
[master 95fe617] [Add] CHShareThird (1.0.2021031001)
1 file changed, 29 insertions(+)
create mode 100644 CHShareThird/1.0.2021031001/CHShareThird.podspec
Pushing the `ishadoo-specs' repo
$ /usr/bin/git -C /Users/wangchuanhai/.cocoapods/repos/ishadoo-specs push origin HEAD
remote: Powered by GITEE.COM [GNK-5.0]
To https://gitee.com/ishadoo/Specs.git
468cfbb..95fe617 HEAD -> master
複製代碼
至此,CHShareThird模塊的第一個版本就成功上傳到Cocoapods私有倉庫。
跟蹤一下遠程倉庫,不難發現新建的CHShareThird模塊已經成功上傳咱們的私有倉庫。
在該主App的Podflie文件中添加以下依賴 pod 'CHShareThird'
# Uncomment this line to define a global platform for your project
platform :ios, '10.0'
source 'https://gitee.com/ishadoo/Specs.git'
source 'https://github.com/CocoaPods/Specs.git'
source 'https://gitee.com/ishadoo/CHShareMine.git'
target 'CHShareMaster' do
pod 'CHShareHome'
pod 'CHShareMine', :git => 'https://gitee.com/ishadoo/CHShareMine.git', :branch => 'develop'
pod 'CHShareDesk'
pod 'CHShareThird'
end
target 'CHShareSDK' do
pod 'CHShareHome'
pod 'CHShareMine', :git => 'https://gitee.com/ishadoo/CHShareMine.git', :branch => 'develop'
pod 'CHShareDesk'
pod 'CHShareThird'
end
複製代碼
~/code/private/CHShare/CHShareMaster(develop*) » pod update
Update all pods
Updating local specs repositories
$ /usr/bin/git -C /Users/wangchuanhai/.cocoapods/repos/ishadoo-specs fetch origin --progress
$ /usr/bin/git -C /Users/wangchuanhai/.cocoapods/repos/ishadoo-specs rev-parse --abbrev-ref HEAD
master
$ /usr/bin/git -C /Users/wangchuanhai/.cocoapods/repos/ishadoo-specs reset --hard origin/master
HEAD is now at 95fe617 [Add] CHShareThird (1.0.2021031001)
$ /usr/bin/git -C /Users/wangchuanhai/.cocoapods/repos/master fetch origin --progress
$ /usr/bin/git -C /Users/wangchuanhai/.cocoapods/repos/master rev-parse --abbrev-ref HEAD
master
$ /usr/bin/git -C /Users/wangchuanhai/.cocoapods/repos/master reset --hard origin/master
HEAD is now at 5b4b6eecd2f8 [Add] TCNetwork 0.2.2
$ /usr/bin/git -C /Users/wangchuanhai/.cocoapods/repos/gitee-ishadoo-chsharemine fetch origin --progress
$ /usr/bin/git -C /Users/wangchuanhai/.cocoapods/repos/gitee-ishadoo-chsharemine rev-parse --abbrev-ref HEAD
master
$ /usr/bin/git -C /Users/wangchuanhai/.cocoapods/repos/gitee-ishadoo-chsharemine reset --hard origin/master
HEAD is now at ec3e546 update
Analyzing dependencies
Pre-downloading: `CHShareMine` from `https://gitee.com/ishadoo/CHShareMine.git`, branch `develop`
Downloading dependencies
Installing CHShareMine 1.0.2021030901
Installing CHShareThird (1.0.2021031001)
Generating Pods project
Integrating client project
Pod installation complete! There are 4 dependencies from the Podfile and 5 total pods installed.
複製代碼
此時CHShareThird成功地添加到了主App當中。