Advertisement
androth

Odin program to quickly generate hello worlds and templates

Aug 17th, 2024
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.60 KB | Source Code | 0 0
  1. package main
  2.  
  3. import "core:fmt"
  4. import "core:os"
  5. import s "core:strings"
  6.  
  7.  
  8. main::proc(){
  9.     switch len(os.args) {
  10.         case 1:
  11.             fmt.print(man)
  12.         case 2, 3:
  13.            
  14.             err : os.Error
  15.             odin_source : s.Builder
  16.             defer s.builder_destroy(&odin_source)
  17.  
  18.             odin_file : s.Builder
  19.             defer s.builder_destroy(&odin_file)
  20.             s.write_string(&odin_file, os.args[1])
  21.             s.write_string(&odin_file, ".odin")
  22.  
  23.             if err = os.make_directory(os.args[1]); err != os.ERROR_NONE {
  24.                     fmt.println(os.error_string(err))
  25.                     break
  26.             }
  27.             os.change_directory(os.args[1])
  28.            
  29.             s.write_string(&odin_source, "package ")
  30.             s.write_string(&odin_source, os.args[1])
  31.             s.write_string(&odin_source, "\n\n")
  32.  
  33.            
  34.             if len(os.args) == 2 {
  35.                 s.write_string(&odin_source, hello_world)
  36.             } else {
  37.                 if os.args[2] == "hello-world" do s.write_string(&odin_source, hello_world)
  38.                 if os.args[2] == "track-alloc" do s.write_string(&odin_source, track_alloc)
  39.                 if os.args[2] == "raylib" do s.write_string(&odin_source, raylib_template)
  40.             }
  41.  
  42.             os.write_entire_file( s.to_string(odin_file), odin_source.buf[:] )
  43.  
  44.  
  45.         case:
  46.             fmt.println("Too many arguments")
  47.             fmt.println("\n", man)
  48.     }
  49. }
  50.  
  51. man :string = `
  52. odininit a tool for quickly starting new odin programs
  53. usage:
  54.     odininit program_name [program_template]
  55.    
  56.     If no program_template is provided, a hello world
  57.     program is created.
  58.  
  59.     program_name:
  60.         creates a folder of program_name, if one does not exist
  61.         changes directory to that folder and creates necessary
  62.         template files there.
  63.  
  64.     program_templates:
  65.         hello-world
  66.         track-alloc
  67.         raylib
  68. `
  69.  
  70. hello_world :string = `
  71. import "core:fmt"
  72.  
  73. main :: proc() {
  74.     fmt.println("Hellope")
  75. }`
  76.  
  77. track_alloc : string = `
  78. import "core:mem"
  79. import "core:fmt"
  80.  
  81. _main :: proc() {
  82.         fmt.println("Hellope")
  83. }
  84.  
  85. main :: proc() {
  86.         track: mem.Tracking_Allocator
  87.         mem.tracking_allocator_init(&track, context.allocator)
  88.         defer mem.tracking_allocator_destroy(&track)
  89.         context.allocator = mem.tracking_allocator(&track)
  90.  
  91.         _main()
  92.  
  93.         for _, leak in track.allocation_map {
  94.                         fmt.printf("%v leaked %m\n", leak.location, leak.size)
  95.         }
  96.         for bad_free in track.bad_free_array {
  97.                         fmt.printf("%v allocation %p was freed badly\n", bad_free.location, bad_free.memory)
  98.         }
  99. }
  100. `
  101.  
  102. raylib_template : string = `
  103. import rl "vendor:raylib"
  104.  
  105. main :: proc() {
  106.         rl.InitWindow(800, 400, "Raylib Basic Window")
  107.  
  108.         for !rl.WindowShouldClose() {
  109.                 rl.BeginDrawing()
  110.                         rl.ClearBackground(rl.LIGHTGRAY)
  111.                         rl.DrawText("Hellope!", 190, 200, 40, rl.DARKGRAY)
  112.                 rl.EndDrawing()
  113.         }
  114.  
  115.         rl.CloseWindow()
  116. }`
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement