Skip to content

Commit

Permalink
refactor: get target project (#231)
Browse files Browse the repository at this point in the history
* refactor: get target project and extract path formatting
  • Loading branch information
ola magdziarek committed Sep 27, 2022
1 parent 0ed9feb commit 9253f07
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 35 deletions.
42 changes: 11 additions & 31 deletions lib/index.ts
Expand Up @@ -286,12 +286,10 @@ async function getAllDepsOneProject(
);
const allSubProjectNames = allProjectDeps.allSubProjectNames;

return subProject
? getSubProject(subProject, allProjectDeps, allSubProjectNames)
: getRootProject(allProjectDeps, allSubProjectNames);
return getTargetProject(subProject, allProjectDeps, allSubProjectNames);
}

function getSubProject(
function getTargetProject(
subProject: string,
allProjectDeps,
allSubProjectNames: string[],
Expand All @@ -300,36 +298,18 @@ function getSubProject(
allSubProjectNames: string[];
gradleProjectName: string;
versionBuildInfo: VersionBuildInfo;
} {
if (!allProjectDeps.projects || !allProjectDeps.projects[subProject]) {
throw new MissingSubProjectError(subProject, Object.keys(allProjectDeps));
}

const { depGraph } = allProjectDeps.projects[subProject];
const { versionBuildInfo } = allProjectDeps;
const gradleProjectName = `${allProjectDeps.defaultProject}/${subProject}`;

return {
depGraph,
allSubProjectNames,
gradleProjectName,
versionBuildInfo,
};
}

function getRootProject(
allProjectDeps,
allSubProjectNames: string[],
): {
depGraph: DepGraph;
allSubProjectNames: string[];
gradleProjectName: string;
versionBuildInfo: VersionBuildInfo;
} {
const { projects, defaultProject, versionBuildInfo } = allProjectDeps;
const { depGraph } = projects[defaultProject];
const targetProject = subProject || defaultProject;
let gradleProjectName = defaultProject;
if (subProject) {
if (!allProjectDeps.projects || !allProjectDeps.projects[subProject]) {
throw new MissingSubProjectError(subProject, Object.keys(allProjectDeps));
}
gradleProjectName = `${allProjectDeps.defaultProject}/${subProject}`;
}

const gradleProjectName = defaultProject;
const { depGraph } = projects[targetProject];

return {
depGraph,
Expand Down
11 changes: 7 additions & 4 deletions lib/init.gradle
Expand Up @@ -248,6 +248,9 @@ def findProjectConfigs(proj, confNameFilter, confAttrSpec) {
return resolvable
}

String formatPath(path) {
return path.replace(':', '/').replaceAll(~/(^\/+?)|(\/+$)/, '')
}

// We are attaching this task to every project, as this is the only reliable way to run it
// when we start with a subproject build.gradle. As a consequence, we need to make sure we
Expand Down Expand Up @@ -309,7 +312,7 @@ allprojects { Project currProj ->
.each({
def projKey = it.name
if (seenSubprojects.get(projKey)) {
projKey = it.path.replace(':', '/').replaceAll(~/(^\/+?)|(\/+$)/, '')
projKey = formatPath(it.path)
if (projKey == "") {
projKey = defaultProjectName
}
Expand Down Expand Up @@ -357,7 +360,7 @@ allprojects { Project currProj ->
def projKey = proj.name
if (projectsDict.get(projKey)) {
debugLog("The deps dict already has a project with key `$projKey'; using the full path")
projKey = proj.path.replace(':', '/').replaceAll(~/(^\/+?)|(\/+$)/, '')
projKey = formatPath(proj.path)
if (projKey == "") {
debugLog("project path is empty (proj.path=$proj.path)! will use defaultProjectName=$defaultProjectName")
projKey = defaultProjectName
Expand Down Expand Up @@ -434,7 +437,7 @@ allprojects { Project currProj ->
.each({
def projKey = it.name
if (seenSubprojects.get(projKey)) {
projKey = it.path.replace(':', '/').replaceAll(~/(^\/+?)|(\/+$)/, '')
projKey = formatPath(it.path)
if (projKey == "") {
projKey = defaultProjectName
}
Expand Down Expand Up @@ -486,7 +489,7 @@ allprojects { Project currProj ->
def projKey = proj.name
if (projectsDict.get(projKey)) {
debugLog("The deps dict already has a project with key `$projKey'; using the full path")
projKey = proj.path.replace(':', '/').replaceAll(~/(^\/+?)|(\/+$)/, '')
projKey = formatPath(proj.path)
if (projKey == "") {
debugLog("project path is empty (proj.path=$proj.path)! will use defaultProjectName=$defaultProjectName")
projKey = defaultProjectName
Expand Down

0 comments on commit 9253f07

Please sign in to comment.