From 4041787b6d3b02a1b781b2cb641018d54084542e Mon Sep 17 00:00:00 2001 From: Mattes Mohr Date: Sat, 7 Jan 2023 20:18:08 +0100 Subject: [PATCH] Update components plugin (#116) * Update components plugin and deploy command * Update the documentation --- Plugins/ComponentsPlugin/plugin.swift | 16 +++-- .../Commands/Components/DeployCommand.swift | 67 ++++++++++--------- .../HTMLKit.docc/Framework/Components.md | 3 + .../HTMLKit.docc/Plugins/ComponentsPlugin.md | 32 +++++++-- .../HTMLKit.docc/Plugins/ConverterPlugin.md | 4 +- 5 files changed, 80 insertions(+), 42 deletions(-) diff --git a/Plugins/ComponentsPlugin/plugin.swift b/Plugins/ComponentsPlugin/plugin.swift index 20b6f2cb..f732a0f8 100644 --- a/Plugins/ComponentsPlugin/plugin.swift +++ b/Plugins/ComponentsPlugin/plugin.swift @@ -15,15 +15,18 @@ struct ComponentsPlugin: CommandPlugin { if usageArgument > 0 { let explanation = """ - USAGE: deploy + USAGE: deploy --target-path ARGUMENTS: + - 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") { @@ -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...") diff --git a/Sources/Commands/Components/DeployCommand.swift b/Sources/Commands/Components/DeployCommand.swift index f2c9bb6a..f6321d81 100644 --- a/Sources/Commands/Components/DeployCommand.swift +++ b/Sources/Commands/Components/DeployCommand.swift @@ -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) } } } diff --git a/Sources/HTMLKit/HTMLKit.docc/Framework/Components.md b/Sources/HTMLKit/HTMLKit.docc/Framework/Components.md index 7f41b45b..9b304529 100644 --- a/Sources/HTMLKit/HTMLKit.docc/Framework/Components.md +++ b/Sources/HTMLKit/HTMLKit.docc/Framework/Components.md @@ -25,6 +25,9 @@ Text { .onHover(perfom: .show("navigation")) ``` +See the article before you get started. + + ## Topics ### Form components diff --git a/Sources/HTMLKit/HTMLKit.docc/Plugins/ComponentsPlugin.md b/Sources/HTMLKit/HTMLKit.docc/Plugins/ComponentsPlugin.md index fdc868b1..b179cd70 100644 --- a/Sources/HTMLKit/HTMLKit.docc/Plugins/ComponentsPlugin.md +++ b/Sources/HTMLKit/HTMLKit.docc/Plugins/ComponentsPlugin.md @@ -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" ``` diff --git a/Sources/HTMLKit/HTMLKit.docc/Plugins/ConverterPlugin.md b/Sources/HTMLKit/HTMLKit.docc/Plugins/ConverterPlugin.md index 74540fc8..9ec41a61 100644 --- a/Sources/HTMLKit/HTMLKit.docc/Plugins/ConverterPlugin.md +++ b/Sources/HTMLKit/HTMLKit.docc/Plugins/ConverterPlugin.md @@ -1,6 +1,6 @@ -# Use conversion +# Converter plugin -Converts HTML markup +Converts HTML markup into HTMLKit syntax. ## Overview