def _patch_cargo(self): cargo_path = path.join(self.directory, TemplateRust.CARGO_TOML) cargo_file = CargoFile(cargo_path) cargo_file.package_name = self.project_name cargo_file.version = "0.0.1" cargo_file.authors = ["you"] cargo_file.edition = "2018" for dependency in cargo_file.get_dependencies().values(): del dependency["path"] for dependency in cargo_file.get_dev_dependencies().values(): del dependency["path"] cargo_file.save()
def _patch_cargo_wasm(self): cargo_path = path.join(self.directory, "wasm", TemplateRust.CARGO_TOML) cargo_file = CargoFile(cargo_path) cargo_file.package_name = f"{self.project_name}-wasm" cargo_file.version = "0.0.1" cargo_file.authors = ["you"] cargo_file.edition = "2018" for dependency in cargo_file.get_dependencies().values(): del dependency["path"] cargo_file.get_dependency(self.template_name)["path"] = ".." cargo_file.save() self._replace_in_files([cargo_path], [(f"[dependencies.{self.template_name}]", f"[dependencies.{self.project_name}]")])
def _patch_cargo_debug(self): cargo_debug_path = path.join(self.directory, "debug", TemplateRust.CARGO_TOML) if not path.exists(cargo_debug_path): return cargo_file_debug = CargoFile(cargo_debug_path) cargo_file_debug.package_name = f"{self.project_name}-debug" cargo_file_debug.version = "0.0.1" cargo_file_debug.authors = ["you"] cargo_file_debug.edition = "2018" for dependency in cargo_file_debug.get_dependencies().values(): del dependency["path"] cargo_file_debug.get_dependency(self.template_name)["path"] = ".." cargo_file_debug.save() self._replace_in_files([cargo_debug_path], [(f"[dependencies.{self.template_name}]", f"[dependencies.{self.project_name}]")])
def _replace_placeholders(self): rust_module = dependencies.get_module_by_key("rust") self.rust_directory = rust_module.get_directory() self.rust_bin_directory = path.join(self.rust_directory, "bin") cargo_path = path.join(self.directory, "Cargo.toml") cargo_debug_path = path.join(self.directory, "debug", "Cargo.toml") launch_path = path.join(self.directory, ".vscode", "launch.json") tasks_path = path.join(self.directory, ".vscode", "tasks.json") debug_main_path = path.join(self.directory, "debug", "src", "main.rs") test_paths = utils.list_files(path.join(self.directory, "test")) logger.info("Updating cargo files...") cargo_file = CargoFile(cargo_path) cargo_file.package_name = self.project_name cargo_file.bin_name = self.project_name cargo_file.version = "0.0.1" cargo_file.authors = ["you"] cargo_file.edition = "2018" cargo_file.save() cargo_file_debug = CargoFile(cargo_debug_path) cargo_file_debug.package_name = f"{self.project_name}-debug" cargo_file_debug.version = "0.0.1" cargo_file_debug.authors = ["you"] cargo_file_debug.edition = "2018" cargo_file_debug.save() logger.info("Applying replacements...") self._replace_in_files([launch_path, tasks_path], [("{{PROJECT_NAME}}", self.project_name), ("{{PATH_RUST_BIN}}", self.rust_bin_directory), ("{{RUSTUP_HOME}}", self.rust_directory), ("{{CARGO_HOME}}", self.rust_directory)]) self._replace_in_files( [debug_main_path], [ # Example "use simple_coin::*" to "use my_project::*" (f"use {self.template_name.replace('-', '_')}::*", f"use {self.project_name.replace('-', '_')}::*") ]) self._replace_in_files([cargo_debug_path], [(f"[dependencies.{self.template_name}]", f"[dependencies.{self.project_name}]")]) self._replace_in_files(test_paths, [(f"adder.wasm", f"myadder.wasm")])
def _patch_cargo_wasm(self): cargo_path = path.join(self.directory, "wasm", TemplateRust.CARGO_TOML) cargo_file = CargoFile(cargo_path) cargo_file.package_name = f"{self.project_name}-wasm" cargo_file.version = "0.0.1" cargo_file.authors = ["you"] cargo_file.edition = "2018" for dependency in cargo_file.get_dependencies().values(): del dependency["path"] # Currently, the following logic is not really needed (we don't have dev-dependencies in wasm/Cargo.toml): for dependency in cargo_file.get_dev_dependencies().values(): del dependency["path"] # Patch the path towards the project crate (one folder above): cargo_file.get_dependency(self.template_name)["path"] = ".." cargo_file.save() self._replace_in_files([cargo_path], [(f"[dependencies.{self.template_name}]", f"[dependencies.{self.project_name}]")])
def _patch_cargo_abi(self): cargo_path = path.join(self.directory, "abi", TemplateRust.CARGO_TOML) if not path.isfile(cargo_path): return cargo_file = CargoFile(cargo_path) cargo_file.package_name = f"{self.project_name}-abi" cargo_file.version = "0.0.0" cargo_file.authors = ["you"] cargo_file.edition = "2018" cargo_file.publish = False for dependency in cargo_file.get_dependencies().values(): del dependency["path"] for dependency in cargo_file.get_dev_dependencies().values(): del dependency["path"] # Patch the path towards the project crate (one folder above): cargo_file.get_dependency(self.template_name)["path"] = ".." cargo_file.save() self._replace_in_files([cargo_path], [(f"[dependencies.{self.template_name}]", f"[dependencies.{self.project_name}]")])