Skip to content

Commit 52f7831

Browse files
DarhkVoydabidlabsgradio-pr-bot
authored
refactor: Use package.json for version management (#5514)
* refactor: Use package.json for version management - uses package.json file for version management. - updated the regex pattern. - removed the logic that creates or updates the version.txt file * load version through package.json * fix code duplication * add changeset * add changeset * fixes * fix * package version * fix * typing * typing * changes * add changeset --------- Co-authored-by: Abubakar Abid <abubakar@huggingface.co> Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
1 parent 99e87a4 commit 52f7831

19 files changed

Lines changed: 72 additions & 51 deletions

.changeset/fix_changelogs.cjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ ${current_changelog.replace(`# ${pkg_name}`, "").trim()}
8282
});
8383

8484
if (python) {
85-
writeFileSync(join(dirs[0], "version.txt"), version);
8685
bump_local_dependents(pkg_name, version);
8786
}
8887
}

.changeset/smart-birds-drop.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@gradio/app": patch
3+
"gradio": patch
4+
"gradio_client": patch
5+
---
6+
7+
feat:refactor: Use package.json for version management

.config/basevite.config.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { join } from "path";
1111
import { fileURLToPath } from "url";
1212

1313
const __dirname = fileURLToPath(new URL(".", import.meta.url));
14-
const version_path = join(__dirname, "..", "gradio", "version.txt");
14+
const version_path = join(__dirname, "..", "gradio", "package.json");
1515
const theme_token_path = join(
1616
__dirname,
1717
"..",
@@ -21,9 +21,7 @@ const theme_token_path = join(
2121
"tokens.css"
2222
);
2323

24-
const version = readFileSync(version_path, { encoding: "utf-8" })
25-
.trim()
26-
.replace(/\./g, "-");
24+
const version = JSON.parse(readFileSync(version_path, { encoding: 'utf-8' })).version.trim().replace(/\./g, '-');
2725

2826
//@ts-ignore
2927
export default defineConfig(({ mode }) => {

.github/workflows/build-pr.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ jobs:
4747
fi
4848
- name: Build pr package
4949
run: |
50-
echo ${{ steps.get_pr_number.outputs.GRADIO_VERSION }} > gradio/version.txt
5150
pnpm i --frozen-lockfile --ignore-scripts
5251
pnpm build
5352
python3 -m build -w

build_pypi.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ set -e
33

44
cd "$(dirname ${0})"
55

6-
# You should update the version in version.txt before running this script
7-
new_version="$(cat gradio/version.txt)"
8-
GRADIO_VERSION=$new_version
6+
# You should update the version in package.json before running this script
7+
FILE="gradio/package.json"
8+
new_version=$(python -c "import json; f = open('$FILE', 'r'); data = json.load(f); print(data['version']); f.close();")
9+
GRADIO_VERSION = $new_version
910

1011
rm -rf gradio/templates/frontend
1112
rm -rf gradio/templates/cdn

client/python/gradio_client/utils.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,20 @@
5858
SpaceStage.PAUSED,
5959
]
6060

61-
__version__ = (pkgutil.get_data(__name__, "version.txt") or b"").decode("ascii").strip()
61+
62+
def get_package_version() -> str:
63+
try:
64+
package_json_data = (
65+
pkgutil.get_data(__name__, "package.json").decode("utf-8").strip() # type: ignore
66+
)
67+
package_data = json.loads(package_json_data)
68+
version = package_data.get("version", "")
69+
return version
70+
except Exception:
71+
return ""
72+
73+
74+
__version__ = get_package_version()
6275

6376

6477
class TooManyRequestsError(Exception):

client/python/gradio_client/version.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

client/python/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ classifiers = [
3838
Homepage = "https://github.com/gradio-app/gradio"
3939

4040
[tool.hatch.version]
41-
path = "gradio_client/version.txt"
42-
pattern = "(?P<version>.+)"
41+
path = "gradio_client/package.json"
42+
pattern = ".*\"version\":\\s*\"(?P<version>[^\"]+)\""
4343

4444
[tool.hatch.metadata.hooks.requirements_txt]
4545
filename = "requirements.txt"

client/python/scripts/check_pypi.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import urllib.request
44
from pathlib import Path
55

6-
version_file = Path(__file__).parent.parent / "gradio_client" / "version.txt"
7-
version = version_file.read_text(encoding="utf8").strip()
6+
version_file = Path(__file__).parent.parent / "gradio_client" / "package.json"
7+
with version_file.open() as f:
8+
version = json.load(f)["version"]
89

910
with urllib.request.urlopen("https://pypi.org/pypi/gradio_client/json") as url:
1011
releases = json.load(url)["releases"]

gradio/__init__.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import pkgutil
1+
import json
22

33
import gradio.components as components
44
import gradio.inputs as inputs
@@ -103,8 +103,6 @@
103103
Webcam,
104104
)
105105
from gradio.themes import Base as Theme
106+
from gradio.utils import get_package_version
106107

107-
current_pkg_version = (
108-
(pkgutil.get_data(__name__, "version.txt") or b"").decode("ascii").strip()
109-
)
110-
__version__ = current_pkg_version
108+
__version__ = get_package_version()

0 commit comments

Comments
 (0)