Skip to content

Commit d73671d

Browse files
feat: add asset management commands (#11)
1 parent 570dd9d commit d73671d

File tree

60 files changed

+1617
-61
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1617
-61
lines changed

AGENTS.md

Lines changed: 0 additions & 24 deletions
This file was deleted.

README.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,31 @@ OPTIONS:
2424
2525
TARGET SUBCOMMANDS:
2626
list-targets List project targets.
27-
set-target Set target for an existing file.
27+
set-target Set target for existing file.
2828
2929
GROUP SUBCOMMANDS:
30-
add-group Add a group to the project.
31-
delete-group Delete a group from the project.
32-
move-group Move a group to a different location within the project.
33-
rename-group Rename a group within the project.
30+
add-group Add group.
31+
delete-group Delete group.
32+
move-group Move group.
33+
rename-group Rename group.
3434
3535
FILE SUBCOMMANDS:
36-
add-file Add a file to specified targets in the project.
37-
delete-file Delete a file from the project.
38-
move-file Move a file to a different location within the project.
39-
rename-file Rename a file within the project.
36+
add-file Add file to specified targets.
37+
delete-file Delete file.
38+
move-file Move file.
39+
rename-file Rename file.
4040
4141
BUILD SETTINGS SUBCOMMANDS:
42-
get-build-setting Get a build setting for specified target in the project.
43-
set-build-setting Set a build setting for specified targets in the project.
42+
get-build-setting Get build setting.
43+
set-build-setting Set build setting.
44+
45+
ASSETS SUBCOMMANDS:
46+
add-image-asset Add image asset.
47+
add-data-asset Add data asset.
48+
add-color-asset Add color asset.
49+
list-assets List all assets.
50+
move-asset Move asset.
51+
delete-asset Delete asset.
4452
4553
See 'xcp help <subcommand>' for detailed help.
4654
```
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
final class ColorAssetBuilder {
2+
private var hexColor: String?
3+
private var darkHexColor: String?
4+
private var colorSpace = "srgb"
5+
6+
func setHexColor(_ hex: String) -> ColorAssetBuilder {
7+
hexColor = hex
8+
return self
9+
}
10+
11+
func setDarkHexColor(_ hex: String?) -> ColorAssetBuilder {
12+
darkHexColor = hex
13+
return self
14+
}
15+
16+
func setColorSpace(_ space: String) -> ColorAssetBuilder {
17+
colorSpace = space
18+
return self
19+
}
20+
21+
func build() -> String {
22+
var darkColorTemplate = ""
23+
24+
if let darkColor = darkHexColor.flatMap(HexColor.init) {
25+
darkColorTemplate = """
26+
,
27+
{
28+
"appearances" : [
29+
{
30+
"appearance" : "luminosity",
31+
"value" : "dark"
32+
}
33+
],
34+
"color" : {
35+
"color-space" : "\(colorSpace)",
36+
"components" : {
37+
"alpha" : "\(darkColor.alpha)",
38+
"blue" : "0x\(darkColor.blue)",
39+
"green" : "0x\(darkColor.green)",
40+
"red" : "0x\(darkColor.red)"
41+
}
42+
},
43+
"idiom" : "universal"
44+
}
45+
"""
46+
}
47+
48+
guard let color = hexColor.flatMap(HexColor.init) else {
49+
return ""
50+
}
51+
52+
return """
53+
{
54+
"colors" : [
55+
{
56+
"color" : {
57+
"color-space" : "\(colorSpace)",
58+
"components" : {
59+
"alpha" : "\(color.alpha)",
60+
"blue" : "0x\(color.blue)",
61+
"green" : "0x\(color.green)",
62+
"red" : "0x\(color.red)"
63+
}
64+
},
65+
"idiom" : "universal"
66+
}\(darkColorTemplate)
67+
],
68+
"info" : {
69+
"author" : "xcode",
70+
"version" : 1
71+
}
72+
}\n
73+
"""
74+
}
75+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
final class DataAssetBuilder {
2+
private let asset: AssetInfo
3+
4+
init(_ asset: AssetInfo) {
5+
self.asset = asset
6+
}
7+
8+
func build() -> String {
9+
"""
10+
{
11+
"data" : [
12+
{
13+
"filename" : "\(asset.fileName)",
14+
"idiom" : "universal"
15+
}
16+
],
17+
"info" : {
18+
"author" : "xcode",
19+
"version" : 1
20+
}
21+
}\n
22+
"""
23+
}
24+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
final class ImageAssetBuilder {
2+
private var renderingMode: RenderingMode = .default
3+
private var includeDark = false
4+
5+
private let asset: AssetInfo
6+
7+
init(_ asset: AssetInfo) {
8+
self.asset = asset
9+
}
10+
11+
func setRenderingMode(_ mode: RenderingMode) -> ImageAssetBuilder {
12+
renderingMode = mode
13+
return self
14+
}
15+
16+
func includeDarkAppearance(_ includeDark: Bool) -> ImageAssetBuilder {
17+
self.includeDark = includeDark
18+
return self
19+
}
20+
21+
func build() -> String {
22+
let templateRendering = renderingMode != .default
23+
? """
24+
,
25+
"properties" : {
26+
"template-rendering-intent" : "\(renderingMode.rawValue)"
27+
}
28+
"""
29+
: ""
30+
31+
let darkAppearance = includeDark
32+
? """
33+
,
34+
{
35+
"appearances" : [
36+
{
37+
"appearance" : "luminosity",
38+
"value" : "dark"
39+
}
40+
],
41+
"filename" : "\(asset.darkFileName)",
42+
"idiom" : "universal"
43+
}
44+
"""
45+
: ""
46+
47+
return """
48+
{
49+
"images" : [
50+
{
51+
"filename" : "\(asset.fileName)",
52+
"idiom" : "universal"
53+
}\(darkAppearance)
54+
],
55+
"info" : {
56+
"author" : "xcode",
57+
"version" : 1
58+
}\(templateRendering)
59+
}\n
60+
"""
61+
}
62+
}

Sources/XcodeProject/Core/Project.swift

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,16 @@ public final class Project {
1212

1313
public init(xcodeProjectPath: String?) throws {
1414
var projectPath = xcodeProjectPath
15-
let pwd = ProcessInfo.processInfo.environment["PWD"]
16-
var currentDir = pwd
17-
18-
#if DEBUG
19-
if pwd == nil || pwd == "/tmp" {
20-
currentDir = #filePath
21-
.components(separatedBy: "/")
22-
.dropLast(4)
23-
.joined(separator: "/")
24-
}
25-
#endif
15+
let currentDir = PathUtils.processWorkingDirectory()
2616

2717
if projectPath == nil, let currentDir {
2818
let contents = try FileManager.default.contentsOfDirectory(atPath: currentDir)
2919
projectPath = contents.first { $0.hasSuffix(".xcodeproj") }
3020
projectPath = InputPath(projectPath ?? "", projectRoot: currentDir).absolutePath
31-
} else if let projectPathUnwrapped = projectPath, let currentDir, !(projectPathUnwrapped as NSString).isAbsolutePath {
32-
projectPath = ("\(currentDir)/\(projectPathUnwrapped)" as NSString).standardizingPath
21+
} else {
22+
projectPath = projectPath.flatMap { PathUtils.toAbsolutePath($0) }
3323
}
3424

35-
projectPath = (projectPath as NSString?)?.expandingTildeInPath
36-
3725
guard let projectPath else {
3826
throw XcodeProjectError.xcodeProjectNotFound
3927
}

0 commit comments

Comments
 (0)