Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add go mod support to "arri use" #117

Merged
merged 1 commit into from
Nov 30, 2024
Merged
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
70 changes: 69 additions & 1 deletion tooling/cli/src/commands/use.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { updateCargoToml, updatePackageJson, updatePubspecYaml } from "./use";
import {
updateCargoToml,
updateGoMod,
updatePackageJson,
updatePubspecYaml,
} from "./use";

describe("updatePackageJson()", () => {
it("updates relevant lines and preserves formatting", () => {
Expand Down Expand Up @@ -115,3 +120,66 @@ tokio = { version = "1.39.2", features = ["full"] }`;
expect(output.updated).toBe(true);
});
});

describe("updateGoMod()", () => {
it("updates relevant lines", () => {
const input = `module main

go 1.23.3

require github.com/modiimedia/arri v0.66.0

require (
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/tidwall/gjson v1.18.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
)
`;
const expectedOutput = `module main

go 1.23.3

require github.com/modiimedia/arri v1.12.0

require (
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/tidwall/gjson v1.18.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
)
`;
const output = updateGoMod(input, "1.12.0");
expect(output.content).toBe(expectedOutput);
});
it("updates relevant lines and preserves comments", () => {
const input = `module main

go 1.23.3

require github.com/modiimedia/arri v0.66.0 // this is a comment

require (
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/tidwall/gjson v1.18.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
)
`;
const expectedOutput = `module main

go 1.23.3

require github.com/modiimedia/arri v1.12.0 // this is a comment

require (
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/tidwall/gjson v1.18.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
)
`;
const output = updateGoMod(input, "1.12.0");
expect(output.content).toBe(expectedOutput);
});
});
48 changes: 48 additions & 0 deletions tooling/cli/src/commands/use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ export default defineCommand({
}
await Promise.all(cargoTomlTasks);

// GO DEPENDENCIES
const goModFiles = await globby(["go.mod", "**/go.mod"]);
const goModFileTasks: Promise<any>[] = [];
for (const file of goModFiles) {
goModFileTasks.push(
readFile(path.resolve(file), "utf8").then((content) => {
const result = updateGoMod(content, version);
if (result.updated) {
fileMap[file] = result.content;
}
}),
);
}

// Update all affected files
const updateTasks: Promise<any>[] = [];
for (const key of Object.keys(fileMap)) {
Expand Down Expand Up @@ -353,3 +367,37 @@ export function updateCargoToml(
}
return { updated, content: newLines.join("\n") };
}

export function updateGoMod(
fileContent: string,
version: string,
): { updated: boolean; content: string } {
const lines = fileContent.split("\n");
const newLines: string[] = [];
let updated = false;
for (const line of lines) {
if (!line.includes("github.com/modiimedia/arri")) {
newLines.push(line);
continue;
}
updated = true;
const lineParts = line.split(" ");
const updatedLineParts: string[] = [];
for (const part of lineParts) {
if (part.startsWith("v") && isNumberChar(part[1] ?? "")) {
updatedLineParts.push(`v${version}`);
continue;
}
updatedLineParts.push(part);
}
newLines.push(updatedLineParts.join(" "));
}
return {
updated,
content: newLines.join("\n"),
};
}

function isNumberChar(char: string) {
return !Number.isNaN(Number(char));
}