yonaskolb / yonaskolb/XcodeGen
Allowing target templates to know the relative path to the targets yml file
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 8.8k
- Forks
- 908
- Avg merge
- 17d 14h
- Merged PRs (30d)
- 2
Description
Say I've got the following project.
├── modules
│ └── MyFramework
│ ├── MyFramework.yml
│ └── Sources
│ └── Hello.swift
└── project.yml
project.yml contains a target template that adds a script that needs the path to the target. This is a contrived example, but linting just the sources for the target would have the same issue.
project.yml contents
name: MyProject
fileGroups:
- modules/MyFramework
include:
- modules/MyFramework/MyFramework.yml
targetTemplates:
CountLinesOfCode:
preBuildScripts:
- name: Count lines of code
script: |
swiftlint "${SRC_ROOT}/${WHAT_GOES_HERE}/${target_name}/Sources" -name "*.swift" -type f \
| sed 's/.*/"&"/' \
| xargs wc -l
MyFramework.yml defines a target that uses that template.
MyFramework.yml contents
targets:
MyFramework:
platform: iOS
type: framework
templates:
- CountLinesOfCode
sources:
- path: "Sources"
Issues
-
There is no way for the target template to know the relative path of the yml file that uses it. This is an issue for scripts that need to know the full path of files related to the target being added.
-
The template is resolved based on the location of the file that defines it rather than the location of the file for the target that uses it. For example, if a project uses the the same folder structure for all framework targets, it'd be useful to define a
BaseFrameworktemplate that setssources: "Sources"and other settings. This currently isn't possible as it'd always be resolved as${SRC_ROOT}/Sources.
Possible solutions
Add a new target template variable
The easiest solution I can think of would be to add another variable along target_name like relative_path. It's contents would be either:
- a
.for any target inproject.ymlor a included yml withrelativePaths: false- so it can be resolved correct when used as part of a longer path like
${SRC_ROOT}/${relative_path}/file.xyz
- so it can be resolved correct when used as part of a longer path like
- the path to the directory containing the included yml for
relativePaths: true
This would allow the first issue/example to replace ${WHAT_GOES_HERE} with the new variable.
The second issue would allow it to use sources: "${relative_path}/Sources" which would remove to the correct path.
Resolve templates the same as the target that contains them
The idea is to treat templates like their content is copied directly into the target that is using it before any paths are resolved.
This would only solve the second issue with properties that are known to be paths. For other properties (like scripts) it would still need the first solution.
Current workarounds
Currently we have a requirement that all targets have to define their relative path (or use a default).
project.yml contents
targetTemplates:
DefaultTemplateAttributes:
templateAttributes:
relativePath: ""
BaseFramework:
platform: iOS
type: framework
sources:
- path: "${relativePath}/Sources"
MyFramework.yml contents
targets:
MyFramework:
templates:
- DefaultTemplateAttributes
- BaseFramework
- CountLinesOfCode
templateAttributes:
relativePath: "modules/MyFramework/"
The main issue is that moving a target requires remembering to update the value for relativePath which is easily forgotten. It also complicates building tooling for creating and updating the yml files.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing how targetTemplates are resolved for project.yml and the included MyFramework.yml, especially how relative paths and script variables are handled. Compare the proposed relative_path behavior with resolving templates in the target's location, then define completion as correct paths for both scripts and target properties without manually maintained relativePath attributes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift, yaml
- Domain
- build-system, cli
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100