Skip to content

Commit

Permalink
feat(common): make the version optional when adding a gradle plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
tinesoft authored Jul 11, 2022
1 parent 017736f commit bd3a182
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
15 changes: 15 additions & 0 deletions packages/common/src/lib/core/jvm/gradle-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,21 @@ describe('gradle-utils', () => {
`);
});

it('should still add plugin to build.gradle file when version is not specified', () => {
tree.write('./build.gradle', '');
const added = addGradlePlugin(tree, '.', 'java', 'java-library');
expect(added).toBe(true);

const buildGradle = tree.read('./build.gradle', 'utf-8');
console.log(buildGradle);
expect(buildGradle).toContain(
stripIndent`
plugins {
id 'java-library'
}
`);
});

});

describe('applySpotlessGradlePlugin', () => {
Expand Down
8 changes: 6 additions & 2 deletions packages/common/src/lib/core/jvm/gradle-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ export function hasGradlePlugin(content: string, pluginId: string, pluginVersion
return plugins.some(plugin => plugin.id === pluginId && (!pluginVersion || plugin.version === pluginVersion));
}

export function addGradlePlugin(tree: Tree, rootFolder: string, language: 'java' | 'kotlin' | 'groovy', pluginId: string, pluginVersion: string, withKotlinDSL = language === 'kotlin') {
export function addGradlePlugin(tree: Tree, rootFolder: string, language: 'java' | 'kotlin' | 'groovy', pluginId: string, pluginVersion?: string, withKotlinDSL = language === 'kotlin') {
const ext = withKotlinDSL ? '.gradle.kts' : '.gradle';
const buildGradle = tree.read(`${rootFolder}/build${ext}`, 'utf-8');
const pluginToAdd = withKotlinDSL ? `id("${pluginId}") version "${pluginVersion}"` : `id '${pluginId}' version '${pluginVersion}'`;
let withVersion = '';
if(pluginVersion) {
withVersion = withKotlinDSL ? ` version "${pluginVersion}"` : ` version '${pluginVersion}'`;
}
const pluginToAdd = withKotlinDSL ? `id("${pluginId}")${withVersion}` : `id '${pluginId}'${withVersion}`;

if (!hasGradlePlugin(buildGradle, pluginId)) {
if (!GRADLE_PLUGINS_REGEX.test(buildGradle)) {
Expand Down

0 comments on commit bd3a182

Please sign in to comment.