import { DirType } from "./types.js"; import { ExternalComponentResource } from "./ExternalComponentResource.js"; export class AngloResource { protected _url = ""; protected _baseUrl = ""; protected _fullUrl = ""; protected _bareComponentUrl = ""; protected _repository = ""; protected _repositoryUrl = ""; protected _componentFolder?: string; protected _implementationUrl = ""; protected _dirType: DirType = DirType.trunk; protected _version?: string; protected _componentName = ""; constructor(baseUrl: string, url: string) { this.parseUrl(baseUrl, url); } baseUrl(): string { return this._baseUrl; } fullUrl(): string { return this._fullUrl; } implementationUrl(): string { return this._implementationUrl; } repositoryName(): string { return this._repository; } repositoryUrl(): string { return this._repositoryUrl; } dirType(): DirType { return this._dirType; } dirTypeAndVersion(): string { if ( (this._dirType === DirType.tags || this._dirType === DirType.branches) && this._version ) { return `${this._dirType}/${this._version}`; } return `${this._dirType}`; } url(): string { return this._url; } version(): string | undefined { return this._version; } private parseUrl(baseUrl: string, url: string): void { this._url = url; this._baseUrl = baseUrl; this._fullUrl = `${baseUrl}${url}`; const regex = /^(https?:\/\/[^]+\/svn\/)/; //const regex = /^(https?:\/\/[^\/]+\/svn\/)/; const match = regex.exec(this._fullUrl); if (!(match && match.length > 1)) { throw new Error("Invalid URL"); } const segments = this._fullUrl.split("/"); const svnIndex = segments.indexOf("svn"); const trunkTagBranchIndex = segments.findIndex( (segment, index) => index > svnIndex && (segment === "trunk" || segment === "tags" || segment === "branches") ); if (svnIndex !== -1 && svnIndex + 1 < segments.length) { this._repository = segments[svnIndex + 1]; this._repositoryUrl = `${this._baseUrl}/${this._repository}`; if (trunkTagBranchIndex !== -1 && trunkTagBranchIndex - 1 > svnIndex) { const nextSegment = segments[trunkTagBranchIndex - 1]; if ( nextSegment !== "trunk" && nextSegment !== "tags" && nextSegment !== "branches" ) { if (this instanceof ExternalComponentResource) { this._componentFolder = nextSegment; } } } if (trunkTagBranchIndex !== -1 && trunkTagBranchIndex < segments.length) { this._dirType = segments[trunkTagBranchIndex] as DirType; if (this._dirType === "tags" || this._dirType === "branches") { if (trunkTagBranchIndex + 1 < segments.length) { this._version = this.normalizeVersion( segments[trunkTagBranchIndex + 1] ); if (trunkTagBranchIndex + 2 < segments.length) { this._componentName = segments[trunkTagBranchIndex + 2]; } } } else if (trunkTagBranchIndex + 1 < segments.length) { this._componentName = segments[trunkTagBranchIndex + 1]; } } this._implementationUrl = segments .slice(0, trunkTagBranchIndex + (this._version === undefined ? 1 : 2)) .join("/"); this._bareComponentUrl = segments .slice(0, trunkTagBranchIndex) .join("/") .replaceAll(baseUrl, ""); } else { throw new Error("Invalid URL"); } } private normalizeVersion(version: string): string { // when a version has more than 3 segments (which is an illegal semver), prefix the remaining segments with -, ie. 1.2.3-4 const segments = version.split("."); if (segments.length <= 3) { return version; } else { const lastSegments = segments.slice(2); // Join the last segments together return `${segments[0]}.${segments[1]}.${lastSegments.join("-")}`; } } }