blob: 9fada5df91913020db28bf94e644e65683904104 [file] [log] [blame]
George Burgess IV18af5632022-08-30 14:10:53 -07001#!/usr/bin/env python3
2# Copyright 2022 The ChromiumOS Authors.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Populates the top-level Cargo.toml with all necessary workspace entries."""
6
7import logging
8import os
9from pathlib import Path
10import sys
11from typing import List
12
13WORKSPACE_FILE_HEADER = """\
14# Copyright 2022 The ChromiumOS Authors.
15# Use of this source code is governed by a BSD-style license that can be
16# found in the LICENSE file.
17#
18# !! Autogenerated by `populate-workspace.py`; please don't edit. !!
19
20"""
21
22
23def main(argv: List[str]):
24 logging.basicConfig(
George Burgess IV7dffc252022-08-31 14:37:01 -070025 format=">> %(asctime)s: %(levelname)s: %(filename)s:%(lineno)d: "
26 "%(message)s",
George Burgess IV18af5632022-08-30 14:10:53 -070027 level=logging.INFO,
28 )
29
30 projects_dir = Path(__file__).resolve().parent
31 projects = []
32 for dir_path, subdirs, files in os.walk(projects_dir):
George Burgess IV7dffc252022-08-31 14:37:01 -070033 if "Cargo.toml" not in files:
George Burgess IV18af5632022-08-30 14:10:53 -070034 continue
35
36 dir_path = Path(dir_path)
37 if dir_path == projects_dir:
38 continue
39
40 projects.append(dir_path.relative_to(projects_dir))
41
42 # It's a waste to descend into src directories.
George Burgess IV7dffc252022-08-31 14:37:01 -070043 if "src" in subdirs:
44 del subdirs[subdirs.index("src")]
George Burgess IV18af5632022-08-30 14:10:53 -070045 else:
46 # ...Though if src/ doesn't exist, Cargo will get confused.
47 # Synthesize one with a nop lib.rs.
George Burgess IV7dffc252022-08-31 14:37:01 -070048 src = dir_path / "src"
George Burgess IV18af5632022-08-30 14:10:53 -070049 src.mkdir()
George Burgess IV7dffc252022-08-31 14:37:01 -070050 (src / "lib.rs").write_bytes(b"")
51 logging.info("Synthesized src/lib.rs for %s", dir_path)
George Burgess IV18af5632022-08-30 14:10:53 -070052
George Burgess IV7dffc252022-08-31 14:37:01 -070053 assert projects, f"No projects found under {projects_dir}"
George Burgess IV18af5632022-08-30 14:10:53 -070054 projects.sort()
55
George Burgess IV7dffc252022-08-31 14:37:01 -070056 logging.info("Identified %d projects", len(projects))
George Burgess IV18af5632022-08-30 14:10:53 -070057
George Burgess IV7dffc252022-08-31 14:37:01 -070058 workspace_toml_file = projects_dir / "Cargo.toml"
59 with workspace_toml_file.open("w", encoding="utf-8") as f:
George Burgess IV18af5632022-08-30 14:10:53 -070060 f.write(WORKSPACE_FILE_HEADER)
George Burgess IV7dffc252022-08-31 14:37:01 -070061 # The `toml` crate writes this as a massive line, which is hard to
62 # read. Since this is simple to write, write it directly.
George Burgess IV18af5632022-08-30 14:10:53 -070063 # TODO(b/242668603): find a toml crate with prettier formatting
64 f.write('[workspace]\nmembers = [\n')
65 for project in projects:
66 project = str(project)
George Burgess IV7dffc252022-08-31 14:37:01 -070067 assert '"' not in project and "\\" not in project, project
George Burgess IV18af5632022-08-30 14:10:53 -070068 f.write(f' "{project}",\n')
George Burgess IV7dffc252022-08-31 14:37:01 -070069 f.write("]")
George Burgess IV18af5632022-08-30 14:10:53 -070070
George Burgess IV7dffc252022-08-31 14:37:01 -070071 logging.info("Workspace Cargo.toml successfully written.")
George Burgess IV18af5632022-08-30 14:10:53 -070072
73
George Burgess IV7dffc252022-08-31 14:37:01 -070074if __name__ == "__main__":
George Burgess IV18af5632022-08-30 14:10:53 -070075 sys.exit(main(sys.argv[1:]))