Skip to content

Commit 75d32e2

Browse files
CopilotjGauravGupta
andcommitted
Refactor Gradle buildDir detection to match PR #245 pattern
Co-authored-by: jGauravGupta <15934072+jGauravGupta@users.noreply.github.com>
1 parent efa142e commit 75d32e2

2 files changed

Lines changed: 35 additions & 31 deletions

File tree

src/main/fish/payara/project/Gradle.ts

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ export class Gradle implements Build {
4242

4343
private microPluginReader: GradleMicroPluginReader | undefined;
4444

45-
private cachedBuildDir: string | undefined;
46-
4745
constructor(public payaraInstance: PayaraInstance | null, public workspaceFolder: WorkspaceFolder) {
4846
this.readBuildConfig();
4947
}
@@ -236,37 +234,15 @@ export class Gradle implements Build {
236234
return gradleExecStr;
237235
}
238236

239-
public detectBuildDir(): string {
240-
if (this.cachedBuildDir !== undefined) {
241-
return this.cachedBuildDir;
242-
}
243-
const gradleFiles = ['build.gradle', 'build.gradle.kts'];
244-
for (const gradleFile of gradleFiles) {
245-
const filePath = path.join(this.workspaceFolder.uri.fsPath, gradleFile);
246-
if (fs.existsSync(filePath)) {
247-
try {
248-
const content = fs.readFileSync(filePath, 'utf8');
249-
const fileMatch = content.match(/buildDir\s*=\s*file\(\s*["']([^"']+)["']\s*\)/);
250-
if (fileMatch) {
251-
this.cachedBuildDir = fileMatch[1];
252-
return this.cachedBuildDir;
253-
}
254-
const stringMatch = content.match(/buildDir\s*=\s*["']([^"']+)["']/);
255-
if (stringMatch) {
256-
this.cachedBuildDir = stringMatch[1];
257-
return this.cachedBuildDir;
258-
}
259-
} catch (e) {
260-
// ignore read errors and fall back to default
261-
}
237+
public getBuildDir(): string {
238+
let buildDirValue = 'build';
239+
if (this.buildReader instanceof GradleBuildReader) {
240+
let customDir = this.buildReader.getBuildDirectory();
241+
if (customDir.length > 0) {
242+
buildDirValue = customDir;
262243
}
263244
}
264-
this.cachedBuildDir = 'build';
265-
return this.cachedBuildDir;
266-
}
267-
268-
public getBuildDir(): string {
269-
let buildDir = path.join(this.workspaceFolder.uri.fsPath, this.detectBuildDir(), 'libs');
245+
let buildDir = path.resolve(this.workspaceFolder.uri.fsPath, buildDirValue, 'libs');
270246
if (!fs.existsSync(buildDir)) {
271247
throw Error("no build dir found: " + buildDir);
272248
}

src/main/fish/payara/project/GradleBuildReader.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export class GradleBuildReader implements BuildReader {
2828
private artifactId: string = '';
2929
private version: string = '';
3030
private finalName: string = '';
31+
private buildDirectory: string = '';
3132

3233
public constructor(public workspaceFolder: WorkspaceFolder) {
3334
this.parseBuild();
@@ -45,6 +46,12 @@ export class GradleBuildReader implements BuildReader {
4546
let build: any = await g2js.parseFile(buildPath);
4647
reader.groupId = build.group;
4748
reader.version = build.version;
49+
reader.buildDirectory = reader.parseBuildDirectory(buildPath);
50+
}
51+
52+
let buildKtsPath = path.join(this.workspaceFolder.uri.fsPath, 'build.gradle.kts');
53+
if (reader.buildDirectory.length < 1 && fs.existsSync(buildKtsPath)) {
54+
reader.buildDirectory = reader.parseBuildDirectory(buildKtsPath);
4855
}
4956

5057
if (fs.existsSync(settingsPath)) {
@@ -57,6 +64,23 @@ export class GradleBuildReader implements BuildReader {
5764
}
5865
}
5966

67+
private parseBuildDirectory(gradleFilePath: string): string {
68+
try {
69+
const content = fs.readFileSync(gradleFilePath, 'utf8');
70+
const fileMatch = content.match(/buildDir\s*=\s*file\(\s*["']([^"']+)["']\s*\)/);
71+
if (fileMatch) {
72+
return fileMatch[1];
73+
}
74+
const stringMatch = content.match(/buildDir\s*=\s*["']([^"']+)["']/);
75+
if (stringMatch) {
76+
return stringMatch[1];
77+
}
78+
} catch (e) {
79+
// ignore read errors and fall back to default
80+
}
81+
return '';
82+
}
83+
6084
public getGroupId(): string {
6185
return this.groupId;
6286
}
@@ -73,4 +97,8 @@ export class GradleBuildReader implements BuildReader {
7397
return this.finalName;
7498
}
7599

100+
public getBuildDirectory(): string {
101+
return this.buildDirectory;
102+
}
103+
76104
}

0 commit comments

Comments
 (0)