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

**URL:** <https://forum.sublimetext.com/t/template-dynamic-builds-with-external-cli-python-script/25481>\
**Category:** General Discussion\
**Created:** [January 11, 2017, 11:27am UTC](https://forum.sublimetext.com/t/template-dynamic-builds-with-external-cli-python-script/25481 "2017-01-11T11:27:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![fico](https://forum.sublimetext.com/letter_avatar_proxy/v4/letter/f/c5a1d2/32.png) [@fico](https://forum.sublimetext.com/u/fico)\
**Post date:** [January 11, 2017, 11:27am UTC](https://forum.sublimetext.com/t/template-dynamic-builds-with-external-cli-python-script/25481/1 "2017-01-11T11:27:32Z")

</div>

I previously posted a [**PowerShell Build System**](https://forum.sublimetext.com/t/template-multi-command-builds-with-input-powershell-script) 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.

&nbsp;  
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**](https://gist.github.com/Enteleform/87b1dce9730f95d3cafce24405256e17) that automatically compiles & runs packages & has verbose output toggle

- a [**Python Build System**](https://gist.github.com/Enteleform/852e7f79c14cbe6c5e737f52d930f9a8) 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_)  
&nbsp;

* * *

* * *

&nbsp;  
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_)  
&nbsp;

###@ `Packages/BuildSystems/Python_BuildSystem.py`

```python
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)

```

&nbsp;  
###@ `Packages/BuildSystems/Python.sublime-build`

```auto
{
	"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,
			},
		]
}

```

&nbsp;

### **Notes:**

I use [**PackageResourceViewer**](https://packagecontrol.io/packages/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`
