Skip to content

Commit

Permalink
copy SourceryFramework to template build folder and link it with binary
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyapuchka committed Apr 24, 2017
1 parent e66132d commit 31ba29a
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions Sourcery/Generating/Template/Swift/SwiftTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class SwiftTemplate: Template {
try? cachePath.delete() // clear old cache
try cachePath.mkdir()
try build().move(binaryPath)
try copyFramework(to: cachePath.parent())
}
} else {
try binaryPath = build()
Expand All @@ -140,15 +141,6 @@ class SwiftTemplate: Template {
let data = NSKeyedArchiver.archivedData(withRootObject: context)
try serializedContextPath.write(data)

#if DEBUG
// this is a sanity check, deserialized object should be equal to initial object
let diff = context.diffAgainst(NSKeyedUnarchiver.unarchiveObject(with: data))
if !diff.isEmpty {
print(diff.description)
}
assert(diff.isEmpty)
#endif

let result = try Process.runCommand(path: binaryPath.description,
arguments: [serializedContextPath.description])

Expand All @@ -160,21 +152,19 @@ class SwiftTemplate: Template {
}

func build() throws -> Path {
let runtimeFiles = try SwiftTemplate.swiftTemplatesRuntime.children().map { file in
return file.description
}

let mainFile = buildDir + Path("main.swift")
let binaryFile = buildDir + Path("bin")

try copyFramework(to: buildDir.parent())
try mainFile.write(code)

let arguments = [mainFile.description] +
runtimeFiles + [
[
"-suppress-warnings",
"-whole-module-optimization",
"-Onone",
"-module-name", "Sourcery",
"-module-name", "main",
"-target", "x86_64-apple-macosx10.10",
"-F", ".",
"-o", binaryFile.description
]

Expand All @@ -190,21 +180,47 @@ class SwiftTemplate: Template {
throw compilationResult.error
}

let linkingResult = try Process.runCommand(path: "/usr/bin/install_name_tool",
arguments: [
"-add_rpath",
"@executable_path/../",
binaryFile.description])
if !linkingResult.error.isEmpty {
throw linkingResult.error
}

try? mainFile.delete()

return binaryFile
}

private func copyFramework(to path: Path) throws {
let sourceryFramework = SwiftTemplate.frameworksPath + "SourceryFramework.framework"

let copyFramework = try Process.runCommand(path: "/usr/bin/rsync", arguments: [
"-av", sourceryFramework.description, path.description
])

if !copyFramework.error.isEmpty {
throw copyFramework.error
}
}

}

fileprivate extension SwiftTemplate {
static var resourcesPath: Path {
return Bundle(for: Sourcery.self).resourcePath.flatMap { Path($0) }!
}

static var frameworksPath: Path {
return Bundle(for: Sourcery.self).privateFrameworksPath.flatMap { Path($0) }!
}

static var swiftTemplatesRuntime: Path {
return resourcesPath + Path("SwiftTemplateRuntime")
}

}

// swiftlint:disable:next force_try
Expand Down

0 comments on commit 31ba29a

Please sign in to comment.