-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7737275
commit 0c8a928
Showing
6 changed files
with
149 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
name: CI | ||
on: push | ||
jobs: | ||
bundler-audit: | ||
name: Bundler Audit | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: ruby/setup-ruby@v1 | ||
with: | ||
bundler-cache: true | ||
- run: bin/bundler-audit check --update | ||
rspec: | ||
name: Test | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: ruby/setup-ruby@v1 | ||
with: | ||
ruby-version: 3.3.0 | ||
bundler-cache: true | ||
- run: bin/test | ||
rubocop: | ||
name: Rubocop | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: ruby/setup-ruby@v1 | ||
with: | ||
bundler-cache: true | ||
- run: bin/rubocop | ||
yarn-audit: | ||
name: Yarn Audit | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-node@v4 | ||
- run: yarn audit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
class Template | ||
EMPTY_STRING = "" | ||
GLOBALS = %i[io context object].freeze | ||
GLOBALS = %i[output error context object].freeze | ||
DEFAULT_TIMEOUT = 0 | ||
|
||
def initialize(input, io: ::StringIO.new, timeout: DEFAULT_TIMEOUT, ruby: {}) | ||
def initialize( | ||
input, | ||
output: StringIO.new, | ||
error: StringIO.new, | ||
timeout: DEFAULT_TIMEOUT | ||
) | ||
@input = input | ||
@parsed = | ||
Timeout.timeout(timeout) { ::Template::Parser.parse(@input).to_raw } | ||
@io = io | ||
@output = output | ||
@error = error | ||
@timeout = timeout || DEFAULT_TIMEOUT | ||
@ruby = | ||
Timeout.timeout(timeout) do | ||
::Code::Ruby.to_code(ruby || {}).code_to_context | ||
end | ||
@context = ::Code::Object::Context.new | ||
end | ||
|
||
def self.parse(input, timeout: DEFAULT_TIMEOUT) | ||
Timeout.timeout(timeout) { Parser.parse(input).to_raw } | ||
end | ||
|
||
def self.evaluate( | ||
input, | ||
context = "", | ||
io: ::StringIO.new, | ||
timeout: DEFAULT_TIMEOUT, | ||
ruby: {} | ||
output: StringIO.new, | ||
error: StringIO.new, | ||
timeout: DEFAULT_TIMEOUT | ||
) | ||
new(input, io:, timeout:, ruby:).evaluate(context) | ||
new(input, output:, error:, timeout:).evaluate | ||
end | ||
|
||
def evaluate(context = "") | ||
def evaluate | ||
Timeout.timeout(timeout) do | ||
context = | ||
if context == EMPTY_STRING | ||
::Code::Object::Context.new | ||
else | ||
::Code.evaluate(context, timeout:, io:, ruby:).code_to_context | ||
end | ||
|
||
unless context.is_a?(::Code::Object::Context) | ||
raise(Error::IncompatibleContext) | ||
end | ||
|
||
context = context.merge(ruby) | ||
parsed = Template.parse(input) | ||
Node::Template.new(parsed).evaluate(context:, output:, error:) | ||
|
||
::Template::Node::Template.new(parsed).evaluate(context:, io:) | ||
|
||
io.is_a?(::StringIO) ? io.string : nil | ||
if output.is_a?(StringIO) | ||
output.string | ||
else | ||
"" | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :input, :parsed, :timeout, :io, :ruby | ||
attr_reader :input, :timeout, :output, :error, :context | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rspec" | ||
require_relative "../lib/template-ruby" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters