Skip to content

Commit 3c22a8a

Browse files
authored
Add UI components (#98)
* Rename the converter * Add the new targets * Add component files and modifier files * Add the css files * Add tests for the comp * Add the deploy command * Add the components plugin * Make the components and the plugin available to use
1 parent 913ab2b commit 3c22a8a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+4954
-9
lines changed

Package.swift

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ let package = Package(
1010
products: [
1111
.library(
1212
name: "HTMLKit",
13-
targets: ["HTMLKit"]
13+
targets: ["HTMLKit", "HTMLKitComponents"]
14+
),
15+
.plugin(
16+
name: "ComponentsPlugin",
17+
targets: ["ComponentsPlugin"]
1418
),
1519
.plugin(
1620
name: "ConverterPlugin",
@@ -31,11 +35,20 @@ let package = Package(
3135
]
3236
),
3337
.target(
34-
name: "Converter",
38+
name: "HTMLKitConverter",
3539
dependencies: [
3640
.target(name: "HTMLKit")
3741
]
3842
),
43+
.target(
44+
name: "HTMLKitComponents",
45+
dependencies: [
46+
.target(name: "HTMLKit")
47+
],
48+
resources: [
49+
.process("Resources")
50+
]
51+
),
3952
.testTarget(
4053
name: "HTMLKitTests",
4154
dependencies: [
@@ -46,27 +59,58 @@ let package = Package(
4659
]
4760
),
4861
.testTarget(
49-
name: "ConverterTests",
62+
name: "HTMLKitConverterTests",
5063
dependencies: [
51-
.target(name: "Converter")
64+
.target(name: "HTMLKitConverter")
5265
],
5366
resources: [
5467
.process("Conversion")
5568
]
5669
),
70+
.testTarget(
71+
name: "HTMLKitComponentsTests",
72+
dependencies: [
73+
.target(name: "HTMLKitComponents"),
74+
.target(name: "HTMLKit")
75+
]
76+
),
5777
.executableTarget(
5878
name: "ConvertCommand",
5979
dependencies: [
60-
.target(name: "Converter")
80+
.target(name: "HTMLKitConverter")
6181
],
62-
path: "Sources/Commands"
82+
path: "Sources/Commands/Converter"
83+
),
84+
.executableTarget(
85+
name: "DeployCommand",
86+
dependencies: [
87+
.target(name: "HTMLKitComponents")
88+
],
89+
path: "Sources/Commands/Components"
6390
),
6491
.plugin(
6592
name: "ConverterPlugin",
66-
capability: .command(intent: .custom(verb: "convert", description: "Convert html content"), permissions: [.writeToPackageDirectory(reason: "The command needs the permission to create the converted file.")]),
93+
capability: .command(
94+
intent: .custom(
95+
verb: "convert",
96+
description: "Convert html content"),
97+
permissions: [.writeToPackageDirectory(reason: "The command needs the permission to create the converted file.")]
98+
),
6799
dependencies: [
68100
.target(name: "ConvertCommand")
69101
]
102+
),
103+
.plugin(
104+
name: "ComponentsPlugin",
105+
capability: .command(
106+
intent: .custom(
107+
verb: "deploy",
108+
description: "Deploy css files"),
109+
permissions: [.writeToPackageDirectory(reason: "The command needs the permission to create the minified css file.")]
110+
),
111+
dependencies: [
112+
.target(name: "DeployCommand")
113+
]
70114
)
71115
]
72116
)

Plugins/ComponentsPlugin/plugin.swift

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import PackagePlugin
2+
import Foundation
3+
4+
@main
5+
struct ConverterPlugin: CommandPlugin {
6+
7+
func performCommand(context: PluginContext, arguments: [String]) async throws {
8+
9+
let tool = try context.tool(named: "DeployCommand")
10+
11+
var extractor = ArgumentExtractor(arguments)
12+
13+
let usageArgument = extractor.extractFlag(named: "command-usage")
14+
15+
if usageArgument > 0 {
16+
17+
let explanation = """
18+
USAGE: deploy
19+
"""
20+
21+
print(explanation)
22+
23+
} else {
24+
25+
var processArguments = [String]()
26+
27+
if let dependency = try context.dependency(named: "HTMLKit") {
28+
29+
if let target = try? dependency.package.targets(named: ["HTMLKitComponents"]).first {
30+
processArguments.insert(target.directory.string, at: 0)
31+
32+
} else {
33+
Diagnostics.error("Missing package.")
34+
}
35+
36+
} else {
37+
Diagnostics.error("Missing package.")
38+
}
39+
40+
processArguments.insert(context.package.directory.string, at: 1)
41+
42+
print("The deploy starts...")
43+
44+
let process = try Process.run(URL(fileURLWithPath: tool.path.string), arguments: processArguments)
45+
process.waitUntilExit()
46+
47+
if process.terminationReason == .exit && process.terminationStatus == 0 {
48+
print("The deploy has finished.")
49+
50+
} else {
51+
Diagnostics.error("The deploy has failed: \(process.terminationReason)")
52+
}
53+
54+
}
55+
}
56+
}
57+
58+
extension PluginContext {
59+
60+
public func dependency(named: String) throws -> PackageDependency? {
61+
62+
for dependency in self.package.dependencies {
63+
64+
if dependency.package.displayName == named {
65+
return dependency
66+
}
67+
}
68+
69+
return nil
70+
}
71+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import Foundation
2+
3+
@main
4+
internal struct DeployCommand {
5+
6+
private static var sourcePath: String {
7+
return CommandLine.arguments[1]
8+
}
9+
10+
private static var targetPath: String {
11+
return CommandLine.arguments[2]
12+
}
13+
14+
internal static func main() throws {
15+
16+
if !FileManager.default.fileExists(atPath: sourcePath) {
17+
print("No valid source path.")
18+
19+
exit(1)
20+
21+
} else {
22+
23+
let resourcesDirectory = URL(fileURLWithPath: sourcePath)
24+
.appendingPathComponent("Resources", isDirectory: true)
25+
26+
let distributionFile = URL(fileURLWithPath: targetPath)
27+
.appendingPathComponent("Public")
28+
.appendingPathComponent("css")
29+
.appendingPathComponent("all")
30+
.appendingPathExtension("css")
31+
32+
if let enumerator = FileManager.default.enumerator(at: resourcesDirectory, includingPropertiesForKeys: nil, options: [.skipsHiddenFiles]) {
33+
34+
for case let path as URL in enumerator {
35+
36+
if !path.hasDirectoryPath {
37+
38+
if !path.isFileURL {
39+
enumerator.skipDescendants()
40+
41+
} else {
42+
43+
let data = try Data(contentsOf: path)
44+
45+
if let handle = try? FileHandle(forWritingTo: distributionFile) {
46+
47+
handle.seekToEndOfFile()
48+
49+
handle.write(data)
50+
51+
handle.closeFile()
52+
53+
} else {
54+
try data.write(to: distributionFile)
55+
}
56+
}
57+
}
58+
}
59+
}
60+
61+
exit(0)
62+
}
63+
}
64+
}

Sources/Commands/Converter/ConvertCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Foundation
2-
import Converter
2+
import HTMLKitConverter
33

44
@main
55
@available(macOS 11.0, *)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Abstract:
3+
The file contains the actions for the components.
4+
*/
5+
6+
public enum Actions {
7+
8+
case show(_ target: String)
9+
case hide(_ target: String)
10+
case animate(_ target: String)
11+
12+
public var script: String {
13+
14+
switch self {
15+
case .show(let target):
16+
return show(target)
17+
18+
case .hide(let target):
19+
return hide(target)
20+
21+
case .animate(let target):
22+
return animate(target)
23+
}
24+
}
25+
26+
private func show(_ target: String) -> String {
27+
28+
return "$('#\(target)').show();"
29+
}
30+
31+
private func hide(_ target: String) -> String {
32+
33+
return "$('#\(target)').hide();"
34+
}
35+
36+
private func animate(_ target: String) -> String {
37+
38+
return "$('#\(target)').animate();"
39+
}
40+
}

0 commit comments

Comments
 (0)