Sublime Forum

[TEMPLATE] Dynamic Builds with External CLI (Python Script)

#1

I previously posted a PowerShell Build System in response to users frequently asking about building with input. After a while, I got tired of dealing with the PowerShell syntax, so I ended up writing a new build system with Python.

 
This implementation is way smaller, easier to read, & faster to implement. Also, each build can be self contained, rather than the hot mess of builds in the previous implementation.

So far, I’ve set up:

  • a Java Build System that automatically compiles & runs packages & has verbose output toggle

  • a Python Build System that injects code before running the file, which includes UTF-encoded console output

Both take sublime-build arguments to automatically include/import file & directory arrays by absolute path (.jar & .java for the Java Build, .py for the Python build)

(those links are for reference only, I have some of the aforementioned import sorcery happening in my personal Build System files, so they won’t compile unless they’re on my system)
 



 
Here’s a simplified Python build system where I added an externalBuild_Enabled parameter to figure out where to route the output (SublimeText for no input, PowerShell for input)
 

###@ Packages/BuildSystems/Python_BuildSystem.py

import sublime, sublime_plugin
import subprocess

class python_build_system_command(sublime_plugin.WindowCommand):
	def run( self, cmd=[], file_regex="",
		externalBuild_Enabled = False,
	):

		filePath, fileName, fileBase, fileDirectory, projectName = cmd
		quoted_FilePath = quote(filePath)

		python_EXE = quote_PowershellCommand("C:\\_Frameworks\\Python\\3\\python.exe")
		__ = ";" # PowerShell Command Separator

		if(externalBuild_Enabled): arg__NoExit = "-NoExit"
		else:                      arg__NoExit = ""

		commands = [
			"powershell", arg__NoExit,
			__, python_EXE, "-u", quoted_FilePath,
		]

		if(externalBuild_Enabled): subprocess.Popen(commands)
		else:                      self.window.run_command( "exec", {"cmd": commands, "shell": False} )


def quote(text):
	text = '"' + text + '"'
	return(text)

def quote_PowershellCommand(text):
	text = '& \"' + text + '"'
	return(text)

 
###@ Packages/BuildSystems/Python.sublime-build

{
	"selector":   "source.python",
	"target":     "python_build_system",
	"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
	"cmd":        ["$file", "$file_name", "$file_base_name", "$file_path", "$project_base_name"],

	"externalBuild_Enabled": false,

	"variants":
		[
			{
				"name": "Input",
				"externalBuild_Enabled": true,
			},
		]
}

 

###Notes:

I use PackageResourceViewer to open any default sublime-build files that I’m overriding & replace their contents with {"selector": "null.null"} so that they no longer show up in the build options.

You can replace powershell (the first line of the commands array) & redefine the command separator if you want to route your output to some other command line interface.

Don’t modify the cmd arguments @ the sublime-build, just create additional parameters at both the Lang_BuildSystem.py & Lang.sublime-build files. I tried passing the file variables that are in the cmd array as separate arguments, but it seems that they only expand properly within cmd

If you create a build system for another language, make sure to change the Lang_BuildSystem.py class name to lang_build_system_command, and change the target in the Lang.sublime-build file to lang_build_system

0 Likes

[TEMPLATE] Multi-Command Builds with Input (PowerShell Script)