Skip to content

Commit

Permalink
Merge pull request #504 from nimblehq/chore/494-make-sh-swift-command
Browse files Browse the repository at this point in the history
[#494] Migrate make.sh to Swift command
  • Loading branch information
blyscuit authored Aug 22, 2023
2 parents 82fd58b + f94af58 commit 00e6deb
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 248 deletions.
26 changes: 25 additions & 1 deletion 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 Expand Up @@ -63,9 +85,11 @@ extension FileManager {
if let enumerator = enumerator(
at: url,
includingPropertiesForKeys: [.isRegularFileKey],
options: [.skipsHiddenFiles, .skipsPackageDescendants]
options: [.skipsPackageDescendants]
) {
let hiddenFolderRegex = "\(directory.replacingOccurrences(of: "/", with: "\\/"))\\/\\..*\\/"
for case let fileURL as URL in enumerator {
guard !(fileURL.relativePath ~= hiddenFolderRegex) else { continue }
let fileAttributes = try? fileURL.resourceValues(forKeys:[.isRegularFileKey])
guard fileAttributes?.isRegularFile ?? false else { continue }
files.append(fileURL)
Expand Down
8 changes: 8 additions & 0 deletions Scripts/Swift/Extensions/String+Utils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extension String {

static func ~= (lhs: String, rhs: String) -> Bool {
guard let regex = try? NSRegularExpression(pattern: rhs) else { return false }
let range = NSRange(location: 0, length: lhs.utf16.count)
return regex.firstMatch(in: lhs, options: [], range: range) != nil
}
}
89 changes: 45 additions & 44 deletions Scripts/Swift/SetUpCICDService.swift
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
#!/usr/bin/swift

import Foundation

let fileManager = FileManager.default

enum CICDService {
case github, bitrise, codemagic, later

init?(_ name: String) {
switch name.lowercased() {
case "g", "github":
self = .github
case "b", "bitrise":
self = .bitrise
case "c", "codemagic":
self = .codemagic
case "l", "later":
self = .later
default:
return nil
struct SetUpCICDService {

enum CICDService {

case github, bitrise, codemagic, later

init?(_ name: String) {
switch name.lowercased() {
case "g", "github":
self = .github
case "b", "bitrise":
self = .bitrise
case "c", "codemagic":
self = .codemagic
case "l", "later":
self = .later
default:
return nil
}
}
}
}

var service: CICDService? = nil
private let fileManager = FileManager.default

while service == nil {
print("Which CI/CD service do you use (Can be edited later) [(g)ithub/(b)itrise/(c)odemagic/(l)ater]: ")
service = CICDService(readLine() ?? "")
}
func perform() {
var service: CICDService? = nil
while service == nil {
print("Which CI/CD service do you use (Can be edited later) [(g)ithub/(b)itrise/(c)odemagic/(l)ater]: ")
service = CICDService(readLine() ?? "")
}

switch service {
case .github:
print("Setting template for Github Actions")
fileManager.removeItems(in: "bitrise.yml")
fileManager.removeItems(in: "codemagic.yaml")
case .bitrise:
print("Setting template for Bitrise")
fileManager.removeItems(in: "codemagic.yaml")
fileManager.removeItems(in: ".github/workflows")
case .codemagic:
print("Setting template for CodeMagic")
fileManager.removeItems(in: "bitrise.yml")
fileManager.removeItems(in: ".github/workflows")
case .later, .none:
print("You can manually setup the template later.")
}
switch service {
case .github:
print("Setting template for Github Actions")
fileManager.removeItems(in: "bitrise.yml")
fileManager.removeItems(in: "codemagic.yaml")
case .bitrise:
print("Setting template for Bitrise")
fileManager.removeItems(in: "codemagic.yaml")
fileManager.removeItems(in: ".github/workflows")
case .codemagic:
print("Setting template for CodeMagic")
fileManager.removeItems(in: "bitrise.yml")
fileManager.removeItems(in: ".github/workflows")
case .later, .none:
print("You can manually setup the template later.")
}

print("✅ Completed")
print("✅ Completed")
}
}
31 changes: 17 additions & 14 deletions Scripts/Swift/SetUpDeliveryConstants.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#!/usr/bin/swift
struct SetUpDeliveryConstants {

import Foundation
func perform() {
print("Do you want to set up Constants values? (Can be edited later) [Y/n]: ")

let fileManager = FileManager.default
let arg = readLine() ?? "y"

print("Do you want to set up Constants values? (Can be edited later) [Y/n]: ")

var arg = readLine() ?? "y"

switch arg.lowercased() {
case "y", "yes", "":
let error = try safeShell("open -a Xcode fastlane/Constants/Constant.swift")
guard let error = error, !error.isEmpty else { break }
print("Could not open Xcode. Make sure Xcode is installed and try again.\nRaw error: \(error)")
default:
print("✅ Completed. You can edit this file at 'fastlane/Constants/Constant.swift'.")
switch arg.lowercased() {
case "y", "yes":
do {
let error = try safeShell("open -a Xcode fastlane/Constants/Constant.swift")
guard let error = error, !error.isEmpty else { break }
print("Could not open Xcode. Make sure Xcode is installed and try again.\nRaw error: \(error)")
} catch {
print("Error: \(error)")
}
default:
print("✅ Completed. You can edit this file at 'fastlane/Constants/Constant.swift'.")
}
}
}
69 changes: 45 additions & 24 deletions Scripts/Swift/SetUpInterface.swift
Original file line number Diff line number Diff line change
@@ -1,25 +1,46 @@
#!/usr/bin/swift

import Foundation

let fileManager = FileManager.default

var interface = "UIKit"

switch CommandLine.arguments[1] {
case "SwiftUI", "s", "S":
print("=> 🦅 Setting up SwiftUI")
interface = "SwiftUI"
let swiftUIAppDirectory = "tuist/Interfaces/SwiftUI/Sources/Application"
fileManager.rename(
file: "\(swiftUIAppDirectory)/App.swift",
to: "\(swiftUIAppDirectory)/\(CommandLine.arguments[2])App.swift"
)
default:
print("=> 🦉 Setting up UIKit")
interface = "UIKit"
}
struct SetUpInterface {

enum Interface {

case swiftUI, uiKit

init?(_ name: String) {
switch name.lowercased() {
case "s", "swiftui":
self = .swiftUI
case "u", "uikit":
self = .uiKit
default: return nil
}
}

var folderName: String {
switch self {
case .swiftUI: return "SwiftUI"
case .uiKit: return "UIKit"
}
}
}

fileManager.moveFiles(in: "tuist/Interfaces/\(interface)/Project", to: "")
fileManager.moveFiles(in: "tuist/Interfaces/\(interface)/Sources", to: "{PROJECT_NAME}/Sources")
fileManager.removeItems(in: "tuist/Interfaces")
private let fileManager = FileManager.default

func perform(_ interface: Interface, _ projectName: String) {
switch interface {
case .swiftUI:
print("=> 🦅 Setting up SwiftUI")
let swiftUIAppDirectory = "tuist/Interfaces/SwiftUI/Sources/Application"
fileManager.rename(
file: "\(swiftUIAppDirectory)/App.swift",
to: "\(swiftUIAppDirectory)/\(projectName)App.swift"
)
case .uiKit:
print("=> 🦉 Setting up UIKit")
}

let folderName = interface.folderName

fileManager.moveFiles(in: "tuist/Interfaces/\(folderName)/Project", to: "")
fileManager.moveFiles(in: "tuist/Interfaces/\(folderName)/Sources", to: "TemplateApp/Sources")
fileManager.removeItems(in: "tuist/Interfaces")
}
}
Loading

0 comments on commit 00e6deb

Please sign in to comment.