Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Move getProjectNameFromPath function to remove circular dependency
Browse files Browse the repository at this point in the history
Signed-off-by: James Wallis <[email protected]>
  • Loading branch information
James Wallis committed Aug 12, 2020
1 parent b468348 commit 797de34
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export async function createProject(req: ICreateProjectParams): Promise<ICreateP
return { "statusCode": 400, "error": { "msg": "projectID, projectType and location are required parameters" }};
}

const projectName = projectUtil.getProjectNameFromPath(projectLocation);
const projectName = utils.getProjectNameFromPath(projectLocation);

// create log storing directory for the project
logger.logInfo("Creating project logs directory");
Expand Down Expand Up @@ -658,7 +658,7 @@ export async function deleteProject(projectID: string): Promise<IDeleteProjectSu

const projectInfo: ProjectInfo = await getProjectInfoFromFile(projectMetadata.infoFile);
const projectLocation = projectInfo.location;
const projectName = projectUtil.getProjectNameFromPath(projectLocation);
const projectName = utils.getProjectNameFromPath(projectLocation);

logger.logProjectTrace("Retrieved project information for project " + projectMetadata.infoFile, projectID);
logger.logProjectTrace(JSON.stringify(projectInfo), projectID);
Expand Down Expand Up @@ -842,7 +842,7 @@ export async function projectDeletion(projectID: string): Promise<number> {
const projectMetadata = getProjectMetadataById(projectID);
const projectInfo: ProjectInfo = await getProjectInfoFromFile(projectMetadata.infoFile);
const projectLocation = projectInfo.location;
const projectName = projectUtil.getProjectNameFromPath(projectLocation);
const projectName = utils.getProjectNameFromPath(projectLocation);

logger.logProjectTrace("Retrieved project information for project " + projectMetadata.infoFile, projectID);
logger.logProjectTrace(JSON.stringify(projectInfo), projectID);
Expand Down Expand Up @@ -1154,7 +1154,7 @@ export function saveProjectInfo(projectID: string, projectInfo: ProjectInfo, sav
const projectJSON = JSON.stringify(projectInfo);
const infoFile = getProjectMetadataById(projectID).infoFile;
projectInfoCache[infoFile] = projectJSON;
const projectName = projectUtil.getProjectNameFromPath(projectInfo.location);
const projectName = utils.getProjectNameFromPath(projectInfo.location);
logger.logProjectTrace(JSON.stringify(projectInfoCache), projectID);
if (saveIntoJsonFile) {
fs.writeFile(infoFile, projectJSON, "utf8", (err) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { DockerProject } from "../projects/DockerProject";
import { ShellExtensionProject } from "../projects/ShellExtensionProject";
import { OdoExtensionProject } from "../projects/OdoExtensionProject";
import { ProjectInfo, ProjectCapabilities, defaultProjectCapabilities } from "../projects/Project";
import { getProjectNameFromPath } from "../projects/projectUtil";
import { workspaceConstants } from "../projects/constants";

export const DOCKER_TYPE = "docker";
Expand Down Expand Up @@ -165,7 +164,7 @@ export function getAllProjectTypes(): Array<string> {
async function getExtensionProjectHandler(projectInfo: ProjectInfo): Promise<any> {

const key = projectInfo.projectID;
const projectName = getProjectNameFromPath(projectInfo.location);
const projectName = utils.getProjectNameFromPath(projectInfo.location);
let handler = extensionProjectHandlers[key];

// is there an extension handler for the project?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { ProjectInfo, BuildLog, AppLog, ProjectCapabilities, defaultProjectCapab
import { Validator } from "./Validator";
import { IExtensionProject } from "../extensions/IExtensionProject";
import { IFileChangeEvent } from "../utils/fileChanges";
import { getProjectNameFromPath } from "../utils/utils";

// skeleton for the logs originated from the extension json
const logsOrigin: logHelper.ILogTypes = {
Expand Down Expand Up @@ -207,7 +208,7 @@ export class OdoExtensionProject implements IExtensionProject {
*/
getContainerName = async (projectID: string, projectLocation: string): Promise<string> => {
let podName: string = undefined;
const projectName: string = projectUtil.getProjectNameFromPath(projectLocation);
const projectName: string = getProjectNameFromPath(projectLocation);
const componentName: string = await projectUtil.getComponentName(projectName);
const args: string[] = [
projectLocation,
Expand Down Expand Up @@ -246,7 +247,7 @@ export class OdoExtensionProject implements IExtensionProject {
let appName: string = undefined;
const projectInfo: ProjectInfo = await projectUtil.getProjectInfo(projectID);
const projectLocation = projectInfo.location;
const projectName = projectUtil.getProjectNameFromPath(projectLocation);
const projectName = getProjectNameFromPath(projectLocation);
const args: string[] = [
projectLocation,
"",
Expand Down Expand Up @@ -284,7 +285,7 @@ export class OdoExtensionProject implements IExtensionProject {
let appPort: string = undefined;
const projectInfo: ProjectInfo = await projectUtil.getProjectInfo(projectID);
const projectLocation = projectInfo.location;
const projectName = projectUtil.getProjectNameFromPath(projectLocation);
const projectName = getProjectNameFromPath(projectLocation);
const componentName: string = await projectUtil.getComponentName(projectName);
const args: string[] = [
projectLocation,
Expand Down Expand Up @@ -323,7 +324,7 @@ export class OdoExtensionProject implements IExtensionProject {
let appBaseURL: string = undefined;
const projectInfo: ProjectInfo = await projectUtil.getProjectInfo(projectID);
const projectLocation = projectInfo.location;
const projectName = projectUtil.getProjectNameFromPath(projectLocation);
const projectName = getProjectNameFromPath(projectLocation);
const componentName: string = await projectUtil.getComponentName(projectName);
const args: string[] = [
projectLocation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import fs from "fs-extra";
import * as path from "path";

import * as projectUtil from "./projectUtil";
import { getProjectNameFromPath } from "../utils/utils";
import { Operation } from "./operation";
import { ProjectInfo, BuildLog, AppLog, ProjectCapabilities, defaultProjectCapabilities } from "./Project";
import { Validator } from "./Validator";
Expand Down Expand Up @@ -112,7 +113,7 @@ export class ShellExtensionProject implements IExtensionProject {
private setLanguage = async (projectInfo: ProjectInfo): Promise<void> => {

const logDir = await logHelper.getLogDir(
projectInfo.projectID, projectUtil.getProjectNameFromPath(projectInfo.location));
projectInfo.projectID, getProjectNameFromPath(projectInfo.location));

const args = [
projectInfo.location,
Expand Down
5 changes: 2 additions & 3 deletions src/pfe/file-watcher/server/src/projects/Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import * as io from "../utils/socket";
import * as locale from "../utils/locale";
import * as logger from "../utils/logger";
import { Operation } from "./operation";
import { getProjectNameFromPath } from "./projectUtil";

/**
* @class
Expand Down Expand Up @@ -58,7 +57,7 @@ export class Validator {
async validateRequiredFiles (requiredFiles: string[]): Promise<void> {
if (requiredFiles) {
const projectID = this.projectID;
const projectName = getProjectNameFromPath(this.location);
const projectName = utils.getProjectNameFromPath(this.location);
const OR_SPLIT = "|";

try {
Expand Down Expand Up @@ -180,7 +179,7 @@ export class Validator {
*/
sendResult(): void {
const projectID = this.projectID;
const projectName = getProjectNameFromPath(this.location);
const projectName = utils.getProjectNameFromPath(this.location);
logger.logProjectInfo("Sending validation result", projectID, projectName);
io.emitOnListener("projectValidated", this.result());
}
Expand Down
4 changes: 2 additions & 2 deletions src/pfe/file-watcher/server/src/projects/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const validate = async function(args: IProjectActionParams): Promise<{ op
"projectType": projectType,
"location": location
} as ProjectInfo;
const projectName = projectUtil.getProjectNameFromPath(location);
const projectName = utils.getProjectNameFromPath(location);
if (args.extensionID) {
projectInfo.extensionID = args.extensionID;
}
Expand Down Expand Up @@ -131,7 +131,7 @@ export const enableautobuild = async function (args: IProjectActionParams): Prom
async function enableAndBuild(projectInfo: ProjectInfo): Promise<void> {
const projectHandler = await projectExtensions.getProjectHandler(projectInfo);
const projectID = projectInfo.projectID;
const projectName = projectUtil.getProjectNameFromPath(projectInfo.location);
const projectName = utils.getProjectNameFromPath(projectInfo.location);

if (projectHandler.hasOwnProperty("setAutoBuild")) {
const operation = new Operation("enableautobuild", projectInfo);
Expand Down
46 changes: 14 additions & 32 deletions src/pfe/file-watcher/server/src/projects/projectUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export async function containerCreate(operation: Operation, script: string, comm
const event = "projectCreation";
const projectLocation = operation.projectInfo.location;
const projectID = operation.projectInfo.projectID;
const projectName = getProjectNameFromPath(projectLocation);
const projectName = utils.getProjectNameFromPath(projectLocation);
const projectType = operation.projectInfo.projectType;
if (projectList.indexOf(projectID) === -1)
projectList.push(projectID);
Expand Down Expand Up @@ -211,7 +211,7 @@ export async function containerUpdate(operation: Operation, script: string, comm

const projectLocation = operation.projectInfo.location;
const projectID = operation.projectInfo.projectID;
const projectName = getProjectNameFromPath(projectLocation);
const projectName = utils.getProjectNameFromPath(projectLocation);
const projectType = operation.projectInfo.projectType;
logger.logProjectInfo("Updating container for " + operation.projectInfo.projectType + " project " + projectLocation, projectID, projectName);
operation.containerName = await getContainerName(operation.projectInfo);
Expand Down Expand Up @@ -316,7 +316,7 @@ export async function containerUpdate(operation: Operation, script: string, comm
async function executeBuildScript(operation: Operation, script: string, args: Array<string>, event: string): Promise<void> {
const projectID = operation.projectInfo.projectID;
const projectLocation = operation.projectInfo.location;
const projectName = getProjectNameFromPath(projectLocation);
const projectName = utils.getProjectNameFromPath(projectLocation);
const projectInfo = {
operationId: operation.operationId,
projectID: operation.projectInfo.projectID
Expand Down Expand Up @@ -612,7 +612,7 @@ export async function getProjectMavenSettings(projectInfo: ProjectInfo): Promise
export async function getProjectLogs(projectInfo: ProjectInfo): Promise<ProjectLog> {
const projectID = projectInfo.projectID;
const projectLocation = projectInfo.location;
const projectName = getProjectNameFromPath(projectLocation);
const projectName = utils.getProjectNameFromPath(projectLocation);
const projectType = projectInfo.projectType;
const projectLogDir = await logHelper.getLogDir(projectID, projectName);
const logDirectory = path.join(projectConstants.projectsLogDir, projectLogDir);
Expand Down Expand Up @@ -655,7 +655,7 @@ export async function getProjectLogs(projectInfo: ProjectInfo): Promise<ProjectL
export async function containerDelete(projectInfo: ProjectInfo, script: string): Promise<void> {

const projectID = projectInfo.projectID;
const projectName = getProjectNameFromPath(projectInfo.location);
const projectName = utils.getProjectNameFromPath(projectInfo.location);
const containerName = await getContainerName(projectInfo);
const imagePushRegistry = projectInfo.deploymentRegistry;
logger.logProjectInfo("containerDelete: Kill running processes and remove container... ", projectID, projectName);
Expand Down Expand Up @@ -725,7 +725,7 @@ export function getLogName(projectID: string, projectLocation: string): string {
const hash = crypto.createHash("sha1", <TransformOptions>"utf8").update(projectLocation);

let logName = projectConstants.containerPrefix + projectID + "-" + hash.digest("hex");
const projectName = getProjectNameFromPath(projectLocation);
const projectName = utils.getProjectNameFromPath(projectLocation);

if (process.env.IN_K8 === "true" && logName.length > 53) {
logName = logName.substring(0, 53);
Expand All @@ -751,7 +751,7 @@ export function getDefaultContainerName(projectID: string, projectLocation: stri
}

// Sanitize project name to ensure project name only supports lower case letter and number
const projectNameOrigin: string = getProjectNameFromPath(projectLocation);
const projectNameOrigin: string = utils.getProjectNameFromPath(projectLocation);
const letterNumber: RegExp = /[A-Za-z0-9]/;
const upperCaseLetter: RegExp = /[A-Z]/;
const defaultProjectName: string = "cw";
Expand Down Expand Up @@ -1249,7 +1249,7 @@ export async function runScript(projectInfo: ProjectInfo, script: string, comman
const containerName = await getContainerName(projectInfo);
const logName = getLogName(projectInfo.projectID, projectInfo.location);
const logDir = await logHelper.getLogDir(projectInfo.projectID, projectInfo.projectName);
const projectName = getProjectNameFromPath(projectInfo.location);
const projectName = utils.getProjectNameFromPath(projectInfo.location);
let args = [projectInfo.location, LOCAL_WORKSPACE, projectID, command, containerName, String(projectInfo.autoBuildEnabled), logName, projectInfo.startMode,
projectInfo.debugPort, "NONE", logDir];

Expand Down Expand Up @@ -1290,7 +1290,7 @@ export async function buildAndRun(operation: Operation, command: string): Promis

const projectLocation = operation.projectInfo.location;
const projectID = operation.projectInfo.projectID;
const projectName = getProjectNameFromPath(projectLocation);
const projectName = utils.getProjectNameFromPath(projectLocation);

if (projectList.indexOf(projectID) === -1)
projectList.push(projectID);
Expand Down Expand Up @@ -1446,7 +1446,7 @@ export async function buildAndRun(operation: Operation, command: string): Promis
*/
async function containerBuildAndRun(event: string, buildInfo: BuildRequest, operation: Operation): Promise<void> {
const normalizedProjectLocation = path.resolve(buildInfo.projectLocation);
const projectName = getProjectNameFromPath(normalizedProjectLocation);
const projectName = utils.getProjectNameFromPath(normalizedProjectLocation);
const logDir = await logHelper.getLogDir(buildInfo.projectID, projectName);
const dockerBuildLog = path.resolve(buildInfo.projectLocation + "/../.logs/" + logDir, logHelper.buildLogs.dockerBuild + logHelper.logExtension);
if (process.env.IN_K8 === "true") {
Expand Down Expand Up @@ -1668,7 +1668,7 @@ async function containerBuildAndRun(event: string, buildInfo: BuildRequest, oper
*/
async function runLocalContainer(buildInfo: BuildRequest): Promise<void> {
const normalizedProjectLocation = path.resolve(buildInfo.projectLocation);
const projectName = getProjectNameFromPath(normalizedProjectLocation);
const projectName = utils.getProjectNameFromPath(normalizedProjectLocation);
const logDir = await logHelper.getLogDir(buildInfo.projectID, projectName);
const appLog = path.resolve(buildInfo.projectLocation + "/../.logs/" + logDir, logHelper.appLogs.app + logHelper.logExtension);
try {
Expand Down Expand Up @@ -1799,7 +1799,7 @@ export async function isApplicationPodUp(buildInfo: BuildRequest, projectName: s
export async function removeProject(projectInfo: ProjectInfo): Promise<void> {

const projectID = projectInfo.projectID;
const projectName = getProjectNameFromPath(projectInfo.location);
const projectName = utils.getProjectNameFromPath(projectInfo.location);
const containerName = await getContainerName(projectInfo);
logger.logProjectInfo("removeProject: Kill running processes and remove container... ", projectID, projectName);
logger.logProjectInfo("Project ID: " + projectInfo.projectID, projectID, projectName);
Expand Down Expand Up @@ -1903,7 +1903,7 @@ async function getPODInfoAndSendToPortal(operation: Operation, event: string = "
const projectInfo = operation.projectInfo;
const projectLocation = projectInfo.location;
const projectID = projectInfo.projectID;
const projectName = getProjectNameFromPath(projectLocation);
const projectName = utils.getProjectNameFromPath(projectLocation);
const keyValuePair: UpdateProjectInfoPair = {
key: "buildRequest",
value: false
Expand Down Expand Up @@ -2203,26 +2203,8 @@ export async function updateDetailedAppStatus(projectID: string, ip: string, por
export async function exposeOverIngress(projectInfo: ProjectInfo, appPort?: number): Promise<void> {
if (process.env.IN_K8) {
const projectID = projectInfo.projectID;
const projectName = getProjectNameFromPath(projectInfo.location);
const projectName = utils.getProjectNameFromPath(projectInfo.location);
projectInfo.appBaseURL = await kubeutil.exposeOverIngress(projectID, projectName, projectInfo.isHttps, appPort, projectInfo.appBaseURL);
await projectsController.saveProjectInfo(projectID, projectInfo, true);
}
}

/**
* Get the projectName from a path
*
* @param location The project's location
*/
export function getProjectNameFromPath(location: string): string {
const splitPaths = location.split("/");
const cwIndex = splitPaths.indexOf("codewind-workspace");
if (cwIndex === -1) {
logger.logError("Unable to get project name from path: codewind-workspace isn't in the path");
// Fall back to old method if codewind-workspace isn't in the path
return path.basename(location);
}
// Project name is the directory after codewind-workspace
const projectName = splitPaths[cwIndex + 1];
return projectName;
}
2 changes: 1 addition & 1 deletion src/pfe/file-watcher/server/src/utils/kubeutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import * as processManager from "./processManager";
import { ProcessResult } from "./processManager";
import { ProjectInfo } from "../projects/Project";
import * as logHelper from "../projects/logHelper";
import { getProjectNameFromPath } from "../projects/projectUtil";
import { getProjectNameFromPath } from "./utils";

const k8s = require("@kubernetes/client-node"); // tslint:disable-line:no-require-imports

Expand Down
2 changes: 1 addition & 1 deletion src/pfe/file-watcher/server/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import * as path from "path";
import { promisify } from "util";
import * as constants from "../projects/constants";
import * as stackTrace from "stack-trace";
import { getProjectNameFromPath } from "../projects/projectUtil";
import { getProjectNameFromPath } from "./utils";
const chalk = require("chalk"); // tslint:disable-line:no-require-imports

const GENERAL_LOG_FILE_NAME = "Turbine.log";
Expand Down
19 changes: 19 additions & 0 deletions src/pfe/file-watcher/server/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as fs from "fs";
import * as util from "util";
import * as logger from "./logger";
import * as fse from "fs-extra";
import * as path from "path";

const promisify = util.promisify;
const accessAsync = promisify(fs.access);
Expand Down Expand Up @@ -156,3 +157,21 @@ export async function asyncReadDir(dir: string): Promise<Array<string>> {
return undefined;
}
}

/**
* Get the projectName from a path
*
* @param location The project's location
*/
export function getProjectNameFromPath(location: string): string {
const splitPaths = location.split("/");
const cwIndex = splitPaths.indexOf("codewind-workspace");
if (cwIndex === -1) {
logger.logError("Unable to get project name from path: codewind-workspace isn't in the path");
// Fall back to old method if codewind-workspace isn't in the path
return path.basename(location);
}
// Project name is the directory after codewind-workspace
const projectName = splitPaths[cwIndex + 1];
return projectName;
}
Loading

0 comments on commit 797de34

Please sign in to comment.