diff --git a/platformio/commands/platform.py b/platformio/commands/platform.py index cc6078d1..827b17ca 100644 --- a/platformio/commands/platform.py +++ b/platformio/commands/platform.py @@ -318,15 +318,6 @@ def platform_update( # pylint: disable=too-many-locals, too-many-arguments # -def init_platform(name, skip_default_package=True, auto_install=True): - try: - return PlatformFactory.new(name) - except UnknownPlatform: - if auto_install: - _platform_install([name], skip_default_package=skip_default_package) - return PlatformFactory.new(name) - - def _print_platforms(platforms): for platform in platforms: click.echo( diff --git a/platformio/commands/run/processor.py b/platformio/commands/run/processor.py index f6b184d6..8accc644 100644 --- a/platformio/commands/run/processor.py +++ b/platformio/commands/run/processor.py @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -from platformio.commands.platform import init_platform from platformio.commands.test.processor import CTX_META_TEST_RUNNING_NAME from platformio.package.commands.install import install_project_env_dependencies +from platformio.platform.factory import PlatformFactory from platformio.project.exception import UndefinedEnvPlatformError # pylint: disable=too-many-instance-attributes @@ -70,7 +70,7 @@ class EnvironmentProcessor(object): {"project_targets": build_targets}, ) - result = init_platform(self.options["platform"]).run( + result = PlatformFactory.new(self.options["platform"]).run( build_vars, build_targets, self.silent, self.verbose, self.jobs ) return result["returncode"] == 0 diff --git a/platformio/commands/test/command.py b/platformio/commands/test/command.py index ae511a9b..4dc6b13b 100644 --- a/platformio/commands/test/command.py +++ b/platformio/commands/test/command.py @@ -23,10 +23,10 @@ import click from tabulate import tabulate from platformio import app, exception, fs, util -from platformio.commands.platform import init_platform from platformio.commands.test.embedded import EmbeddedTestProcessor from platformio.commands.test.helpers import get_test_names from platformio.commands.test.native import NativeTestProcessor +from platformio.platform.factory import PlatformFactory from platformio.project.config import ProjectConfig @@ -142,7 +142,9 @@ def cli( # pylint: disable=redefined-builtin cls = ( EmbeddedTestProcessor if config.get(section, "platform") - and init_platform(config.get(section, "platform")).is_embedded() + and PlatformFactory.new( + config.get(section, "platform"), autoinstall=True + ).is_embedded() else NativeTestProcessor ) tp = cls( diff --git a/platformio/debug/command.py b/platformio/debug/command.py index e5865b96..278ca463 100644 --- a/platformio/debug/command.py +++ b/platformio/debug/command.py @@ -23,12 +23,12 @@ import subprocess import click from platformio import app, exception, fs, proc -from platformio.commands.platform import init_platform from platformio.compat import IS_WINDOWS from platformio.debug import helpers from platformio.debug.config.factory import DebugConfigFactory from platformio.debug.exception import DebugInvalidOptionsError from platformio.debug.process.gdb import GDBClientProcess +from platformio.platform.factory import PlatformFactory from platformio.project.config import ProjectConfig from platformio.project.exception import ProjectEnvsNotAvailableError from platformio.project.helpers import is_platformio_project @@ -96,7 +96,9 @@ def debug_cmd( with fs.cd(project_dir): debug_config = DebugConfigFactory.new( - init_platform(env_options["platform"]), project_config, env_name + PlatformFactory.new(env_options["platform"], autoinstall=True), + project_config, + env_name, ) if "--version" in __unprocessed: diff --git a/platformio/platform/factory.py b/platformio/platform/factory.py index f6cddfdb..f79d54dd 100644 --- a/platformio/platform/factory.py +++ b/platformio/platform/factory.py @@ -36,8 +36,9 @@ class PlatformFactory(object): raise UnknownPlatform(name) @classmethod - def new(cls, pkg_or_spec): + def new(cls, pkg_or_spec, autoinstall=False): # pylint: disable=import-outside-toplevel + from platformio.package.manager.platform import PlatformPackageManager platform_dir = None platform_name = None @@ -47,17 +48,20 @@ class PlatformFactory(object): elif isinstance(pkg_or_spec, (str, bytes)) and os.path.isdir(pkg_or_spec): platform_dir = pkg_or_spec else: - from platformio.package.manager.platform import PlatformPackageManager - pkg = PlatformPackageManager().get_package(pkg_or_spec) - if not pkg: - raise UnknownPlatform(pkg_or_spec) - platform_dir = pkg.path - platform_name = pkg.metadata.name + if pkg: + platform_dir = pkg.path + platform_name = pkg.metadata.name if not platform_dir or not os.path.isfile( os.path.join(platform_dir, "platform.json") ): + if autoinstall: + return cls.new( + PlatformPackageManager().install( + pkg_or_spec, skip_dependencies=True + ) + ) raise UnknownPlatform(pkg_or_spec) if not platform_name: