Skip to content

Commit 9b236f2

Browse files
author
Artem Novichkov
committed
Merge branch 'no-script-section-check'
2 parents 7b15232 + 32b1753 commit 9b236f2

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

Sources/CartingCore/Models/Project.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ final class Project {
1414
let body: String
1515
let targetsRange: Range<String.Index>
1616
let targets: [Target]
17-
let scriptsRange: Range<String.Index>
17+
let scriptsRange: Range<String.Index>?
1818
var scripts: [Script]
1919
var frameworkScripts: [FrameworkScript]
2020

2121
init(name: String,
2222
body: String,
2323
targetsRange: Range<String.Index>,
2424
targets: [Target],
25-
scriptsRange: Range<String.Index>,
25+
scriptsRange: Range<String.Index>?,
2626
scripts: [Script],
2727
frameworkScripts: [FrameworkScript]) {
2828
self.name = name

Sources/CartingCore/Services/ProjectService.swift

+21-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ final class ProjectService {
1313
enum Error: Swift.Error {
1414
case noProjectFile
1515
case cannotReadProject
16+
case cannotFindProjectResources
1617
}
1718

1819
enum PathType {
@@ -26,6 +27,7 @@ final class ProjectService {
2627
static let outputPath = "$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/"
2728
static let projectExtension = "xcodeproj"
2829
static let frameworkExtension = "framework"
30+
static let resourcesBuildSectionEnd = "/* End PBXResourcesBuildPhase section */"
2931
}
3032

3133
let fileManager: FileManager
@@ -59,7 +61,7 @@ final class ProjectService {
5961
throw Error.cannotReadProject
6062
}
6163
let (targetsRange, targets) = try targetsService.targets(fromProjectString: body)
62-
let (scriptsRange, scripts) = try shellScriptsService.scripts(fromProjectString: body)
64+
let (scriptsRange, scripts) = shellScriptsService.scripts(fromProjectString: body)
6365
let (_, frameworkScripts) = try frameworksService.scripts(fromProjectString: body)
6466

6567
return Project(name: projectFileName,
@@ -75,15 +77,29 @@ final class ProjectService {
7577
/// - Parameter project: a project for updating.
7678
/// - Throws: throws if it can not white a project to project file.
7779
func update(_ project: Project) throws {
78-
let newScriptsProjectString = project.body.replacingCharacters(in: project.scriptsRange,
80+
let newScriptsProjectString: String
81+
if let scriptsRange = project.scriptsRange {
82+
newScriptsProjectString = project.body.replacingCharacters(in: scriptsRange,
7983
with: shellScriptsService.string(from: project.scripts))
84+
}
85+
else if let range = project.body.range(of: Keys.resourcesBuildSectionEnd) {
86+
var body = project.body
87+
let scriptsString = shellScriptsService.string(from: project.scripts,
88+
needSectionBlock: true)
89+
body.insert(contentsOf: "\n\n\(scriptsString)".characters,
90+
at: range.upperBound)
91+
newScriptsProjectString = body
92+
}
93+
else {
94+
throw Error.cannotFindProjectResources
95+
}
8096
let newTargetsProjectString = newScriptsProjectString.replacingCharacters(in: project.targetsRange,
8197
with: targetsService.string(from: project.targets))
8298

8399
let path = fileManager.currentDirectoryPath + "/\(project.name)" + Keys.projectPath
84100
try newTargetsProjectString.write(toFile: path,
85-
atomically: true,
86-
encoding: .utf8)
101+
atomically: true,
102+
encoding: .utf8)
87103
}
88104

89105
/// - Parameters:
@@ -130,6 +146,7 @@ extension ProjectService.Error: LocalizedError {
130146
switch self {
131147
case .noProjectFile: return "Can't find any .pbxproj file."
132148
case .cannotReadProject: return "Can't read a project."
149+
case .cannotFindProjectResources: return "Can't fine Resources section in a project."
133150
}
134151
}
135152
}

Sources/CartingCore/Services/ShellScriptsService.swift

+19-11
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ final class ShellScriptsService {
2020
}
2121

2222
/// - Parameter string: a string from project.pbxproj file.
23-
/// - Returns: a tuple with a range of scripts and an array of mapped scripts.
24-
func scripts(fromProjectString string: String) throws -> (Range<String.Index>, [Script]) {
25-
let (range, scriptsString) = try self.scriptsString(fromProjectString: string)
26-
let scanner = Scanner(string: scriptsString)
23+
/// - Returns: a tuple with a range of scripts and an array of mapped scripts. If it is a new project, returns no range and empty array.
24+
func scripts(fromProjectString string: String) -> (Range<String.Index>?, [Script]) {
25+
let (range, scriptsString) = self.scriptsString(fromProjectString: string)
26+
guard let nonEmptyScriptsString = scriptsString else {
27+
return (range, [])
28+
}
29+
let scanner = Scanner(string: nonEmptyScriptsString)
2730
var identifier: NSString?
2831
var name: NSString?
2932
var bodyString: NSString?
@@ -49,20 +52,25 @@ final class ShellScriptsService {
4952
return (range, scripts)
5053
}
5154

52-
/// - Parameter scripts: an array of scripts.
55+
/// - Parameters:
56+
/// - scripts: an array of scripts.
57+
/// - needSectionBlock: if true, returns whole section block
5358
/// - Returns: formatted string with all scripts for insertion into project.
54-
func string(from scripts: [Script]) -> String {
59+
func string(from scripts: [Script], needSectionBlock: Bool = false) -> String {
5560
let scriptStrings: [String] = scripts.map { $0.description }
56-
return scriptStrings.joined(separator: "") + "\n"
61+
var scriptString = scriptStrings.joined(separator: "") + "\n"
62+
if needSectionBlock {
63+
scriptString = Keys.buildPhaseSectionBegin + scriptString + Keys.buildPhaseSectionEnd
64+
}
65+
return scriptString
5766
}
5867

5968
/// - Parameter projectString: a string from project.pbxproj file.
60-
/// - Returns: a tuple with scripts range and scripts section string.
61-
/// - Throws: an error if there is no scripts section in project string.
62-
private func scriptsString(fromProjectString string: String) throws -> (Range<String.Index>, String) {
69+
/// - Returns: a tuple with scripts range and scripts section string. If it is a new project, returns nils.
70+
private func scriptsString(fromProjectString string: String) -> (Range<String.Index>?, String?) {
6371
guard let startRange = string.range(of: Keys.buildPhaseSectionBegin),
6472
let endRange = string.range(of: Keys.buildPhaseSectionEnd) else {
65-
throw Error.noScripts
73+
return (nil, nil)
6674
}
6775
let scriptsRange = startRange.upperBound..<endRange.lowerBound
6876
return (scriptsRange, string.substring(with: scriptsRange))

0 commit comments

Comments
 (0)