Skip to content

Commit

Permalink
Merge pull request #15 from bannzai/imp/replace_directory_name
Browse files Browse the repository at this point in the history
Imp/replace directory name
  • Loading branch information
bannzai committed Jan 17, 2018
2 parents 4cdf545 + 9ac7ea5 commit c1c12d9
Show file tree
Hide file tree
Showing 52 changed files with 136 additions and 90 deletions.
2 changes: 1 addition & 1 deletion KuriDemo/Kuri.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DefaultTemplateDirectoryPath: ./KuriTemplate/
DefaultTemplateDirectoryPath: .templates/KuriTemplate/
ProjectRootPath: ./
ProjectFileName: KuriDemo.xcodeproj
GenerateRootPath: ./KuriDemo/
Expand Down
12 changes: 12 additions & 0 deletions KuriDemo/KuriDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
J4QXUUP0H88BEQ5F78PLJCS9 /* HogeUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = ZGRWNTN8TDJ8TUKXMDRQOLB9 /* HogeUseCase.swift */; };
KA67GINSIV1LC97SDMI37XDF /* PiyoTranslator.swift in Sources */ = {isa = PBXBuildFile; fileRef = Z1CTBJE9GTGLY1I2HJFPB6BE /* PiyoTranslator.swift */; };
LCEVTGOH286TAOET53QBACR1 /* PiyoDataStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57FBVZGRP03V8IY6PGH4S0OT /* PiyoDataStore.swift */; };
LTJ981SDPE3TNX3R201QM3QS /* HogeHogeDynamicGenerateFilePath.swift in Sources */ = {isa = PBXBuildFile; fileRef = PQ5JEAIFBWC3V88T7NWBEIKE /* HogeHogeDynamicGenerateFilePath.swift */; };
ML4EFUYIGIJ5DZ8XWQT5E7SM /* PiyoTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8MEFM9QW3B181IE6HSTB8KZ4 /* PiyoTableViewCell.swift */; };
ODDIN2YDCIIWM3DIBRQFT6F1 /* HogeWireframe.swift in Sources */ = {isa = PBXBuildFile; fileRef = XNEBQPI9HE0M2AITC251LCPG /* HogeWireframe.swift */; };
P5PYEDZJG2Z0DW44KIE4K0FU /* FugaWireframe.swift in Sources */ = {isa = PBXBuildFile; fileRef = NK90RPQFCUJNWGHICIVW9ZM9 /* FugaWireframe.swift */; };
Expand Down Expand Up @@ -116,6 +117,7 @@
OA20QBGSZDSSQI2UC3DZ23T8 /* HogeViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HogeViewController.swift; sourceTree = "<group>"; };
OE4ZX4ZSISCR5ZNKMR72PNZK /* HogePresenter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HogePresenter.swift; sourceTree = "<group>"; };
OOBOKU2OILY2WVLNQI5XQQ83 /* HogeDataStore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HogeDataStore.swift; sourceTree = "<group>"; };
PQ5JEAIFBWC3V88T7NWBEIKE /* HogeHogeDynamicGenerateFilePath.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HogeHogeDynamicGenerateFilePath.swift; sourceTree = "<group>"; };
QEM4X3DTKPGJS28F9531Q8LQ /* PiyoUseCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PiyoUseCase.swift; sourceTree = "<group>"; };
R0ZWL75196MO0G7X61I561DY /* FugaRepository.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FugaRepository.swift; sourceTree = "<group>"; };
RDMKKI58T3FC0Y9DIFFCDC9C /* FugaCollectionViewController.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = FugaCollectionViewController.storyboard; sourceTree = "<group>"; };
Expand Down Expand Up @@ -187,6 +189,7 @@
6QEHK1RS5SPP75TS6ZN29UTQ /* View */,
NO2Q7N0U8E7L0UDIBR7DWCEB /* Builder */,
N82T550MCC6YBOTEL4GVA99B /* UseCase */,
XUBAC1MYOMKZANL2S3SHUIQ5 /* Hoge */,
);
path = Hoge;
sourceTree = "<group>";
Expand Down Expand Up @@ -480,6 +483,14 @@
path = Presenter;
sourceTree = "<group>";
};
XUBAC1MYOMKZANL2S3SHUIQ5 /* Hoge */ = {
isa = PBXGroup;
children = (
PQ5JEAIFBWC3V88T7NWBEIKE /* HogeHogeDynamicGenerateFilePath.swift */,
);
path = Hoge;
sourceTree = "<group>";
};
YEBDY0OV71KZ3GZ9DCMRZI96 /* View */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -693,6 +704,7 @@
RUS35PKYW0IBU2Q51HWZNE8J /* FugaCollectionViewController.swift in Sources */,
1GOISMFREV12TGR7NU9AHCK9 /* FugaBuilder.swift in Sources */,
4PGJI10SW8WBZNC2TJO60MI8 /* FugaUseCase.swift in Sources */,
LTJ981SDPE3TNX3R201QM3QS /* HogeHogeDynamicGenerateFilePath.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>KuriDemo.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>
11 changes: 11 additions & 0 deletions KuriDemo/KuriDemo/Hoge/Hoge/HogeHogeDynamicGenerateFilePath.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// HogeDynamicGenerateFilePath
// Kuri
//
// Created by hirose on 2018/1/17.
// Copyright © 2018 hirose. All rights reserved.
//

import UIKit

print("HogeDynamicGenerateFilePath")
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// __PREFIX__DynamicGenerateFilePath
// Kuri
//
// Created by __USERNAME__ on __DATE__.
// Copyright © __YEAR__ __USERNAME__. All rights reserved.
//

import UIKit

print("__PREFIX__DynamicGenerateFilePath")
File renamed without changes.
5 changes: 3 additions & 2 deletions Sources/KuriCore/Command/GenerateComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ struct GenerateComponent {
return Array(templateRelativePath.components(separatedBy: "/").dropLast())
}

var generateDirectoryPath: [String] {
return templateDirectoryPath
func makeGeneratingDirectoryPath(prefix: String, targetName: String) -> [String] {
let replacedPath = templateDirectoryPath.map { $0.replaceEnvironmentText(prefix: prefix, targetName: targetName) }
return replacedPath
}
}
45 changes: 5 additions & 40 deletions Sources/KuriCore/Command/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,41 +155,6 @@ extension Generator {
}

fileprivate extension Generator {
struct DateComponent {
let year: Int
let month: Int
let day: Int

var date: String {
return "\(year)/\(month)/\(day)"
}
}

fileprivate func convert(content: String, prefix: String, targetName: String) -> String {
let userName = run(bash: "echo $USER").stdout
let date: DateComponent = { _ -> DateComponent in
let component = Calendar(identifier: .gregorian).dateComponents([.year, .month, .day], from: Date())
guard
let year = component.year,
let month = component.month,
let day = component.day
else {
fatalError("Can't get system date")
}
return DateComponent(year: year, month: month, day: day)
}(())

let replacedContent = content
.replacingOccurrences(of: "__PREFIX__", with: prefix)
.replacingOccurrences(of: "__TARGET__", with: targetName)
.replacingOccurrences(of: "__USERNAME__", with: userName)
.replacingOccurrences(of: "__DATE__", with: date.date)
.replacingOccurrences(of: "__YEAR__", with: "\(date.year)")
.replacingOccurrences(of: "__MONTH__", with: "\(date.month)")
.replacingOccurrences(of: "__DAY__", with: "\(date.day)")
return replacedContent
}

fileprivate func generate(with prefix: String, for components: [GenerateComponent], and templateHeadPath: String) throws {
print("Begin generate")
defer {
Expand All @@ -204,9 +169,10 @@ fileprivate extension Generator {
let projectRootPath = yamlReader.projectRootPath(for: typeFor)
let projectFileName = yamlReader.projectFileName(for: typeFor)

let targetName = yamlReader.targetName(for: typeFor)
let projectFilePath = projectRootPath + projectFileName + "/"
let directoryPath = generateRootPath + component.generateDirectoryPath.joined(separator: "/") + "/"
let filePath = directoryPath + prefix + component.templateFileName
let generatingDirectoryPath = generateRootPath + component.makeGeneratingDirectoryPath(prefix: prefix, targetName: targetName).joined(separator: "/") + "/"
let filePath = (generatingDirectoryPath + prefix + component.templateFileName).replaceEnvironmentText(prefix: prefix, targetName: targetName)

let project: XcodeProject
if let alreadyExistsProject = pathAndXcodeProject[projectFilePath] {
Expand All @@ -223,9 +189,8 @@ fileprivate extension Generator {
print("can't find: \(componentType)")
return
}
let targetName = yamlReader.targetName(for: typeFor)
let writeCotent = convert(content: templateContent, prefix: prefix, targetName: targetName)
try fileOperator.createDirectory(for: directoryPath)
let writeCotent = templateContent.replaceEnvironmentText(prefix: prefix, targetName: targetName)
try fileOperator.createDirectory(for: generatingDirectoryPath)
fileOperator.createFile(for: filePath)

project.appendFilePath(
Expand Down
File renamed without changes.
45 changes: 0 additions & 45 deletions Sources/KuriCore/KuriExtension.swift

This file was deleted.

File renamed without changes.
File renamed without changes.
77 changes: 77 additions & 0 deletions Sources/KuriCore/Util/KuriExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//
// KuriExtension.swift
// Kuri
//
// Created by kingkong999yhirose on 2016/12/22.
// Copyright © 2016年 kingkong999yhirose. All rights reserved.
//

import Foundation

protocol Enumerable {
associatedtype Element = Self
}

extension Enumerable where Element: RawRepresentable, Element.RawValue == Int {
static var enumerate: AnySequence<Element> {
return AnySequence { () -> AnyIterator<Element> in
var i = 0
return AnyIterator { () -> Element? in
let element = Element(rawValue: i)
i += 1
return element
}
}
}
static var elements: [Element] {
return Array(enumerate)
}
}


extension String {
fileprivate struct DateComponent {
let year: Int
let month: Int
let day: Int

var date: String {
return "\(year)/\(month)/\(day)"
}
}
func replaceEnvironmentText(prefix: String, targetName: String) -> String {
let userName = run(bash: "echo $USER").stdout
let date: DateComponent = { _ -> DateComponent in
let component = Calendar(identifier: .gregorian).dateComponents([.year, .month, .day], from: Date())
guard
let year = component.year,
let month = component.month,
let day = component.day
else {
fatalError("Can't get system date")
}
return DateComponent(year: year, month: month, day: day)
}(())

let replacedContent = self
.replacingOccurrences(of: "__PREFIX__", with: prefix)
.replacingOccurrences(of: "__TARGET__", with: targetName)
.replacingOccurrences(of: "__USERNAME__", with: userName)
.replacingOccurrences(of: "__DATE__", with: date.date)
.replacingOccurrences(of: "__YEAR__", with: "\(date.year)")
.replacingOccurrences(of: "__MONTH__", with: "\(date.month)")
.replacingOccurrences(of: "__DAY__", with: "\(date.day)")
return replacedContent
}

}

extension Array {
subscript(safe index: Int) -> Element? {
return 0 <= index && index < count ? self[index] : nil
}

var second: Element? {
return self[safe: 1]
}
}
4 changes: 2 additions & 2 deletions Sources/KuriCore/Yaml/YamlReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public struct YamlReader {

private func readYamlForComponent(generateComponent: GenerateComponent, from yaml: Yaml) -> Yaml {
var yamlForComponent: Yaml = yaml
generateComponent.generateDirectoryPath.forEach { directory in
generateComponent.templateDirectoryPath.forEach { directory in
yamlForComponent = yamlForComponent[.string(directory)]
if yamlForComponent == .null {
fatalError("Can't find \(directory) from \(generateComponent.generateDirectoryPath)")
fatalError("Can't find \(directory) from \(generateComponent.templateDirectoryPath)")
}
}
return yamlForComponent
Expand Down

0 comments on commit c1c12d9

Please sign in to comment.