DEV Community

Cover image for Automate packaging of Visual Studio Code extension
Allan Simonsen
Allan Simonsen

Posted on

Automate packaging of Visual Studio Code extension

During my work with the Visual Studio Code extension AngularTools I more than once annoyingly forgot one of the steps in the deployment.

Its not a complicated set of tasks but in my list of things to do when packaging a new version I have to:

  • Verify that the version number in the package.json has been updated.
  • Verify that the file has been updated with a description of bugfixes and new features.
  • Create a tag for the version in the Git repository.
  • Run vsce package to create the vsix package file.

So I automated the tasks with the python script below:

import os
import json
import re
from git import Repo, TagReference

def readVersionFromPackageJson():
  packageJson = open("package.json", "r")
  contentRaw =
  contentJson = json.loads(contentRaw)
  return contentJson["version"]

def isPackageJsonVersionTagged(repo, packageJsonVersion):
  packageJsonVersionTagFound = False
  for tag in repo.tags:
    if == packageJsonVersion:
      packageJsonVersionTagFound = True
  return packageJsonVersionTagFound

def isChangeLogUpdatedWithPackageJsonVersion(packageJsonVersion):
  packageJsonVersionChangeLogEntryFound = False
  changeLog = open("", "r")
  changeLogContent = changeLog.readlines()
  for line in changeLogContent:
    match ="^## Version {packageJsonVersion}$", line)
    if match:
      packageJsonVersionChangeLogEntryFound = True
  return packageJsonVersionChangeLogEntryFound

def packageExtension():
  os.system("vsce package")

def main():
  packageJsonVersion = readVersionFromPackageJson()
  repo = Repo("./")
  packageJsonVersionTagFound = isPackageJsonVersionTagged(repo, packageJsonVersion)
  packageJsonVersionChangeLogEntryFound = isChangeLogUpdatedWithPackageJsonVersion(packageJsonVersion)

  if not packageJsonVersionChangeLogEntryFound:
    print("Fail: not update!")
    if not packageJsonVersionTagFound:
      print(f"New version found in package.json: {packageJsonVersion}.")
      print("Creating tag in Git...")
      print("Creating vsix package...")
      print(f"Fail: Version already tagged: {packageJsonVersion}.")
      print("No vsix package created")


Enter fullscreen mode Exit fullscreen mode

I hope you find it useful or that it will inspire you to automate some of your boring or annoying tasks.

You can find the script and the code for AngularTools on GitHub.

The script running

Top comments (0)