From c20e8e2d4c88d524aa7fbec44f9b7632611f730a Mon Sep 17 00:00:00 2001 From: Vlad Gorlov Date: Wed, 9 Oct 2019 04:43:35 +0200 Subject: [PATCH] Added Linux support. (#172) * Added check for os(Linux) * Sources adopted to Swift Linux Compiler v5.0 * Added Docker configuration to be used for Linux builds. * Renamed Script. * Docker configuration update. * Fixes Linux compile errors. * Nicification. --- LinuxMain.swift | 7 +++++++ .../Attribute Collections/ColorCollection.swift | 2 +- .../Attribute Collections/FontCollection.swift | 2 +- .../ParagraphStyleCollection.swift | 2 +- .../BlockBackgroundColorAttribute.swift | 2 +- .../QuoteStripeAttribute.swift | 2 +- .../ThematicBreakAttribute.swift | 2 +- .../Helpers/Extensions/CGPoint+Translate.swift | 4 ++++ .../Helpers/Extensions/CGRect+Helpers.swift | 4 ++++ .../Helpers/Extensions/UIFont+Traits.swift | 2 +- .../Helpers/ListItemParagraphStyler.swift | 2 +- .../DownDebugLayoutManager.swift | 2 +- .../Layout Managers/DownLayoutManager.swift | 2 +- .../AST/Styling/Options/CodeBlockOptions.swift | 2 +- .../AST/Styling/Options/ListItemOptions.swift | 2 +- .../Styling/Options/QuoteStripeOptions.swift | 4 ++++ .../Styling/Options/ThematicBreakOptions.swift | 2 +- Source/AST/Styling/Stylers/DownStyler.swift | 2 +- .../Stylers/DownStylerConfiguration.swift | 2 +- .../Styling/Text Views/DownDebugTextView.swift | 2 +- .../AST/Styling/Text Views/DownTextView.swift | 2 +- .../AST/Visitors/AttributedStringVisitor.swift | 3 +++ Source/Down.swift | 7 +++++-- Source/Extensions/NSAttributedString+HTML.swift | 3 +++ .../DownAttributedStringRenderable.swift | 2 ++ Source/Views/DownView.swift | 2 ++ docker/Dockerfile | 6 ++++++ docker/README.md | 6 ++++++ docker/docker-compose.yml | 12 ++++++++++++ docker/down-rebuild.sh | 17 +++++++++++++++++ 30 files changed, 92 insertions(+), 19 deletions(-) create mode 100644 LinuxMain.swift create mode 100644 docker/Dockerfile create mode 100644 docker/README.md create mode 100644 docker/docker-compose.yml create mode 100755 docker/down-rebuild.sh diff --git a/LinuxMain.swift b/LinuxMain.swift new file mode 100644 index 0000000..b45b7af --- /dev/null +++ b/LinuxMain.swift @@ -0,0 +1,7 @@ +import XCTest + +var tests = [XCTestCaseEntry]() + +// FIXME: Run on macOS `swift test --generate-linuxmain` and maintain Linux Tests. + +XCTMain(tests) diff --git a/Source/AST/Styling/Attribute Collections/ColorCollection.swift b/Source/AST/Styling/Attribute Collections/ColorCollection.swift index 25b0093..0ed19ff 100644 --- a/Source/AST/Styling/Attribute Collections/ColorCollection.swift +++ b/Source/AST/Styling/Attribute Collections/ColorCollection.swift @@ -6,7 +6,7 @@ // Copyright © 2016-2019 Down. All rights reserved. // -#if !os(watchOS) +#if !os(watchOS) && !os(Linux) #if canImport(UIKit) diff --git a/Source/AST/Styling/Attribute Collections/FontCollection.swift b/Source/AST/Styling/Attribute Collections/FontCollection.swift index ff78724..10ff93c 100644 --- a/Source/AST/Styling/Attribute Collections/FontCollection.swift +++ b/Source/AST/Styling/Attribute Collections/FontCollection.swift @@ -6,7 +6,7 @@ // Copyright © 2016-2019 Down. All rights reserved. // -#if !os(watchOS) +#if !os(watchOS) && !os(Linux) #if canImport(UIKit) diff --git a/Source/AST/Styling/Attribute Collections/ParagraphStyleCollection.swift b/Source/AST/Styling/Attribute Collections/ParagraphStyleCollection.swift index 70da7f2..fb4b989 100644 --- a/Source/AST/Styling/Attribute Collections/ParagraphStyleCollection.swift +++ b/Source/AST/Styling/Attribute Collections/ParagraphStyleCollection.swift @@ -6,7 +6,7 @@ // Copyright © 2016-2019 Down. All rights reserved. // -#if !os(watchOS) +#if !os(watchOS) && !os(Linux) #if canImport(UIKit) diff --git a/Source/AST/Styling/Custom Attributes/BlockBackgroundColorAttribute.swift b/Source/AST/Styling/Custom Attributes/BlockBackgroundColorAttribute.swift index 391c5a0..d5ff2f7 100644 --- a/Source/AST/Styling/Custom Attributes/BlockBackgroundColorAttribute.swift +++ b/Source/AST/Styling/Custom Attributes/BlockBackgroundColorAttribute.swift @@ -6,7 +6,7 @@ // Copyright © 2016-2019 Down. All rights reserved. // -#if !os(watchOS) +#if !os(watchOS) && !os(Linux) #if canImport(UIKit) diff --git a/Source/AST/Styling/Custom Attributes/QuoteStripeAttribute.swift b/Source/AST/Styling/Custom Attributes/QuoteStripeAttribute.swift index e97e9d2..e2b2ca2 100644 --- a/Source/AST/Styling/Custom Attributes/QuoteStripeAttribute.swift +++ b/Source/AST/Styling/Custom Attributes/QuoteStripeAttribute.swift @@ -6,7 +6,7 @@ // Copyright © 2016-2019 Down. All rights reserved. // -#if !os(watchOS) +#if !os(watchOS) && !os(Linux) #if canImport(UIKit) diff --git a/Source/AST/Styling/Custom Attributes/ThematicBreakAttribute.swift b/Source/AST/Styling/Custom Attributes/ThematicBreakAttribute.swift index d199a59..2c280d6 100644 --- a/Source/AST/Styling/Custom Attributes/ThematicBreakAttribute.swift +++ b/Source/AST/Styling/Custom Attributes/ThematicBreakAttribute.swift @@ -6,7 +6,7 @@ // Copyright © 2016-2019 Down. All rights reserved. // -#if !os(watchOS) +#if !os(watchOS) && !os(Linux) #if canImport(UIKit) diff --git a/Source/AST/Styling/Helpers/Extensions/CGPoint+Translate.swift b/Source/AST/Styling/Helpers/Extensions/CGPoint+Translate.swift index 9c224f8..2ecb5a0 100644 --- a/Source/AST/Styling/Helpers/Extensions/CGPoint+Translate.swift +++ b/Source/AST/Styling/Helpers/Extensions/CGPoint+Translate.swift @@ -6,6 +6,8 @@ // Copyright © 2016-2019 Down. All rights reserved. // +#if !os(Linux) + #if canImport(UIKit) import UIKit @@ -22,3 +24,5 @@ extension CGPoint { return CGPoint(x: x + point.x, y: y + point.y) } } + +#endif diff --git a/Source/AST/Styling/Helpers/Extensions/CGRect+Helpers.swift b/Source/AST/Styling/Helpers/Extensions/CGRect+Helpers.swift index 89adaac..57b2f2c 100644 --- a/Source/AST/Styling/Helpers/Extensions/CGRect+Helpers.swift +++ b/Source/AST/Styling/Helpers/Extensions/CGRect+Helpers.swift @@ -6,6 +6,8 @@ // Copyright © 2016-2019 Down. All rights reserved. // +#if !os(Linux) + #if canImport(UIKit) import UIKit @@ -26,3 +28,5 @@ extension CGRect { return CGRect(origin: origin.translated(by: point), size: size) } } + +#endif diff --git a/Source/AST/Styling/Helpers/Extensions/UIFont+Traits.swift b/Source/AST/Styling/Helpers/Extensions/UIFont+Traits.swift index a94af1f..72dc056 100644 --- a/Source/AST/Styling/Helpers/Extensions/UIFont+Traits.swift +++ b/Source/AST/Styling/Helpers/Extensions/UIFont+Traits.swift @@ -6,7 +6,7 @@ // Copyright © 2016-2019 Down. All rights reserved. // -#if !os(watchOS) +#if !os(watchOS) && !os(Linux) #if canImport(UIKit) diff --git a/Source/AST/Styling/Helpers/ListItemParagraphStyler.swift b/Source/AST/Styling/Helpers/ListItemParagraphStyler.swift index 90b54ec..f010775 100644 --- a/Source/AST/Styling/Helpers/ListItemParagraphStyler.swift +++ b/Source/AST/Styling/Helpers/ListItemParagraphStyler.swift @@ -6,7 +6,7 @@ // Copyright © 2016-2019 Down. All rights reserved. // -#if !os(watchOS) +#if !os(watchOS) && !os(Linux) #if canImport(UIKit) diff --git a/Source/AST/Styling/Layout Managers/DownDebugLayoutManager.swift b/Source/AST/Styling/Layout Managers/DownDebugLayoutManager.swift index 997f09c..e96835d 100644 --- a/Source/AST/Styling/Layout Managers/DownDebugLayoutManager.swift +++ b/Source/AST/Styling/Layout Managers/DownDebugLayoutManager.swift @@ -6,7 +6,7 @@ // Copyright © 2016-2019 Down. All rights reserved. // -#if !os(watchOS) +#if !os(watchOS) && !os(Linux) #if canImport(UIKit) diff --git a/Source/AST/Styling/Layout Managers/DownLayoutManager.swift b/Source/AST/Styling/Layout Managers/DownLayoutManager.swift index a717047..dea1078 100644 --- a/Source/AST/Styling/Layout Managers/DownLayoutManager.swift +++ b/Source/AST/Styling/Layout Managers/DownLayoutManager.swift @@ -6,7 +6,7 @@ // Copyright © 2016-2019 Down. All rights reserved. // -#if !os(watchOS) +#if !os(watchOS) && !os(Linux) #if canImport(UIKit) diff --git a/Source/AST/Styling/Options/CodeBlockOptions.swift b/Source/AST/Styling/Options/CodeBlockOptions.swift index 10a041e..9b19142 100644 --- a/Source/AST/Styling/Options/CodeBlockOptions.swift +++ b/Source/AST/Styling/Options/CodeBlockOptions.swift @@ -6,7 +6,7 @@ // Copyright © 2016-2019 Down. All rights reserved. // -#if !os(watchOS) +#if !os(watchOS) && !os(Linux) #if canImport(UIKit) diff --git a/Source/AST/Styling/Options/ListItemOptions.swift b/Source/AST/Styling/Options/ListItemOptions.swift index 244cf61..765f18f 100644 --- a/Source/AST/Styling/Options/ListItemOptions.swift +++ b/Source/AST/Styling/Options/ListItemOptions.swift @@ -6,7 +6,7 @@ // Copyright © 2016-2019 Down. All rights reserved. // -#if !os(watchOS) +#if !os(watchOS) && !os(Linux) #if canImport(UIKit) diff --git a/Source/AST/Styling/Options/QuoteStripeOptions.swift b/Source/AST/Styling/Options/QuoteStripeOptions.swift index f5439b7..f70525e 100644 --- a/Source/AST/Styling/Options/QuoteStripeOptions.swift +++ b/Source/AST/Styling/Options/QuoteStripeOptions.swift @@ -6,6 +6,8 @@ // Copyright © 2016-2019 Down. All rights reserved. // +#if !os(Linux) + #if canImport(UIKit) import UIKit @@ -21,3 +23,5 @@ public struct QuoteStripeOptions { public var thickness: CGFloat = 2 public var spacingAfter: CGFloat = 8 } + +#endif diff --git a/Source/AST/Styling/Options/ThematicBreakOptions.swift b/Source/AST/Styling/Options/ThematicBreakOptions.swift index e44e05e..873f72e 100644 --- a/Source/AST/Styling/Options/ThematicBreakOptions.swift +++ b/Source/AST/Styling/Options/ThematicBreakOptions.swift @@ -6,7 +6,7 @@ // Copyright © 2016-2019 Down. All rights reserved. // -#if !os(watchOS) +#if !os(watchOS) && !os(Linux) #if canImport(UIKit) diff --git a/Source/AST/Styling/Stylers/DownStyler.swift b/Source/AST/Styling/Stylers/DownStyler.swift index 6622132..5f669a3 100644 --- a/Source/AST/Styling/Stylers/DownStyler.swift +++ b/Source/AST/Styling/Stylers/DownStyler.swift @@ -6,7 +6,7 @@ // Copyright © 2016-2019 Down. All rights reserved. // -#if !os(watchOS) +#if !os(watchOS) && !os(Linux) #if canImport(UIKit) diff --git a/Source/AST/Styling/Stylers/DownStylerConfiguration.swift b/Source/AST/Styling/Stylers/DownStylerConfiguration.swift index 0e8ee20..ebb7355 100644 --- a/Source/AST/Styling/Stylers/DownStylerConfiguration.swift +++ b/Source/AST/Styling/Stylers/DownStylerConfiguration.swift @@ -6,7 +6,7 @@ // Copyright © 2016-2019 Down. All rights reserved. // -#if !os(watchOS) +#if !os(watchOS) && !os(Linux) /// A configuration object used to initialze the `DownStyler`. public struct DownStylerConfiguration { diff --git a/Source/AST/Styling/Text Views/DownDebugTextView.swift b/Source/AST/Styling/Text Views/DownDebugTextView.swift index 995f236..8c97b64 100644 --- a/Source/AST/Styling/Text Views/DownDebugTextView.swift +++ b/Source/AST/Styling/Text Views/DownDebugTextView.swift @@ -6,7 +6,7 @@ // Copyright © 2016-2019 Down. All rights reserved. // -#if !os(watchOS) +#if !os(watchOS) && !os(Linux) #if canImport(UIKit) diff --git a/Source/AST/Styling/Text Views/DownTextView.swift b/Source/AST/Styling/Text Views/DownTextView.swift index cb0191c..98982e9 100644 --- a/Source/AST/Styling/Text Views/DownTextView.swift +++ b/Source/AST/Styling/Text Views/DownTextView.swift @@ -6,7 +6,7 @@ // Copyright © 2016-2019 Down. All rights reserved. // -#if !os(watchOS) +#if !os(watchOS) && !os(Linux) #if canImport(UIKit) diff --git a/Source/AST/Visitors/AttributedStringVisitor.swift b/Source/AST/Visitors/AttributedStringVisitor.swift index 0a48916..5134e0a 100644 --- a/Source/AST/Visitors/AttributedStringVisitor.swift +++ b/Source/AST/Visitors/AttributedStringVisitor.swift @@ -5,6 +5,8 @@ // Created by John Nguyen on 09.04.19. // +#if !os(Linux) + import Foundation /// This class is used to generated an `NSMutableAttributedString` from the abstract syntax @@ -221,3 +223,4 @@ private extension String { return lines.joined(separator: .lineSeparator) } } +#endif // !os(Linux) diff --git a/Source/Down.swift b/Source/Down.swift index 4746bdb..22d695e 100644 --- a/Source/Down.swift +++ b/Source/Down.swift @@ -9,8 +9,7 @@ import Foundation public struct Down: DownASTRenderable, DownHTMLRenderable, DownXMLRenderable, - DownLaTeXRenderable, DownGroffRenderable, DownCommonMarkRenderable, - DownAttributedStringRenderable { + DownLaTeXRenderable, DownGroffRenderable, DownCommonMarkRenderable { /// A string containing CommonMark Markdown public var markdownString: String @@ -21,3 +20,7 @@ public struct Down: DownASTRenderable, DownHTMLRenderable, DownXMLRenderable, self.markdownString = markdownString } } + +#if !os(Linux) +extension Down: DownAttributedStringRenderable { } +#endif // !os(Linux) diff --git a/Source/Extensions/NSAttributedString+HTML.swift b/Source/Extensions/NSAttributedString+HTML.swift index e9e74a6..c480757 100644 --- a/Source/Extensions/NSAttributedString+HTML.swift +++ b/Source/Extensions/NSAttributedString+HTML.swift @@ -6,6 +6,8 @@ // Copyright © 2016-2019 Down. All rights reserved. // +#if !os(Linux) + #if os(macOS) import AppKit #else @@ -32,3 +34,4 @@ extension NSAttributedString { } } +#endif // !os(Linux) diff --git a/Source/Renderers/DownAttributedStringRenderable.swift b/Source/Renderers/DownAttributedStringRenderable.swift index 76e00ce..c274712 100644 --- a/Source/Renderers/DownAttributedStringRenderable.swift +++ b/Source/Renderers/DownAttributedStringRenderable.swift @@ -6,6 +6,7 @@ // Copyright © 2016-2019 Down. All rights reserved. // +#if !os(Linux) import Foundation import libcmark @@ -56,3 +57,4 @@ extension DownAttributedStringRenderable { return document.accept(visitor) } } +#endif // !os(Linux) diff --git a/Source/Views/DownView.swift b/Source/Views/DownView.swift index 840e5c1..4b95eef 100644 --- a/Source/Views/DownView.swift +++ b/Source/Views/DownView.swift @@ -6,6 +6,7 @@ // Copyright © 2016-2019 Down. All rights reserved. // +#if !os(Linux) #if os(tvOS) || os(watchOS) // Sorry, not available for tvOS nor watchOS #else @@ -205,3 +206,4 @@ private extension WKNavigationDelegate { } #endif +#endif // !os(Linux) diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..b91e39e --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,6 @@ +# You can set the Swift version to what you need for your app. Versions can be found here: https://hub.docker.com/_/swift +FROM swift:5.1 + +RUN apt-get -qq update \ + && apt-get -q -y install libssl-dev zlib1g-dev \ + && rm -r /var/lib/apt/lists/* diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 0000000..b0abcf6 --- /dev/null +++ b/docker/README.md @@ -0,0 +1,6 @@ +# Building on Linux (Docker) + +- Go to "docker" directory: `cd $REPO/docker` +- Compose docket image: `docker-compose build`. +- Build sources: `docker-compose run --rm down`. +- Login docker image (i.e. to troubleshoot build failures): `docker-compose run --rm down bash`. After that you can run `swift build --package-path /app` or `cd /app && swift build`. diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml new file mode 100644 index 0000000..0e632c3 --- /dev/null +++ b/docker/docker-compose.yml @@ -0,0 +1,12 @@ +version: "3.3" +services: + down: + build: + context: . + dockerfile: Dockerfile + volumes: + - ../:/app + working_dir: /app + stdin_open: true + tty: true + command: /app/docker/down-rebuild.sh diff --git a/docker/down-rebuild.sh b/docker/down-rebuild.sh new file mode 100755 index 0000000..a632316 --- /dev/null +++ b/docker/down-rebuild.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +echo "- Cleaning App" +swift package clean +if [ $? != 0 ]; then + echo "❌ Linux build failed." + exit 1 +fi + +echo "- Building App" +swift build --configuration release +if [ $? != 0 ]; then + echo "❌ Linux build failed." + exit 1 +fi + +echo "✅ Linux build completed!"