Skip to content

Commit

Permalink
Update csv builder with swift-syntax (#38)
Browse files Browse the repository at this point in the history
* Remove example of CsvComposition from public API

* Add `CsvCompositionParser`

* Add comment

* Add txt file
  • Loading branch information
fummicc1 authored Feb 1, 2023
1 parent 6f53163 commit ca42f36
Show file tree
Hide file tree
Showing 15 changed files with 317 additions and 45 deletions.
7 changes: 7 additions & 0 deletions Csv2ImageApp/Csv2ImageApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
D359BADC28621BFE006DFDCE /* Type.swift in Sources */ = {isa = PBXBuildFile; fileRef = D359BADB28621BFE006DFDCE /* Type.swift */; };
D35DB0D4285FA754009C252D /* CsvViewer.swift in Sources */ = {isa = PBXBuildFile; fileRef = D35DB0D3285FA754009C252D /* CsvViewer.swift */; };
D35DB0D6285FADA5009C252D /* HistoryModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D35DB0D5285FADA5009C252D /* HistoryModel.swift */; };
D3776F22298A589200A09A90 /* CsvBuilder in Frameworks */ = {isa = PBXBuildFile; productRef = D3776F21298A589200A09A90 /* CsvBuilder */; };
D3A4EB18284F11A3002E3499 /* Csv2ImageAppApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3A4EB17284F11A3002E3499 /* Csv2ImageAppApp.swift */; };
D3A4EB1C284F11A4002E3499 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D3A4EB1B284F11A4002E3499 /* Assets.xcassets */; };
D3A4EB1F284F11A4002E3499 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D3A4EB1E284F11A4002E3499 /* Preview Assets.xcassets */; };
Expand Down Expand Up @@ -108,6 +109,7 @@
buildActionMask = 2147483647;
files = (
D3095CC6286394720086EEEE /* Csv2Img in Frameworks */,
D3776F22298A589200A09A90 /* CsvBuilder in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -310,6 +312,7 @@
name = Csv2ImageApp;
packageProductDependencies = (
D3095CC5286394720086EEEE /* Csv2Img */,
D3776F21298A589200A09A90 /* CsvBuilder */,
);
productName = Csv2ImageApp;
productReference = D3A4EB14284F11A3002E3499 /* Csv2ImageApp.app */;
Expand Down Expand Up @@ -818,6 +821,10 @@
isa = XCSwiftPackageProductDependency;
productName = Csv2Img;
};
D3776F21298A589200A09A90 /* CsvBuilder */ = {
isa = XCSwiftPackageProductDependency;
productName = CsvBuilder;
};
/* End XCSwiftPackageProductDependency section */

/* Begin XCVersionGroup section */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
"revision": "3303b164430d9a7055ba484c8ead67a52f7b74f6",
"version": "1.0.0"
}
},
{
"package": "SwiftSyntax",
"repositoryURL": "https://github.com/apple/swift-syntax",
"state": {
"branch": "main",
"revision": "dfb8deb846e16d98e1d5b2261ed608121e096037",
"version": null
}
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
D308AB9228B67E1900ECA831 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D308AB9128B67E1900ECA831 /* ContentView.swift */; };
D308AB9428B67E1A00ECA831 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D308AB9328B67E1A00ECA831 /* Assets.xcassets */; };
D308AB9828B67E1A00ECA831 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D308AB9728B67E1A00ECA831 /* Preview Assets.xcassets */; };
D34D4D3928B67F3200A018FB /* Csv2Img in Frameworks */ = {isa = PBXBuildFile; productRef = D34D4D3828B67F3200A018FB /* Csv2Img */; };
D34D4D3B28B67F3200A018FB /* CsvBuilder in Frameworks */ = {isa = PBXBuildFile; productRef = D34D4D3A28B67F3200A018FB /* CsvBuilder */; };
D3776F25298A58D500A09A90 /* Csv2Img in Frameworks */ = {isa = PBXBuildFile; productRef = D3776F24298A58D500A09A90 /* Csv2Img */; };
D3776F27298A58D500A09A90 /* CsvBuilder in Frameworks */ = {isa = PBXBuildFile; productRef = D3776F26298A58D500A09A90 /* CsvBuilder */; };
D3776F28298A5A1900A09A90 /* CsvCompositionExample.swift in Resources */ = {isa = PBXBuildFile; fileRef = D3C6FFB7298A543A00CAF771 /* CsvCompositionExample.swift */; };
D3776F2B298A5A5E00A09A90 /* CsvCompositionExample.txt in Resources */ = {isa = PBXBuildFile; fileRef = D3776F29298A5A3E00A09A90 /* CsvCompositionExample.txt */; };
D3776F2D298A7A7400A09A90 /* SecondContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3776F2C298A7A7400A09A90 /* SecondContentView.swift */; };
D3C6FFB8298A543A00CAF771 /* CsvCompositionExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3C6FFB7298A543A00CAF771 /* CsvCompositionExample.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -22,16 +26,19 @@
D308AB9328B67E1A00ECA831 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
D308AB9528B67E1A00ECA831 /* CsvBuilderExample.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = CsvBuilderExample.entitlements; sourceTree = "<group>"; };
D308AB9728B67E1A00ECA831 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
D308AB9F28B67E3700ECA831 /* Csv2Img */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = Csv2Img; path = ../..; sourceTree = "<group>"; };
D3776F23298A58C800A09A90 /* Csv2Img */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = Csv2Img; path = ../..; sourceTree = "<group>"; };
D3776F29298A5A3E00A09A90 /* CsvCompositionExample.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = CsvCompositionExample.txt; sourceTree = "<group>"; };
D3776F2C298A7A7400A09A90 /* SecondContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecondContentView.swift; sourceTree = "<group>"; };
D3C6FFB7298A543A00CAF771 /* CsvCompositionExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CsvCompositionExample.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
D308AB8928B67E1900ECA831 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
D34D4D3928B67F3200A018FB /* Csv2Img in Frameworks */,
D34D4D3B28B67F3200A018FB /* CsvBuilder in Frameworks */,
D3776F25298A58D500A09A90 /* Csv2Img in Frameworks */,
D3776F27298A58D500A09A90 /* CsvBuilder in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -41,7 +48,7 @@
D308AB8328B67E1900ECA831 = {
isa = PBXGroup;
children = (
D308AB9E28B67E3700ECA831 /* Packages */,
D3C6FFBC298A580E00CAF771 /* Packages */,
D308AB8E28B67E1900ECA831 /* CsvBuilderExample */,
D308AB8D28B67E1900ECA831 /* Products */,
D34D4D3728B67F3200A018FB /* Frameworks */,
Expand All @@ -59,10 +66,13 @@
D308AB8E28B67E1900ECA831 /* CsvBuilderExample */ = {
isa = PBXGroup;
children = (
D308AB8F28B67E1900ECA831 /* CsvBuilderExampleApp.swift */,
D308AB9528B67E1A00ECA831 /* CsvBuilderExample.entitlements */,
D308AB9128B67E1900ECA831 /* ContentView.swift */,
D308AB8F28B67E1900ECA831 /* CsvBuilderExampleApp.swift */,
D3C6FFB7298A543A00CAF771 /* CsvCompositionExample.swift */,
D3776F2C298A7A7400A09A90 /* SecondContentView.swift */,
D3776F29298A5A3E00A09A90 /* CsvCompositionExample.txt */,
D308AB9328B67E1A00ECA831 /* Assets.xcassets */,
D308AB9528B67E1A00ECA831 /* CsvBuilderExample.entitlements */,
D308AB9628B67E1A00ECA831 /* Preview Content */,
);
path = CsvBuilderExample;
Expand All @@ -76,19 +86,19 @@
path = "Preview Content";
sourceTree = "<group>";
};
D308AB9E28B67E3700ECA831 /* Packages */ = {
D34D4D3728B67F3200A018FB /* Frameworks */ = {
isa = PBXGroup;
children = (
D308AB9F28B67E3700ECA831 /* Csv2Img */,
);
name = Packages;
name = Frameworks;
sourceTree = "<group>";
};
D34D4D3728B67F3200A018FB /* Frameworks */ = {
D3C6FFBC298A580E00CAF771 /* Packages */ = {
isa = PBXGroup;
children = (
D3776F23298A58C800A09A90 /* Csv2Img */,
);
name = Frameworks;
name = Packages;
sourceTree = "<group>";
};
/* End PBXGroup section */
Expand All @@ -108,8 +118,8 @@
);
name = CsvBuilderExample;
packageProductDependencies = (
D34D4D3828B67F3200A018FB /* Csv2Img */,
D34D4D3A28B67F3200A018FB /* CsvBuilder */,
D3776F24298A58D500A09A90 /* Csv2Img */,
D3776F26298A58D500A09A90 /* CsvBuilder */,
);
productName = CsvBuilderExample;
productReference = D308AB8C28B67E1900ECA831 /* CsvBuilderExample.app */;
Expand Down Expand Up @@ -139,6 +149,8 @@
Base,
);
mainGroup = D308AB8328B67E1900ECA831;
packageReferences = (
);
productRefGroup = D308AB8D28B67E1900ECA831 /* Products */;
projectDirPath = "";
projectRoot = "";
Expand All @@ -153,6 +165,8 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D3776F2B298A5A5E00A09A90 /* CsvCompositionExample.txt in Resources */,
D3776F28298A5A1900A09A90 /* CsvCompositionExample.swift in Resources */,
D308AB9828B67E1A00ECA831 /* Preview Assets.xcassets in Resources */,
D308AB9428B67E1A00ECA831 /* Assets.xcassets in Resources */,
);
Expand All @@ -165,6 +179,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D3776F2D298A7A7400A09A90 /* SecondContentView.swift in Sources */,
D3C6FFB8298A543A00CAF771 /* CsvCompositionExample.swift in Sources */,
D308AB9228B67E1900ECA831 /* ContentView.swift in Sources */,
D308AB9028B67E1900ECA831 /* CsvBuilderExampleApp.swift in Sources */,
);
Expand Down Expand Up @@ -382,11 +398,11 @@
/* End XCConfigurationList section */

/* Begin XCSwiftPackageProductDependency section */
D34D4D3828B67F3200A018FB /* Csv2Img */ = {
D3776F24298A58D500A09A90 /* Csv2Img */ = {
isa = XCSwiftPackageProductDependency;
productName = Csv2Img;
};
D34D4D3A28B67F3200A018FB /* CsvBuilder */ = {
D3776F26298A58D500A09A90 /* CsvBuilder */ = {
isa = XCSwiftPackageProductDependency;
productName = CsvBuilder;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,36 @@
"repositoryURL": "https://github.com/apple/swift-argument-parser",
"state": {
"branch": null,
"revision": "df9ee6676cd5b3bf5b330ec7568a5644f547201b",
"version": "1.1.3"
"revision": "9f39744e025c7d377987f30b03770805dcb0bcd1",
"version": "1.1.4"
}
},
{
"package": "SwiftDocCPlugin",
"repositoryURL": "https://github.com/apple/swift-docc-plugin",
"state": {
"branch": null,
"revision": "3303b164430d9a7055ba484c8ead67a52f7b74f6",
"revision": "10bc670db657d11bdd561e07de30a9041311b2b1",
"version": "1.1.0"
}
},
{
"package": "SymbolKit",
"repositoryURL": "https://github.com/apple/swift-docc-symbolkit",
"state": {
"branch": null,
"revision": "b45d1f2ed151d057b54504d653e0da5552844e34",
"version": "1.0.0"
}
},
{
"package": "SwiftSyntax",
"repositoryURL": "https://github.com/apple/swift-syntax",
"state": {
"branch": "main",
"revision": "dfb8deb846e16d98e1d5b2261ed608121e096037",
"version": null
}
}
]
},
Expand Down
10 changes: 2 additions & 8 deletions Examples/CsvBuilderExample/CsvBuilderExample/ContentView.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
//
// ContentView.swift
// CsvBuilderExample
//
// Created by Fumiya Tanaka on 2022/08/25.
//

import SwiftUI
import CsvBuilder

struct ContentView: View {

@State private var composition: ExampleComposition = .init()
@State private var composition: CsvCompositionExample = .init()
@State private var image: CGImage?

var body: some View {
Expand All @@ -27,6 +20,7 @@ struct ContentView: View {
}
.padding()
.task {
composition = .init()
composition.ages.append(contentsOf: ["98", "99", "100"])
composition.names.append(contentsOf: ["Yamada", "Tanaka", "Sato"])
let csv = try! composition.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ import SwiftUI
struct CsvBuilderExampleApp: App {
var body: some Scene {
WindowGroup {
ContentView()
TabView {
ContentView()
.tabItem {
Text("Example 1")
}
SecondContentView()
.tabItem {
Text("Example 2")
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import CsvBuilder

struct CsvCompositionExample: CsvComposition {
@CsvRows(column: "age")
var ages: [String]

@CsvRows(column: "name")
var names: [String]

init() { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import CsvBuilder

struct CsvCompositionExample: CsvComposition {
@CsvRows(column: "age")
var ages: [String]

@CsvRows(column: "name")
var names: [String]

init() { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import SwiftUI
import Csv2Img
import CsvBuilder


struct SecondContentView: View {
@State private var composition: CsvCompositionExample = .init()
@State private var image: CGImage?

var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
if let image = image {
Image(nsImage: NSImage(cgImage: image, size: CGSize(width: image.width, height: image.height)))
.resizable()
.aspectRatio(contentMode: .fit)
}
}
.padding()
.task {
let yamada = Csv.Row(index: 0, values: ["98", "Yamada"])
let tanaka = Csv.Row(index: 0, values: ["99", "Tanaka"])
let sato = Csv.Row(index: 0, values: ["100", "Sato"])
let csv = try! CsvCompositionParser.parse(type: CsvCompositionExample.self, rows: [yamada, tanaka, sato,])
let data = try! await csv.generate(fontSize: 20, exportType: .png)
self.image = data.base as! CGImage
}
}
}

struct SecondContentView_Previews: PreviewProvider {
static var previews: some View {
SecondContentView()
}
}
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
"revision": "3303b164430d9a7055ba484c8ead67a52f7b74f6",
"version": "1.0.0"
}
},
{
"package": "SwiftSyntax",
"repositoryURL": "https://github.com/apple/swift-syntax",
"state": {
"branch": "main",
"revision": "dfb8deb846e16d98e1d5b2261ed608121e096037",
"version": null
}
}
]
},
Expand Down
11 changes: 9 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ let argumentParser: PackageDescription.Package.Dependency = .package(
let docc: PackageDescription.Package.Dependency = .package(
url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"
)
let swiftSyntax = PackageDescription.Package.Dependency.package(
url: "https://github.com/apple/swift-syntax",
branch: "main"
)

let package = Package(
name: "Csv2Img",
Expand All @@ -33,7 +37,8 @@ let package = Package(
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
argumentParser,
docc
docc,
swiftSyntax,
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand All @@ -48,7 +53,9 @@ let package = Package(
.target(
name: "CsvBuilder",
dependencies: [
"Csv2Img"
"Csv2Img",
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
]
),
.testTarget(
Expand Down
4 changes: 4 additions & 0 deletions Sources/Csv2Img/CsvColumn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ extension Csv.Column {
}
return styles
}

public static func random() -> Style {
random(count: 1).first!
}
}
}

Expand Down
Loading

0 comments on commit ca42f36

Please sign in to comment.