From 0b3de91d5c9af97b63bb2c977f1d139630c4f026 Mon Sep 17 00:00:00 2001 From: Mattes Mohr Date: Mon, 19 Sep 2022 21:07:43 +0200 Subject: [PATCH] Add a plugin for the conversion (#97) * Separate the converter from htmlkit and declare it as an independent target * Add the converter plugin * Refactor the converter Separate the parsing method from the converter by creating a new class and also use the string conversion as the primary method. * Add the convert command * Update the converter plugin * Refactor the converter Change the access of the method for the string conversion to private . Also unify the conversion methods for files and directories. * Update the convert command * Update the converter plugin * Add a check for the output option Debug and file should be the only allowed options. The check should make sure of it. --- Package.swift | 34 +- Plugins/ConverterPlugin/plugin.swift | 74 + .../Commands/Converter/ConvertCommand.swift | 69 + Sources/Converter/Converter.swift | 117 ++ Sources/Converter/InitRepresentable.swift | 39 + Sources/Converter/Parser.swift | 1772 +++++++++++++++++ .../Features/Conversion/Converter.swift | 1506 -------------- .../Conversion/articles/article.html | 0 .../Conversion/component.html | 0 .../Conversion/index.html | 0 .../ConversionTests.swift | 21 +- 11 files changed, 2105 insertions(+), 1527 deletions(-) create mode 100644 Plugins/ConverterPlugin/plugin.swift create mode 100644 Sources/Commands/Converter/ConvertCommand.swift create mode 100644 Sources/Converter/Converter.swift create mode 100644 Sources/Converter/InitRepresentable.swift create mode 100644 Sources/Converter/Parser.swift delete mode 100644 Sources/HTMLKit/Internal/Features/Conversion/Converter.swift rename Tests/{HTMLKitTests => ConverterTests}/Conversion/articles/article.html (100%) rename Tests/{HTMLKitTests => ConverterTests}/Conversion/component.html (100%) rename Tests/{HTMLKitTests => ConverterTests}/Conversion/index.html (100%) rename Tests/{HTMLKitTests => ConverterTests}/ConversionTests.swift (53%) diff --git a/Package.swift b/Package.swift index 365be613..2350e9d4 100644 --- a/Package.swift +++ b/Package.swift @@ -26,13 +26,43 @@ let package = Package( .product(name: "Collections", package: "swift-collections") ] ), + .target( + name: "Converter", + dependencies: [ + .target(name: "HTMLKit") + ] + ), .testTarget( name: "HTMLKitTests", - dependencies: ["HTMLKit"], + dependencies: [ + .target(name: "HTMLKit") + ], + resources: [ + .process("Localization") + ] + ), + .testTarget( + name: "ConverterTests", + dependencies: [ + .target(name: "Converter") + ], resources: [ - .process("Localization"), .process("Conversion") ] + ), + .executableTarget( + name: "ConvertCommand", + dependencies: [ + .target(name: "Converter") + ], + path: "Sources/Commands" + ), + .plugin( + name: "ConverterPlugin", + capability: .command(intent: .custom(verb: "convert", description: "Convert html content"), permissions: [.writeToPackageDirectory(reason: "The command needs the permission to create the converted file.")]), + dependencies: [ + .target(name: "ConvertCommand") + ] ) ] ) diff --git a/Plugins/ConverterPlugin/plugin.swift b/Plugins/ConverterPlugin/plugin.swift new file mode 100644 index 00000000..6472d7c2 --- /dev/null +++ b/Plugins/ConverterPlugin/plugin.swift @@ -0,0 +1,74 @@ +import PackagePlugin +import Foundation + +@main +struct ConverterPlugin: CommandPlugin { + + func performCommand(context: PluginContext, arguments: [String]) async throws { + + let outputOptions = ["debug", "file"] + + let tool = try context.tool(named: "ConvertCommand") + + var extractor = ArgumentExtractor(arguments) + + let usageArgument = extractor.extractFlag(named: "command-usage") + + if usageArgument > 0 { + + let explanation = """ + USAGE: convert --output-option