Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- feat(maven): Add maven target to deploy to Maven Central (#258)
- feat(symbol-collector): Add symbol-collector target (#266)
- ref(maven): Support BOM files in `maven` target (#270)
- feat(maven): Remove Gradle properties after publishing (#276)

## 0.24.4

Expand Down
20 changes: 12 additions & 8 deletions src/targets/__tests__/maven.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ jest.mock('fs', () => ({
writeFile: jest.fn(() => Promise.resolve()),
readFile: jest.fn((file: string) => file),
readdir: async () => Promise.resolve([]), // empty dir
access: jest.fn().mockImplementation(() => {
// do nothing
}),
unlink: jest.fn(),
},
}));

Expand Down Expand Up @@ -107,10 +105,14 @@ describe('publish', () => {
test('main flow', async () => {
const callOrder: string[] = [];
const mvnTarget = createMavenTarget();
const gradlePropsMock = jest.fn(
async () => void callOrder.push('gradleProps')
const createGradlePropsMock = jest.fn(
async () => void callOrder.push('createGradleProps')
);
mvnTarget.createUserGradlePropsFile = gradlePropsMock;
mvnTarget.createUserGradlePropsFile = createGradlePropsMock;
const deleteGradlePropsMock = jest.fn(
async () => void callOrder.push('deleteGradleProps')
);
mvnTarget.deleteUserGradlePropsFile = deleteGradlePropsMock;
const uploadMock = jest.fn(async () => void callOrder.push('upload'));
mvnTarget.upload = uploadMock;
(retrySpawnProcess as jest.MockedFunction<
Expand All @@ -121,17 +123,19 @@ describe('publish', () => {

const revision = 'r3v1s10n';
await mvnTarget.publish('1.0.0', revision);
expect(gradlePropsMock).toHaveBeenCalledTimes(1);
expect(createGradlePropsMock).toHaveBeenCalledTimes(1);
expect(uploadMock).toHaveBeenCalledTimes(1);
expect(uploadMock).toHaveBeenLastCalledWith(revision);
expect(deleteGradlePropsMock).toHaveBeenCalledTimes(1);
expect(retrySpawnProcess).toHaveBeenCalledTimes(1);
expect(retrySpawnProcess).toHaveBeenCalledWith(DEFAULT_OPTION_VALUE, [
Comment thread
iker-barriocanal marked this conversation as resolved.
'closeAndReleaseRepository',
]);
expect(callOrder).toStrictEqual([
'gradleProps',
'createGradleProps',
'upload',
'closeAndRelease',
'deleteGradleProps',
]);
});

Expand Down
26 changes: 14 additions & 12 deletions src/targets/maven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { BaseTarget } from './base';
import { homedir } from 'os';
import { basename, extname, join, parse } from 'path';
import { constants as fsConstants, promises as fsPromises } from 'fs';
import { promises as fsPromises } from 'fs';
import { checkExecutableIsPresent, extractZipArchive } from '../utils/system';
import { retrySpawnProcess } from '../utils/async';
import { withTempDir } from '../utils/files';
Expand All @@ -23,7 +23,7 @@ const GRADLE_PROPERTIES_FILENAME = 'gradle.properties';
const DEFAULT_GRADLE_USER_HOME = join(homedir(), '.gradle');
export const POM_DEFAULT_FILENAME = 'pom-default.xml';
const POM_FILE_EXT = '.xml'; // Must include the leading `.`
const BOM_FILE_KEY_REGEXP = stringToRegexp('/<packaging>pom</packaging>/');
const BOM_FILE_KEY_REGEXP = new RegExp('<packaging>pom</packaging>');

export const targetSecrets = ['OSSRH_USERNAME', 'OSSRH_PASSWORD'] as const;
type SecretsType = typeof targetSecrets[number];
Expand Down Expand Up @@ -186,6 +186,7 @@ export class MavenTarget extends BaseTarget {
await retrySpawnProcess(this.mavenConfig.gradleCliPath, [
'closeAndReleaseRepository',
]);
await this.deleteUserGradlePropsFile();
}

/**
Expand All @@ -205,6 +206,15 @@ export class MavenTarget extends BaseTarget {
);
}

/**
* Deletes the user's `gradle.properties` file.
*/
public deleteUserGradlePropsFile(): Promise<void> {
return fsPromises.unlink(
join(this.getGradleHomeDir(), GRADLE_PROPERTIES_FILENAME)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who creates this file? If it is the closeAndReleaseRepository script, then shouldn't this be its responsibility (to remove the file I mean)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's created by the target itself (by the method right above it)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then at this point, I'd convert this into something like withTempDir with auto-clean up even in case of exceptions for safety. This is much like a Python Context Manager

);
}

/**
* Retrieves the Gradle Home path.
*
Expand Down Expand Up @@ -292,17 +302,10 @@ export class MavenTarget extends BaseTarget {
// the BOM renamed in such a way that isn't handled by the target.
const filesInDir = await fsPromises.readdir(distDir);
const potentialPoms = filesInDir
.filter(f => extname(f) === POM_FILE_EXT)
.filter(f => f !== POM_DEFAULT_FILENAME)
.filter(f => f !== POM_DEFAULT_FILENAME && extname(f) === POM_FILE_EXT)
.map(f => join(distDir, f));

for (const f of potentialPoms) {
if (await this.isBomFile(f)) {
return f;
}
}

return undefined;
return potentialPoms.find(f => this.isBomFile(f));
}

/**
Expand All @@ -316,7 +319,6 @@ export class MavenTarget extends BaseTarget {
*/
public async isBomFile(pomFilepath: string): Promise<boolean> {
try {
await fsPromises.access(pomFilepath, fsConstants.R_OK);
const fileContents = await fsPromises.readFile(pomFilepath, {
encoding: 'utf8',
});
Expand Down