From 3afb56a5e5274611c3c5d0f7e2f4a8965575ff40 Mon Sep 17 00:00:00 2001 From: William Carroll Date: Tue, 11 Feb 2020 17:04:03 +0000 Subject: [PATCH] Begin supporting run I'd like to be able to just call `run file.py` and have a program DWIM. I'm working on run as a step in this direction. Define a simple configuration that maps file extensions to template strings where "$file" is replaced with the argv[1]. It basically works but there are outstanding TODOs. See the README and source code for more information. --- run/.envrc | 2 ++ run/README.md | 30 ++++++++++++++++++++++++++++++ run/default.nix | 16 ++++++++++++++++ run/main.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ run/shell.nix | 7 +++++++ 5 files changed, 104 insertions(+) create mode 100644 run/.envrc create mode 100644 run/README.md create mode 100644 run/default.nix create mode 100644 run/main.go create mode 100644 run/shell.nix diff --git a/run/.envrc b/run/.envrc new file mode 100644 index 000000000..b80e28b4b --- /dev/null +++ b/run/.envrc @@ -0,0 +1,2 @@ +source_up +eval "$(lorri direnv)" diff --git a/run/README.md b/run/README.md new file mode 100644 index 000000000..d3cccecf9 --- /dev/null +++ b/run/README.md @@ -0,0 +1,30 @@ +# run + +Simplify the commands you call to run scripts on the command line. + +```shell +> run path/to/file.py +> run path/to/file.ts +``` + +## How? + +Define a run.json configuration mapping commands to filename extensions like +so: +```json +{ + ".ts": "npx ts-node $file", + ".py": "python3 $file" +} +``` + +Then call `run path/to/some/file.ts` on the command line, and `npx ts-node +file.ts` will run. + +## Installation + +Install `run` using Nix. + +```shell +> nix-env -iA briefcase.run +``` diff --git a/run/default.nix b/run/default.nix new file mode 100644 index 000000000..113491536 --- /dev/null +++ b/run/default.nix @@ -0,0 +1,16 @@ +{ + pkgs ? import {}, + depot ? import {}, + briefcase ? import {}, + ... +}: + +depot.buildGo.program { + name = "run"; + srcs = [ + ./main.go + ]; + deps = with briefcase.gopkgs; [ + utils + ]; +} diff --git a/run/main.go b/run/main.go new file mode 100644 index 000000000..04906ece9 --- /dev/null +++ b/run/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "os" + "os/exec" + "path/filepath" + "strings" + "utils" +) + +func main() { + if len(os.Args) != 2 { + log.Fatal("You can only call run with a single file at a time.") + } + + rulesPath := utils.Resolve("run.json", []string{"/home/wpcarro/.config/run/run.json"}) + b, err := ioutil.ReadFile(rulesPath) + if err != nil { + log.Fatal("Could not locate a run.json file: ", err) + } + rules := map[string]string{} + err = json.Unmarshal(b, &rules) + if err != nil { + log.Fatal("Could not decode run.json as JSON: ", err) + } + + fileName := os.Args[1] + ext := filepath.Ext(fileName) + cmd, ok := rules[ext] + + if !ok { + log.Fatalf("No rules for extension, %s, have been defined.", ext) + } + + // TODO(wpcarro): Support more sophisticated parsing than just string + // splitting. To handle 'cases like this'. + tokens := strings.Split(strings.Replace(cmd, "$file", fileName, 1), " ") + c := exec.Command(tokens[0], tokens[1:]...) + err = c.Start() + // TODO(wpcarro): Forward STDERR and STDOUT. + if err != nil { + log.Fatal(err) + } + fmt.Println(c.Wait()) +} diff --git a/run/shell.nix b/run/shell.nix new file mode 100644 index 000000000..8b97f04ca --- /dev/null +++ b/run/shell.nix @@ -0,0 +1,7 @@ +{ pkgs ? import {}, ... }: + +pkgs.mkShell { + buildInputs = with pkgs; [ + go + ]; +}