How do I do this portably?

I am trying to selectively embed some files based on a list provided via an environment variable and a flag. This works in Linux and unix-like things, but it won’t work on Windows.

Any ideas on how to do this portably?

  class LexerFiles
    extend BakedFileSystem

    macro bake_selected_lexers
      {% for lexer in `echo -n "$TT_LEXERS"`.split "," %}
      bake_file {{ lexer }}+".xml", {{ read_file "lexers/" + lexer + ".xml" }}
      {% end %}
    end

    {% if flag?(:nolexers) %}
      bake_selected_lexers
    {% else %}
      bake_folder "../lexers", __DIR__
    {% end %}
  end
1 Like

You can use Crystal::Macros - Crystal 1.13.3 to access the ENV var vs using the shell command which should make it more portable.

1 Like

I tried and failed :-D

I get

In src/lexer.cr:11:26

 11 | {% for lexer in ENV["TT_LEXERS"].split "," %}
                         ^
Error: undefined method '[]' for TypeNode of type ENV (must be a tuple or named tuple type)

In macros, you need to use the env macro method that @Blacksmoke16 linked to rather than the ENV constant.

{% for lexer in env("TT_LEXERS").split "," %}
3 Likes