Skip to content

Commit

Permalink
Merge pull request #30 from IdeasOnCanvas/enhancement/cli-options
Browse files Browse the repository at this point in the history
Add more cli options
  • Loading branch information
Patrick-Kladek authored Feb 8, 2022
2 parents c6d84ce + 1d2473e commit 66dbf50
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 32 deletions.
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
disabled_rules:
- line_length
- inclusive_language
- nesting
- todo
excluded:
Expand Down
8 changes: 7 additions & 1 deletion Screenshot Framer CLI/Export.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ final class Export: ParsableCommand {
@Flag(help: "Ignore Warnings about potential issues like clipped text")
var ignoreWarnings: Bool = false

@Option(help: "Export only image with given number")
var image: Int?

@Option(help: "Export only the given language")
var language: String?

// MARK: - Export

func validate() throws {
Expand Down Expand Up @@ -84,7 +90,7 @@ extension Export {
exportController.delegate = self

ConsoleIO.writeMessage("Project: \(project.lastPathComponent)")
let exportErrors = exportController.saveAllImages()
let exportErrors = exportController.saveAllImages(language: self.language, start: self.image, end: self.image)

if self.checkedErrors(exportErrors).hasElements {
ConsoleIO.writeMessage("Something went wrong while exporting. Please check the projects for detailed information", to: .error)
Expand Down
9 changes: 5 additions & 4 deletions Screenshot Framer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@
};
CD9395551FF6AC6D00D3D831 = {
CreatedOnToolsVersion = 9.2;
LastSwiftMigration = 1310;
ProvisioningStyle = Automatic;
};
CDFF69941FD827B800E652EE = {
Expand Down Expand Up @@ -511,7 +512,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "# Swift Lint\n(exec ./.tools/SwiftLint/swiftlint)\n";
shellScript = "# Swift Lint\nexec swiftlint\n";
};
CDAEE1FC1FED46EE00F4B1BA /* Swift Lint */ = {
isa = PBXShellScriptBuildPhase;
Expand All @@ -525,7 +526,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "# Swift Lint\n(exec ./.tools/SwiftLint/swiftlint)\n";
shellScript = "# Swift Lint\nexec swiftlint\n";
};
/* End PBXShellScriptBuildPhase section */

Expand Down Expand Up @@ -802,7 +803,7 @@
ENABLE_HARDENED_RUNTIME = YES;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -815,7 +816,7 @@
ENABLE_HARDENED_RUNTIME = YES;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ final class ContentViewController: NSViewController, NSMenuItemValidation {
self.zoomToFit(nil)
}

//swiftlint:disable:next cyclomatic_complexity
// swiftlint:disable:next cyclomatic_complexity
func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
self.updateMenuItem(menuItem)
guard let action = menuItem.action else { return false }
Expand Down Expand Up @@ -300,7 +300,7 @@ final class ContentViewController: NSViewController, NSMenuItemValidation {
self.layoutController.highlightLayer = self.tableView.selectedRow
self.inspectorViewController?.updateUI()
self.inspectorViewController?.updateUIFromViewState()
self.scrollView.documentView = self.layoutController.layouthierarchy(layers: self.lastLayerState.layers)
self.scrollView.documentView = self.layoutController.layoutHierarchy(layers: self.lastLayerState.layers)
self.layoutWarningButton.isHidden = self.layoutController.layoutErrors.isEmpty

self.textFieldOutput.stringValue = self.lastLayerState.outputConfig.output
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,39 +44,43 @@ final class ExportController {
let fileManager = FileManager()
let viewStateController = ViewStateController(viewState: viewState)
let layoutController = LayoutController(viewStateController: viewStateController, languageController: self.languageController, fileController: self.fileController)
guard let view = layoutController.layouthierarchy(layers: self.lastLayerState.layers) else { return [.noLayers] }

let data = view.pngData()
guard let view = layoutController.layoutHierarchy(layers: self.lastLayerState.layers) else { return [.noLayers] }
guard let url = self.fileController.outputURL(for: self.lastLayerState, viewState: viewState) else { return [.noOutputFile] }

try? fileManager.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true, attributes: nil)
try? data?.write(to: url, options: .atomic)
if let data = view.pngData() {
try? data.write(to: url, options: .atomic)
}

return layoutController.layoutErrors
}

@discardableResult
func saveAllImages() -> [LayoutError] {
func saveAllImages(language: String? = nil, start: Int? = nil, end: Int? = nil) -> [LayoutError] {
self.shouldCancel = false

let viewStateController = ViewStateController()
let layoutController = LayoutController(viewStateController: viewStateController, languageController: self.languageController, fileController: self.fileController)
let fileManager = FileManager()

let totalSteps = self.calculatePossibleComabinations(languageController: self.languageController)
let totalSteps = self.calculatePossibleComabinations(languageController: self.languageController, langauge: language, start: start, end: end)
var currentStep = 0

for language in self.languageController.allLanguages() {
for language in self.languageController.allLanguages(prefered: language) {
viewStateController.newViewState(language: language)
for index in self.lastLayerState.outputConfig.fromImageNumber...self.lastLayerState.outputConfig.toImageNumber {
viewStateController.newViewState(imageNumber: index)
guard let view = layoutController.layouthierarchy(layers: self.lastLayerState.layers) else { continue } // TODO: is called from a background thread
guard let lower = self.lastLayerState.outputConfig.prefered(from: start) else { continue }
guard let upper = self.lastLayerState.outputConfig.prefered(end: end) else { continue }

let data = view.pngData()
for index in lower...upper {
viewStateController.newViewState(imageNumber: index)
guard let view = layoutController.layoutHierarchy(layers: self.lastLayerState.layers) else { continue } // TODO: is called from a background thread
guard let url = self.fileController.outputURL(for: self.lastLayerState, viewState: viewStateController.viewState) else { return [.noOutputFile] }

try? fileManager.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true, attributes: nil)
try? data?.write(to: url, options: .atomic)
if let data = view.pngData() {
try? data.write(to: url, options: .atomic)
}

currentStep += 1
let progress = Double(currentStep) / Double(totalSteps)
Expand Down Expand Up @@ -105,7 +109,7 @@ final class ExportController {
for currentImageNumber in 0...numberOfImages {
tempViewStateController.newViewState(imageNumber: currentImageNumber + firstImageNumber)

guard let image = tempLayoutController.layouthierarchy(layers: self.lastLayerState.layers) else { continue }
guard let image = tempLayoutController.layoutHierarchy(layers: self.lastLayerState.layers) else { continue }

let offsetX = image.frame.width * CGFloat(currentImageNumber)
image.frame = image.frame.offsetBy(dx: offsetX, dy: 0)
Expand Down Expand Up @@ -142,7 +146,7 @@ final class ExportController {
let language = allLanguages[currentImageNumber]
tempViewStateController.newViewState(language: language)

guard let image = tempLayoutController.layouthierarchy(layers: self.lastLayerState.layers) else { continue }
guard let image = tempLayoutController.layoutHierarchy(layers: self.lastLayerState.layers) else { continue }

let offsetX = image.frame.width * CGFloat(currentImageNumber)
image.frame = image.frame.offsetBy(dx: offsetX, dy: 0)
Expand Down Expand Up @@ -170,14 +174,19 @@ final class ExportController {

private extension ExportController {

func calculatePossibleComabinations(languageController: LanguageController) -> Int {
func calculatePossibleComabinations(languageController: LanguageController, langauge: String? = nil, start: Int? = nil, end: Int? = nil) -> Int {
let outputConfig = self.lastLayerState.outputConfig

if let lower = outputConfig.prefered(from: start), let upper = outputConfig.prefered(end: end) {
let totalSteps = upper - lower + 1
return languageController.allLanguages(prefered: langauge).count * totalSteps
}

// because we use a for-loop and `for n in 1...1` would
// mean 1 execution but (1 - 1 = 0) we handle this special case
// by adding +1 so the progressBar is still updated correctly
let totalSteps = outputConfig.toImageNumber - outputConfig.fromImageNumber + 1

return languageController.allLanguages().count * totalSteps
return languageController.allLanguages(prefered: langauge).count * totalSteps
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class LanguageController {

// MARK: - LanguageController

func allLanguages() -> [String] {
func allLanguages(prefered: String? = nil) -> [String] {
let fileManager = FileManager()
guard let projectURL = self.fileCapsule.projectURL else { return [] }
guard let contents = try? fileManager.contentsOfDirectory(at: projectURL, includingPropertiesForKeys: nil, options: [.skipsSubdirectoryDescendants, .skipsHiddenFiles]) else { return [] }
Expand All @@ -35,6 +35,12 @@ final class LanguageController {
}.compactMap { $0.lastPathComponent }

let blackList = ["backgrounds", "device_frames", "export"]
return allLanguages.subtracting(blackList, caseSensitive: false)
let filteredLanguages = allLanguages.subtracting(blackList, caseSensitive: false)
if let prefered = prefered {
if filteredLanguages.contains(where: { $0.caseInsensitiveCompare(prefered) == .orderedSame }) {
return [prefered]
}
}
return filteredLanguages
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class LayoutController {

// MARK: - Public Functions

func layouthierarchy(layers: [LayoutableObject]) -> NSView? {
func layoutHierarchy(layers: [LayoutableObject]) -> NSView? {
self.layoutErrors = []
guard layers.hasElements else { self.layoutErrors = [.noLayers]; return nil }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class ProgressWindowController: NSWindowController {
// MARK: - Properties
weak var delegate: ProgressWindowControllerDelegate?
var maxProgress: Double {
set { self.progressBar.maxValue = newValue }
get { return self.progressBar.maxValue }
set { self.progressBar.maxValue = newValue }
}

var progress: Double {
set { self.progressBar.doubleValue = newValue }
get { return self.progressBar.doubleValue }
set { self.progressBar.doubleValue = newValue }
}


Expand Down
22 changes: 22 additions & 0 deletions Screenshot Framer/Model/OutputConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,25 @@ struct OutputConfig: Codable {
let fromImageNumber: Int
let toImageNumber: Int
}

extension OutputConfig {

func prefered(from: Int?) -> Int? {
guard let from = from else { return nil }

if from >= self.fromImageNumber && from <= self.toImageNumber {
return from
}

return nil
}

func prefered(end: Int?) -> Int? {
guard let end = end else { return nil }

if end >= self.fromImageNumber && end <= self.toImageNumber {
return end
}
return nil
}
}
9 changes: 5 additions & 4 deletions Screenshot Framer/Supporting Files/Code/NSColor+Hex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ extension NSColor {
* - returns: NSColor with rgba value
*/
convenience init?(hex: String) {
var cString = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
cString.cleanHexPrefix()
var trimmed = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
trimmed.cleanHexPrefix()

var hexNumber: UInt32 = 0
Scanner(string: cString).scanHexInt32(&hexNumber)
var hexNumber: UInt64 = 0
Scanner(string: trimmed).scanHexInt64(&hexNumber)

self.init(red: CGFloat( (hexNumber & 0xFF000000) >> 24) / 255.0,
green: CGFloat( (hexNumber & 0x00FF0000) >> 16) / 255.0,
Expand All @@ -31,6 +31,7 @@ extension NSColor {

func hexString() -> String {
guard let standardizedColor = self.usingColorSpace(.sRGB) else { return "" }

let red = Int(standardizedColor.redComponent * 255.0)
let green = Int(standardizedColor.greenComponent * 255.0)
let blue = Int(standardizedColor.blueComponent * 255.0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class TimeTravelWindowController: NSWindowController {

override var document: AnyObject? {
get { return nil }
set {}
set {} // swiftlint:disable:this unused_setter_value
}


Expand Down

0 comments on commit 66dbf50

Please sign in to comment.