Skip to content

Commit f4cf832

Browse files
committed
Improved renaming flow
1 parent 3d2c6c3 commit f4cf832

2 files changed

Lines changed: 67 additions & 20 deletions

File tree

fastlane/Definitionsfile

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,36 @@ end
3333

3434
##### Rename #####
3535

36+
def ensure_project_was_renamed
37+
if File.exist?('../Template.xcodeproj')
38+
UI.error("Rename the project first: bundle exec fastlane rename new_name:YourAppName")
39+
raise "BLOCKED: Project still named 'Template'"
40+
end
41+
end
42+
3643
def rename_project(oldName, newName)
44+
UI.header('Step: rename_project')
45+
46+
# Remove spaces from new name to avoid issues with file paths and bundle identifiers
47+
newName = newName.gsub(/\s+/, '')
48+
UI.message("Renaming project from '#{oldName}' to '#{newName}'")
49+
3750
# Rename folders and files
51+
# .github/workflows
52+
# sh("sed -i '' 's|#{oldName}|#{newName}|g' ../.github/workflows/build.yml")
53+
# sh("sed -i '' 's|#{oldName}|#{newName}|g' ../.github/workflows/releasing.yml")
54+
55+
# Fastlane
56+
sh("sed -i '' 's|#{oldName}|#{newName}|g' ../fastlane/.env")
57+
sh("sed -i '' 's|#{oldName.downcase}|#{newName.downcase}|g' ../fastlane/.env")
58+
sh("sed -i '' 's|#{oldName}|#{newName}|g' ../fastlane/.env.prd")
59+
sh("sed -i '' 's|#{oldName}|#{newName}|g' ../fastlane/.env.stg")
60+
sh("sed -i '' 's|#{oldName}/Assets|#{newName}/Assets|g' ../fastlane/Definitionsfile")
61+
# sh("sed -i '' 's|#{oldName.downcase}|#{newName.downcase}|g' ../fastlane/Deliverfile")
62+
sh("sed -i '' 's|#{oldName}|#{newName}|g' ../fastlane/Fastfile")
63+
# sh("sed -i '' 's|#{oldName.downcase}|#{newName.downcase}|g' ../fastlane/Matchfile")
64+
sh("sed -i '' 's|#{oldName}|#{newName}|g' ../fastlane/Setupfile")
65+
3866
# Project file
3967
sh("mv ../#{oldName}.xcodeproj/xcshareddata/xcschemes/#{oldName}.xcscheme ../#{oldName}.xcodeproj/xcshareddata/xcschemes/#{newName}.xcscheme")
4068
sh("mv ../#{oldName}.xcodeproj ../#{newName}.xcodeproj")
@@ -56,18 +84,32 @@ def rename_project(oldName, newName)
5684
sh("mv ../#{newName}UITests/#{oldName}UITestsLaunchTests.swift ../#{newName}UITests/#{newName}UITestsLaunchTests.swift")
5785
sh("rm -rf ../#{oldName}UITests")
5886

59-
# Search and Replace occurences of <oldName>
60-
sh("sed -i '' 's/#{oldName}.xcodeproj/#{newName}.xcodeproj/g' ../README.md")
61-
sh("sed -i '' 's/#{oldName}/#{newName}/g' ../fastlane/Fastfile")
62-
sh("sed -i '' 's/#{oldName}/#{newName}/g' ../fastlane/.env.stg")
63-
sh("sed -i '' 's/#{oldName}/#{newName}/g' ../fastlane/.env.prd")
64-
sh("sed -i '' 's/#{oldName}.xcodeproj/#{newName}.xcodeproj/g' ../hooks/pre-commit")
65-
sh("sed -i '' 's/#{oldName}/#{newName}/g' ../scripts/generate-swiftlint-filelist.sh")
66-
sh("sed -i '' 's/#{oldName}/#{newName}/g' ../#{newName}.xcodeproj/project.pbxproj")
67-
sh("sed -i '' 's/#{oldName}/#{newName}/g' ../#{newName}.xcodeproj/xcshareddata/xcschemes/#{newName}.xcscheme")
87+
# Search and Replace occurrences of <oldName>
88+
sh("sed -i '' 's|#{oldName}.xcodeproj|#{newName}.xcodeproj|g' ../README.md")
89+
sh("sed -i '' 's|#{oldName}.xcodeproj|#{newName}.xcodeproj|g' ../hooks/pre-commit")
90+
sh("sed -i '' 's|#{oldName}|#{newName}|g' ../#{newName}/Info.plist")
91+
sh("sed -i '' 's|#{oldName}|#{newName}|g' ../scripts/generate-swiftlint-filelist.sh")
92+
sh("sed -i '' 's|#{oldName}|#{newName}|g' ../#{newName}.xcodeproj/project.pbxproj")
93+
sh("sed -i '' 's|#{oldName}|#{newName}|g' ../#{newName}.xcodeproj/xcshareddata/xcschemes/#{newName}.xcscheme")
6894

69-
sh("find ../#{newName}* -name '*.swift' -print0 | xargs -0 sed -i '' 's/#{oldName}/#{newName}/g'")
95+
sh("find ../#{newName}* -name '*.swift' -print0 | xargs -0 sed -i '' 's|#{oldName}|#{newName}|g'")
7096

7197
# Remove build phases results to prevent compile issues with SwiftLint file lists
7298
sh("rm -rf ../.build/build_phases")
73-
end
99+
end
100+
101+
def update_marketing_version(new_name)
102+
UI.header('Step: update_marketing_version')
103+
104+
# Set the default version number to <year>.<week>.0 using ISO week number whenever the project is renamed.
105+
current_date = Time.new
106+
year = current_date.strftime("%G")
107+
week = current_date.strftime("%-V")
108+
patch = 0
109+
version_number = "#{year}.#{week}.#{patch}"
110+
111+
# Replace MARKETING_VERSION = 1.0; with the computed version in the project file
112+
project_file = File.expand_path("../#{new_name}.xcodeproj/project.pbxproj")
113+
UI.message("Setting MARKETING_VERSION to `#{version_number}` in `#{project_file}`")
114+
sh("sed -i '' -E 's/(MARKETING_VERSION = )1\\.0;/\\1#{version_number};/g' \"#{project_file}\"")
115+
end

fastlane/Setupfile

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ platform :ios do
9797
desc ' * **`ssh`**: Set to `true`, if you are currently not using SSH. Defaults to `false`'
9898
desc ' * **`clean`**: Set to `true`, if you want to clean all provisioning profiles. Defaults to `false`'
9999
lane :setup do |options|
100+
ensure_project_was_renamed
101+
100102
setup_git
101103

102104
if options[:ssh] || false
@@ -109,15 +111,6 @@ platform :ios do
109111
clean_provisioning_profiles(clean)
110112
end
111113

112-
desc 'Renames the project including all occurences of `Template` to a new name of your choice.'
113-
desc '#### Example:'
114-
desc "```\nbundle exec fastlane rename new_name:Tahdith\n```"
115-
desc '#### Options:'
116-
desc ' * **`new_name`**: New project name of your choice.'
117-
lane :rename do |options|
118-
rename_project("Template", options[:new_name])
119-
end
120-
121114
desc 'Updates the status bar of all booted simulators.'
122115
desc '#### Example:'
123116
desc "```\nbundle exec fastlane simulator\n```"
@@ -129,4 +122,16 @@ platform :ios do
129122
sh("xcrun simctl status_bar \"#{device}\" override --time 9:41 --dataNetwork wifi --wifiMode active --wifiBars 3 --operatorName 'Hoppsen' --cellularMode active --cellularBars 4 --batteryState charged --batteryLevel 100")
130123
end
131124
end
125+
126+
desc 'Renames the project including all occurences of `Template` to a new name of your choice.'
127+
desc '#### Example:'
128+
desc "```\nbundle exec fastlane rename new_name:Tahdith\n```"
129+
desc '#### Options:'
130+
desc ' * **`new_name`**: New project name of your choice.'
131+
lane :rename do |options|
132+
new_name = options[:new_name]
133+
rename_project("Template", new_name)
134+
135+
update_marketing_version(new_name)
136+
end
132137
end

0 commit comments

Comments
 (0)