dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

ob-powershell.el (2366B)


      1 ;;; ob-powershell.el --- Run Powershell from org mode source blocks
      2 ;;; SPDX-License-Identifier: MIT
      3 
      4 ;; Copyright (C) 2022 Rob Kiggen
      5 
      6 ;; Author: Rob Kiggen <robby.kiggen@essential-it.be>
      7 ;; Maintainer: Mois Moshev <mois.moshev@bottleshipvfx.com>
      8 ;; Version: 1.0
      9 ;; Package-Requires: ((emacs "26.1"))
     10 ;; Keywords: powershell, shell, execute, outlines, processes
     11 ;; URL: https://github.com/rkiggen/ob-powershell
     12 
     13 ;;; Commentary:
     14 
     15 ;; Currently this only supports the external compilation and execution
     16 ;; of Powershell code blocks (i.e., no session support).
     17 ;;; Code:
     18 
     19 (require 'ob)
     20 
     21 (defvar org-babel-tangle-lang-exts)
     22 (add-to-list 'org-babel-tangle-lang-exts '("powershell" . "ps1"))
     23 
     24 (defcustom ob-powershell-powershell-command "powershell"
     25   "Name of command used to evaluate powershell blocks."
     26   :group 'org-babel
     27   :version "24.3"
     28   :type 'string)
     29 
     30 
     31 (defun org-babel-execute:powershell (body params)
     32   "Execute a block of Powershell code BODY with Babe passing PARAMS.
     33 This function is called by `org-babel-execute-src-block'."
     34   (let ((scriptfile (org-babel-temp-file "powershell-script-" ".ps1"))
     35         (full-body (org-babel-expand-body:generic
     36 		                body params (org-babel-variable-assignments:powershell params))))
     37     (message "%s" full-body)
     38     (with-temp-file scriptfile (insert full-body))
     39     (org-babel-eval (concat ob-powershell-powershell-command " " scriptfile) "")))
     40 
     41 (defun org-babel-variable-assignments:powershell (params)
     42   "Return a list of Powershell statements parsed from PARAMS, assigning the block's variables."
     43   (mapcar
     44    (lambda (pair)
     45      (format "$env:%s=%s"
     46              (car pair)
     47              (ob-powershell-var-to-powershell (cdr pair))))
     48    (org-babel--get-vars params)))
     49 
     50 (defun ob-powershell-var-to-powershell (var)
     51   "Convert :var into a powershell variable.
     52 Convert an elisp value, VAR, into a string of poershell source code
     53 specifying a variable of the same value."
     54   (if (listp var)
     55       (concat "[" (mapconcat #'ob-powershell-var-to-powershell var ", ") "]")
     56     (format "$%S" var)))
     57 
     58 (defun org-babel-prep-session:powershell (session params)
     59   "Return an error because Powershell does not support sessions.
     60 SESSION refers to the babel session.
     61 PARAMS are the passed parameters."
     62   (error "Sessions are not (yet) supported for Powershell"))
     63 
     64 
     65 (provide 'ob-powershell)
     66 ;;; ob-powershell.el ends here