Skip to content

Commit

Permalink
PLATUI-1190: Fixes webjar directory layout and add SHA1 hash
Browse files Browse the repository at this point in the history
This is needed as expected by the build and deploy script.
  • Loading branch information
matthewmascord committed Jun 10, 2021
1 parent 70a68fd commit 7c22613
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 30 deletions.
25 changes: 22 additions & 3 deletions tasks/gulp/__tests__/after-build-webjar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('webjar/', () => {
let packagePackageJson;

const cwd = process.cwd();
const jarPath = './webjar-dist/uk/gov/hmrc/webjars/hmrc-frontend/';

beforeAll(() => {
packagePackageJson = JSON.parse(readFileSync('package/package.json', 'utf8'));
Expand All @@ -31,19 +32,31 @@ describe('webjar/', () => {
it('should generate a jar file with the correct name', () => {
const { version } = packagePackageJson;

expect(existsSync(`./webjar-dist/hmrc-frontend-${version}.jar`)).toEqual(true);
expect(existsSync(`${jarPath}/${version}/hmrc-frontend-${version}.jar`)).toEqual(true);
});

it('should generate a pom file with the correct name', () => {
const { version } = packagePackageJson;

expect(existsSync(`./webjar-dist/hmrc-frontend-${version}.pom`)).toEqual(true);
expect(existsSync(`${jarPath}/${version}/hmrc-frontend-${version}.pom`)).toEqual(true);
});

it('should generate a pom hash file with the correct name', () => {
const { version } = packagePackageJson;

expect(existsSync(`${jarPath}/${version}/hmrc-frontend-${version}.pom.sha1`)).toEqual(true);
});

it('should generate a jar hash file with the correct name', () => {
const { version } = packagePackageJson;

expect(existsSync(`${jarPath}/${version}/hmrc-frontend-${version}.jar.sha1`)).toEqual(true);
});

it('should generate a jar file containing the correct files', async () => {
const { version } = packagePackageJson;

const zip = new AdmZip(`./webjar-dist/hmrc-frontend-${version}.jar`);
const zip = new AdmZip(`${jarPath}/${version}/hmrc-frontend-${version}.jar`);
const zipEntries = zip.getEntries().map((entry) => entry.entryName).filter((path) => !path.endsWith('/')).sort();
const webjarFiles = await getFiles('webjar');

Expand All @@ -58,6 +71,12 @@ describe('webjar/', () => {
expect(groupId).toEqual('uk.gov.hmrc.webjars');
});

it('should generate a POM hash file', async () => {
const groupId = await getHmrcPomField('/project/groupId');

expect(groupId).toEqual('uk.gov.hmrc.webjars');
});

it('should have the correct version', async () => {
const version = await getHmrcPomField('/project/version');

Expand Down
85 changes: 58 additions & 27 deletions tasks/gulp/webjar-helpers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const { src, series, dest } = require('gulp');
const fs = require('fs');
const {
existsSync, copyFileSync, readFileSync, writeFileSync, mkdirSync,
} = require('fs');
const del = require('del');
const rename = require('gulp-rename');
const { exec } = require('child_process');
const crypto = require('crypto');

const groupId = 'uk.gov.hmrc.webjars';

Expand All @@ -12,7 +14,13 @@ const webJarHelpers = ({
const mavenPath = `${webjarPath}/META-INF/maven/${groupId}/${artifactId}`;
const pomPath = `${mavenPath}/pom.xml`;

const getPackageJson = () => JSON.parse(fs.readFileSync(`${packagePath}/package.json`, 'utf8'));
const getPackageJson = () => JSON.parse(readFileSync(`${packagePath}/package.json`, 'utf8'));

const getArtifactDirectory = () => {
const { version } = getPackageJson();

return `${webjarDistPath}/${groupId.replace(/\./g, '/')}/${artifactId}/${version}`;
};

const clean = () => del([
webjarPath,
Expand All @@ -34,24 +42,43 @@ const webJarHelpers = ({
.pipe(dest(`${webjarPath}/META-INF/resources/webjars/${artifactId}/${version}/`));
};

const createJar = () => {
const { version } = getPackageJson();
const createArtifactDirectory = async () => {
const artifactPath = getArtifactDirectory();

if (!fs.existsSync(webjarDistPath)) {
fs.mkdirSync(webjarDistPath);
if (!existsSync(artifactPath)) {
mkdirSync(artifactPath, { recursive: true });
}
};

const getArtifactPathPrefix = () => {
const { version } = getPackageJson();

return `${getArtifactDirectory()}/${artifactId}-${version}`;
};

const getJarArtifactPath = () => `${getArtifactPathPrefix()}.jar`;

return exec(`jar cMf ${webjarDistPath}/${artifactId}-${version}.jar -C ${webjarPath} .`);
const createJar = () => exec(`jar cMf ${getJarArtifactPath()} -C ${webjarPath} .`);

const createShaFile = (path) => {
const fileBuffer = readFileSync(path);
const hashSum = crypto.createHash('sha1');
hashSum.update(fileBuffer);

writeFileSync(`${path}.sha1`, hashSum.digest('hex'));
};

const createJarSha = async () => {
createShaFile(getJarArtifactPath());
};

const createPomDirectory = (cb) => {
if (!fs.existsSync(mavenPath)) {
fs.mkdirSync(mavenPath, { recursive: true });
const createPomDirectory = async () => {
if (!existsSync(mavenPath)) {
mkdirSync(mavenPath, { recursive: true });
}
cb();
};

const createPom = (cb) => {
const createPom = async () => {
const { version, dependencies = {} } = getPackageJson();

const githubConnection = `${githubUrl}.git`;
Expand Down Expand Up @@ -93,28 +120,32 @@ const webJarHelpers = ({
</dependencies>
</project>`;

fs.writeFileSync(`${pomPath}`, pom);

cb();
writeFileSync(pomPath, pom);
};

const copyPom = () => {
const { version } = getPackageJson();
const getPomArtifactPath = () => `${getArtifactPathPrefix()}.pom`;

return src(pomPath)
.pipe(rename(`${artifactId}-${version}.pom`))
.pipe(dest(`${webjarDistPath}/`));
const copyPom = async () => {
copyFileSync(pomPath, getPomArtifactPath());
};

const createPomSha = async () => {
createShaFile(getPomArtifactPath());
};

const buildWebjar = series(
clean, copyWebjarPackageFiles, createPomDirectory, createPom, createJar, copyPom,
clean,
copyWebjarPackageFiles,
createPomDirectory,
createPom,
createArtifactDirectory,
createJar,
createJarSha,
copyPom,
createPomSha,
);

const publishLocalWebjar = () => {
const { version } = getPackageJson();

return exec(`mvn install:install-file -Dfile=${webjarDistPath}/${artifactId}-${version}.jar -DpomFile=${webjarDistPath}/${artifactId}-${version}.pom`);
};
const publishLocalWebjar = () => exec(`mvn install:install-file -Dfile=${getJarArtifactPath()} -DpomFile=${getPomArtifactPath()}`);

return {
buildWebjar,
Expand Down

0 comments on commit 7c22613

Please sign in to comment.