Skip to content

Commit

Permalink
chore: refactored gem package to join useful methods
Browse files Browse the repository at this point in the history
  • Loading branch information
angeliski committed Mar 30, 2021
1 parent 6c2bfe5 commit 4f2e74f
Show file tree
Hide file tree
Showing 2 changed files with 273 additions and 89 deletions.
136 changes: 57 additions & 79 deletions plugins/gem/src/index.ts
Expand Up @@ -99,8 +99,7 @@ export default class GemPlugin implements IPlugin {
auto.hooks.version.tapPromise(
this.name,
async ({ bump, dryRun, quiet }) => {
const [version, versionFile] = await this.getVersion(auto);
const newTag = inc(version, bump as ReleaseType);
const [version, newTag, versionFile] = await this.getNewVersion(auto, bump as ReleaseType)

if (dryRun && newTag) {
if (quiet) {
Expand All @@ -112,58 +111,16 @@ export default class GemPlugin implements IPlugin {
return;
}

if (!newTag) {
throw new Error(
`The version "${version}" parsed from your version file "${versionFile}" was invalid and could not be incremented. Please fix this!`
);
}

const content = await readFile(versionFile, { encoding: "utf8" });
await writeFile(versionFile, content.replace(version, newTag));
await this.writeNewVersion(version, newTag, versionFile)
}
);

auto.hooks.canary.tapPromise(
this.name,
async ({ bump, canaryIdentifier, dryRun, quiet }) => {
if (
isCi &&
!fs.existsSync("~/.gem/credentials") &&
process.env.RUBYGEMS_API_KEY
) {
const home = process.env.HOME || "~";
const gemDir = path.join(home, ".gem");

if (!fs.existsSync(gemDir)) {
auto.logger.verbose.info(`Creating ${gemDir} directory`);
await mkdir(gemDir);
}
await this.writeCredentials(auto)

const credentials = path.join(gemDir, "credentials");

await writeFile(
credentials,
endent`
---
:rubygems_api_key: ${process.env.RUBYGEMS_API_KEY}
`
);
auto.logger.verbose.success(`Wrote ${credentials}`);

execSync(`chmod 0600 ${credentials}`, {
stdio: "inherit",
});
}

const [version, versionFile] = await this.getVersion(auto);
const newTag = inc(version, bump as ReleaseType);

if (!newTag) {
throw new Error(
`The version "${version}" parsed from your version file "${versionFile}" was invalid and could not be incremented. Please fix this!`
);
}
const [version, newTag, versionFile] = await this.getNewVersion(auto, bump as ReleaseType)

const canaryVersion = `${newTag}.pre${canaryIdentifier.replace('-','.')}`

Expand All @@ -177,9 +134,7 @@ export default class GemPlugin implements IPlugin {
return;
}


const content = await readFile(versionFile, { encoding: "utf8" });
await writeFile(versionFile, content.replace(version, canaryVersion));
await this.writeNewVersion(version, canaryVersion, versionFile)

/** Commit the new version. we wait because "rake build" changes the lock file */
/** we don't push that version, is just to clean the stage */
Expand Down Expand Up @@ -211,35 +166,7 @@ export default class GemPlugin implements IPlugin {
});

auto.hooks.publish.tapPromise(this.name, async () => {
if (
isCi &&
!fs.existsSync("~/.gem/credentials") &&
process.env.RUBYGEMS_API_KEY
) {
const home = process.env.HOME || "~";
const gemDir = path.join(home, ".gem");

if (!fs.existsSync(gemDir)) {
auto.logger.verbose.info(`Creating ${gemDir} directory`);
await mkdir(gemDir);
}

const credentials = path.join(gemDir, "credentials");

await writeFile(
credentials,
endent`
---
:rubygems_api_key: ${process.env.RUBYGEMS_API_KEY}
`
);
auto.logger.verbose.success(`Wrote ${credentials}`);

execSync(`chmod 0600 ${credentials}`, {
stdio: "inherit",
});
}
await this.writeCredentials(auto)

const [version] = await this.getVersion(auto);

Expand Down Expand Up @@ -286,6 +213,57 @@ export default class GemPlugin implements IPlugin {
return content.match(GEM_SPEC_NAME_REGEX)?.[1];
}

private async writeCredentials(auto: Auto) {
if (
isCi &&
!fs.existsSync("~/.gem/credentials") &&
process.env.RUBYGEMS_API_KEY
) {
const home = process.env.HOME || "~";
const gemDir = path.join(home, ".gem");

if (!fs.existsSync(gemDir)) {
auto.logger.verbose.info(`Creating ${gemDir} directory`);
await mkdir(gemDir);
}

const credentials = path.join(gemDir, "credentials");

await writeFile(
credentials,
endent`
---
:rubygems_api_key: ${process.env.RUBYGEMS_API_KEY}
`
);
auto.logger.verbose.success(`Wrote ${credentials}`);

execSync(`chmod 0600 ${credentials}`, {
stdio: "inherit",
});
}
}


private async getNewVersion(auto: Auto, bump: ReleaseType) {
const [version, versionFile] = await this.getVersion(auto);
const newTag = inc(version, bump);

if (!newTag) {
throw new Error(
`The version "${version}" parsed from your version file "${versionFile}" was invalid and could not be incremented. Please fix this!`
);
}

return [version, newTag, versionFile];
}

private async writeNewVersion(version: string, newVersion: string, versionFile: string){
const content = await readFile(versionFile, { encoding: "utf8" });
await writeFile(versionFile, content.replace(version, newVersion));

}

/** Get the current version of the gem and where it was found */
private async getVersion(auto: Auto) {
Expand Down

0 comments on commit 4f2e74f

Please sign in to comment.