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

Implemented AMBER_HEADER and AMBER_FOOTER variables #682

Open
wants to merge 4 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,30 @@ impl AmberCompiler {
}

let now = Local::now().format("%Y-%m-%d %H:%M:%S").to_string();
let header = include_str!("header.sh")

let header_template =
if let Ok(dynamic) = env::var("AMBER_HEADER") {
fs::read_to_string(dynamic.to_string()).expect(format!("Couldn't read the dynamic header file from {dynamic}").as_str())
} else {
include_str!("header.sh").to_string()
};

let footer_template =
if let Ok(dynamic) = env::var("AMBER_FOOTER") {
fs::read_to_string(dynamic.to_string()).expect(format!("Couldn't read the dynamic footer file from {dynamic}").as_str())
} else {
String::new()
};

let header = header_template
.replace("{{ version }}", env!("CARGO_PKG_VERSION"))
.replace("{{ date }}", now.as_str());

let footer = footer_template
Comment on lines +202 to +221
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section is pretty long and adds up to the length of the translate method. Would you consider extracting it to a new private gen_header (or something similar) method?

.replace("{{ version }}", env!("CARGO_PKG_VERSION"))
.replace("{{ date }}", now.as_str());
Ok(format!("{}{}", header, result))

Ok(format!("{}\n{}\n{}", header, result, footer))
}

pub fn document(&self, block: Block, meta: ParserMetadata, output: Option<String>) {
Expand Down
2 changes: 1 addition & 1 deletion src/header.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
# Written in [Amber](https://amber-lang.com/)
# version: {{ version }}
# date: {{ date }}
# date: {{ date }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding newline in the end of the file

Suggested change
# date: {{ date }}
# date: {{ date }}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've outlined before why it has to be hardcoded, so adding a newline here would mean that there are two newlines in the output

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes. We could header.trim_end() so that this file can be saved properly. Developers using custom headers would have to remember not to put the additional line too.

Loading