Swiftgen: The path to fonts is not relative and includes the user's home directory path

Created on 15 Aug 2017  路  8Comments  路  Source: SwiftGen/SwiftGen

According to the docs here...
https://github.com/SwiftGen/SwiftGenKit/blob/master/Documentation/Fonts.md

... the font paths should be relative to the directory however it is not the case as shown by the generated code below:

enum FontFamily {
  enum Exo2 {
    static let light = FontConvertible(name: "Exo2-Light", family: "Exo 2", path: "/Users/ben/Development/Projects/[company]/[repo]/[target]/Exo2-Light.ttf")
    static let regular = FontConvertible(name: "Exo2-Regular", family: "Exo 2", path: "/Users/ben/Development/Projects/[company]/[repo]/[target]/Exo2-Regular.ttf")
  }
  enum GalanoGrotesque {
    static let bold = FontConvertible(name: "GalanoGrotesque-Bold", family: "Galano Grotesque", path: "/Users/ben/Development/Projects/[company]/[repo]/[target]/Galano Grotesque Bold.otf")
    static let extraLight = FontConvertible(name: "GalanoGrotesque-ExtraLight", family: "Galano Grotesque", path: "/Users/ben/Development/Projects/[company]/[repo]/[target]/Galano Grotesque ExtraLight.otf")
    static let light = FontConvertible(name: "GalanoGrotesque-Light", family: "Galano Grotesque", path: "/Users/ben/Development/Projects/[company]/[repo]/[target]/Galano Grotesque Light.otf")
    static let medium = FontConvertible(name: "GalanoGrotesque-Medium", family: "Galano Grotesque", path: "/Users/ben/Development/Projects/[company]/[repo]/[target]/Galano Grotesque Medium.otf")
    static let regular = FontConvertible(name: "GalanoGrotesque-Regular", family: "Galano Grotesque", path: "/Users/ben/Development/Projects/[company]/[repo]/[target]/Galano Grotesque.otf")
    static let semiBold = FontConvertible(name: "GalanoGrotesque-SemiBold", family: "Galano Grotesque", path: "/Users/ben/Development/Projects/[company]/[repo]/[target]/Galano Grotesque SemiBold.otf")
    static let thin = FontConvertible(name: "GalanoGrotesque-Thin", family: "Galano Grotesque", path: "/Users/ben/Development/Projects/[company]/[repo]/[target]/Galano Grotesque Thin.otf")
  }
  enum Lato {
    static let regular = FontConvertible(name: "Lato-Regular", family: "Lato", path: "/Users/ben/Development/Projects/[company]/[repo]/[target]/lato-regular.ttf")
  }
}

The command I am using:

$ Pods/SwiftGen/bin/swiftgen fonts [target] --template swift3 --output [target]/Fonts.swift

The version of SwiftGen:

$ Pods/SwiftGen/bin/swiftgen --version
SwiftGen v5.0.0 (Stencil v0.9.0, StencilSwiftKit v2.0.1, SwiftGenKit v2.0.0)
bufix

All 8 comments

Interesting. Thanks for the bug report!

The code is supposed to generate a path relative to the path of the folder you passed to the command line, i.e. the folder containing the fonts. So it shouldn't indeed contain the whole path like you have in your case.

The code responsible for determining the relative path is there. Maybe our algorithm for determining that relative path is wrong or is missing some special case.

Do you see anything special with your full path that might explain a case that we could have forgotten to take into account? Maybe some accents / diacritics in the path or some spaces in your [company]/[repo]/[target] that would mess our algorithm up and explain what we need to do to fix it?

@AliSoftware nothing in that path is special. For the record, I'm only removing these words/names because I can't be bothered about making sure I'm not violating any kind of NDA.

[company]/[repo]/[target] could be exemplified by (including the full path in the example) /Users/ben/Development/Projects/Microsoft/ios-Outlook/Outlook/....

Could it be that the hyphen has something to do with it? I'll try renaming that folder and see if it makes a difference...

I _think_ what happens is that our PathKit.Path.relative(to:) function returns nil, which leads to this line in our code to use the full path (using the ?? file fallback). So we just have to understand why that function returns nil.

Maybe it's because when calling the function, we have one of the path (e.g. self) being a full path but the other one (e.g. parent) representing a relative path when the function is called? (and thus considering that expressing self = "/Users/ben/Development/Projects/MyCompany/MyRepo/MyTarget/Foo.ttf" relative to parent = "MyTarget/" is impossible because it considers that parent isn't a prefix of self)

Could you try calling swiftgen fonts with the full path to [target] instead of the relative path to confirm that theory?

$ Pods/SwiftGen/bin/swiftgen fonts /Users/ben/Development/Projects/[company]/[repo]/[target] --template swift3 --output [target]/Fonts.swift

I have confirmed that the hyphen has no impact. Same result with or without the hyphen.
I have also confirmed that passing the full path fixes the problem. The command I have used:

$ Pods/SwiftGen/bin/swiftgen fonts /Users/ben/Development/Projects/[company]/[repo]/[taraget] --template swift3 --output [target]/Fonts.swift

Ah, interesting. I think that explains it then, thanks

Until we release a proper fix for this issue, you can at least use "$PROJECT_DIR/[target]" in your build phase instead of hard-coding the full path then.

Trying that out right now. Thanks for the prompt response @AliSoftware!

Worked! Thanks again!

I also realized another potential problem with that generated path, see #324. Would love to get your thoughts about that and confirm (or deny) that other potential problem

Was this page helpful?
0 / 5 - 0 ratings

Related issues

KonstantinBerkow picture KonstantinBerkow  路  5Comments

taykay08 picture taykay08  路  3Comments

gereons picture gereons  路  5Comments

romk1n picture romk1n  路  7Comments

jrmybrault picture jrmybrault  路  7Comments