Skip to content

Commit

Permalink
Update components plugin (#116)
Browse files Browse the repository at this point in the history
* Update components plugin and deploy command

* Update the documentation
  • Loading branch information
mattesmohr authored Jan 7, 2023
1 parent ad19782 commit 4041787
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 42 deletions.
16 changes: 12 additions & 4 deletions Plugins/ComponentsPlugin/plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ struct ComponentsPlugin: CommandPlugin {
if usageArgument > 0 {

let explanation = """
USAGE: deploy
USAGE: deploy --target-path <path>
ARGUMENTS:
<target path> - The path, where the converted files should be saved into.
"""

print(explanation)

} else {

let targetArgument = extractor.extractOption(named: "target-path")

var processArguments = [String]()

if let dependency = try context.dependency(named: "HTMLKit") {
Expand All @@ -32,14 +35,19 @@ struct ComponentsPlugin: CommandPlugin {
processArguments.insert(target.directory.string, at: 0)

} else {
Diagnostics.error("Missing package.")
Diagnostics.error("The target 'HTMLKitComponents' could not be found.")
}

} else {
Diagnostics.error("Missing package.")
Diagnostics.error("The target 'HTMLKit' could not be found.")
}

processArguments.insert(context.package.directory.string, at: 1)
if let target = targetArgument.first {
processArguments.insert(target, at: 1)

} else {
Diagnostics.error("Missing argument --target-path.")
}

print("The deploy starts...")

Expand Down
67 changes: 35 additions & 32 deletions Sources/Commands/Components/DeployCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,58 @@ internal struct DeployCommand {
return CommandLine.arguments[2]
}

private static let manager = FileManager.default

internal static func main() throws {

if !FileManager.default.fileExists(atPath: sourcePath) {
if !manager.fileExists(atPath: sourcePath) {
print("No valid source path.")

exit(1)

} else {

let resourcesDirectory = URL(fileURLWithPath: sourcePath)
.appendingPathComponent("Resources", isDirectory: true)
try compose(source: sourcePath + "/Resources/css/", to: targetPath + "/htmlkit/", with: "all.css")

try compose(source: sourcePath + "/Resources/js/", to: targetPath + "/htmlkit/", with: "all.js")

let distributionFile = URL(fileURLWithPath: targetPath)
.appendingPathComponent("Public", isDirectory: true)
.appendingPathComponent("HtmlKit", isDirectory: true)
.appendingPathComponent("css")
.appendingPathComponent("all")
.appendingPathExtension("css")
exit(0)
}
}

/// Iterates through the directory and collects data to create a single file
private static func compose(source: String, to target: String, with filename: String) throws {

if !manager.fileExists(atPath: target) {
try manager.createDirectory(atPath: target, withIntermediateDirectories: true)
}

if !manager.fileExists(atPath: target + filename) {
manager.createFile(atPath: target + filename, contents: nil)
}

if let enumerator = manager.enumerator(at: URL(fileURLWithPath: source), includingPropertiesForKeys: nil, options: [.skipsHiddenFiles]) {

if let enumerator = FileManager.default.enumerator(at: resourcesDirectory, includingPropertiesForKeys: nil, options: [.skipsHiddenFiles]) {
for case let path as URL in enumerator {

for case let path as URL in enumerator {
if !path.hasDirectoryPath {

if !path.hasDirectoryPath {
if !path.isFileURL {
enumerator.skipDescendants()

if !path.isFileURL {
enumerator.skipDescendants()

} else {

let data = try Data(contentsOf: path)

if let handle = try? FileHandle(forWritingTo: distributionFile) {

handle.seekToEndOfFile()

handle.write(data)

handle.closeFile()

} else {
try data.write(to: distributionFile)
}
} else {

if let handle = FileHandle(forWritingAtPath: target + filename) {

handle.seekToEndOfFile()

handle.write(try Data(contentsOf: path))

handle.closeFile()
}
}
}
}

exit(0)
}
}
}
3 changes: 3 additions & 0 deletions Sources/HTMLKit/HTMLKit.docc/Framework/Components.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Text {
.onHover(perfom: .show("navigation"))
```

See the article <doc:ComponentsPlugin> before you get started.


## Topics

### Form components
Expand Down
32 changes: 28 additions & 4 deletions Sources/HTMLKit/HTMLKit.docc/Plugins/ComponentsPlugin.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
# Use the components
# Components plugin

Deploys the components dependencies
Deploys the components dependencies.

## Overview

The framework comes with a plugin, wich minifies and deploys the dependencies of the components into the public directory of your Vapor project.
The framework comes with a plugin, wich minifies and deploys the dependencies of the components at the target path.

### Command

Use the following command in your terminal to start the deployment.

```sh
swift package --allow-writing-to-package-directory deploy
swift package --allow-writing-to-package-directory deploy --target-path [target-path]
```

It will create a folder like this. Containing the composed CSS and JavaScript file.

```
htmlkit/
├── all.css
└── all.js
```

Feel free to move the files or copy the content at the place you want it to be.

### Steps

Open the terminal and change the directory (for example to your project folder).

```sh
cd ExampleProject
```

Enter the command and name the target path.

```sh
swift package --allow-writing-to-package-directory deploy --target-path "/ExampleProject/Public"
```
4 changes: 2 additions & 2 deletions Sources/HTMLKit/HTMLKit.docc/Plugins/ConverterPlugin.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Use conversion
# Converter plugin

Converts HTML markup
Converts HTML markup into HTMLKit syntax.

## Overview

Expand Down

0 comments on commit 4041787

Please sign in to comment.