Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

workspace clean proposal #17763

Draft
wants to merge 8 commits into
base: develop2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions conan/api/subapi/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ def remove(self, path):
self._check_ws()
return self._ws.remove(path)

def clean(self):
self._check_ws()
return self._ws.clean()

def info(self):
self._check_ws()
return {"name": self.name,
Expand Down
9 changes: 9 additions & 0 deletions conan/cli/commands/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ def workspace_build(conan_api: ConanAPI, parser, subparser, *args):
conan_api.local.build(conanfile)


@conan_subcommand()
def workspace_clean(conan_api: ConanAPI, parser, subparser, *args):
"""
Clean the temporary build folders when possible
"""
parser.parse_args(*args)
conan_api.workspace.clean()


@conan_command(group="Consumer")
def workspace(conan_api, parser, *args): # noqa
"""
Expand Down
13 changes: 12 additions & 1 deletion conan/internal/model/workspace.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import shutil

import yaml

Expand All @@ -19,7 +20,7 @@ def __init__(self, folder, conan_api):
self.output = ConanOutput(scope=self.name())

def name(self):
return os.path.basename(self.folder)
return self.conan_data.get("name") or os.path.basename(self.folder)

def _conan_load_data(self):
data_path = os.path.join(self.folder, "conanws.yml")
Expand Down Expand Up @@ -60,6 +61,16 @@ def remove(self, path):
save(os.path.join(self.folder, "conanws.yml"), yaml.dump(self.conan_data))
return found_ref

def clean(self):
for ref, info in self.conan_data.get("editables", {}).items():
if info.get("output_folder"):
of = os.path.join(self.folder, info["output_folder"])
try:
self.output.info(f"Removing {ref} output folder: {of}")
shutil.rmtree(of)
except OSError as e:
self.output.warning(f"Error removing {ref} output folder: {str(e)}")

def _conan_rel_path(self, path):
if path is None:
return None
Expand Down
21 changes: 21 additions & 0 deletions test/integration/workspace/test_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,24 @@ def test_new():
assert "liba/0.1" in c.out
assert "libb/0.1" in c.out
assert "app1/0.1" in c.out


class TestClean:
def test_clean(self):
# Using cmake_layout, we can clean the build folders
c = TestClient()
c.save({"conanws.yml": "name: my_workspace"})
pkga = GenConanfile("pkga", "0.1").with_settings("build_type")
pkgb = GenConanfile("pkgb", "0.1").with_requires("pkga/0.1").with_settings("build_type")
c.save({"pkga/conanfile.py": pkga ,
"pkgb/conanfile.py": pkgb})
c.run("workspace add pkga -of=build/pkga")
c.run("workspace add pkgb -of=build/pkgb --product")
c.run("workspace build")
assert os.path.exists(os.path.join(c.current_folder, "build", "pkga"))
assert os.path.exists(os.path.join(c.current_folder, "build", "pkgb"))
c.run("workspace clean")
assert "my_workspace: Removing pkga/0.1 output folder" in c.out
assert "my_workspace: Removing pkgb/0.1 output folder" in c.out
assert not os.path.exists(os.path.join(c.current_folder, "build", "pkga"))
assert not os.path.exists(os.path.join(c.current_folder, "build", "pkgb"))
Loading