Skip to content

Commit e1b980a

Browse files
authored
Differentiate Python 3 goody name from its module name (#154)
The module name needs to be a valid Python 3 module name, whereas in the goody name it might be nice to support other characters. For example, I like calling the goody `int.digits` even though that's not a valid module name. This also matches with how Java and Kotlin goodies already work.
1 parent 292df94 commit e1b980a

File tree

14 files changed

+81
-33
lines changed

14 files changed

+81
-33
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "int.digits"
3+
}

tools/adventure-pack/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"goodies:kotlin:format": "(cd goodies/kotlin && ./gradlew ktfmtCustom)",
2525
"goodies:kotlin:install": "javac -version",
2626
"goodies:kotlin:test": "(cd goodies/kotlin && ./gradlew test)",
27-
"goodies:python3:format": "bash goodies/python3/format.sh",
27+
"goodies:python3:format": "prettier --write goodies/python3 && bash goodies/python3/format.sh",
2828
"goodies:python3:install": "bash goodies/python3/install.sh",
2929
"goodies:python3:test": "bash goodies/python3/test.sh",
3030
"goodies:typescript:format": "prettier --write goodies/typescript",

tools/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ fun lcm(a: Int, b: Int): Int = a / gcd(a, b) * b
867867
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
868868
`;
869869
870-
exports[`App can equip single goody: Python 3 int_digits 1`] = `
870+
exports[`App can equip single goody: Python 3 int.digits 1`] = `
871871
"########################## BEGIN ADVENTURE PACK CODE ###########################
872872
# Adventure Pack commit fake-commit-hash
873873
# Running at: https://example.com/

tools/adventure-pack/src/app/__tests__/__snapshots__/render-test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ import gcd_int_int.gcd
530530
fun lcm(a: Int, b: Int): Int = a / gcd(a, b) * b"
531531
`;
532532
533-
exports[`App can render goody: Python 3 int_digits 1`] = `
533+
exports[`App can render goody: Python 3 int.digits 1`] = `
534534
"from typing import Generator
535535
536536

tools/adventure-pack/src/app/__tests__/equip-test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ function setUnsafe(
1717
(map as Record<string, unknown>)[properties.at(-1)!] = value;
1818
}
1919

20-
setUnsafe(globalThis, ["requestIdleCallback"], <T>(fn: () => T): T => fn());
2120
setUnsafe(globalThis, ["window", "location", "href"], "https://example.com/");
2221

2322
describe("App", () => {
@@ -27,8 +26,7 @@ describe("App", () => {
2726
for (const language of Object.keys(goodiesByLanguage) as Language[]) {
2827
const goodies = goodiesByLanguage[language];
2928
for (const goodyName of Object.keys(goodies)) {
30-
// eslint-disable-next-line no-await-in-loop
31-
const mergedCode = await mergeCode({
29+
const mergedCode = mergeCode({
3230
commitHash: "fake-commit-hash",
3331
goodies,
3432
language,

tools/adventure-pack/src/scripts/package-goodies/java/readGoodies.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { JavaGoody } from "../../../app/parsers/javaGoodyParser";
77
import { GOODIES_DIRECTORY } from "./constants";
88
import { type JavaGoodyBase, readBaseGoody } from "./readBaseGoody";
99
import { fillOutImportedByAndSortImports } from "../fillOutImportedByAndSortImports";
10+
import { normalizeGoodyNameToPackageOrModuleName } from "../normalizeGoodyNameToPackageOrModuleName";
1011

1112
export async function readGoodies(): Promise<Record<string, JavaGoody>> {
1213
const fileEntries = await fsPromises.readdir(GOODIES_DIRECTORY, {
@@ -26,11 +27,9 @@ export async function readGoodies(): Promise<Record<string, JavaGoody>> {
2627
// eslint-disable-next-line no-await-in-loop
2728
const baseGoody = await readBaseGoody(packageName);
2829

29-
const expectedPackageName = baseGoody.name
30-
.replace(/[A-Z]+/g, ([upper]) => "_" + upper.toLowerCase())
31-
.replace(/[^a-z0-9]+/gi, "_")
32-
.replace(/_$/, "")
33-
.replace(/^_/, "");
30+
const expectedPackageName = normalizeGoodyNameToPackageOrModuleName(
31+
baseGoody.name,
32+
);
3433
invariant(
3534
packageName === expectedPackageName,
3635
`Mismatched package name for goody! Expected ${JSON.stringify(expectedPackageName)} based on goody name ${JSON.stringify(baseGoody.name)} but got ${JSON.stringify(packageName)}.`,

tools/adventure-pack/src/scripts/package-goodies/kotlin/readCode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import path from "node:path";
33

44
import { GOODIES_DIRECTORY } from "./constants";
55

6-
export async function readCode(packageName: string): Promise<string> {
7-
return await fsPromises.readFile(
6+
export function readCode(packageName: string): Promise<string> {
7+
return fsPromises.readFile(
88
path.join(GOODIES_DIRECTORY, packageName, "Main.kt"),
99
"utf8",
1010
);

tools/adventure-pack/src/scripts/package-goodies/kotlin/readGoodies.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { KotlinGoody } from "../../../app/parsers/kotlinGoodyParser";
77
import { GOODIES_DIRECTORY } from "./constants";
88
import { type KotlinGoodyBase, readBaseGoody } from "./readBaseGoody";
99
import { fillOutImportedByAndSortImports } from "../fillOutImportedByAndSortImports";
10+
import { normalizeGoodyNameToPackageOrModuleName } from "../normalizeGoodyNameToPackageOrModuleName";
1011

1112
export async function readGoodies(): Promise<Record<string, KotlinGoody>> {
1213
const fileEntries = await fsPromises.readdir(GOODIES_DIRECTORY, {
@@ -26,11 +27,9 @@ export async function readGoodies(): Promise<Record<string, KotlinGoody>> {
2627
// eslint-disable-next-line no-await-in-loop
2728
const baseGoody = await readBaseGoody(packageName);
2829

29-
const expectedPackageName = baseGoody.name
30-
.replace(/[A-Z]+/g, ([upper]) => "_" + upper.toLowerCase())
31-
.replace(/[^a-z0-9]+/gi, "_")
32-
.replace(/_$/, "")
33-
.replace(/^_/, "");
30+
const expectedPackageName = normalizeGoodyNameToPackageOrModuleName(
31+
baseGoody.name,
32+
);
3433
invariant(
3534
packageName === expectedPackageName,
3635
`Mismatched package name for goody! Expected ${JSON.stringify(expectedPackageName)} based on goody name ${JSON.stringify(baseGoody.name)} but got ${JSON.stringify(packageName)}.`,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function normalizeGoodyNameToPackageOrModuleName(
2+
goodyName: string,
3+
): string {
4+
return goodyName
5+
.replace(/[A-Z]+/g, ([upper]) => "_" + upper.toLowerCase())
6+
.replace(/[^a-z0-9]+/gi, "_")
7+
.replace(/_$/, "")
8+
.replace(/^_/, "");
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import path from "node:path";
2+
3+
export const GOODIES_DIRECTORY = path.join("goodies", "python3", "src");

0 commit comments

Comments
 (0)