Sublime build system to open file at local server address - bash

For a website I'm developing with the help of a local server (running at http://127.0.0.1:4000), I try to write a build system for my Sublime Text project settings.
I have access to these variables (provided by Sublime Text):
$file: Stores the absolute path to the file on disk:C:\Users\User\dev\repos\base\dir\index.html
$project_path: Stores the location of the project file (usually the root of the project):C:\Users\User\dev\repos\base
Now what I want is the content of $file but instead of the $project_path I want http://127.0.0.1:4000/~base. For that task I tried the following with Bash cmd.exe:
CALL SET result=%file:%project_path%=http://127.0.0.1:4000\~base\% && echo %result%
This gives the desired result, however I can't seem to be able to apply it to the build system inside Sublime Text.
For now, I try to generate the correct address and output it via cmd.exe:
{
"build_systems":
[
{
"name": "Preview in browser",
"selector": "text.html",
"windows":
{
"shell": true,
"cmd": [
"start", "cmd", "/k",
"CALL SET result=$file:$project_path=http://127.0.0.1:4000\\~base\\ && echo $result"
]
}
}
]
}
Result:
> echo %result%
C:\Users\Philipp\dev\repos\base\dir\index.html:C:\Users\Philipp\dev\repos
\base=http://127.0.0.1:4000\~base\
So the substitution is not working when doing it in the build system, but in cmd.exe it does. I'm confused.

You can do something like this:
{
"build_systems":
[
{
"name": "Preview in browser",
"selector": "text.html",
"windows":
{
"shell_cmd": "CALL SET filePath=$file && CALL SET result=%filePath:$project_path=http://127.0.0.1:4000\\~base% && CALL C:/Progra~2/Google/Chrome/Application/chrome.exe %result%"
}
}
]
}
The problem was with windows replacement and variable asignation rather than with sublime builds.
As it has been said in chat and comments multiple CALL are necessary to use the real value variable because if not used windows will expand their value at parse time before the wanted value is asigned at execution time. In addition, shell_cmd can be used to run a unique command in shell.

Related

Jenkins Pipeline does not store Windows env variables from a cmd file

In a Jenkins Pipeline (groovy) i try to run a windows script and use the variables set in this script afterwards.
I broke down the situation to this Pipeline attached.
Here I call "set" twice. Once before the script and once after the script.
My expectation is that i see the new variable in the second output. But the output does not change at all. I do see some (PATH, ...), so it is not just empty.
The goal is to have scripts working that set for example qt environment variables.
Does anybody know if I call the script in a "bad" manor? or sth else?! that I miss here.
timestamps {
parallel(
"build_windows": {
node('winddk-build') {
stage('create bat file') {
bat """
>mybat.cmd echo set MYVAR=1234
>>mybat.cmd echo cmd /k
"""
}
stage('run bat file') {
bat """
set
cmd /k mybat.cmd
set
"""
}
stage('clean up bat file') {
bat """
del /Q /S mybat.cmd
"""
}
}
}
)
}

Save terminal tabs to saved workspace VSCode

Is it possible to save Terminal settings to a workspace? For most of my projects I always have two terminal tabs open. One where I do all of my git work and one where I run gulp tasks. These are two different folders and neither are the project root. When I open a saved workspace it always just opens one tab to the project root.
Look at the Restore Terminals extension. For example, in your settings.json:
"restoreTerminals.runOnStartup": false, // true is the default
// set to false if using a keybinding or command palette
"restoreTerminals.terminals": [
{
"splitTerminals": [
{
"name": "git",
"commands": [
"cd <your directory>",
"npm run test" // your git command(s)
]
}
]
},
{
"splitTerminals": [
{
"name": "gulp",
"commands": [
"cd zip",
"gulp sass"
]
}
]
}
]
will open two terminals, one for your git work and one for the gulp work. They can each take multiple commands.
Example keybinding:
{
"key": "shift+alt+t", // whatever keybinding if you wish
"command": "restore-terminals.restoreTerminals",
},
or you can it run at start-up.
The Tasks feature is the current recommended way to handle this. There is no need for an extension. See Automating launching of terminals in the VS Code documentation.
Then make sure that tasks are run automatically when the folder is opened by running command Tasks: Manage Automatic Tasks in Folder and choosing Allow Automatic Tasks in Folder (see Run behavior).
Please also see the note here that:
Task support is only available when working on a workspace folder. It is not available when editing single files."
For a Linux not-so-officially-recommended way that works. The xdotool key emulator for linux can be used. I'm sure any Windows key emulator could be used to accomplish the same thing.
First, set the Terminal: Change Color to a keyboard shortcut. Here I'm using Ctrl+Shift+C. This is done by bringing up the command pallet (Cntrl+Shift+P) and, typing Terminal: Change Color
Here is my Restore Terminals json from VS Code settings. The lengthy cli command to update the colors needs to be activated on the last tab, as it is the tab that is focused when Restore Tabs is finished restoring tabs.
We need a bit of a timeout between the setting of each tab color, as VS Code takes just about a 10th of a second to complete the command. The built in keyboard shortcut cntrl+Page_Up is used to bring the previous tabs into focus. This script is for a WorkSpace that includes a Docker-Compose enviroment that contains 3 repos.
Be sure to save this in your Workspace settings, not your user settings. For more info, visit: https://code.visualstudio.com/docs/getstarted/settings
"restoreTerminals.terminals": [
{
"splitTerminals": [
{
"name": "docker-compose",
"commands": ["cd ..", "cd docker-compose", "clear"]
},
]
},
{
"splitTerminals": [
{
"name": "api",
"commands": ["cd ..", "cd api", "clear"]
},
]
},
{
"splitTerminals": [
{
"name": "ui",
"commands": [
"cd ..",
"cd ui",
"xdotool key ctrl+shift+c && xdotool type 'cyan' && xdotool key Return && sleep .2",
"xdotool key ctrl+Page_Up && xdotool key ctrl+shift+c && xdotool type 'green' && xdotool key Return && sleep .2",
"xdotool key ctrl+Page_Up && xdotool key ctrl+shift+c && xdotool type 'yellow' && xdotool key Return",
"clear"
]
},
]
}
],
And the end result... Is that all tab colors are updated after they are restored.
Of course, anyone who has ever used keyboard emulation for automating tasks should know you can not be entering text or clicking elsewhere until the task is complete.
If you're on windows you can use powershell to create a similar effect to #CodeBloodedChris's solution.
Create a powershell script in your workspace root directory (or where ever the last terminal's final path is) and name it something like 'restore-terminal-customization.ps1' or something like that. Then add the following code to it:
# Uses windows forms to send keystrokes to customize vscode Terminals
Add-Type -AssemblyName System.Windows.Forms
$tabDelay = .6
# Last Terminal
[System.Windows.Forms.SendKeys]::SendWait("^+cRed~")
# Second to last Terminal
[System.Windows.Forms.SendKeys]::SendWait("^{PGUP}"); Start-Sleep -s $tabDelay
[System.Windows.Forms.SendKeys]::SendWait("^+cYellow~")
# Third to last Terminal
[System.Windows.Forms.SendKeys]::SendWait("^{PGUP}"); Start-Sleep -s $tabDelay
[System.Windows.Forms.SendKeys]::SendWait("^+cMagenta~")
In this script the '^+c' is ctrl+shift+c and '~' is the enter key.
Similarly I had also set up a keybinding for the icons which uses ctrl+shift+i '^+i'. Here's an example of setting both the color and the icon of a terminal:
# Fourth to last Terminal
[System.Windows.Forms.SendKeys]::SendWait("^{PGUP}"); Start-Sleep -s $tabDelay
[System.Windows.Forms.SendKeys]::SendWait("^+cGreen~^+iorganization~")
In your restoreTerminal's config call the script you created as a command in the last terminal on your list.
"restoreTerminals.terminals": [
{
"splitTerminals": [
{
"name": "docker-compose",
"commands": ["cd ..", "cd docker-compose", "clear"]
},
]
},
{
"splitTerminals": [
{
"name": "api",
"commands": ["cd ..", "cd api", "clear"]
},
]
},
{
"splitTerminals": [
{
"name": "ui",
"commands": [
"cd ..",
"cd ui",
".\restore-terminal-customization.ps1",
"clear"
]
},
]
}
],
Don't forget to set the keybindings to the Terminal: Change Color and Terminal: Change Icon commands in VsCode.

How to suppress output in Sublime Text cmd?

How can I suppress the output in Sublime Text Build System output?
I tried this but it says Error: unrecognized or incomplete command line.
{
"cmd": ["ipconfig", ">nul 2>&1"],
"name": "ipconfig",
"shell": "true"
}
{
"name": "ipconfig",
"shell_cmd": "ipconfig >nul 2>&1"
}
I always use shell_cmd because you can just write the command like you would on the command line instead of trying to figure out the multiple arguments nonsense in the array. This worked for me.

Can not take jstat metrics using bash as Sensu plugin

I have created bash scirpt that takes jstat metrics of my jvm instances!
Here is the output example :
demo.server1.sms.jstat.eden 24.34 0
demo.server1.lcms.jstat.eden 54.92 0
demo.server1.lms.jstat.eden 89.49 0
demo.server1.tms.jstat.eden 86.05 0
But when the Sensu-client runs this script it returns
Could not attach to 8584
Could not attach to 8588
Could not attach to 17141
Could not attach to 8628
demo.server1.sms.jstat.eden 0
demo.server1.lcms.jstat.eden 0
demo.server1.lms.jstat.eden 0
demo.server1.tms.jstat.eden 0
Here is the example of check_cron.json
{
"checks": {
"jstat_metrics": {
"type": "metric",
"handlers": ["graphite"],
"command": "/etc/sensu/plugins/jstat-metrics.sh",
"interval": 5,
"subscribers": [ "webservers" ]
}
}
}
And piece of my bash script
jvm_list=("sms:$sms" "lcms:$lcms" "lms:$lms" "tms:$tms" "ums:$ums")
for jvm_instance in ${jvm_list[#]}; do
project=${jvm_instance%%:*}
pid=${jvm_instance#*:}
if [ "$pid" ]; then
metric=`jstat -gc $pid|tail -n 1`
output=$output$'\n'"demo.server1.$project.jstat.eden"$'\t'`echo $metric |awk '{ print $3}'`$'\t0'
fi
done
echo "$output"
I find out that problem is with jstat and i tried to write full jstat path like /usr/lib/jvm/java-1.7.0-openjdk.x86_64/bin/jstat -gc $pid|tail -n 1 but it didn't help!
By the way if i will comment this row the output like "Could not attach to 8584" disappears!
I'm not a Java or Sensu user, but I can guess what happens.
Most likely, sensu-client runs your script as a user different from the one you use when testing manually, which doesn't have permissions to "attach" (whatever that means) to your jvm instances.
To verify this you can add invocation of "whoami" to your script, run it from sensu-client again, see what user it runs your script under and, if it is different, try to run your script as that user.
Yes you're right sensu runs all script as sensu user. To use jstat you have to add sensu to a sudoers.
just add file /etc/sudoers.d/sensu
Example:
Defaults:sensu !requiretty
Defaults:sensu secure_path =
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
sensu ALL = NOPASSWD: /etc/sensu/plugins/jsat-metrics.rb

Go-Sublime-build configuration

Im having issues trying to set up go to run the current file from Sublime text 2.
Here's what I have in my go.sublime-build file
{
"cmd": [ "go", "run", "${file}" ]
}
When I try to run build on a go source file, I get the error
[Error 6] The handle is invalid
[cmd: [u'go run', u'C:\\Users\\gprasant\\Documents\\GitHub\\programming_pearls\\src\\go\\quicksort.go']]
[dir: C:\Users\gprasant\Documents\GitHub\programming_pearls\src\go]
Is there any way to get this fixed ? Or is there another plugin in Sublime text for Go development?
Installing GoSublime should get this working for you. After installing and restarting ST2: do ctrl-B, type "run" and hit enter.
I got by with
{
"cmd": "go run $file",
"shell" : true
}
In ST3: it is changed to be:
{
"shell_cmd": "go run ${file}"
}
On my mac, I needed the following code in:
/Users/your_user_name/Library/Application Support/Sublime Text 2/Packages/User/go.sublime-build
go.sublime-build
{
"cmd": ["go run '${file}'"],
"selector": "source.go",
"path": "/usr/local/go/bin",
"shell": true
}
"cmd" line quoting is to correctly handle file paths with spaces.
"shell" line is needed since commenting it out breaks it.
"path" line is needed because the basic shell, doesn't have access to my .zshrc file include the export GOPATH statement defining the go path.
After that any .go file should build and run with command+B, leaving the stdout message in a console built into sublime text 2.
what about:
{
"cmd": ["go", "run", "${file}"],
"path": "/user/local/go/bin"
}
I like GoSublime, just hate to type run each time when click Command + B
SublimeText 2
build-system for golang, making F4/shift-F4 work (next error/prev error)
1st, create a file: ~/gosublime_build.sh
GOPATH=~/go
export GOPATH
echo "GOPATH:$GOPATH"
if [ "$3." = "RUN." ]
then
EXENAME=${1##*/}
EXENAME=$GOPATH/bin/$EXENAME
echo $EXENAME
$($EXENAME)
echo "code: $?"
exit
fi
echo "go build $2"
cd /usr/local/go/bin
./go build -o ~/temp.go.compiled $2
if [ $? -eq 0 ]
then
cd $1
echo "Project: " $1
/usr/local/go/bin/go install
echo "go install exit code: $?"
else
echo "go build exit code: $?"
fi
2nd:
chmod 777 ~/gosublime_build.sh
3rd: create a new sublime2 build-system for "go" (Tools/Build System/New)
{
"cmd": ["~/gosublime_build.sh $file_path $file"]
,"shell": true
,"selector": "source.go"
,"file_regex": "([\\w/_-]+[.]{1}[\\w./_-]+?):([0-9]+):?([0-9]+)?(.*)?"
}
4th: select your new build-system (Tools/Build System)
5th: build with Ctrl-B, F4/Shift-F4: next/prev error
If anybody knows how to instruct the go compiler to inform FULL PATH of file and line for each error, this process can be simplified

Resources