Skip to content

Commit

Permalink
[#494] Add renaming to SetUpiOSProject.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
blyscuit committed Jul 20, 2023
1 parent e15c14c commit b139359
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 194 deletions.
22 changes: 22 additions & 0 deletions Scripts/Swift/Extensions/FileManager+Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ extension FileManager {
}
}

func copy(file: String, to destination: String) {
let currentDirectory = currentDirectoryPath
do {
try copyItem(
atPath: "\(currentDirectory)/\(file)",
toPath:"\(currentDirectory)/\(destination)"
)
} catch {
print("Error \(error)")
}
}

func createFile(name: String, at directory: String) {
let currentDirectory = currentDirectoryPath
do {
try createDirectory(atPath: "\(currentDirectory)/\(directory)", withIntermediateDirectories: true, attributes: nil)
} catch {
print("Error \(error)")
}
createFile(atPath: "\(currentDirectory)\(directory)\(name)", contents: nil)
}

func removeItems(in directory: String) {
let currentDirectory = currentDirectoryPath
do {
Expand Down
158 changes: 81 additions & 77 deletions Scripts/Swift/SetUpiOSProject.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#!/usr/bin/swift

let CONSTANT_PROJECT_NAME = "TemplateApp"
let CONSTANT_BUNDLE_PRODUCTION = "co.nimblehq.ios.templates"
let CONSTANT_BUNDLE_STAGING = "co.nimblehq.ios.templates.staging"
let CONSTANT_MINIMUM_VERSION = ""
let CONSTANT_PROJECT_NAME = "{PROJECT_NAME}"
let CONSTANT_BUNDLE_PRODUCTION = "{BUNDLE_ID_PRODUCTION}"
let CONSTANT_BUNDLE_STAGING = "{BUNDLE_ID_STAGING}"
let CONSTANT_MINIMUM_VERSION = "{TARGET_VERSION}"

var bundleIdProduction = ""
var bundleIdStaging = ""
var projectName = ""
var minimumVersion = ""
var interface: SetUpInterface.Interface?

var isCI = !(ProcessInfo.processInfo.environment["CI"] ?? "").isEmpty

// TODO: Should be replaced with ArgumentParser instead of command line
for (index, argument) in CommandLine.arguments.enumerated() {
switch index {
Expand All @@ -23,6 +25,10 @@ for (index, argument) in CommandLine.arguments.enumerated() {
}
}

if isCI {
minimumVersion = "14.0"
}

func checkPackageName(_ name: String) -> Bool {
let packageNameRegex="^[a-z][a-z0-9_]*(\\.[a-z0-9_-]+)+[0-9a-z_-]$"
let valid = name ~= packageNameRegex
Expand Down Expand Up @@ -61,104 +67,102 @@ while interface == nil {
print("Interface [(S)wiftUI or (U)IKit]:")
interface = SetUpInterface.Interface(readLine() ?? "")
}
let projectNameNoSpace = projectName.trimmingCharacters(in: .whitespacesAndNewlines)

// Select the Interface
SetUpInterface().perform(interface ?? .uiKit, projectName)

print("=> 🐢 Starting init \(projectName) ...")

print("=> 🔎 Replacing files structure...")

/*
# Rename files structure
echo "=> 🔎 Replacing files structure..."
## user define function
rename_folder(){
local DIR=$1
local NEW_DIR=$2
if [ -d "$DIR" ]
then
mv ${DIR} ${NEW_DIR}
fi
}
let fileManager = FileManager.default

# Rename test folder structure
rename_folder "${CONSTANT_PROJECT_NAME}Tests" "${PROJECT_NAME_NO_SPACES}Tests"
// Rename test folder structure
fileManager.rename(file: "\(CONSTANT_PROJECT_NAME)Tests", to: "\(projectNameNoSpace)Tests")

# Rename KIF UI Test folder structure
rename_folder "${CONSTANT_PROJECT_NAME}KIFUITests" "${PROJECT_NAME_NO_SPACES}KIFUITests"
// Rename KIF UI Test folder structure
fileManager.rename(file: "\(CONSTANT_PROJECT_NAME)KIFUITests", to: "\(projectNameNoSpace)KIFUITests")

# Rename app folder structure
rename_folder "${CONSTANT_PROJECT_NAME}" "${PROJECT_NAME_NO_SPACES}"
// Rename app folder structure
fileManager.rename(file: "\(CONSTANT_PROJECT_NAME)", to: "\(projectNameNoSpace)")

# Duplicate the env example file and rename it to env file
cp "./.env.example" "./.env"
// Duplicate the env example file to env file
fileManager.copy(file: ".env.example", to: ".env")

# Add AutoMockable.generated.swift file
mkdir -p "${PROJECT_NAME_NO_SPACES}Tests/Sources/Mocks/Sourcery"
touch "${PROJECT_NAME_NO_SPACES}Tests/Sources/Mocks/Sourcery/AutoMockable.generated.swift"
// Add AutoMockable.generated.swift file
fileManager.createFile(name: "AutoMockable.generated.swift", at: "\(projectNameNoSpace)Tests/Sources/Mocks/Sourcery")

# Add R.generated.swift file
mkdir -p "${PROJECT_NAME_NO_SPACES}/Sources/Supports/Helpers/Rswift"
touch "${PROJECT_NAME_NO_SPACES}/Sources/Supports/Helpers/Rswift/R.generated.swift"
// Add AutoMockable.generated.swift file
fileManager.createFile(name: "R.generated.swift", at: "\(projectNameNoSpace)/Sources/Supports/Helpers/Rswift")

echo "✅ Completed"
print("✅ Completed")

# Search and replace in files
echo "=> 🔎 Replacing package and package name within files..."
BUNDLE_ID_PRODUCTION_ESCAPED="${bundle_id_production//./\.}"
BUNDLE_ID_STAGING_ESCAPED="${bundle_id_staging//./\.}"
LC_ALL=C find $WORKING_DIR -type f -exec sed -i "" "s/$CONSTANT_BUNDLE_STAGING/$BUNDLE_ID_STAGING_ESCAPED/g" {} +
LC_ALL=C find $WORKING_DIR -type f -exec sed -i "" "s/$CONSTANT_BUNDLE_PRODUCTION/$BUNDLE_ID_PRODUCTION_ESCAPED/g" {} +
LC_ALL=C find $WORKING_DIR -type f -exec sed -i "" "s/$CONSTANT_PROJECT_NAME/$PROJECT_NAME_NO_SPACES/g" {} +
LC_ALL=C find $WORKING_DIR -type f -exec sed -i "" "s/$CONSTANT_MINIMUM_VERSION/$minimum_version/g" {} +
echo "✅ Completed"
// Search and replace in files

# check for tuist and install
if ! command -v tuist &> /dev/null
then
echo "Tuist could not be found"
echo "Installing tuist"
readonly TUIST_VERSION=`cat .tuist-version`
curl -Ls https://install.tuist.io | bash
tuist install ${TUIST_VERSION}
fi
print("=> 🔎 Replacing package and package name within files...")

# Generate with tuist
echo "Tuist found"
tuist generate --no-open
echo "✅ Completed"
try fileManager.replaceAllOccurrences(of: CONSTANT_BUNDLE_STAGING, to: bundleIdStaging)
try fileManager.replaceAllOccurrences(of: CONSTANT_BUNDLE_PRODUCTION, to: bundleIdProduction)
try fileManager.replaceAllOccurrences(of: CONSTANT_PROJECT_NAME, to: projectNameNoSpace)
try fileManager.replaceAllOccurrences(of: CONSTANT_MINIMUM_VERSION, to: minimumVersion)
print("✅ Completed")

# Install dependencies
echo "Installing gems"
bundle install
// check for tuist and install
let tuistLocation = try safeShell("command -v tuist")
if let tuistLocation, tuistLocation.isEmpty {
print("Tuist could not be found")
print("Installing tuist")
try safeShell(
"""
readonly TUIST_VERSION=`cat .tuist-version`
curl -Ls https://install.tuist.io | bash
tuist install ${TUIST_VERSION}
"""
)
}

echo "Run Arkana"
bundle exec arkana
// Generate with tuist
try safeShell("tuist generate --no-open")
print("✅ Completed")

// Install dependencies
print("Installing gems")
try safeShell("bundle install")

// Install dependencies
print("Run Arkana")
try safeShell("bundle exec arkana")

print("Installing pod dependencies")
try safeShell("bundle exec pod install --repo-update")
print("✅ Completed")

/*
echo "Installing pod dependencies"
bundle exec pod install --repo-update
echo "✅ Completed"
# Remove gitkeep files
echo "Remove gitkeep files from project"
sed -i "" "s/.*\(gitkeep\).*,//" $PROJECT_NAME_NO_SPACES.xcodeproj/project.pbxproj
echo "✅ Completed"
echo "✅ Complete"
# Remove Tuist files
echo "Remove tuist files"
rm -rf .tuist-version
rm -rf tuist
rm -rf Project.swift
rm -rf Workspace.swift
# Remove script files and git/index
echo "Remove script files and git/index"
rm -rf make.sh
rm -rf .github/workflows/test_install_script.yml
rm -f .git/index
git reset
*/

// Remove Tuist files
print("Remove tuist files")
fileManager.removeItems(in: ".tuist-version")
fileManager.removeItems(in: "tuist")
fileManager.removeItems(in: "Project.swift")
fileManager.removeItems(in: "Workspace.swift")

// Remove script files and git/index
print("Remove script files and git/index")
fileManager.removeItems(in: "make.sh")
fileManager.removeItems(in: ".github/workflows/test_install_script.yml")
fileManager.removeItems(in: ".git/index")
try safeShell("git reset")

/*
if [[ -z "${CI}" ]]; then
rm -rf fastlane/Tests
Expand Down
121 changes: 4 additions & 117 deletions make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ project_name=""
minimum_version=""
interface=""

readonly CONSTANT_PROJECT_NAME="TemplateApp"
readonly CONSTANT_BUNDLE_PRODUCTION="co.nimblehq.ios.templates"
readonly CONSTANT_BUNDLE_STAGING="co.nimblehq.ios.templates.staging"
readonly CONSTANT_MINIMUM_VERSION=""
readonly CONSTANT_PROJECT_NAME="{PROJECT_NAME}"
readonly CONSTANT_BUNDLE_PRODUCTION="{BUNDLE_ID_PRODUCTION}"
readonly CONSTANT_BUNDLE_STAGING="{BUNDLE_ID_STAGING}"
readonly CONSTANT_MINIMUM_VERSION="{TARGET_VERSION}"

while [ $# -gt 0 ] ; do
case "$1" in
Expand Down Expand Up @@ -72,116 +72,3 @@ while [ $# -gt 0 ] ; do
done

cat Scripts/Swift/SetUpiOSProject.swift Scripts/Swift/SetUpInterface.swift Scripts/Swift/Extensions/FileManager+Utils.swift Scripts/Swift/Extensions/String+Utils.swift Scripts/Swift/Helpers/SafeShell.swift > t.swift && swift t.swift $bundle_id_production $bundle_id_staging $project_name $minimum_version $interface && rm -rf 't.swift'

# Trim spaces in APP_NAME
readonly PROJECT_NAME_NO_SPACES=$(echo "$project_name" | sed "s/ //g")

# Rename files structure
echo "=> 🔎 Replacing files structure..."


## user define function
rename_folder(){
local DIR=$1
local NEW_DIR=$2
if [ -d "$DIR" ]
then
mv ${DIR} ${NEW_DIR}
fi
}

# Rename test folder structure
rename_folder "${CONSTANT_PROJECT_NAME}Tests" "${PROJECT_NAME_NO_SPACES}Tests"

# Rename KIF UI Test folder structure
rename_folder "${CONSTANT_PROJECT_NAME}KIFUITests" "${PROJECT_NAME_NO_SPACES}KIFUITests"

# Rename app folder structure
rename_folder "${CONSTANT_PROJECT_NAME}" "${PROJECT_NAME_NO_SPACES}"

# Duplicate the env example file and rename it to env file
cp "./.env.example" "./.env"

# Add AutoMockable.generated.swift file
mkdir -p "${PROJECT_NAME_NO_SPACES}Tests/Sources/Mocks/Sourcery"
touch "${PROJECT_NAME_NO_SPACES}Tests/Sources/Mocks/Sourcery/AutoMockable.generated.swift"

# Add R.generated.swift file
mkdir -p "${PROJECT_NAME_NO_SPACES}/Sources/Supports/Helpers/Rswift"
touch "${PROJECT_NAME_NO_SPACES}/Sources/Supports/Helpers/Rswift/R.generated.swift"

echo "✅ Completed"

# Search and replace in files
echo "=> 🔎 Replacing package and package name within files..."
BUNDLE_ID_PRODUCTION_ESCAPED="${bundle_id_production//./\.}"
BUNDLE_ID_STAGING_ESCAPED="${bundle_id_staging//./\.}"
LC_ALL=C find $WORKING_DIR -type f -exec sed -i "" "s/$CONSTANT_BUNDLE_STAGING/$BUNDLE_ID_STAGING_ESCAPED/g" {} +
LC_ALL=C find $WORKING_DIR -type f -exec sed -i "" "s/$CONSTANT_BUNDLE_PRODUCTION/$BUNDLE_ID_PRODUCTION_ESCAPED/g" {} +
LC_ALL=C find $WORKING_DIR -type f -exec sed -i "" "s/$CONSTANT_PROJECT_NAME/$PROJECT_NAME_NO_SPACES/g" {} +
LC_ALL=C find $WORKING_DIR -type f -exec sed -i "" "s/$CONSTANT_MINIMUM_VERSION/$minimum_version/g" {} +
echo "✅ Completed"

# check for tuist and install
if ! command -v tuist &> /dev/null
then
echo "Tuist could not be found"
echo "Installing tuist"
readonly TUIST_VERSION=`cat .tuist-version`
curl -Ls https://install.tuist.io | bash
tuist install ${TUIST_VERSION}
fi

# Generate with tuist
echo "Tuist found"
tuist generate --no-open
echo "✅ Completed"

# Install dependencies
echo "Installing gems"
bundle install

echo "Run Arkana"
bundle exec arkana

echo "Installing pod dependencies"
bundle exec pod install --repo-update
echo "✅ Completed"

# Remove gitkeep files
echo "Remove gitkeep files from project"
sed -i "" "s/.*\(gitkeep\).*,//" $PROJECT_NAME_NO_SPACES.xcodeproj/project.pbxproj
echo "✅ Completed"

# Remove Tuist files
echo "Remove tuist files"
rm -rf .tuist-version
rm -rf tuist
rm -rf Project.swift
rm -rf Workspace.swift

# Remove script files and git/index
echo "Remove script files and git/index"
rm -rf make.sh
rm -rf .github/workflows/test_install_script.yml
rm -f .git/index
git reset

if [[ -z "${CI}" ]]; then
rm -rf fastlane/Tests
rm -f set_up_test_testflight.sh
cat Scripts/Swift/SetUpCICDService.swift Scripts/Swift/Extensions/FileManager+Utils.swift Scripts/Swift/Helpers/SafeShell.swift > t.swift && swift t.swift && rm -rf 't.swift'
cat Scripts/Swift/SetUpDeliveryConstants.swift Scripts/Swift/Extensions/FileManager+Utils.swift Scripts/Swift/Helpers/SafeShell.swift > t.swift && swift t.swift && rm -rf 't.swift'
rm -rf Scripts
fi


echo "✅ Completed"

# Done!
echo "=> 🚀 Done! App is ready to be tested 🙌"

if [[ -z "${CI}" ]]; then
echo "=> 🛠 Opening the project."
open -a Xcode $PROJECT_NAME_NO_SPACES.xcworkspace
fi

0 comments on commit b139359

Please sign in to comment.