Skip to content

Commit

Permalink
Merge pull request #4 from bannzai/imp/specifity_target
Browse files Browse the repository at this point in the history
Imp/specifity target
  • Loading branch information
bannzai authored Nov 13, 2017
2 parents c47a698 + 6411f09 commit 0607360
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 117 deletions.
18 changes: 18 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
{
"object": {
"pins": [
{
"package": "Commander",
"repositoryURL": "https://github.com/kylef/Commander.git",
"state": {
"branch": null,
"revision": "e5b50ad7b2e91eeb828393e89b03577b16be7db9",
"version": "0.8.0"
}
},
{
"package": "Spectre",
"repositoryURL": "https://github.com/kylef/Spectre.git",
"state": {
"branch": null,
"revision": "e34d5687e1e9d865e3527dd58bc2f7464ef6d936",
"version": "0.8.0"
}
},
{
"package": "XcodeProject",
"repositoryURL": "https://github.com/bannzai/XcodeProject.git",
Expand Down
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ let package = Package(
name: "ResourceKit",
dependencies: [
.package(url: "https://github.com/bannzai/XcodeProject.git", from: Version(0, 1, 1)),
.package(url: "https://github.com/kylef/Commander.git", from: Version(0, 8, 0)),
],
targets: [
.target(
Expand All @@ -13,7 +14,7 @@ let package = Package(
),
.target(
name: "ResourceKitCore",
dependencies: ["XcodeProject"]
dependencies: ["XcodeProject", "Commander"]
),
.testTarget(name: "ResourceKitCoreTests", dependencies: ["ResourceKitCore"]),
],
Expand Down
2 changes: 1 addition & 1 deletion ResourceKitDemo/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 92652161d639f7d92903c222c5510fae64667b93

COCOAPODS: 1.1.1
COCOAPODS: 1.2.0.beta.1
2 changes: 1 addition & 1 deletion ResourceKitDemo/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified ResourceKitDemo/ResourceKit
Binary file not shown.
2 changes: 1 addition & 1 deletion ResourceKitDemo/ResourceKitDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"$SRCROOT/ResourceKit\"";
shellScript = "$SRCROOT/ResourceKit --project-file-path $SRCROOT/ResourceKitDemo.xcodeproj ";
};
C54D35B7368100D449D5DBB9 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
Expand Down
237 changes: 124 additions & 113 deletions Sources/ResourceKit/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,126 +8,137 @@
import Foundation
import XcodeProject
import ResourceKitCore
import Commander

if !ResourceKitConfig.Debug.isDebug {
do {
try Environment.verifyUseEnvironment()
} catch {
exit(1)
}
}

let outputPath = ResourceKitConfig.outputPath
let config: Config = ConfigImpl(outputPath: outputPath)

do {
let outputUrl = URL(fileURLWithPath: outputPath)
var resourceValue: AnyObject?
try (outputUrl as NSURL).getResourceValue(&resourceValue, forKey: URLResourceKey.isDirectoryKey)

let writeUrl: URL = outputUrl.appendingPathComponent(ResourceKitConfig.outputFileName, isDirectory: false)

let projectFilePath = ResourceKitConfig.Debug.projectFilePath != nil ? URL(fileURLWithPath: ResourceKitConfig.Debug.projectFilePath!) : Environment.PROJECT_FILE_PATH.path
guard let pbxprojectPath = URL(string: projectFilePath.absoluteString + "project.pbxproj") else {
throw ResourceKitErrorType.xcodeProjectError(xcodeURL: projectFilePath, target: "Unknown", errorInfo: "Can't find project.pbxproj")
}
let projectTarget = ResourceKitConfig.Debug.projectTarget ?? Environment.TARGET_NAME.element
let parser = try ProjectResourceParser(xcodeURL: pbxprojectPath, target: projectTarget, writeResource: ProjectResource.shared, config: config)
let paths = ProjectResource.shared.paths

paths
.filter { $0.pathExtension == "storyboard" }
.forEach { try? StoryboardParserImpl(url: $0, writeResource: ProjectResource.shared, config: config).parse() }

paths
.filter { $0.pathExtension == "xib" }
.forEach { try? XibPerserImpl(url: $0, writeResource: ProjectResource.shared).parse() }

let importsContent = ImportOutputImpl(writeUrl: writeUrl, config: config).declaration

let viewControllerContent = try ProjectResource
.shared
.viewControllers
.map { (viewController) in
try ViewControllerTranslator(config: config, viewControllers: ProjectResource.shared.viewControllers).translate(for: viewController).declaration
let projectFilePath = ResourceKitConfig.Debug.projectFilePath != nil ? ResourceKitConfig.Debug.projectFilePath! : Environment.PROJECT_FILE_PATH.rawValue
let main = command(
Option<String>("project-file-path", default: projectFilePath)
) { projectFilePath in
if !ResourceKitConfig.Debug.isDebug {
do {
try Environment.verifyUseEnvironment()
} catch {
print("verify use environtment")
exit(1)
}
.joined()

let tableViewCellContent: String
let collectionViewCellContent: String

if config.reusable.identifier {
tableViewCellContent = try ProjectResource.shared.tableViewCells
.flatMap { try ReusableTranslator().translate(for: $0).declaration + Const.newLine }
.joined(separator: Const.newLine)
.appendNewLineIfNotEmpty()

collectionViewCellContent = try ProjectResource.shared.collectionViewCells
.flatMap { try ReusableTranslator().translate(for: $0).declaration + Const.newLine }
.joined(separator: Const.newLine)
.appendNewLineIfNotEmpty()

} else {
tableViewCellContent = ""
collectionViewCellContent = ""
}

let xibContent: String
if config.nib.xib {
xibContent = try ProjectResource.shared.xibs
.flatMap { try XibTranslator().translate(for: $0).declaration }
.joined(separator: Const.newLine)
.appendNewLineIfNotEmpty()
} else {
xibContent = ""
}
let outputPath = ResourceKitConfig.outputPath
let config: Config = ConfigImpl(outputPath: outputPath)

let imageContent = try ImageTranslator(config: config).translate(
for: (
assets: ImageAssetRepositoryImpl().load(),
resources: ImageResourcesRepositoryImpl().load())
)
.declaration

let stringContent: String

if config.string.localized {
stringContent = try LocalizedStringTranslator()
.translate(
for: LocalizedStringRepositoryImpl(urls: ProjectResource.shared.localizablePaths).load()
do {
let outputUrl = URL(fileURLWithPath: outputPath)
var resourceValue: AnyObject?
try (outputUrl as NSURL).getResourceValue(&resourceValue, forKey: URLResourceKey.isDirectoryKey)

let writeUrl: URL = outputUrl.appendingPathComponent(ResourceKitConfig.outputFileName, isDirectory: false)

let projectFileURL = URL(fileURLWithPath: projectFilePath)
guard let pbxprojectPath = URL(string: projectFileURL.absoluteString + "project.pbxproj") else {
throw ResourceKitErrorType.xcodeProjectError(xcodeURL: projectFileURL, target: "Unknown", errorInfo: "Can't find project.pbxproj")
}
let projectTarget = ResourceKitConfig.Debug.projectTarget ?? Environment.TARGET_NAME.element
let parser = try ProjectResourceParser(xcodeURL: pbxprojectPath, target: projectTarget, writeResource: ProjectResource.shared, config: config)
let paths = ProjectResource.shared.paths

paths
.filter { $0.pathExtension == "storyboard" }
.forEach { try? StoryboardParserImpl(url: $0, writeResource: ProjectResource.shared, config: config).parse() }

paths
.filter { $0.pathExtension == "xib" }
.forEach { try? XibPerserImpl(url: $0, writeResource: ProjectResource.shared).parse() }

let importsContent = ImportOutputImpl(writeUrl: writeUrl, config: config).declaration

let viewControllerContent = try ProjectResource
.shared
.viewControllers
.map { (viewController) in
try ViewControllerTranslator(config: config, viewControllers: ProjectResource.shared.viewControllers).translate(for: viewController).declaration
}
.joined()

let tableViewCellContent: String
let collectionViewCellContent: String

if config.reusable.identifier {
tableViewCellContent = try ProjectResource.shared.tableViewCells
.flatMap { try ReusableTranslator().translate(for: $0).declaration + Const.newLine }
.joined(separator: Const.newLine)
.appendNewLineIfNotEmpty()

collectionViewCellContent = try ProjectResource.shared.collectionViewCells
.flatMap { try ReusableTranslator().translate(for: $0).declaration + Const.newLine }
.joined(separator: Const.newLine)
.appendNewLineIfNotEmpty()

} else {
tableViewCellContent = ""
collectionViewCellContent = ""
}

let xibContent: String
if config.nib.xib {
xibContent = try ProjectResource.shared.xibs
.flatMap { try XibTranslator().translate(for: $0).declaration }
.joined(separator: Const.newLine)
.appendNewLineIfNotEmpty()
} else {
xibContent = ""
}

let imageContent = try ImageTranslator(config: config).translate(
for: (
assets: ImageAssetRepositoryImpl().load(),
resources: ImageResourcesRepositoryImpl().load())
)
.declaration
} else {
stringContent = ""
}

let content = (
Const.Header
+ importsContent + Const.newLine
+ ExtensionsOutputImpl().reusableProtocolContent + Const.newLine + Const.newLine
+ ExtensionsOutputImpl().xibProtocolContent + Const.newLine + Const.newLine
+ ExtensionsOutputImpl().tableViewExtensionContent + Const.newLine + Const.newLine
+ ExtensionsOutputImpl().collectionViewExtensionContent + Const.newLine + Const.newLine
+ viewControllerContent + Const.newLine
+ tableViewCellContent + Const.newLine
+ collectionViewCellContent + Const.newLine
+ xibContent + Const.newLine
+ imageContent + Const.newLine
+ stringContent + Const.newLine
)

func write(_ code: String, fileURL: URL) throws {
try code.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)
}

try write(content, fileURL: writeUrl)
} catch {
if let e = error as? ResourceKitErrorType {
print(e.description())

} else {
print(error)
let stringContent: String

if config.string.localized {
stringContent = try LocalizedStringTranslator()
.translate(
for: LocalizedStringRepositoryImpl(urls: ProjectResource.shared.localizablePaths).load()
)
.declaration
} else {
stringContent = ""
}

let content = (
Const.Header
+ importsContent + Const.newLine
+ ExtensionsOutputImpl().reusableProtocolContent + Const.newLine + Const.newLine
+ ExtensionsOutputImpl().xibProtocolContent + Const.newLine + Const.newLine
+ ExtensionsOutputImpl().tableViewExtensionContent + Const.newLine + Const.newLine
+ ExtensionsOutputImpl().collectionViewExtensionContent + Const.newLine + Const.newLine
+ viewControllerContent + Const.newLine
+ tableViewCellContent + Const.newLine
+ collectionViewCellContent + Const.newLine
+ xibContent + Const.newLine
+ imageContent + Const.newLine
+ stringContent + Const.newLine
)

func write(_ code: String, fileURL: URL) throws {
try code.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)
}

try write(content, fileURL: writeUrl)
} catch {
if let e = error as? ResourceKitErrorType {
print(e.description())

} else {
print(error)
}

print("missing write content")
exit(3)
}

exit(3)
}


main.run()

0 comments on commit 0607360

Please sign in to comment.