DEV Community

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

Posted on

1 1

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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay