Skip to content
This repository was archived by the owner on Dec 19, 2020. It is now read-only.
Open
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
31 changes: 31 additions & 0 deletions lib/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
from lib.common import WitUserError, error
from lib.gitrepo import GitRepo
import re
from glob import glob
import pathlib

log = getLogger()

Expand Down Expand Up @@ -71,6 +73,11 @@ def main() -> None:

subparsers.add_parser('fetch-scala', help='Fetch dependencies for Scala projects')

clean_parser = subparsers.add_parser('clean', help=('delete non-package subdirectories '
'in your workspace'))
clean_parser.add_argument('--dry-run', dest='dry_run', action="store_true",
help='do a dry run, showing what would have been deleted')

args = parser.parse_args()
if args.verbose > 3:
log.setLevel('TRACE')
Expand Down Expand Up @@ -129,6 +136,9 @@ def main() -> None:

elif args.command == 'fetch-scala':
fetch_scala(ws, args, agg=False)

elif args.command == 'clean':
clean(ws, args)
except WitUserError as e:
error(e)

Expand Down Expand Up @@ -319,3 +329,24 @@ def version() -> None:
version = re.sub(r"^v", "", version)

print("wit {}".format(version))


def clean(ws, args) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment in the wit foreach PR -- Unfortunately wit-manifest.json isn't a reliable indicator of package-ness. Iterating through the packages from the wit resolution algorithm is the safest way to do this.

subdirs_str = glob(str(ws.path)+'/*/')
subdirs = [pathlib.PosixPath(f) for f in subdirs_str]

pkg_folders = [pkg.get_path() for pkg in ws.lock.packages]

non_pkg_folders = [f for f in subdirs if f not in pkg_folders]
if not non_pkg_folders:
log.info("No non-package directories found.")
return

if args.dry_run:
log.info("Skipping execution because of --dry-run. Would have run:")

for folder in non_pkg_folders:
if args.dry_run:
log.info("rm -rf {}".format(folder))
else:
folder.rmdir()