The art of laziness: bundle.yaml creation tip - snippets in vscode

2022-10-13_12-04-43
If you are lazy enough /smart enough to be lazy
here is a small trick for bundle.yaml creation for buttons descriptions and title:

{
	"Bundle.yaml for pyRevit": {
		"scope": "yaml",
		"prefix": "yy",
		"body": [
			"title:",
			"  fr_fr: ${1: french title}",
			"  en_us: ${2: english title}",
			"tooltip: ",
			"  fr_fr: ${3: french tooltip}",
			"  en_us: ${4: english tooltip}",
	 	],
		"description": "bundle.yaml for pyRevit"
	}
}

for details on how to use: Snippets in Visual Studio Code

Nice trick!

Always searching to make things easier and quicker, I created a pyrevit button to create a new button whenever I want to start a script. It basically just creates the folders, copies in a script.py, bundle.yaml, and icon.png file from a template folder, opens the folder, and reloads pyrevit. It’s a great way to fill your toolbar with work in progress scripts that you never finish!

# -*- coding=utf-8 -*-
#pylint: disable=import-error,invalid-name,broad-except
import os
import shutil
import webbrowser
from pyrevit.forms import ask_for_string, alert
from pyrevit import script
from pyrevit.loader import sessionmgr
from pyrevit.loader import sessioninfo

def runscript():
    lib = os.path.dirname(os.path.dirname(__file__))
    template_folder = r"YOUR TEMPLATE FOLDER LOCATION HERE"

    newname = ask_for_string(title="New Folder", instructions="Specify name for new command")
    if not newname: return
    newfolder = os.path.join(lib, newname + ".pushbutton")

    if os.path.exists(newfolder):
        alert("Folder already exists")
    else:
        os.mkdir(newfolder)
        for f in os.listdir(template_folder):
            file = os.path.join(template_folder, f)
            shutil.copy(file, newfolder)
        webbrowser.open(newfolder)


        logger = script.get_logger()
        results = script.get_results()

        # re-load pyrevit session.
        logger.info('Reloading....')
        sessionmgr.reload_pyrevit()

        results.newsession = sessioninfo.get_session_uuid()

I wanted to do exactly that. Thanks for sharing.
Do you mind if I add this to the code base of pyRevit? @Archibum
What do you think @eirannejad ?

Yes please. Maybe we can create an extension template that has a button to make buttons? @Jean-Marc I use this vscode extension to include project-level snippets. You can add all yours to .vscode/snippets.code-snippets in the pyRevit projects and commit so others can use them as well

Yeah for sure go for it! Happy to have helped contribute something to this awesome tool!

Hi, is there any reason why this piece of code doesn’t work ?

I just replace the template folder location with a folder Path and when I click on the button, nothing happens.

Show the code @danselkaz

Of course, but it must be exactly the same as above …
Maybe I forgot to add something to run the script ?

# -*- coding=utf-8 -*-
#pylint: disable=import-error,invalid-name,broad-except
import os
import shutil
import webbrowser
from pyrevit.forms import ask_for_string, alert
from pyrevit import script
from pyrevit.loader import sessionmgr
from pyrevit.loader import sessioninfo

def runscript():
    lib = os.path.dirname(os.path.dirname(__file__))
    template_folder = r"C:/Users/micr112/Documents/Template"

    newname = ask_for_string(title="New Folder", instructions="Specify name for new command")
    if not newname: return
    newfolder = os.path.join(lib, newname + ".pushbutton")

    if os.path.exists(newfolder):
        alert("Folder already exists")
    else:
        os.mkdir(newfolder)
        for f in os.listdir(template_folder):
            file = os.path.join(template_folder, f)
            shutil.copy(file, newfolder)
        webbrowser.open(newfolder)


        logger = script.get_logger()
        results = script.get_results()

        # re-load pyrevit session.
        logger.info('Reloading....')
        sessionmgr.reload_pyrevit()

        results.newsession = sessioninfo.get_session_uuid()

You need to call the function. I forgot to include it when I copy/pasted the code.

Here is your code with it added in:

# -*- coding=utf-8 -*-
#pylint: disable=import-error,invalid-name,broad-except
import os
import shutil
import webbrowser
from pyrevit.forms import ask_for_string, alert
from pyrevit import script
from pyrevit.loader import sessionmgr
from pyrevit.loader import sessioninfo

def runscript():
    lib = os.path.dirname(os.path.dirname(__file__))
    template_folder = r"C:/Users/micr112/Documents/Template"

    newname = ask_for_string(title="New Folder", instructions="Specify name for new command")
    if not newname: return
    newfolder = os.path.join(lib, newname + ".pushbutton")

    if os.path.exists(newfolder):
        alert("Folder already exists")
    else:
        os.mkdir(newfolder)
        for f in os.listdir(template_folder):
            file = os.path.join(template_folder, f)
            shutil.copy(file, newfolder)
        webbrowser.open(newfolder)


        logger = script.get_logger()
        results = script.get_results()

        # re-load pyrevit session.
        logger.info('Reloading....')
        sessionmgr.reload_pyrevit()

        results.newsession = sessioninfo.get_session_uuid()

runscript()

@eirannejad
PR in the pyRevit repo added python and yaml snippets and extension for single button creation by jmcouffin · Pull Request #1664 · eirannejad/pyRevit · GitHub
it contains both the python/yaml snippets and the button creation extension _ I kept it pretty simple for now. I might add a 3 button stack creation button later and the possibility to pick a different destination folder to make a separate extension from the start.

@Archibum I adjusted things here and there, especially the specific path you gave

see [new builtin extension] Bundles Creator Extension for the follow up

Here’s an adapted version of @Jean-Marc 's vscode snippet! I kept forgetting the line break options for my wordy tooltips, so I embedded them in the snippet.

	"Bundle.yaml for pyRevit": {
		"scope": "yaml",
		"prefix": "yy",
		"body": [
			"title:",
			"  en_us: ${1:english title}",
			"tooltip: ",
			"# en_us: \"slash n adds a line break \\nin the note\"",
  			"# en_us: \"For long notes, I like to add a line breaks ",
  			"#         in the yaml but NOT in the tooltip. Spaces at the ",
  			"#         start of a new line are ignored.\"",
			"  en_us: ${2:english tooltip}",
			"Author: ${3:script author}",
			"min_revit_version: ${4:version}",
			"is_beta: ${5:false}",
	 	],
		"description": "bundle.yaml for pyRevit"
	}

And here’s the resulting output:

title:
  en_us: english title
tooltip: 
# en_us: "slash n adds a line break \nin the note"
# en_us: "For long notes, you need to be able add a line break 
#         in the yaml but NOT in the note. The at the start of 
#         the new line are ignored."
  en_us: english tooltip
Author: script author
min_revit_version: version
is_beta: false