crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.2k stars 1.61k forks source link

Visit the Time.local in the macro. #14741

Open zw963 opened 1 week ago

zw963 commented 1 week ago

Feature Request

Check discuss on this

Following is a example code

module College
  VERSION          = {{ `shards version "#{__DIR__}"`.chomp.stringify }}
  DEPLOYED_VERSION = {{ `git rev-parse --short HEAD`.chomp.stringify + `crystal eval 'puts Time.local.to_s(" %Y/%m/%d %H:%M:%S")'`.chomp.stringify }}
end

The output like this:

Deployed version: f5ca099 2024/06/21 18:16:48

Above time serve as a build time, deploy time, or release date for CLI app, is used for confirm we are packaged the binary or deployed the website which built on specified time.

I consider we should get the Time.local use a better way than awful crystal eval ... workaround.

crysbot commented 1 week ago

This issue has been mentioned on Crystal Forum. There might be relevant details there:

https://forum.crystal-lang.org/t/print-compile-time-when-app-starts/6278/16

straight-shoota commented 1 week ago

I don't see a reasonable use case for embedding the build time. It can be considered an antipattern because it hinders reproducible builds. If you really want do this, you can use the run macro. But I don't think there's a good reason to encourage this via a macro API.

zw963 commented 1 week ago

Users have their own purposes for use Time.local in the compile time, this is a so basic function, don't expect to seek help from date crystal run or crystal eval.