Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import tag for reusable macros #111

Merged
merged 4 commits into from
Jul 29, 2022
Merged

Conversation

cornr
Copy link

@cornr cornr commented Jan 8, 2019

Implemented @nferruzzi node to import other Templates

@cornr
Copy link
Author

cornr commented Jan 8, 2019

I'm not sure with xcodeproj changes.
To bootstrap the project and run the tests I did swift package generate-xcodeproj

@djbe
Copy link
Member

djbe commented Jan 8, 2019

I don't think we've ever used swift package generate-xcodeproj. If you add files you should run pod install.

@djbe
Copy link
Member

djbe commented Jan 8, 2019

It might be better to undo the changes from swift package generate-xcodeproj, it seems to have added some unneeded files.

@cornr
Copy link
Author

cornr commented Jan 8, 2019

@djbe thanks for your help. I reverted the changes from swift package generate-xcodeproj and executed bundle exec pod install and now I am running into CocoaPods/CocoaPods#7708

Fixed with usage of cocoa pods 1.6 pre release

@nferruzzi
Copy link

thank you guys! I totally forgot about this <3

@cornr
Copy link
Author

cornr commented Jan 8, 2019

Sorry the PR is not working as expected. While I was testing the import Tag with Sourcery I figured that the loaded template is not populated to the context and therefore the macros are not visible.
@nferruzzi in your code snippet you call _ = try template.render(context) which is a non open function and therefore not visible in the ImportNode. The open template.render(context.flatten()) function uses a new context.
Any ideas to populate the new parsed tokens into the current context?

@nferruzzi
Copy link

sorry but I think I may not be of great help now, too many months since I created that node, but I keep using it daily and without any problem so it should be something minor; if I do remember properly the trick _ = .. was to avoid populating the output with the imported file output and template.render(context.flatten()) was to inject the context with the macro content.

My usage, is like this :

{% import "Label.swift.stencil" %}
{% import "Button.swift.stencil" %}
{% import "Activity.swift.stencil" %}

... from here I can {%call anyMacro %}

@nferruzzi
Copy link

Ok sorry my render function is a bit different now

    func render(_ context: Context) throws -> String {
        guard let templateName = try self.templateName.resolve(context) as? String else {
            throw TemplateSyntaxError("'\(self.templateName)' could not be resolved as a string")
        }

        let template = try context.environment.loadTemplate(name: templateName)

        _ = try template.render(context)
        return ""
    }

there is no flatten , maybe this helps

@djbe djbe changed the title #91: reusable macro templates via import tag import tag for reusable macros Jan 8, 2019
@djbe
Copy link
Member

djbe commented Jan 8, 2019

@cornr I've made a separate PR that updates cocoapods & swiftlint (#112), which has been merged. You should rebase this PR on master to get those changes.

Do note that if you run pod install, make sure to afterwards (in Xcode) select the Pods.xcodeproj and set the text indentation settings back to indent with spaces and 2:
screenshot 2019-01-08 at 23 33 33
This ensures everyone is writing with the same text editor settings.

@cornr
Copy link
Author

cornr commented Jan 9, 2019

@nferruzzi your code does not work with the current Stencil Release as its using a non open function: template.render(context)

@djbe I wonder why the usage of the include-tag does not work with macros?
I checked the code Include.swift and it looks pretty much the same as @nferruzzi implementation. Maybe the StencilSwiftKit tags are not registered in the used Environment.

@AliSoftware AliSoftware changed the base branch from master to stable June 10, 2020 23:01
@djbe
Copy link
Member

djbe commented Jul 21, 2022

The difference is subtle, but quite important:

  • the include tag from Stencil surrounds the render call with a context.push(dictionary: subContext) {
  • whereas this import tag does not

Surrounding the render with a context push causes all changes during that render (such as creating macro's, setting variables, etc…) to only be applied temporarily, and "popped" away when the render is done.

@djbe djbe force-pushed the feature/importTag branch from 2bf203c to 33a21b8 Compare July 21, 2022 21:42
@djbe
Copy link
Member

djbe commented Jul 21, 2022

The current implementation was broken, because it was applying the changes to context.flatten(), which generates a dictionary copy. So the original context is not modified as it should be.

I've updated the implementation and written tests that actually test modification. The implementation currently does not compile, because it depends on the non-open function render which accepts a context. We'll need to modify Stencil to open this up first.

@djbe djbe force-pushed the feature/importTag branch from 33a21b8 to 0a366d9 Compare July 21, 2022 21:49
@djbe djbe marked this pull request as draft July 29, 2022 18:05
@djbe djbe force-pushed the feature/importTag branch 2 times, most recently from 789712d to 270ce99 Compare July 29, 2022 22:16
@djbe djbe added this to the 2.10.0 milestone Jul 29, 2022
@djbe djbe force-pushed the feature/importTag branch from 270ce99 to 1b2f01a Compare July 29, 2022 23:25
@djbe djbe marked this pull request as ready for review July 29, 2022 23:25
@djbe djbe force-pushed the feature/importTag branch from 1b2f01a to be7591a Compare July 29, 2022 23:31
@djbe djbe merged commit 402aaf4 into SwiftGen:stable Jul 29, 2022
@djbe djbe linked an issue Jul 29, 2022 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

reusable macro templates
3 participants