Skip to content

Commit

Permalink
Merge pull request #27 from Fgerthoffert/develop
Browse files Browse the repository at this point in the history
Refactored modules installation / deploy of modules
  • Loading branch information
Fgerthoffert authored Feb 5, 2020
2 parents 0e26d2d + 2d95b3c commit cd5c345
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/commands/manifest/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class ManifestCreate extends Command {
branch: 'master',
directory: '/tmp/',
id: 'ldap',
filepath: '/tmp/abcldap-4.0.0-SNAPSHOT.jar',
deploy: true,
},
{
type: 'asset',
Expand Down
11 changes: 9 additions & 2 deletions src/commands/manifest/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,20 @@ export default class ManifestRun extends Command {
await assetsFetch(job);
} else if (job.type === 'build') {
// eslint-disable-next-line no-await-in-loop
await buildModule(
const builtModules = await buildModule(
job.directory,
job.id,
job.branch,
job.repository,
job.filepath,
);
console.log(builtModules);
if (job.deploy === true) {
// eslint-disable-next-line max-depth
for (const jahiaModule of builtModules) {
// eslint-disable-next-line no-await-in-loop
await installModule(flags, jahiaModule);
}
}
} else if (job.type === 'module') {
// eslint-disable-next-line no-await-in-loop
await installModule(flags, job.filepath, job.id);
Expand Down
19 changes: 13 additions & 6 deletions src/commands/modules/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Command from '../../base';
import { exit } from '@oclif/errors';

import buildModule from '../../components/modules/build';
import installModule from '../../components/modules/install';

export default class ModulesBuild extends Command {
static description = 'Installs a module';
Expand Down Expand Up @@ -38,10 +39,10 @@ export default class ModulesBuild extends Command {
default: 'master',
description: 'Git repository branch',
}),
filpath: flags.string({
required: true,
default: '/tmp/abcldap-4.0.0-SNAPSHOT.jar',
description: 'Filename of the artifact built',
deploy: flags.string({
required: false,
default: 'true',
description: 'Trigger a deployment of the module',
}),
};

Expand All @@ -56,14 +57,20 @@ export default class ModulesBuild extends Command {
exit();
}

await buildModule(
const buildModules = await buildModule(
flags.directory,
flags.id,
flags.branch,
flags.repository,
flags.filpath,
);

if (JSON.parse(flags.deploy)) {
for (const jahiaModule of buildModules) {
// eslint-disable-next-line no-await-in-loop
await installModule(flags, jahiaModule);
}
}

const t1 = performance.now();
console.log(
'Total Exceution time: ' + Math.round(t1 - t0) + ' milliseconds.',
Expand Down
74 changes: 41 additions & 33 deletions src/components/modules/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,29 @@ import * as simpleGit from 'simple-git/promise';
import * as mvn from 'maven';
import * as xmljs from 'xml2js';

// https://gist.github.com/qwtel/fd82ab097cbe1db50ded9505f183ccb8
const getFiles = async (dir: string, searchStr: string) => {
let allEntries: Array<string> = [];
const dirContent = await fs.readdirSync(dir, { withFileTypes: true });
for (const dirEntry of dirContent) {
const filepath = path.resolve(dir, dirEntry.name);
if (dirEntry.isDirectory()) {
// eslint-disable-next-line no-await-in-loop
const newScan: Array<string> = await getFiles(filepath, searchStr);
allEntries = [...allEntries, ...newScan];
} else if (filepath.includes(searchStr)) {
allEntries.push(filepath);
}
}
return allEntries;
};

/* eslint max-params: ["error", 5] */
const buildModule = async (
directory: string,
id: string,
branch: string,
repository: string,
filpath: string,
) => {
const dstDir = path.join(directory, id);

Expand Down Expand Up @@ -60,6 +76,26 @@ const buildModule = async (
const pomStr = fs.readFileSync(pomXml);
const pomObj = await xmljs.parseStringPromise(pomStr);

// Some modules reference a set of submodules being built and that need to be imported into Jahia as well.
console.log(pomObj);
let profiles = [];
if (
pomObj.project.profiles !== undefined &&
pomObj.project.profiles[0] !== undefined &&
pomObj.project.profiles[0].profile
) {
const defaultProfile = pomObj.project.profiles[0].profile.find((p: any) =>
p.id.includes('default'),
);
if (
defaultProfile !== undefined &&
defaultProfile.modules[0] !== undefined &&
defaultProfile.modules[0].module !== undefined
) {
profiles = defaultProfile.modules[0].module;
}
}

let moduleVersion = null;
if (
pomObj.project === undefined ||
Expand All @@ -72,20 +108,6 @@ const buildModule = async (
moduleVersion = pomObj.project.version[0];
}

let artifactId = null;
if (
pomObj.project === undefined ||
pomObj.project.artifactId === undefined ||
pomObj.project.artifactId[0] === undefined
) {
console.log(
'ERROR: Unable to detect module artifact id from the pom.xml file',
);
exit();
} else {
artifactId = pomObj.project.artifactId[0];
}

// eslint-disable-next-line noImplicitAnyForClassMembers
const mvnProject = mvn.create({ cwd: dstDir });
// console.log(mvnProject);
Expand All @@ -96,25 +118,11 @@ const buildModule = async (
});
console.log('Build done');

const artifactSrc = path.join(
dstDir,
'target/',
artifactId + '-' + moduleVersion + '.jar',
);

if (!fs.existsSync(artifactSrc)) {
console.log(
'ERROR: Unable to detect the built artifact in the filesystem: ' +
artifactSrc,
);
exit();
}
// We blindly look for generated jar files
const jarFiles = await getFiles(dstDir, moduleVersion + '.jar');

const artifactDst = path.join(filpath);
console.log(
'Copying artifact file from: ' + artifactSrc + ' to ' + artifactDst,
);
fs.copyFileSync(artifactSrc, artifactDst);
console.log(jarFiles);
return jarFiles;
};

export default buildModule;
43 changes: 28 additions & 15 deletions src/components/modules/install/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,48 @@ import installMod from '../utils/install-module';
const installModule = async (
flags: ConfigFlags,
moduleFilepath: string,
moduleId: string,
moduleId?: string | undefined,
moduleVersion?: string | undefined,
) => {
const installedModules = await getModules(
flags.jahiaAdminUrl,
flags.jahiaAdminUsername,
flags.jahiaAdminUsername,
);
if (isInstalled(installedModules, moduleId, moduleVersion) === false) {
console.log('Module needs to be installed');
// ModuleId is undefined, we blindly push the module but don't check for proper installation
if (moduleId === undefined) {
console.log('Submitting: ' + moduleFilepath + ' for installation');
await installMod(
flags.jahiaAdminUrl,
flags.jahiaAdminUsername,
flags.jahiaAdminUsername,
moduleFilepath,
);
const checkInstalledModules = await getModules(
} else {
const installedModules = await getModules(
flags.jahiaAdminUrl,
flags.jahiaAdminUsername,
flags.jahiaAdminUsername,
);
if (isInstalled(checkInstalledModules, moduleId, moduleVersion) === true) {
console.log('Installation of the module successful');
if (isInstalled(installedModules, moduleId, moduleVersion) === false) {
console.log('Module needs to be installed');
await installMod(
flags.jahiaAdminUrl,
flags.jahiaAdminUsername,
flags.jahiaAdminUsername,
moduleFilepath,
);
const checkInstalledModules = await getModules(
flags.jahiaAdminUrl,
flags.jahiaAdminUsername,
flags.jahiaAdminUsername,
);
if (
isInstalled(checkInstalledModules, moduleId, moduleVersion) === true
) {
console.log('Installation of the module successful');
} else {
console.log('Error: Unable to install module');
exit();
}
} else {
console.log('Error: Unable to install module');
exit();
console.log('Module already installed');
}
} else {
console.log('Module already installed');
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/modules/utils/install-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const installModule = async (
console.log(error);
}
if (installResponse.data !== undefined) {
console.log(installResponse.data.bundleInfos);
console.log('Submission response: ', installResponse.data.bundleInfos);
return installResponse.data.bundleInfos;
}
return false;
Expand Down

0 comments on commit cd5c345

Please sign in to comment.