2021-04-08 16:26:57 +00:00
|
|
|
#!/usr/bin/env nix-shell
|
2024-07-27 06:49:29 +00:00
|
|
|
#! nix-shell -i python3 -p "python3.withPackages (ps: with ps; [ requests click click-log packaging ])" bundix bundler nix-update nurl prefetch-yarn-deps
|
2021-12-19 01:06:50 +00:00
|
|
|
from __future__ import annotations
|
2021-04-08 16:26:57 +00:00
|
|
|
|
|
|
|
import click
|
|
|
|
import click_log
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
import re
|
|
|
|
import logging
|
|
|
|
import subprocess
|
2021-07-16 19:40:57 +00:00
|
|
|
import os
|
|
|
|
import stat
|
|
|
|
import json
|
|
|
|
import requests
|
2021-08-18 13:19:15 +00:00
|
|
|
import textwrap
|
2021-12-19 01:06:50 +00:00
|
|
|
from functools import total_ordering
|
2024-06-05 15:53:02 +00:00
|
|
|
from packaging.version import Version
|
2021-07-16 19:40:57 +00:00
|
|
|
from pathlib import Path
|
2021-12-19 01:06:50 +00:00
|
|
|
from typing import Union, Iterable
|
2021-04-08 16:26:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-12-19 01:06:50 +00:00
|
|
|
@total_ordering
|
|
|
|
class DiscourseVersion:
|
|
|
|
"""Represents a Discourse style version number and git tag.
|
|
|
|
|
|
|
|
This takes either a tag or version string as input and
|
|
|
|
extrapolates the other. Sorting is implemented to work as expected
|
|
|
|
in regard to A.B.C.betaD version numbers - 2.0.0.beta1 is
|
|
|
|
considered lower than 2.0.0.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
tag: str = ""
|
|
|
|
version: str = ""
|
|
|
|
split_version: Iterable[Union[None, int, str]] = []
|
|
|
|
|
|
|
|
def __init__(self, version: str):
|
|
|
|
"""Take either a tag or version number, calculate the other."""
|
|
|
|
if version.startswith('v'):
|
|
|
|
self.tag = version
|
|
|
|
self.version = version.lstrip('v')
|
|
|
|
else:
|
|
|
|
self.tag = 'v' + version
|
|
|
|
self.version = version
|
2024-06-05 15:53:02 +00:00
|
|
|
|
|
|
|
self._version = Version(self.version)
|
2021-12-19 01:06:50 +00:00
|
|
|
|
|
|
|
def __eq__(self, other: DiscourseVersion):
|
|
|
|
"""Versions are equal when their individual parts are."""
|
2024-06-05 15:53:02 +00:00
|
|
|
return self._version == other._version
|
2021-12-19 01:06:50 +00:00
|
|
|
|
|
|
|
def __gt__(self, other: DiscourseVersion):
|
2024-06-05 15:53:02 +00:00
|
|
|
"""Check if this version is greater than the other."""
|
|
|
|
return self._version > other._version
|
2021-12-19 01:06:50 +00:00
|
|
|
|
|
|
|
|
2021-04-08 16:26:57 +00:00
|
|
|
class DiscourseRepo:
|
2021-12-19 01:06:50 +00:00
|
|
|
version_regex = re.compile(r'^v\d+\.\d+\.\d+(\.beta\d+)?$')
|
2021-07-16 19:40:57 +00:00
|
|
|
_latest_commit_sha = None
|
|
|
|
|
2021-04-08 16:26:57 +00:00
|
|
|
def __init__(self, owner: str = 'discourse', repo: str = 'discourse'):
|
|
|
|
self.owner = owner
|
|
|
|
self.repo = repo
|
|
|
|
|
|
|
|
@property
|
2021-12-19 01:06:50 +00:00
|
|
|
def versions(self) -> Iterable[str]:
|
2021-04-08 16:26:57 +00:00
|
|
|
r = requests.get(f'https://api.github.com/repos/{self.owner}/{self.repo}/git/refs/tags').json()
|
|
|
|
tags = [x['ref'].replace('refs/tags/', '') for x in r]
|
|
|
|
|
|
|
|
# filter out versions not matching version_regex
|
2021-12-19 01:06:50 +00:00
|
|
|
versions = filter(self.version_regex.match, tags)
|
|
|
|
versions = [DiscourseVersion(x) for x in versions]
|
|
|
|
versions.sort(reverse=True)
|
2021-04-08 16:26:57 +00:00
|
|
|
return versions
|
|
|
|
|
2021-07-16 19:40:57 +00:00
|
|
|
@property
|
|
|
|
def latest_commit_sha(self) -> str:
|
|
|
|
if self._latest_commit_sha is None:
|
|
|
|
r = requests.get(f'https://api.github.com/repos/{self.owner}/{self.repo}/commits?per_page=1')
|
|
|
|
r.raise_for_status()
|
|
|
|
self._latest_commit_sha = r.json()[0]['sha']
|
|
|
|
|
|
|
|
return self._latest_commit_sha
|
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
def get_yarn_lock_hash(self, rev: str, path: str):
|
|
|
|
yarnLockText = self.get_file(path, rev)
|
2022-04-15 01:41:22 +00:00
|
|
|
with tempfile.NamedTemporaryFile(mode='w') as lockFile:
|
|
|
|
lockFile.write(yarnLockText)
|
2024-06-05 15:53:02 +00:00
|
|
|
hash = subprocess.check_output(['prefetch-yarn-deps', lockFile.name]).decode().strip()
|
|
|
|
return subprocess.check_output(["nix", "hash", "to-sri", "--type", "sha256", hash]).decode().strip()
|
2022-04-15 01:41:22 +00:00
|
|
|
|
2021-04-08 16:26:57 +00:00
|
|
|
def get_file(self, filepath, rev):
|
2022-04-15 01:41:22 +00:00
|
|
|
"""Return file contents at a given rev.
|
|
|
|
|
|
|
|
:param str filepath: the path to the file, relative to the repo root
|
|
|
|
:param str rev: the rev to fetch at :return:
|
2021-04-08 16:26:57 +00:00
|
|
|
|
|
|
|
"""
|
2021-12-19 01:06:50 +00:00
|
|
|
r = requests.get(f'https://raw.githubusercontent.com/{self.owner}/{self.repo}/{rev}/{filepath}')
|
|
|
|
r.raise_for_status()
|
|
|
|
return r.text
|
2021-04-08 16:26:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _call_nix_update(pkg, version):
|
2022-04-15 01:41:22 +00:00
|
|
|
"""Call nix-update from nixpkgs root dir."""
|
2021-07-16 19:40:57 +00:00
|
|
|
nixpkgs_path = Path(__file__).parent / '../../../../'
|
2021-04-08 16:26:57 +00:00
|
|
|
return subprocess.check_output(['nix-update', pkg, '--version', version], cwd=nixpkgs_path)
|
|
|
|
|
|
|
|
|
2021-07-16 19:40:57 +00:00
|
|
|
def _nix_eval(expr: str):
|
|
|
|
nixpkgs_path = Path(__file__).parent / '../../../../'
|
2021-08-18 13:19:15 +00:00
|
|
|
try:
|
2021-12-06 16:07:01 +00:00
|
|
|
output = subprocess.check_output(['nix-instantiate', '--strict', '--json', '--eval', '-E', f'(with import {nixpkgs_path} {{}}; {expr})'], text=True)
|
2021-08-18 13:19:15 +00:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
return None
|
|
|
|
return json.loads(output)
|
2021-07-16 19:40:57 +00:00
|
|
|
|
|
|
|
|
2021-04-08 16:26:57 +00:00
|
|
|
def _get_current_package_version(pkg: str):
|
2021-07-16 19:40:57 +00:00
|
|
|
return _nix_eval(f'{pkg}.version')
|
2021-04-08 16:26:57 +00:00
|
|
|
|
|
|
|
|
2021-12-19 01:06:50 +00:00
|
|
|
def _diff_file(filepath: str, old_version: DiscourseVersion, new_version: DiscourseVersion):
|
2021-04-08 16:26:57 +00:00
|
|
|
repo = DiscourseRepo()
|
|
|
|
|
2021-07-16 19:40:57 +00:00
|
|
|
current_dir = Path(__file__).parent
|
2021-04-08 16:26:57 +00:00
|
|
|
|
2021-12-19 01:06:50 +00:00
|
|
|
old = repo.get_file(filepath, old_version.tag)
|
|
|
|
new = repo.get_file(filepath, new_version.tag)
|
2021-04-08 16:26:57 +00:00
|
|
|
|
|
|
|
if old == new:
|
|
|
|
click.secho(f'{filepath} is unchanged', fg='green')
|
|
|
|
return
|
|
|
|
|
|
|
|
with tempfile.NamedTemporaryFile(mode='w') as o, tempfile.NamedTemporaryFile(mode='w') as n:
|
|
|
|
o.write(old), n.write(new)
|
|
|
|
width = shutil.get_terminal_size((80, 20)).columns
|
|
|
|
diff_proc = subprocess.run(
|
|
|
|
['diff', '--color=always', f'--width={width}', '-y', o.name, n.name],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
cwd=current_dir,
|
|
|
|
text=True
|
|
|
|
)
|
|
|
|
|
2021-12-19 01:06:50 +00:00
|
|
|
click.secho(f'Diff for {filepath} ({old_version.version} -> {new_version.version}):', fg='bright_blue', bold=True)
|
2021-04-08 16:26:57 +00:00
|
|
|
click.echo(diff_proc.stdout + '\n')
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2021-08-18 13:19:15 +00:00
|
|
|
def _remove_platforms(rubyenv_dir: Path):
|
|
|
|
for platform in ['arm64-darwin-20', 'x86_64-darwin-18',
|
|
|
|
'x86_64-darwin-19', 'x86_64-darwin-20',
|
2021-12-30 13:39:12 +00:00
|
|
|
'x86_64-linux', 'aarch64-linux']:
|
2021-08-18 13:19:15 +00:00
|
|
|
with open(rubyenv_dir / 'Gemfile.lock', 'r') as f:
|
|
|
|
for line in f:
|
|
|
|
if platform in line:
|
|
|
|
subprocess.check_output(
|
|
|
|
['bundle', 'lock', '--remove-platform', platform], cwd=rubyenv_dir)
|
|
|
|
break
|
|
|
|
|
|
|
|
|
2021-04-08 16:26:57 +00:00
|
|
|
@click_log.simple_verbosity_option(logger)
|
|
|
|
|
|
|
|
|
|
|
|
@click.group()
|
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@click.argument('rev', default='latest')
|
|
|
|
@click.option('--reverse/--no-reverse', default=False, help='Print diffs from REV to current.')
|
|
|
|
def print_diffs(rev, reverse):
|
|
|
|
"""Print out diffs for files used as templates for the NixOS module.
|
|
|
|
|
|
|
|
The current package version found in the nixpkgs worktree the
|
|
|
|
script is run from will be used to download the "from" file and
|
|
|
|
REV used to download the "to" file for the diff, unless the
|
|
|
|
'--reverse' flag is specified.
|
|
|
|
|
|
|
|
REV should be the git rev to find changes in ('vX.Y.Z') or
|
|
|
|
'latest'; defaults to 'latest'.
|
|
|
|
|
|
|
|
"""
|
|
|
|
if rev == 'latest':
|
|
|
|
repo = DiscourseRepo()
|
2021-12-19 01:06:50 +00:00
|
|
|
rev = repo.versions[0].tag
|
2021-04-08 16:26:57 +00:00
|
|
|
|
2021-12-19 01:06:50 +00:00
|
|
|
old_version = DiscourseVersion(_get_current_package_version('discourse'))
|
|
|
|
new_version = DiscourseVersion(rev)
|
2021-04-08 16:26:57 +00:00
|
|
|
|
|
|
|
if reverse:
|
|
|
|
old_version, new_version = new_version, old_version
|
|
|
|
|
|
|
|
for f in ['config/nginx.sample.conf', 'config/discourse_defaults.conf']:
|
|
|
|
_diff_file(f, old_version, new_version)
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@click.argument('rev', default='latest')
|
|
|
|
def update(rev):
|
|
|
|
"""Update gem files and version.
|
|
|
|
|
2022-04-15 01:41:22 +00:00
|
|
|
REV: the git rev to update to ('vX.Y.Z[.betaA]') or
|
2021-12-19 01:06:50 +00:00
|
|
|
'latest'; defaults to 'latest'.
|
2021-04-08 16:26:57 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
repo = DiscourseRepo()
|
|
|
|
|
|
|
|
if rev == 'latest':
|
2021-12-19 01:06:50 +00:00
|
|
|
version = repo.versions[0]
|
|
|
|
else:
|
|
|
|
version = DiscourseVersion(rev)
|
2021-04-08 16:26:57 +00:00
|
|
|
|
2021-12-19 01:06:50 +00:00
|
|
|
logger.debug(f"Using rev {version.tag}")
|
|
|
|
logger.debug(f"Using version {version.version}")
|
2021-04-08 16:26:57 +00:00
|
|
|
|
2021-07-16 19:40:57 +00:00
|
|
|
rubyenv_dir = Path(__file__).parent / "rubyEnv"
|
2021-04-08 16:26:57 +00:00
|
|
|
|
|
|
|
for fn in ['Gemfile.lock', 'Gemfile']:
|
|
|
|
with open(rubyenv_dir / fn, 'w') as f:
|
2021-12-19 01:06:50 +00:00
|
|
|
f.write(repo.get_file(fn, version.tag))
|
2021-04-08 16:26:57 +00:00
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
# work around https://github.com/nix-community/bundix/issues/8
|
|
|
|
os.environ["BUNDLE_FORCE_RUBY_PLATFORM"] = "true"
|
2021-04-08 16:26:57 +00:00
|
|
|
subprocess.check_output(['bundle', 'lock'], cwd=rubyenv_dir)
|
2021-08-18 13:19:15 +00:00
|
|
|
_remove_platforms(rubyenv_dir)
|
2021-04-08 16:26:57 +00:00
|
|
|
subprocess.check_output(['bundix'], cwd=rubyenv_dir)
|
|
|
|
|
2021-12-19 01:06:50 +00:00
|
|
|
_call_nix_update('discourse', version.version)
|
|
|
|
|
2022-04-15 01:41:22 +00:00
|
|
|
old_yarn_hash = _nix_eval('discourse.assets.yarnOfflineCache.outputHash')
|
2024-10-29 11:11:06 +00:00
|
|
|
new_yarn_hash = repo.get_yarn_lock_hash(version.tag, "yarn.lock")
|
2024-06-05 15:53:02 +00:00
|
|
|
click.echo(f"Updating yarn lock hash: {old_yarn_hash} -> {new_yarn_hash}")
|
|
|
|
|
2022-04-15 01:41:22 +00:00
|
|
|
with open(Path(__file__).parent / "default.nix", 'r+') as f:
|
|
|
|
content = f.read()
|
|
|
|
content = content.replace(old_yarn_hash, new_yarn_hash)
|
|
|
|
f.seek(0)
|
|
|
|
f.write(content)
|
|
|
|
f.truncate()
|
|
|
|
|
2021-04-08 16:26:57 +00:00
|
|
|
|
2021-07-16 19:40:57 +00:00
|
|
|
@cli.command()
|
2022-04-15 01:41:22 +00:00
|
|
|
@click.argument('rev', default='latest')
|
|
|
|
def update_mail_receiver(rev):
|
|
|
|
"""Update discourse-mail-receiver.
|
|
|
|
|
|
|
|
REV: the git rev to update to ('vX.Y.Z') or 'latest'; defaults to
|
|
|
|
'latest'.
|
2021-07-16 19:40:57 +00:00
|
|
|
|
|
|
|
"""
|
2022-04-15 01:41:22 +00:00
|
|
|
repo = DiscourseRepo(repo="mail-receiver")
|
|
|
|
|
|
|
|
if rev == 'latest':
|
|
|
|
version = repo.versions[0]
|
|
|
|
else:
|
|
|
|
version = DiscourseVersion(rev)
|
|
|
|
|
|
|
|
_call_nix_update('discourse-mail-receiver', version.version)
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
def update_plugins():
|
|
|
|
"""Update plugins to their latest revision."""
|
2021-07-16 19:40:57 +00:00
|
|
|
plugins = [
|
2021-12-06 16:07:01 +00:00
|
|
|
{'name': 'discourse-assign'},
|
2022-09-22 12:36:57 +00:00
|
|
|
{'name': 'discourse-bbcode-color'},
|
2021-08-05 21:33:18 +00:00
|
|
|
{'name': 'discourse-calendar'},
|
2021-07-16 19:40:57 +00:00
|
|
|
{'name': 'discourse-canned-replies'},
|
2021-12-06 16:07:01 +00:00
|
|
|
{'name': 'discourse-chat-integration'},
|
2021-08-05 21:33:18 +00:00
|
|
|
{'name': 'discourse-checklist'},
|
|
|
|
{'name': 'discourse-data-explorer'},
|
2021-12-06 16:07:01 +00:00
|
|
|
{'name': 'discourse-docs'},
|
2021-07-16 19:40:57 +00:00
|
|
|
{'name': 'discourse-github'},
|
2021-08-22 07:53:02 +00:00
|
|
|
{'name': 'discourse-ldap-auth', 'owner': 'jonmbake'},
|
2021-07-16 19:40:57 +00:00
|
|
|
{'name': 'discourse-math'},
|
2021-08-05 21:33:18 +00:00
|
|
|
{'name': 'discourse-migratepassword', 'owner': 'discoursehosting'},
|
2021-12-19 01:06:50 +00:00
|
|
|
{'name': 'discourse-openid-connect'},
|
2023-02-02 18:25:31 +00:00
|
|
|
{'name': 'discourse-prometheus'},
|
|
|
|
{'name': 'discourse-reactions'},
|
2021-12-06 16:07:01 +00:00
|
|
|
{'name': 'discourse-saved-searches'},
|
2021-07-16 19:40:57 +00:00
|
|
|
{'name': 'discourse-solved'},
|
|
|
|
{'name': 'discourse-spoiler-alert'},
|
2021-12-06 16:07:01 +00:00
|
|
|
{'name': 'discourse-voting'},
|
2021-07-16 19:40:57 +00:00
|
|
|
{'name': 'discourse-yearly-review'},
|
|
|
|
]
|
|
|
|
|
|
|
|
for plugin in plugins:
|
|
|
|
fetcher = plugin.get('fetcher') or "fetchFromGitHub"
|
|
|
|
owner = plugin.get('owner') or "discourse"
|
|
|
|
name = plugin.get('name')
|
|
|
|
repo_name = plugin.get('repo_name') or name
|
|
|
|
|
2024-07-27 06:49:29 +00:00
|
|
|
if fetcher == "fetchFromGitHub":
|
|
|
|
url = f"https://github.com/{owner}/{repo_name}"
|
|
|
|
else:
|
|
|
|
raise NotImplementedError(f"Missing URL pattern for {fetcher}")
|
|
|
|
|
2021-07-16 19:40:57 +00:00
|
|
|
repo = DiscourseRepo(owner=owner, repo=repo_name)
|
2021-08-18 13:19:15 +00:00
|
|
|
|
2021-12-19 01:06:50 +00:00
|
|
|
# implement the plugin pinning algorithm laid out here:
|
|
|
|
# https://meta.discourse.org/t/pinning-plugin-and-theme-versions-for-older-discourse-installs/156971
|
|
|
|
# this makes sure we don't upgrade plugins to revisions that
|
|
|
|
# are incompatible with the packaged Discourse version
|
|
|
|
try:
|
|
|
|
compatibility_spec = repo.get_file('.discourse-compatibility', repo.latest_commit_sha)
|
|
|
|
versions = [(DiscourseVersion(discourse_version), plugin_rev.strip(' '))
|
|
|
|
for [discourse_version, plugin_rev]
|
2024-06-05 15:53:02 +00:00
|
|
|
in [line.lstrip("< ").split(':')
|
2021-12-19 01:06:50 +00:00
|
|
|
for line
|
2023-10-09 19:29:22 +00:00
|
|
|
in compatibility_spec.splitlines() if line != '']]
|
2021-12-19 01:06:50 +00:00
|
|
|
discourse_version = DiscourseVersion(_get_current_package_version('discourse'))
|
|
|
|
versions = list(filter(lambda ver: ver[0] >= discourse_version, versions))
|
|
|
|
if versions == []:
|
|
|
|
rev = repo.latest_commit_sha
|
|
|
|
else:
|
|
|
|
rev = versions[0][1]
|
|
|
|
print(rev)
|
|
|
|
except requests.exceptions.HTTPError:
|
|
|
|
rev = repo.latest_commit_sha
|
|
|
|
|
2021-08-18 13:19:15 +00:00
|
|
|
filename = _nix_eval(f'builtins.unsafeGetAttrPos "src" discourse.plugins.{name}')
|
|
|
|
if filename is None:
|
|
|
|
filename = Path(__file__).parent / 'plugins' / name / 'default.nix'
|
|
|
|
filename.parent.mkdir()
|
|
|
|
|
|
|
|
has_ruby_deps = False
|
2021-12-19 01:06:50 +00:00
|
|
|
for line in repo.get_file('plugin.rb', rev).splitlines():
|
2021-08-18 13:19:15 +00:00
|
|
|
if 'gem ' in line:
|
|
|
|
has_ruby_deps = True
|
|
|
|
break
|
|
|
|
|
|
|
|
with open(filename, 'w') as f:
|
|
|
|
f.write(textwrap.dedent(f"""
|
|
|
|
{{ lib, mkDiscoursePlugin, fetchFromGitHub }}:
|
|
|
|
|
|
|
|
mkDiscoursePlugin {{
|
|
|
|
name = "{name}";"""[1:] + ("""
|
|
|
|
bundlerEnvArgs.gemdir = ./.;""" if has_ruby_deps else "") + f"""
|
|
|
|
src = {fetcher} {{
|
|
|
|
owner = "{owner}";
|
|
|
|
repo = "{repo_name}";
|
|
|
|
rev = "replace-with-git-rev";
|
|
|
|
sha256 = "replace-with-sha256";
|
|
|
|
}};
|
|
|
|
meta = with lib; {{
|
|
|
|
homepage = "";
|
|
|
|
maintainers = with maintainers; [ ];
|
|
|
|
license = licenses.mit; # change to the correct license!
|
|
|
|
description = "";
|
|
|
|
}};
|
|
|
|
}}"""))
|
|
|
|
|
|
|
|
all_plugins_filename = Path(__file__).parent / 'plugins' / 'all-plugins.nix'
|
|
|
|
with open(all_plugins_filename, 'r+') as f:
|
|
|
|
content = f.read()
|
|
|
|
pos = -1
|
|
|
|
while content[pos] != '}':
|
|
|
|
pos -= 1
|
|
|
|
content = content[:pos] + f' {name} = callPackage ./{name} {{}};' + os.linesep + content[pos:]
|
|
|
|
f.seek(0)
|
|
|
|
f.write(content)
|
|
|
|
f.truncate()
|
|
|
|
|
|
|
|
else:
|
|
|
|
filename = filename['file']
|
|
|
|
|
2021-07-16 19:40:57 +00:00
|
|
|
prev_commit_sha = _nix_eval(f'discourse.plugins.{name}.src.rev')
|
|
|
|
|
2021-12-19 01:06:50 +00:00
|
|
|
if prev_commit_sha == rev:
|
2021-07-16 19:40:57 +00:00
|
|
|
click.echo(f'Plugin {name} is already at the latest revision')
|
|
|
|
continue
|
|
|
|
|
|
|
|
prev_hash = _nix_eval(f'discourse.plugins.{name}.src.outputHash')
|
|
|
|
new_hash = subprocess.check_output([
|
2024-07-27 06:49:29 +00:00
|
|
|
"nurl",
|
|
|
|
"--fetcher", fetcher,
|
|
|
|
"--hash",
|
|
|
|
url,
|
|
|
|
rev,
|
2021-07-16 19:40:57 +00:00
|
|
|
], text=True).strip("\n")
|
|
|
|
|
2021-12-19 01:06:50 +00:00
|
|
|
click.echo(f"Update {name}, {prev_commit_sha} -> {rev} in {filename}")
|
2021-07-16 19:40:57 +00:00
|
|
|
|
|
|
|
with open(filename, 'r+') as f:
|
|
|
|
content = f.read()
|
2021-12-19 01:06:50 +00:00
|
|
|
content = content.replace(prev_commit_sha, rev)
|
2021-07-16 19:40:57 +00:00
|
|
|
content = content.replace(prev_hash, new_hash)
|
|
|
|
f.seek(0)
|
|
|
|
f.write(content)
|
|
|
|
f.truncate()
|
|
|
|
|
|
|
|
rubyenv_dir = Path(filename).parent
|
|
|
|
gemfile = rubyenv_dir / "Gemfile"
|
2021-12-19 01:06:50 +00:00
|
|
|
version_file_regex = re.compile(r'.*File\.expand_path\("\.\./(.*)", __FILE__\)')
|
2021-07-16 19:40:57 +00:00
|
|
|
gemfile_text = ''
|
2023-02-22 10:55:15 +00:00
|
|
|
plugin_file = repo.get_file('plugin.rb', rev)
|
|
|
|
plugin_file = plugin_file.replace(",\n", ", ") # fix split lines
|
|
|
|
for line in plugin_file.splitlines():
|
2021-07-16 19:40:57 +00:00
|
|
|
if 'gem ' in line:
|
2022-08-21 13:32:41 +00:00
|
|
|
line = ','.join(filter(lambda x: ":require_name" not in x, line.split(',')))
|
2021-07-16 19:40:57 +00:00
|
|
|
gemfile_text = gemfile_text + line + os.linesep
|
|
|
|
|
2021-12-19 01:06:50 +00:00
|
|
|
version_file_match = version_file_regex.match(line)
|
|
|
|
if version_file_match is not None:
|
|
|
|
filename = version_file_match.groups()[0]
|
|
|
|
content = repo.get_file(filename, rev)
|
|
|
|
with open(rubyenv_dir / filename, 'w') as f:
|
|
|
|
f.write(content)
|
|
|
|
|
2021-07-16 19:40:57 +00:00
|
|
|
if len(gemfile_text) > 0:
|
|
|
|
if os.path.isfile(gemfile):
|
|
|
|
os.remove(gemfile)
|
|
|
|
|
|
|
|
subprocess.check_output(['bundle', 'init'], cwd=rubyenv_dir)
|
|
|
|
os.chmod(gemfile, stat.S_IREAD | stat.S_IWRITE | stat.S_IRGRP | stat.S_IROTH)
|
|
|
|
|
|
|
|
with open(gemfile, 'a') as f:
|
|
|
|
f.write(gemfile_text)
|
|
|
|
|
2021-08-18 13:19:15 +00:00
|
|
|
subprocess.check_output(['bundle', 'lock', '--add-platform', 'ruby'], cwd=rubyenv_dir)
|
2021-07-16 19:40:57 +00:00
|
|
|
subprocess.check_output(['bundle', 'lock', '--update'], cwd=rubyenv_dir)
|
2021-08-18 13:19:15 +00:00
|
|
|
_remove_platforms(rubyenv_dir)
|
2021-07-16 19:40:57 +00:00
|
|
|
subprocess.check_output(['bundix'], cwd=rubyenv_dir)
|
|
|
|
|
2021-04-08 16:26:57 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
cli()
|