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 CHANGELOG.md 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 = packageJson.read()
  contentJson = json.loads(contentRaw)
  packageJson.close()
  return contentJson["version"]

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

def isChangeLogUpdatedWithPackageJsonVersion(packageJsonVersion):
  packageJsonVersionChangeLogEntryFound = False
  changeLog = open("CHANGELOG.md", "r")
  changeLogContent = changeLog.readlines()
  changeLog.close()
  for line in changeLogContent:
    match = re.search(f"^## Version {packageJsonVersion}$", line)
    if match:
      packageJsonVersionChangeLogEntryFound = True
      break
  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: CHANGELOG.md not update!")
  else:
    if not packageJsonVersionTagFound:
      print(f"New version found in package.json: {packageJsonVersion}.")
      print("Creating tag in Git...")
      repo.create_tag(packageJsonVersion)
      print("Creating vsix package...")
      packageExtension()
    else:
      print(f"Fail: Version already tagged: {packageJsonVersion}.")
      print("No vsix package created")

main()

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)