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

**URL:** <https://forum.sublimetext.com/t/template-multi-command-builds-with-input-powershell-script/18683>\
**Category:** General Discussion\
**Created:** [March 22, 2016, 10:46am UTC](https://forum.sublimetext.com/t/template-multi-command-builds-with-input-powershell-script/18683 "2016-03-22T10:46:26Z")\
**Posts on this page:** 11\
**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:** [March 22, 2016, 10:46am UTC](https://forum.sublimetext.com/t/template-multi-command-builds-with-input-powershell-script/18683/1 "2016-03-22T10:46:26Z")

</div>

#INTRO:

&nbsp;  
I’ve seen a few threads about capturing input in builds recently, so I decided to put together an easy way to manage builds from a central location.

The script requires a Windows PC with PowerShell.  
_( tested with: PowerShell: v5.0, build 10586, rev 122 )_

&nbsp;  
**Benefits of PowerShell & This Script:**

- chain multiple commands
- run parallel builds
- `SublimeBuild` for output only and/or debugging
- `PowerShell` for input
- both builds are independent & can receive different commands
- define all of your build commands in one file
- **`sublime-build`** files follow a simple template
- strong system integration
- customizable visual style
- select & copy text
- syntax is easier to work with than batch files

# Script [@ GitHub](https://github.com/Enteleform/-SCRIPTS-/tree/master/SublimeText/PowerShell_Build)

&nbsp;

* * *

* * *

#DEMO:

## `@ Java`

![](https://forum.sublimetext.com/uploads/default/original/3X/9/3/93827e8206facf89c4646c2423f05e926c4777e8.gif)

## `@ Python`

![](https://forum.sublimetext.com/uploads/default/original/3X/d/2/d28e7c1bd16a5af16092cb18b8c36b78b1e1d714.gif)

&nbsp;

* * *

* * *

#INFO:

&nbsp;  
There are 3 main sections in the script:

- **`USER SETTINGS`**
- **`FUNCTIONS`**
- **`SCRIPT`**

&nbsp;  
**`USER SETTINGS`** is the only part of the script that needs to be modified.

It has 2 sub-sections:

### **`Variables`**

- default **`PowerShell`** & **`SublimeBuild`** states
- header options ( **`file`** | **`fileName`** | **`fileBase`** | **`filePath`** | **`projectName`** )

### **`Build Settings`**

- first build to run ( **`PowerShell`** or **`SublimeBuild`** )
- commands to be run @ **`PowerShell`**
- commands to be run @ **`SublimeBuild`**
- [_optional_] disable **`PowerShell`** or **`SublimeBuild`**
- [_optional_] disable headers  
&nbsp;

* * *

* * *

#EXAMPLES:

## `@ SublimeText_Builds.ps1/BuildSettings`

```auto
ElseIf ( $MODE -ieq "Java_Input" )
	{
		$firstBuild = $SUBLIME_BUILD
		$sublimeBuild_Commands += `
			"If ( Test-Path " + $sQuote + $filePathSlash + $fileBase + ".class" + $sQuote + " )" + `
			"{ Remove-Item " + $sQuote + $filePathSlash + $fileBase + ".class" + $sQuote + " }"
		$sublimeBuild_Commands += "cd " + $sQuote + $filePath + $sQuote
		$sublimeBuild_Commands += "javac -encoding UTF-8 " + $sQuote + $fileName + $sQuote
		$powerShell_Commands += "cd " + $pQuote + $filePath + $pQuote
		$powerShell_Commands += "java " + $fileBase
	}
ElseIf ( $MODE -ieq "Java_NoInput" )
	{
		$firstBuild = $SUBLIME_BUILD
		$sublimeBuild_Commands += `
			"If ( Test-Path " + $sQuote + $filePathSlash + $fileBase + ".class" + $sQuote + " )" + `
			"{ Remove-Item " + $sQuote + $filePathSlash + $fileBase + ".class" + $sQuote + " }"
		$sublimeBuild_Commands += "cd " + $sQuote + $filePath + $sQuote
		$sublimeBuild_Commands += "javac -encoding UTF-8 " + $sQuote + $fileName + $sQuote
		$sublimeBuild_Commands += "java " + $fileBase
		$powerShell_Enabled = $FALSE
		$bottomHeader_Enabled = $FALSE
	}

```

## @ `JavaC.sublime-build`

```auto
{
	"selector": "source.java",
	"cmd": ["PowerShell", "SublimeText_Builds.ps1", "Java_NoInput", "\"$file\"", "\"$file_name\"", "\"$file_base_name\"", "\"$file_path\"", "\"$project_base_name\""],
	"file_regex": "^(...*?):([0-9]*):?([0-9]*)",

	"variants":
		[
			{
				"name": "Input",
				"cmd": ["PowerShell", "SublimeText_Builds.ps1", "Java_Input", "\"$file\"", "\"$file_name\"", "\"$file_base_name\"", "\"$file_path\"", "\"$project_base_name\""],
			},
		]
}

```

The **`sublime-build`** template only requires 3 updates when creating a new build:

- **`selector`**
- **`file_regex`**
- the 3rd argument of each **`"cmd"`**
- ( must match @ **`SublimeText_Builds.ps1/BuildSettings`** )

---

<div class="post-metadata">

**Author:** ![kingkeith](https://forum.sublimetext.com/user_avatar/forum.sublimetext.com/kingkeith/32/50274_2.png) [@kingkeith](https://forum.sublimetext.com/u/kingkeith)\
**Post date:** [March 24, 2016, 11:04am UTC](https://forum.sublimetext.com/t/template-multi-command-builds-with-input-powershell-script/18683/2 "2016-03-24T11:04:19Z")

</div>

@fico, what is your gif generation process, out of interest? On Windows, I currently record with [Icecream Screen Recorder](http://icecreamapps.com/Screen-Recorder/), and then use a free website [http://ezgif.com/video-to-gif](http://ezgif.com/video-to-gif) to convert it, which is far from ideal…

---

<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:** [March 24, 2016, 11:39am UTC](https://forum.sublimetext.com/t/template-multi-command-builds-with-input-powershell-script/18683/3 "2016-03-24T11:39:51Z")

</div>

For a while, I was using **[ScreenToGIF](https://screentogif.codeplex.com/)** for GIFs and Icecream Screen Recorder for screencasts.

I just started using **[ActivePresenter](http://atomisystems.com/activepresenter/)** in the last few weeks and… it is **BEASTLY**.

It is the most efficient editor I’ve used for this type of thing, out of ANYTHING ( including Premiere, After Effects, etc. ).

The best part of it is the **slide** functionality. &nbsp; You can record a full screencast, and then break it up very easily into sections. &nbsp; It also has the best **annotation** features I’ve seen in this genre of software. _( Check out the last GIF on **[this thread](https://autohotkey.com/boards/viewtopic.php?f=6&t=14946)** )_

The paid versions also have some **[pretty crazy features](http://atomisystems.com/demo/creating-comment-microsoft-word-2013/)**.  
_( I’m on the free version for now, but it still has huge amounts of functionality )_

The only things it’s lacking right now are GIF output & individual slide output.  
My workaround is to export video, and run it through the ScreenToGIF editor.  
&nbsp;

* * *

* * *

&nbsp;  
If I need to export multiple slides, I have two ways of doing it. &nbsp; Both are kind of a pain in the ass.

- A **[GIF splitting script](https://gist.github.com/Enteleform/91278ebca279845f0edd)** I wrote for Processing with the `gifAnimation` library. &nbsp; It’s super slow & requires manual split points to be entered.
- Save the ActivePresenter file for as many slides as there are, delete all slides leaving a single unique slide in each file, export each file to a video, and run each video through SplitToGIF.

Neither is ideal, but it still beats anything else that’s available. &nbsp; Plus it’s still super quick if you’re just working with one GIF at a time.

I’m going to write a blog post demoing their software, targeted towards developers. &nbsp; The subject will be how to create useful, informative posts on forums & communities like SO. &nbsp; Hopefully I can sway them to add the missing features I mentioned earlier, considering that the online dev community is pretty huge & their software is pretty much the best way for devs to communicate graphically.

---

<div class="post-metadata">

**Author:** ![kingkeith](https://forum.sublimetext.com/user_avatar/forum.sublimetext.com/kingkeith/32/50274_2.png) [@kingkeith](https://forum.sublimetext.com/u/kingkeith)\
**Post date:** [March 24, 2016, 12:12pm UTC](https://forum.sublimetext.com/t/template-multi-command-builds-with-input-powershell-script/18683/4 "2016-03-24T12:12:07Z")

</div>

Wow, that is neat! Thanks for the comprehensive info!

I look forward to the aforementioned blog post, and hope it will indeed sway the devs to add GIF output and individual slide output 👍

---

<div class="post-metadata">

**Author:** ![jfcherng](https://forum.sublimetext.com/user_avatar/forum.sublimetext.com/jfcherng/32/78056_2.png) [@jfcherng](https://forum.sublimetext.com/u/jfcherng)\
**Post date:** [March 28, 2016, 12:02am UTC](https://forum.sublimetext.com/t/template-multi-command-builds-with-input-powershell-script/18683/5 "2016-03-28T00:02:43Z")

</div>

FYI, for the aspect of input, I think [Console Exec](https://packagecontrol.io/packages/Console%20Exec) is more ideal than your script.

- easier to use (add `"target": "console_exec"` to an existing build setting and that’s all)
- cross-platform (Win/Linux, not for Mac)

Although, the screenshot in the readme of `Console Exec` looks pretty lame and it does not mention it resolves the input problem which most people care about.

---

<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:** [March 28, 2016, 12:20am UTC](https://forum.sublimetext.com/t/template-multi-command-builds-with-input-powershell-script/18683/6 "2016-03-28T00:20:49Z")

</div>

> [@jfcherng](#):
>
> FYI, for the aspect of input, I think Console Exec is more ideal than your script.

&nbsp;  
Thanks for the tip, I had actually checked out **Console Exec** before putting this together. &nbsp; Your solution is great for basic input & much more user friendly, but I was also looking for a few extra features in my build process.

&nbsp;

I shared it in case anyone else happens to get use from it, but the main reasons I created it for my personal use are:

- To manage a single workspace for all of my builds

- To bypass the limitations of inline `JSON` definitions, giving me the ability to include variables and functions for more complex build solutions & future extensibility

- Powershell has more powerful system integration than `cmd`. &nbsp; It’s also a lot easier on the eyes since it can be styled, and it allows you to select specific text from the results.

---

<div class="post-metadata">

**Author:** ![mark1](https://forum.sublimetext.com/letter_avatar_proxy/v4/letter/m/e0b2c6/32.png) [@mark1](https://forum.sublimetext.com/u/mark1)\
**Post date:** [January 10, 2017, 11:00pm UTC](https://forum.sublimetext.com/t/template-multi-command-builds-with-input-powershell-script/18683/7 "2017-01-10T23:00:20Z")

</div>

could u add support for c? (using clang) so it opens and puts the output in a cygwin terminal :d

---

<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, 5:34am UTC](https://forum.sublimetext.com/t/template-multi-command-builds-with-input-powershell-script/18683/8 "2017-01-11T05:34:29Z")

</div>

> [@mark1](#):
>
> could u add support for c? (using clang) so it opens and puts the output in a cygwin terminal :d

&nbsp;

##See:  
##[**[TEMPLATE] Dynamic Builds with External CLI (Python Script)**](https://forum.sublimetext.com/t/template-dynamic-builds-with-external-cli-python-script/25481)

&nbsp;  
That method is way cleaner & way easier to use, so you can probably just swap out a few lines to make it work for you.

---

<div class="post-metadata">

**Author:** ![mark1](https://forum.sublimetext.com/letter_avatar_proxy/v4/letter/m/e0b2c6/32.png) [@mark1](https://forum.sublimetext.com/u/mark1)\
**Post date:** [January 11, 2017, 5:42pm UTC](https://forum.sublimetext.com/t/template-multi-command-builds-with-input-powershell-script/18683/9 "2017-01-11T17:42:05Z")

</div>

{  
“shell\_cmd” : “clang-3.8.exe -o “$file\_base\_name” “${file\_name}” && “./${file\_base\_name}””,  
“selector” : “source.c”,  
“path” : “C:\cygwin64\bin;$path”,  
“working\_dir” : “$file\_path”  
}

I genuinely have no idea… thats what im currently using

---

<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 13, 2017, 2:07am UTC](https://forum.sublimetext.com/t/template-multi-command-builds-with-input-powershell-script/18683/10 "2017-01-13T02:07:39Z")

</div>

> [@mark1](#):
>
> {“shell\_cmd” : “clang-3.8.exe -o "$file\_base\_name" "${file\_name}" && "./${file\_base\_name}"”,“selector” : “source.c”,“path” : “C:\cygwin64\bin;$path”,“working\_dir” : “$file\_path”}
> 
> I genuinely have no idea… thats what im currently using

&nbsp;  
Idk man, haven’t worked with `C` or `Clang`.

You basically just need to figure out the command/argument formats that `cygwin` and `clang` use, and then manipulate the strings passed from SublimeText into those formats.

&nbsp;

For `PowerShell`, I had to figure out a way to allow quoted commands in case the filepath contained spaces. The solution was to prefix the initial command with `&` followed by a space. For your system, it might work right away, or you can figure out the proper syntax, or you can just ditch the quotes if your `cygwin` path doesn’t contain spaces.

I also had to pass the `-NoExit` arg to keep the `PowerShell` window open, so you might need to figure out something similar for `cygwin`.

&nbsp;

Here’s my best attempt not having really worked with your toolset, you might need to play around with what’s quoted and what’s not and maybe some other formatting.

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

```python
import sublime, sublime_plugin
import subprocess

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

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

		cygwin_EXE = quote("C:\\PATH\\TO\\cygwin.exe")
		__ = ";" # Cygwin Command Separator

		command__SetWorkingDirectory = "cd " + quote(filePath)
		
		clang_EXE = quote("C:\\PATH\\TO\\clang-3.8.exe")
		arg__Output = "-o "
		command __BuildWithClang = clang_EXE + " " + arg__ Output + quote(fileBase) + " " + quote(fileName) + " && " + quote("./$" + fileBase)

		commands = [
			cygwin_EXE,
			__, command__ SetWorkingDirectory,
			__, command__ BuildWithClang,
		]

		subprocess.Popen(commands)

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

```

&nbsp;

### @ `Packages/BuildSystems/Clang.sublime-build`

```auto
{
	"selector": "source.c, source.c++",
	"target": "clang_build_system",
	"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
	"cmd": ["$file", "$file_name", "$file_base_name", "$file_path", "$project_base_name"],
}

```

---

<div class="post-metadata">

**Author:** ![mark1](https://forum.sublimetext.com/letter_avatar_proxy/v4/letter/m/e0b2c6/32.png) [@mark1](https://forum.sublimetext.com/u/mark1)\
**Post date:** [January 14, 2017, 5:20pm UTC](https://forum.sublimetext.com/t/template-multi-command-builds-with-input-powershell-script/18683/11 "2017-01-14T17:20:28Z")

</div>

I mean, even if you did it in PowerShell I wouldn’t mind as long as it worked… but I tried the above and ctrl b had no effect whatsoever.

Also I believe both fields should be in the C file not BuildSystems. Since they didn’t appear as Builds until I moved them to the @Packages/C
