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

How to write odin info as json #48

Merged
merged 4 commits into from
Aug 29, 2024
Merged
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
1 change: 1 addition & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:
odin check glfw/window $FLAGS

odin check json/load_json $FLAGS
odin check json/odin_info $FLAGS

odin check learn_metal/00-window $FLAGS
odin check learn_metal/01-primitive $FLAGS
Expand Down
49 changes: 49 additions & 0 deletions json/odin_info/odin_info.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Demonstrate how you can create a json file
*/
package main

import "base:builtin"
import "base:runtime"
import "core:encoding/json"
import "core:fmt"
import "core:os"

main :: proc() {
fmt.println("Some of Odin's builtin constants")

path := len(os.args) > 1 ? os.args[1] : "odin_info.json"

info: struct {
ODIN_OS: runtime.Odin_OS_Type,
ODIN_ARCH: runtime.Odin_Arch_Type,
ODIN_ENDIAN: runtime.Odin_Endian_Type,
ODIN_VENDOR: string,
ODIN_VERSION: string,
ODIN_ROOT: string,
ODIN_DEBUG: bool,
} = {ODIN_OS, ODIN_ARCH, ODIN_ENDIAN, ODIN_VENDOR, ODIN_VERSION, ODIN_ROOT, ODIN_DEBUG}

fmt.println("Odin:")
fmt.printfln("%#v", info)

fmt.println("JSON:")
json_data, err := json.marshal(info, {
pretty = true,
use_enum_names = true,
})
if err != nil {
fmt.eprintfln("Unable to marshal JSON: %v", err)
os.exit(1)
}

fmt.printfln("%s", json_data)
fmt.printfln("Writing: %s", path)
werr := os.write_entire_file_or_err(path, json_data)
if werr != nil {
fmt.eprintfln("Unable to write file: %v", werr)
os.exit(1)
}

fmt.println("Done")
}