Advertisement
Metaxal

Recompile and restart

Jan 11th, 2013
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 2.18 KB | None | 0 0
  1. #lang racket/gui
  2.  
  3. ;;; Recompile and restart test
  4. ;;; Author: Laurent Orseau <laurent orseau gmail com> -- 2013-01-11
  5. ;;; License: WTFPL - http://www.wtfpl.net
  6.  
  7. (require racket/runtime-path
  8.          mzlib/os ; for getpid (warning: deprecated)
  9.          compiler/cm
  10.          )
  11.  
  12. ; TODO: Shouldn't we wait for the parent to finish?
  13.  
  14. (define pid (getpid))
  15.  
  16. (define-runtime-path this-file (syntax-source-module #'here #t))
  17.  
  18. (define (dprintf fmt . lstr)
  19.   (apply printf (string-append "[" (number->string pid) "] " fmt) lstr))
  20.  
  21. (define (wait-msg str)
  22.   (dprintf "~a... " str))
  23.  
  24. (define (ok)
  25.   (printf "Ok.\n"))
  26.  
  27. (define (recompile-restart)
  28.   (define this-file-string (path->string this-file))
  29.  
  30.   (wait-msg "Modifying file")
  31.   (display-lines-to-file
  32.    (list (~s `(dprintf ,(string-append "Ancestor: " (number->string pid) "\n"))))
  33.    this-file #:exists 'append)
  34.   (ok)
  35.  
  36.   (with-handlers ([exn:fail?
  37.                    (λ(e)
  38.                      (dprintf "Something went wrong during recompile & restart:\n")
  39.                      (displayln (exn-message e))
  40.                      (dprintf "Aborting procedure.\n"))])
  41.     ; Recompile this file (like "raco make ....")
  42.     (wait-msg "Recompiling")
  43.     (managed-compile-zo this-file)
  44.     (ok)
  45.        
  46.     (wait-msg "Running child")
  47.     (define-values (sp a b c)
  48.       (subprocess (current-output-port) (current-input-port) (current-error-port)
  49.                   (find-executable-path (find-system-path 'exec-file))
  50.                   this-file-string))
  51.     (ok)
  52.        
  53.     ; I don't think I need to close the ports since we use our parent's ones
  54.     ;(displayln (list sp a b c))
  55.    
  56.     ; If all went ok, terminate program by hiding the frame
  57.     (wait-msg "Closing frame")
  58.     (send my-frame show #f)    
  59.     (ok)
  60.    
  61.     (newline)
  62.     ; Make sure to exit in case anything hangs (like some non-closed frame%)
  63.     (exit)
  64.     ))
  65.  
  66. ;;; A simple GUI
  67. (define my-frame (new frame% [label "my-frame"] [min-width 200] [min-height 200]))
  68. (define bt (new button% [parent my-frame] [label "Recompile && Restart"]
  69.                 [callback (λ(bt ev)(recompile-restart))]))
  70.  
  71.  
  72. (send my-frame show #t)
  73. (dprintf "I'm alive!\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement