Skip to content

Commit

Permalink
automate the installation of vscode
Browse files Browse the repository at this point in the history
  • Loading branch information
GillySpace27 committed Dec 4, 2024
1 parent 179909a commit f292059
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 26 deletions.
24 changes: 0 additions & 24 deletions pyfitsserver/install_pyfitsVSC.py

This file was deleted.

74 changes: 74 additions & 0 deletions pyfitsserver/lib/install_pyfitsVSC.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import subprocess
import sys
import logging
import os
from importlib.resources import files
import requests

def is_vscode_installed():
try:
# Check if VSCode is installed by running 'code --version'
subprocess.run(["code", "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return True
except subprocess.CalledProcessError:
return False

def download_vscode_extension():
url = "https://github.com/GillySpace27/pyFitsServer/releases/download/v0.0.26/pyfitsvsc-0.0.4.vsix"
local_filename = "pyfitsserver/lib/pyfitsvsc-0.0.4.vsix"

# Ensure the directory exists
os.makedirs(os.path.dirname(local_filename), exist_ok=True)

logging.info(f"Downloading {url}")
try:
with requests.get(url, stream=True) as r:
r.raise_for_status()
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
logging.info(f"Downloaded to {local_filename}")
except Exception as e:
logging.error(f"Failed to download VSIX file: {e}")
sys.exit(1)

def install_vscode_extension():
vsix_filename = "pyfitsvsc-0.0.4.vsix"
try:
# Locate the .vsix file in the package
vsix_path = files("pyfitsserver.lib").joinpath(vsix_filename)

# Check if the file exists, if not download it
if not vsix_path.exists():
logging.info(f"VSIX file not found at: {vsix_path}, attempting to download...")
download_vscode_extension()

# Re-check after download attempt
if not vsix_path.exists():
logging.error(f"VSCode extension file still not found at: {vsix_path}")
sys.exit(1)

# Check if VSCode is installed
if not is_vscode_installed():
logging.error("VSCode is not installed. Please install VSCode first.")
sys.exit(1)

# Try to install the extension using the VSCode CLI
logging.info(f"Attempting to install VSCode extension from {vsix_path}")
subprocess.run(["code", "--install-extension", str(vsix_path)], check=True)
logging.info("VSCode extension installed successfully!")
except subprocess.CalledProcessError as e:
logging.error(f"Automatic installation failed: {e}")
logging.info("Falling back to opening VSCode with the extension visible.")

try:
# Attempt to open VSCode with the folder of the extension
subprocess.run(["code", "--folder-uri", os.path.dirname(vsix_path)], check=True)
except subprocess.CalledProcessError as e:
logging.error(f"Failed to open VSCode: {e}")
logging.info("Please install the extension manually:")
logging.info(f" code --install-extension {vsix_path}")

if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
install_vscode_extension()
74 changes: 74 additions & 0 deletions pyfitsserver/scripts/install_pyFitsVSC.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import subprocess
import sys
import logging
import os
from importlib.resources import files
import requests

def is_vscode_installed():
try:
# Check if VSCode is installed by running 'code --version'
subprocess.run(["code", "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return True
except subprocess.CalledProcessError:
return False

def download_vscode_extension():
url = "https://github.com/GillySpace27/pyFitsServer/releases/download/v0.0.26/pyfitsvsc-0.0.4.vsix"
local_filename = "pyfitsserver/lib/pyfitsvsc-0.0.4.vsix"

# Ensure the directory exists
os.makedirs(os.path.dirname(local_filename), exist_ok=True)

logging.info(f"Downloading {url}")
try:
with requests.get(url, stream=True) as r:
r.raise_for_status()
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
logging.info(f"Downloaded to {local_filename}")
except Exception as e:
logging.error(f"Failed to download VSIX file: {e}")
sys.exit(1)

def install_vscode_extension():
vsix_filename = "pyfitsvsc-0.0.4.vsix"
try:
# Locate the .vsix file in the package
vsix_path = files("pyfitsserver.lib").joinpath(vsix_filename)

# Check if the file exists, if not download it
if not vsix_path.exists():
logging.info(f"VSIX file not found at: {vsix_path}, attempting to download...")
download_vscode_extension()

# Re-check after download attempt
if not vsix_path.exists():
logging.error(f"VSCode extension file still not found at: {vsix_path}")
sys.exit(1)

# Check if VSCode is installed
if not is_vscode_installed():
logging.error("VSCode is not installed. Please install VSCode first.")
sys.exit(1)

# Try to install the extension using the VSCode CLI
logging.info(f"Attempting to install VSCode extension from {vsix_path}")
subprocess.run(["code", "--install-extension", str(vsix_path)], check=True)
logging.info("VSCode extension installed successfully!")
except subprocess.CalledProcessError as e:
logging.error(f"Automatic installation failed: {e}")
logging.info("Falling back to opening VSCode with the extension visible.")

try:
# Attempt to open VSCode with the folder of the extension
subprocess.run(["code", "--folder-uri", os.path.dirname(vsix_path)], check=True)
except subprocess.CalledProcessError as e:
logging.error(f"Failed to open VSCode: {e}")
logging.info("Please install the extension manually:")
logging.info(f" code --install-extension {vsix_path}")

if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
install_vscode_extension()
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "pyfitsserver"
version = "0.0.26"
version = "0.0.27"
description = "A lightweight server to facilitate the rendering and previewing of FITS files."
readme = "README.md"
authors = [
Expand Down Expand Up @@ -38,6 +38,7 @@ dev = ["setuptools>=42", "wheel"]

[project.scripts]
pyfitsserver = "pyfitsserver.server:main"
install_pyFitsVSC = "pyfitsserver.install_pyfitsVSC:install_vscode_extension"

[tool.flit.metadata]
module = "pyfitsserver"
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='pyfitsserver',
version='v0.0.26',
version='v0.0.27',
description='A lightweight server to facilitate the rendering and previewing of FITS files.',
long_description=open("README.md").read(), # Reads the content of your README.md
long_description_content_type="text/markdown", # Specifies that README is in Markdown format
Expand Down

0 comments on commit f292059

Please sign in to comment.