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

Schneems/pre erb #90

Merged
merged 9 commits into from
Dec 11, 2024
Merged

Schneems/pre erb #90

merged 9 commits into from
Dec 11, 2024

Conversation

schneems
Copy link
Member

Placing pre.erb in-front of another command will allow dynmaic templating via Ruby's ERB syntax.

For example:

:::>> pre.erb $ echo "The answer to everything is <%= 6*7 %>"

When this runs, it will first replace the template with the result of the ERB. It would be the same as this:

```
:::>> $ echo "The answer to everything is 42"
```

The binding (variable and method scope) for pre.erb is shared across all executions and the default print.erb command. That means you can use it to persist data or logic and re-use it:

```ruby
:::-- print.erb <%
  # Won't be rendered because it's using `--` visibility
  def lol
    "haha"
  end

  user = "Schneems"
%>
```

```
:::>> pre.erb $ echo <%= user %> said <%= lol() %> | tr '[:lower:]' '[:upper:]'
```

When run, this would produce:

```
$ echo Schneems said haha | tr '[:lower:]' '[:upper:]'
SCHNEEMS SAID HAHA
```

Multi-line commands are also supported

```
:::>> pre.erb file.write "lol.txt"
Super secret key:
  <%= "#{key}" %>
```

The only thing to watch out for is if the resulting template contains a :::>> (or similar) rundoc marker at the beginning of the line; Rundoc will think it is a new command rather than a part of pre.erb template.

The visibility of the pre.erb is forwarded to whatever command is run.

There's many different places that can execute code now, so it's a bit confusing when trying to add new features or do things like partial results are written to disk in the event of a failure (this is still basically broken due to this problem). Instead of having commands that actually execute other commands instead, they can now modify the stack by pushing another command onto it and that will be executed next in line.
The file commands e.g. `file.write` don't have to be in their own block, they just needs to not have anything else in the block printed.
The `@contents` come from the loose concept of STDIN defined by rundoc. It makes sense for `file.write` etc. but not for basic commands. Anywho. If it's set, we need to combine it with the first line separated by a newline
@schneems schneems merged commit f28fa14 into main Dec 11, 2024
13 checks passed
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.

1 participant