Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions .github/workflows/ci-setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ jobs:
# subset: tool + plugin only, keep the monorepo layout.
variant: [hoist-all, subset]
fail-fast: false
env:
SETUP_AUTHOR: 'Example "Author" \ Contributor'
SETUP_DESCRIPTION: 'Tools for "search" in C:\work — 日本語 🧪'

steps:
- name: Checkout code
Expand All @@ -48,14 +51,13 @@ jobs:
python-version: '3.10'

- name: Install hatch
run: pip install hatch pyyaml
run: pip install hatch pyyaml tomli

- name: Run setup script (hoist-all)
if: matrix.variant == 'hoist-all'
working-directory: python
# Answers: package name, components, author, email, github user,
# description (defaults), confirm, hoist.
run: printf 'acme\n1,2,3,4,5,6,7\n\n\n\n\ny\ny\n' | python setup_template.py
# Exercise quotes, backslashes, and Unicode in free-form metadata.
run: printf '%s\n' acme '1,2,3,4,5,6,7' "$SETUP_AUTHOR" '' '' "$SETUP_DESCRIPTION" y y | python setup_template.py

- name: Run setup script (subset)
if: matrix.variant == 'subset'
Expand All @@ -71,6 +73,14 @@ jobs:
test -f .github/workflows/ci.yml
test -f .github/workflows/publish.yml
python -c "import yaml,glob; [yaml.safe_load(open(f)) for f in glob.glob('.github/workflows/*.yml') + ['.github/dependabot.yml']]"
python - <<'PY'
import os
import tomli
with open("pyproject.toml", "rb") as file:
project = tomli.load(file)["project"]
assert project["description"] == os.environ["SETUP_DESCRIPTION"]
assert project["authors"][0]["name"] == os.environ["SETUP_AUTHOR"]
PY
hatch run prepare

- name: Run checks on generated project (subset)
Expand All @@ -89,6 +99,9 @@ jobs:
matrix:
variant: [hoist-all, subset]
fail-fast: false
env:
SETUP_AUTHOR: 'Example "Author" \ Contributor'
SETUP_DESCRIPTION: 'Tools for "search" in C:\work — 日本語 🧪'

steps:
- name: Checkout code
Expand All @@ -108,7 +121,7 @@ jobs:
- name: Run setup script (hoist-all)
if: matrix.variant == 'hoist-all'
working-directory: typescript
run: printf 'acme\n1,2,3,4,5,6,7\n\n\n\n\ny\ny\n' | npx tsx setup-template.ts
run: printf '%s\n' acme '1,2,3,4,5,6,7' "$SETUP_AUTHOR" '' '' "$SETUP_DESCRIPTION" y y | npx tsx setup-template.ts

- name: Run setup script (subset)
if: matrix.variant == 'subset'
Expand All @@ -125,6 +138,13 @@ jobs:
test -f .github/workflows/publish.yml
python3 -m pip install --quiet pyyaml
python3 -c "import yaml,glob; [yaml.safe_load(open(f)) for f in glob.glob('.github/workflows/*.yml') + ['.github/dependabot.yml']]"
node --input-type=module <<'JS'
import assert from 'node:assert/strict'
import fs from 'node:fs'
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'))
assert.equal(pkg.description, process.env.SETUP_DESCRIPTION)
assert.equal(pkg.author, `${process.env.SETUP_AUTHOR} <your.email@example.com>`)
JS
npm install
npm run check
npm run build
Expand Down
4 changes: 4 additions & 0 deletions python/setup_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
python setup_template.py
"""

import json
import os
import re
import shutil
Expand Down Expand Up @@ -132,6 +133,9 @@ def replace_in_file(filepath: str, replacements: dict[str, str]) -> None:
content = f.read()

for old, new in replacements.items():
if filepath == "pyproject.toml":
# TOML basic strings need JSON escapes and an explicit escape for DEL.
new = json.dumps(new, ensure_ascii=False)[1:-1].replace("\x7f", "\\u007f")
content = content.replace(old, new)

with open(filepath, "w", encoding="utf-8") as f:
Expand Down
8 changes: 7 additions & 1 deletion typescript/setup-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,13 @@ function applyReplacements(value: string, replacements: Record<string, string>):

function replaceInFile(filepath: string, replacements: Record<string, string>): void {
const content = fs.readFileSync(filepath, 'utf8')
fs.writeFileSync(filepath, applyReplacements(content, replacements), 'utf8')
const fileReplacements =
filepath === 'package.json'
? Object.fromEntries(
Object.entries(replacements).map(([key, value]) => [key, JSON.stringify(value).slice(1, -1)])
)
: replacements
fs.writeFileSync(filepath, applyReplacements(content, fileReplacements), 'utf8')
}

// ---------------------------------------------------------------------------
Expand Down