DEVTOOLS-186 Exporter les images de resgen en svg #12
@ -87,38 +87,42 @@ class XcassetsGenerator {
|
|||||||
|
|
||||||
let convertArguments = parsedImage.convertArguments
|
let convertArguments = parsedImage.convertArguments
|
||||||
|
|
||||||
if parsedImage.imageExtensions.contains(.vector) {
|
if imageData.ext == "svg" {
|
||||||
let output = "\(imagesetPath)/\(parsedImage.name).\(OutputImageExtension.svg.rawValue)"
|
if parsedImage.imageExtensions.contains(.png) {
|
||||||
let tempURL = URL(fileURLWithPath: output)
|
|
||||||
|
|
||||||
do {
|
// /usr/local/bin/rsvg-convert path/to/image.png -w 200 -h 300 -o path/to/output.png
|
||||||
if FileManager.default.fileExists(atPath: tempURL.path) {
|
// /usr/local/bin/rsvg-convert path/to/image.png -w 200 -o path/to/output.png
|
||||||
try FileManager.default.removeItem(atPath: tempURL.path)
|
// /usr/local/bin/rsvg-convert path/to/image.png -h 300 -o path/to/output.png
|
||||||
|
var command1x = ["\(svgConverter)", "\(imageData.path)"]
|
||||||
|
var command2x = ["\(svgConverter)", "\(imageData.path)"]
|
||||||
|
var command3x = ["\(svgConverter)", "\(imageData.path)"]
|
||||||
|
|
||||||
|
self.addConvertArgument(command: &command1x, convertArgument: convertArguments.x1)
|
||||||
|
self.addConvertArgument(command: &command2x, convertArgument: convertArguments.x2)
|
||||||
|
self.addConvertArgument(command: &command3x, convertArgument: convertArguments.x3)
|
||||||
|
|
||||||
|
command1x.append(contentsOf: ["-o", output1x])
|
||||||
|
command2x.append(contentsOf: ["-o", output2x])
|
||||||
|
command3x.append(contentsOf: ["-o", output3x])
|
||||||
|
|
||||||
|
Shell.shell(command1x)
|
||||||
|
Shell.shell(command2x)
|
||||||
|
Shell.shell(command3x)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
let output = "\(imagesetPath)/\(parsedImage.name).\(OutputImageExtension.svg.rawValue)"
|
||||||
|
let tempURL = URL(fileURLWithPath: output)
|
||||||
|
|
||||||
|
do {
|
||||||
|
if FileManager.default.fileExists(atPath: tempURL.path) {
|
||||||
|
try FileManager.default.removeItem(atPath: tempURL.path)
|
||||||
|
}
|
||||||
|
try FileManager.default.copyItem(atPath: imageData.path, toPath: tempURL.path)
|
||||||
|
} catch {
|
||||||
|
print(error.localizedDescription)
|
||||||
}
|
}
|
||||||
try FileManager.default.copyItem(atPath: imageData.path, toPath: tempURL.path)
|
|
||||||
} catch {
|
|
||||||
print(error.localizedDescription)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if imageData.ext == "svg" {
|
|
||||||
// /usr/local/bin/rsvg-convert path/to/image.png -w 200 -h 300 -o path/to/output.png
|
|
||||||
// /usr/local/bin/rsvg-convert path/to/image.png -w 200 -o path/to/output.png
|
|
||||||
// /usr/local/bin/rsvg-convert path/to/image.png -h 300 -o path/to/output.png
|
|
||||||
var command1x = ["\(svgConverter)", "\(imageData.path)"]
|
|
||||||
var command2x = ["\(svgConverter)", "\(imageData.path)"]
|
|
||||||
var command3x = ["\(svgConverter)", "\(imageData.path)"]
|
|
||||||
|
|
||||||
self.addConvertArgument(command: &command1x, convertArgument: convertArguments.x1)
|
|
||||||
self.addConvertArgument(command: &command2x, convertArgument: convertArguments.x2)
|
|
||||||
self.addConvertArgument(command: &command3x, convertArgument: convertArguments.x3)
|
|
||||||
|
|
||||||
command1x.append(contentsOf: ["-o", output1x])
|
|
||||||
command2x.append(contentsOf: ["-o", output2x])
|
|
||||||
command3x.append(contentsOf: ["-o", output3x])
|
|
||||||
|
|
||||||
Shell.shell(command1x)
|
|
||||||
Shell.shell(command2x)
|
|
||||||
Shell.shell(command3x)
|
|
||||||
} else {
|
} else {
|
||||||
// convert path/to/image.png -resize 200x300 path/to/output.png
|
// convert path/to/image.png -resize 200x300 path/to/output.png
|
||||||
// convert path/to/image.png -resize 200x path/to/output.png
|
// convert path/to/image.png -resize 200x path/to/output.png
|
||||||
@ -135,7 +139,7 @@ class XcassetsGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write Content.json
|
// Write Content.json
|
||||||
guard let imagesetContentJson = parsedImage.contentJson else { return }
|
guard let imagesetContentJson = parsedImage.generateContentJson(isVector: imageData.ext == "svg") else { return }
|
||||||
let contentJsonFilePath = "\(imagesetPath)/Contents.json"
|
let contentJsonFilePath = "\(imagesetPath)/Contents.json"
|
||||||
|
|
||||||
let contentJsonFilePathURL = URL(fileURLWithPath: contentJsonFilePath)
|
let contentJsonFilePathURL = URL(fileURLWithPath: contentJsonFilePath)
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
enum ImageExtension: String {
|
enum ImageExtension: String {
|
||||||
case vector
|
case png
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ParsedImage {
|
struct ParsedImage {
|
||||||
@ -61,10 +61,12 @@ struct ParsedImage {
|
|||||||
|
|
||||||
// MARK: - Assets
|
// MARK: - Assets
|
||||||
|
|
||||||
var contentJson: String? {
|
func generateContentJson(isVector: Bool) -> String? {
|
||||||
let encoder = JSONEncoder()
|
let encoder = JSONEncoder()
|
||||||
encoder.outputFormatting = .prettyPrinted
|
encoder.outputFormatting = .prettyPrinted
|
||||||
|
|
||||||
|
let imageContent = generateImageContent(isVector: isVector)
|
||||||
|
|
||||||
guard let data = try? encoder.encode(imageContent) else {
|
guard let data = try? encoder.encode(imageContent) else {
|
||||||
let error = ImagesError.writeFile("Contents.json", "Error encoding json file")
|
let error = ImagesError.writeFile("Contents.json", "Error encoding json file")
|
||||||
print(error.description)
|
print(error.description)
|
||||||
@ -74,8 +76,10 @@ struct ParsedImage {
|
|||||||
return String(data: data, encoding: .utf8)
|
return String(data: data, encoding: .utf8)
|
||||||
}
|
}
|
||||||
|
|
||||||
var imageContent: AssetContent {
|
func generateImageContent(isVector: Bool) -> AssetContent {
|
||||||
if imageExtensions.contains(.vector) {
|
|
||||||
|
if !imageExtensions.contains(.png) && isVector {
|
||||||
|
//Generate svg
|
||||||
return AssetContent(
|
return AssetContent(
|
||||||
images: [
|
images: [
|
||||||
AssetImageDescription(
|
AssetImageDescription(
|
||||||
@ -93,7 +97,7 @@ struct ParsedImage {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
//Generate png
|
||||||
return AssetContent(
|
return AssetContent(
|
||||||
images: [
|
images: [
|
||||||
AssetImageDescription(
|
AssetImageDescription(
|
||||||
|
@ -17,8 +17,8 @@ class ImageFileParserTests: XCTestCase {
|
|||||||
#
|
#
|
||||||
# SMAAS Support
|
# SMAAS Support
|
||||||
#
|
#
|
||||||
id image_one 25 ? vector
|
id image_one 25 ? png
|
||||||
di image_two ? 50 webp vector
|
di image_two ? 50 webp png
|
||||||
d image_three 25 ?
|
d image_three 25 ?
|
||||||
d image_four 75 ?
|
d image_four 75 ?
|
||||||
"""
|
"""
|
||||||
@ -38,7 +38,7 @@ class ImageFileParserTests: XCTestCase {
|
|||||||
XCTAssertEqual(firstImage!.tags, "id")
|
XCTAssertEqual(firstImage!.tags, "id")
|
||||||
XCTAssertEqual(firstImage!.width, 25)
|
XCTAssertEqual(firstImage!.width, 25)
|
||||||
XCTAssertEqual(firstImage!.height, -1)
|
XCTAssertEqual(firstImage!.height, -1)
|
||||||
XCTAssertEqual(firstImage!.imageExtensions, [.vector])
|
XCTAssertEqual(firstImage!.imageExtensions, [.png])
|
||||||
|
|
||||||
let secondImage = parsedImages.first {
|
let secondImage = parsedImages.first {
|
||||||
$0.name == "image_two"
|
$0.name == "image_two"
|
||||||
@ -47,6 +47,6 @@ class ImageFileParserTests: XCTestCase {
|
|||||||
XCTAssertEqual(secondImage!.tags, "di")
|
XCTAssertEqual(secondImage!.tags, "di")
|
||||||
XCTAssertEqual(secondImage!.width, -1)
|
XCTAssertEqual(secondImage!.width, -1)
|
||||||
XCTAssertEqual(secondImage!.height, 50)
|
XCTAssertEqual(secondImage!.height, 50)
|
||||||
XCTAssertEqual(firstImage!.imageExtensions, [.vector])
|
XCTAssertEqual(firstImage!.imageExtensions, [.png])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -127,7 +127,7 @@ final class ParsedImageTests: XCTestCase {
|
|||||||
height: 10)
|
height: 10)
|
||||||
|
|
||||||
// When
|
// When
|
||||||
let property = parsedImage.imageContent
|
let property = parsedImage.generateImageContent(isVector: false)
|
||||||
|
|
||||||
// Expect
|
// Expect
|
||||||
let expect = AssetContent(
|
let expect = AssetContent(
|
||||||
@ -157,33 +157,41 @@ final class ParsedImageTests: XCTestCase {
|
|||||||
XCTAssertEqual(property, expect)
|
XCTAssertEqual(property, expect)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAssetVector() {
|
func testAssetPng() {
|
||||||
// Given
|
// Given
|
||||||
let imageName = "the_name"
|
let imageName = "the_name"
|
||||||
let parsedImage = ParsedImage(name: imageName,
|
let parsedImage = ParsedImage(name: imageName,
|
||||||
tags: "id",
|
tags: "id",
|
||||||
width: 10,
|
width: 10,
|
||||||
height: 10,
|
height: 10,
|
||||||
imageExtensions: [.vector])
|
imageExtensions: [.png])
|
||||||
|
|
||||||
// When
|
// When
|
||||||
let property = parsedImage.imageContent
|
let property = parsedImage.generateImageContent(isVector: false)
|
||||||
|
|
||||||
// Expect
|
// Expect
|
||||||
|
|
||||||
let expect = AssetContent(
|
let expect = AssetContent(
|
||||||
images: [
|
images: [
|
||||||
AssetImageDescription(
|
AssetImageDescription(
|
||||||
idiom: "universal",
|
idiom: "universal",
|
||||||
filename: "\(parsedImage.name).\(OutputImageExtension.svg.rawValue)"
|
scale: "1x",
|
||||||
|
filename: "\(parsedImage.name).\(OutputImageExtension.png.rawValue)"
|
||||||
|
),
|
||||||
|
AssetImageDescription(
|
||||||
|
idiom: "universal",
|
||||||
|
scale: "2x",
|
||||||
|
filename: "\(parsedImage.name)@2x.\(OutputImageExtension.png.rawValue)"
|
||||||
|
),
|
||||||
|
AssetImageDescription(
|
||||||
|
idiom: "universal",
|
||||||
|
scale: "3x",
|
||||||
|
filename: "\(parsedImage.name)@3x.\(OutputImageExtension.png.rawValue)"
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
info: AssetInfo(
|
info: AssetInfo(
|
||||||
version: 1,
|
version: 1,
|
||||||
author: "ResgenSwift-Imagium"
|
author: "ResgenSwift-Imagium"
|
||||||
),
|
|
||||||
properties: AssetProperties(
|
|
||||||
preservesVectorRepresentation: true,
|
|
||||||
templateRenderingIntent: .template
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user