egomobile / vscode-powertools

A swiss army knife with lots of tools, extensions and (scriptable) enhancements for Visual Studio Code.
https://marketplace.visualstudio.com/items?itemName=egomobile.vscode-powertools
GNU Lesser General Public License v3.0
69 stars 9 forks source link

[Question] about executing powershell #7

Open kraabrsg opened 2 years ago

kraabrsg commented 2 years ago

Hi thank you for this amazing tool!

i have some questions(WIN 10):

1.) Applying a setting like this (settings.json):

{
    "ego.power-tools": {
        "buttons": [
            {
                "text": "testtask2",
                "action": {
                    "type": "shell",
                    "command": "powershell.exe -noProfile -File C:\\_xxxx\\vscodefolder\\.vscode\\testpower2.ps1 ${activeFile}",
                    "cwd": "C:\\Windows\System32\\WindowsPowerShell\\V1.0"
                    "silent": false
                }
            }
        ]
    }
}

and running it, nothing is shown in the vscode terminal (the powershell is executed but i would like to start this powershell as task ). As opposed to when making a task like this in a task.json and running it(output is shown in the Terminal like expected)

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "windows": {
        "options": {
            "shell": {
                "executable": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
                "args": [
                    "-NoProfile",
                    "-ExecutionPolicy",
                    "Bypass",
                    "-Command"
                ]
            }
        }
    },
    "tasks": [
        {
            "label": "testtask1",
            "type": "shell",
            "windows": {
                "options": {
                    "cwd": "${workspaceFolder}",
                },
                "command": "powershell -noProfile -File C:\\_xxxx\\vscodefolder\\.vscode\\testpower1.ps1 "
            },
            "presentation": {
                "echo": false,
                "reveal": "silent",
                "focus": true,
                "panel": "shared",
                "showReuseMessage": false,
                "clear": false,
             //   "close": true
            },
            "problemMatcher": []
        }
    ]
}

2.) Can i start a powershell task via command ? (found nothing in the wiki about that), maybe somewhow via javascript or a workbench action ???

3.)Can i define more then 1 context menu entry per file ending (e.g. when ending is .txt "populate" 3 context menu entries to start different powershell tasks, ending .json "populate" 4 contextmenu entries to start different powershell tasks) Now you have to click "execute PowerCommand" and then you choose from the available commands(one click more)

4.) I can execute a powershell script on startup, like on 1.) can i run this as powershell task at startup too ?

5.) for events like "file created" "file.changed" "file.saved" and "file.deleted" i would like to trigger a powershell task (according to the documentation it is not possible) ...? Or would is there a possibility to trigger that from javascript ???

6.) I want some buttons to be shown only on defined endings. e.g. .xxx or .yyy , i tried various strings like

"ifFile": "xxx$" but that does not seem to work ???

Would you be so kind and provide examples for these questions?

Thank you!

ghost commented 2 years ago

@kraabrsg

1.) search for the OUTPUT tab next to TERMINAL in the bottom area and select Power Tools by e.GO ... results are shown there, because a process is executed, but not as virtial terminal

2.) Yes, with JavaScript: https://github.com/egomobile/vscode-powertools/wiki/Commands ... s. example below

3.) No, this is technically not possible ... the VS Code API does not support this

4.) Yes, this is possible: https://github.com/egomobile/vscode-powertools/wiki/Startups#shell-commands

5.) you can use JavaScript for this: https://github.com/egomobile/vscode-powertools/wiki/Events#scripts ... s. example below

6.) your expression works for me ... when I create a test.xxx file, it is only shown if this file is open (or any other file with .xxx) ... can you post your settings here? ... maybe check out, if there is a .log file in your ~/.vscode-powertools folder which might help

Example how to ex

const child_process = require('child_process');

exports.execute = async (args) => {
  // args => https://egomobile.github.io/vscode-powertools/api/interfaces/contracts.workspacecommandscriptarguments.html

  // https://nodejs.org/api/child_process.html#child_processexecfilesyncfile-args-options
  const stdout = child_process.execFileSync('C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', [
    "-NoProfile",  "-ExecutionPolicy",  "Bypass",  "-Command"
  ]);

  // OUTPUT >> Power Tools by e.GO
  args.output.appendLine('PowerShell output: ' + String(stdout));
};
kraabrsg commented 2 years ago

Hi @mkloubertego thanks!

can confirm 6.) works like expected, my fault.

as of 2.) thanks, that might also help. Maybe easier / fits better here to do is run a task via javascript:

settings:

            "mycommand1":{
                "script": "mypower.js",
                "forFile": true,
                "forFolder": true,
                "button": {
                    "text": "$(extensions-install-count) command1"
                }

mypower.js

exports.execute = async (args) => {
    // args => https://egomobile.github.io/vscode-powertools/api/interfaces/_contracts_.workspacecommandscriptarguments.html

    // s. https://code.visualstudio.com/api/references/vscode-api
    const vscode = args.require('vscode');

    // load task list
    var allTasks = await vscode.tasks.fetchTasks();

    for (var t of allTasks) {
        if (t.name === "MYPOWER") {
            break;
        }
    }
    vscode.tasks.executeTask(t);
    vscode.window.showInformationMessage(
        `Hello, from '${ args.command }'!`
    );
};

It should be done by passing in the taskname instead of hardcoding it, so i will use ${args-command} as identification.

Thank you very much for this awesome tools, now i can use one extension for automation instead of two, and it has way more functionality!!!

:-)

kraabrsg commented 2 years ago

Hi @mkloubertego

add as of 2.) in mypower.js

    for (var t of allTasks) {
        if (t.name === args.command) {
            break;
        }
    }
vscode.tasks.executeTask(t);

args.file is undefined in this script

task.json

            "label": "MYPOWER",
            "type": "shell",
            "windows": {
                "options": {
                    "cwd": "${workspaceFolder}",
                },
                "command": "powershell -noProfile -File  C:\\xxxxxx\\_VSCODE_TEST\\yyyyy\\data\\USER\\.vscode\\mypower.ps1 '${file}'"
            },

settings.json unchanged like above

How can i pass in file and folder name (mypower.js) ?

according to last example of the wiki (last exapmple, it should work this way), https://github.com/egomobile/vscode-powertools/wiki/Commands

I also copied the settings.json in the .vscode folder (this is a portable installation, normally in data/user-data/User)

The links to api/interfaces give a 404...

Thanks!

ghost commented 2 years ago

@kraabrsg

But only, if you execute the command from file explorer:

let fileOrFolderUri;
if (args.file) {
  fileOrFolderUri = args.file;  // element is file
} else if (args.file) {
  fileOrFolderUri = args.folder;  // element is folder
}
ghost commented 2 years ago

@kraabrsg

and you have to set the settings forFile and/or forFolder to (true)

schoko11 commented 2 years ago

Hi @mkloubertego well i have to try that again. But how could i pass in the file(in js script) into the powershell task? Is there a property like t.args??? Because now i get an error that "-file" is missing.. (this is my other github user -> to avoid confusion)