felangel / mason

Tools which allow developers to create and consume reusable templates called bricks.
https://docs.brickhub.dev
931 stars 92 forks source link

chore: What is the process for setting up a whitelist to restrict modifications by the Mason CLI for specific files? #951

Open pradipaub36 opened 1 year ago

pradipaub36 commented 1 year ago

Description

I have created a custom template using Stencil. When I include this template under the "bricks" directory and use a specific brick, it automatically modifies the Stencil template without defining the variables in the brick.yaml file. How can I prevent this from happening?

Here is brick.yaml file content

name: boilerplate_brick
description: A new brick created with the Mason CLI.

vars:
  project_name:
    type: string
    description: Provide your project name
    default: SampleProject
    prompt: What is your project name?

  bundle_id:
    type: string
    description: Provide your project bundle id
    default: com.example.sampleproject
    prompt: What is your project bundle id?

Here is the Stencil template


// swiftlint:disable all
// Generated using SwiftGen β€” https://github.com/SwiftGen/SwiftGen
{% if catalogs %}
{% set enumName %}{{param.enumName|default:"Asset"}}{% endset %}
{% set forceNamespaces %}{{param.forceProvidesNamespaces|default:"false"}}{% endset %}
{% set accessModifier %}{% if param.publicAccess %}public{% else %}internal{% endif %}{% endset %}
{% macro enumBlock assets %}
  {% call casesBlock assets %}
{% endmacro %}
{% macro casesBlock assets %}
  {% for asset in assets %}
  {% if asset.type == "color" %}

  static var {{asset.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}: UIColor {
      return {{enumName}}.Colors.{{asset.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}.color
  }
  {% elif asset.items and ( forceNamespaces == "true" or asset.isNamespaced == "true" ) %}
  {% call casesBlock asset.items %}
  {% elif asset.items %}
  {% call casesBlock asset.items %}
  {% endif %}
  {% endfor %}
{% endmacro %}

import UIKit

// swiftlint:disable identifier_name line_length nesting type_body_length type_name
{{accessModifier}} extension UIColor {
  {% if catalogs.count > 1 or param.forceFileNameEnum %}
  {% for catalog in catalogs %}
  {% call enumBlock catalog.assets %}
  {% endfor %}
  {% else %}
  {% call enumBlock catalogs.first.assets %}
  {% endif %}
}
// swiftlint:enable identifier_name line_length nesting type_body_length type_name

{% else %}
// No assets found
{% endif %}

Updated the Stencil template by Mason CLI

// swiftlint:disable all
// Generated using SwiftGen β€” https://github.com/SwiftGen/SwiftGen
{% if catalogs %}
{% set enumName %}{% endset %}
{% set forceNamespaces %}{% endset %}
{% set accessModifier %}{% if param.publicAccess %}public{% else %}internal{% endif %}{% endset %}
{% macro enumBlock assets %}
  {% call casesBlock assets %}
{% endmacro %}
{% macro casesBlock assets %}
  {% for asset in assets %}
  {% if asset.type == "color" %}

  static var : UIColor {
      return .Colors..color
  }
  {% elif asset.items and ( forceNamespaces == "true" or asset.isNamespaced == "true" ) %}
  {% call casesBlock asset.items %}
  {% elif asset.items %}
  {% call casesBlock asset.items %}
  {% endif %}
  {% endfor %}
{% endmacro %}

import UIKit

// swiftlint:disable identifier_name line_length nesting type_body_length type_name
 extension UIColor {
  {% if catalogs.count > 1 or param.forceFileNameEnum %}
  {% for catalog in catalogs %}
  {% call enumBlock catalog.assets %}
  {% endfor %}
  {% else %}
  {% call enumBlock catalogs.first.assets %}
  {% endif %}
}
// swiftlint:enable identifier_name line_length nesting type_body_length type_name

{% else %}
// No assets found
{% endif %}

Requirements The Mason CLI should refrain from modifying the Stencil template unless the variables are defined in the brick.yaml file.

felangel commented 11 months ago

Hi @pradipaub36 πŸ‘‹ Thanks for opening an issue!

Can you please elaborate a bit more? What is the Mason CLI modifying specifically?

pradipaub36 commented 11 months ago

Hi @felangel Thank you for your prompt response!

The issue I encountered with the Mason CLI is related to how it handles the Stencil template when using a specific brick that includes custom variables defined in the brick.yaml file. The problem is that when I include my custom template under the "bricks" directory and use a brick with custom variables, the Mason CLI automatically modifies the Stencil template, even if the variables are not defined in the brick.yaml file. This behavior is not desired as it can lead to unexpected changes in the generated code.

Here's the specific scenario:

  1. I have a custom Stencil template with placeholders for variables like {{param.enumName}}, {{param.forceNamespaces}}, and {{param.publicAccess}}.
  2. I include this template in the Mason CLI's "bricks" directory to be used as a brick template for generating code.
  3. In my brick's brick.yaml file, I have defined custom variables like project_name and bundle_id.
  4. When I use the brick with the project_name and bundle_id variables, the Mason CLI correctly prompts me for input and generates code accordingly. However, it also modifies the Stencil template, even though the template does not define these specific variables. The result is an updated Stencil template with missing variable names like {{param.enumName}}, {{param.forceNamespaces}}, and {{param.publicAccess}}.

Ideally, the Mason CLI should refrain from modifying the Stencil template unless the variables are explicitly defined in the brick.yaml file. This way, the custom template remains intact, and users can use their bricks without unintended changes to the Stencil template.

Is there any way to prevent the Mason CLI from automatically modifying the Stencil template when using custom bricks with specific variables? If not, is there a recommended approach or workaround to ensure the template remains unchanged while using custom bricks with additional variables?

Thank you for your attention to this matter, and I'm looking forward to your guidance on resolving this issue.