Created
May 11, 2022 21:18
-
-
Save jgaskins/d6c530788bfbb2858df9d5cb5dbb3a6a to your computer and use it in GitHub Desktop.
Project-local requires in Crystal
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
# Experiment with macros to allow for project-local requires | |
# https://forum.crystal-lang.org/t/project-relative-require/4617 | |
{% begin %} | |
ROOT = "{{system("pwd").strip.id}}" | |
{% end %} | |
macro spec(file) | |
macro load_spec | |
\{% path = __DIR__.gsub(%r{\A{{ROOT.id}}}, "").gsub(%r{[^/]+}, "..").id %} | |
require "\{{path[1..]}}/spec/{{file.id}}" | |
end | |
load_spec | |
end | |
macro src(file) | |
macro load_src | |
\{% path = __DIR__.gsub(%r{\A{{ROOT.id}}}, "").gsub(%r{[^/]+}, "..").id %} | |
require "\{{path[1..]}}/src/{{file.id}}" | |
end | |
load_src | |
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
# Load spec_helper to ensure the macro is loaded | |
require "../spec_helper" | |
spec "factories/user" | |
src "queries/user_query" | |
describe UserQuery do | |
# ... | |
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
require "../src/config/require" | |
# Macro is loaded downstream from environment configuration | |
src "config/env" |
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
src "queries/query" | |
src "models/user" | |
struct UserQuery < Query(User) | |
# ... | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment