diff --git a/HISTORY.rst b/HISTORY.rst index 99fd4a5929..b89a276045 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -5,6 +5,9 @@ Release History --------- * Implemented `platformio settings `_ command +* Improved `platformio init `_ command. + Added new option ``--project-dir`` where you can specify another path to + directory where new project will be initialized (`issue #31 `_) * Added *Migration Manager* which simplifies process with upgrading to a major release * Added *Telemetry Service* which should help us make *PlatformIO* better diff --git a/docs/userguide/cmd_init.rst b/docs/userguide/cmd_init.rst index 78ef1164f6..280c6900bf 100644 --- a/docs/userguide/cmd_init.rst +++ b/docs/userguide/cmd_init.rst @@ -10,7 +10,7 @@ Usage .. code-block:: bash - platformio init + platformio init [OPTIONS] Description @@ -21,22 +21,50 @@ Initialize new PlatformIO based project. This command will create: -* ``.pioenvs`` - a temporary working directory. -* ``lib`` - a directory for project specific libraries. PlatformIO will - compile them to static libraries and link to executable file -* ``src`` - a source directory. Put your source code here. * :ref:`projectconf` +* ``src`` - a source directory. Put your source code here +* ``lib`` - a directory for the project specific libraries. PlatformIO will + compile them to static libraries and link to executable file + +Options +------- + +.. option:: + --project-dir, -d + +Specified path to the directory where *PlatformIO* will initialize new project. Examples -------- +1. Create new project in the current working directory + .. code-block:: bash - # Change directory to the future project - $ cd /path/to/empty/directory $ platformio init - Project has been initialized! - Please put your source code to `src` directory, external libraries to `lib` - and setup environments in `platformio.ini` file. - Then process project with `platformio run` command. + The current working directory *** will be used for the new project. + You can specify another project directory via + `platformio init -d %PATH_TO_PROJECT_DIR%` command. + + The next files/directories will be created in *** + platformio.ini - Project Configuration File + src - a source directory. Put your source code here + lib - a directory for the project specific libraries + Do you want to continue? [y/N]: y + Project has been successfully initialized! + Now you can process it with `platformio run` command. + + +2. Create new project in the specified directory + +.. code-block:: bash + + $ platformio init -d %PATH_TO_DIR% + The next files/directories will be created in *** + platformio.ini - Project Configuration File + src - a source directory. Put your source code here + lib - a directory for the project specific libraries + Do you want to continue? [y/N]: y + Project has been successfully initialized! + Now you can process it with `platformio run` command. diff --git a/platformio/commands/init.py b/platformio/commands/init.py index 418783b2ca..8c540e54d5 100644 --- a/platformio/commands/init.py +++ b/platformio/commands/init.py @@ -1,30 +1,58 @@ # Copyright (C) Ivan Kravets # See LICENSE for details. -from os import makedirs +from os import getcwd, makedirs from os.path import isdir, isfile, join from shutil import copyfile -from click import command, secho +import click from platformio.exception import ProjectInitialized from platformio.util import get_source_dir -@command("init", short_help="Initialize new PlatformIO based project") -def cli(): +@click.command("init", short_help="Initialize new PlatformIO based project") +@click.option("--project-dir", "-d", default=getcwd(), + type=click.Path(exists=True, file_okay=False, dir_okay=True, + writable=True, resolve_path=True)) +def cli(project_dir): - if isfile("platformio.ini") and isdir("src"): + project_file = join(project_dir, "platformio.ini") + src_dir = join(project_dir, "src") + lib_dir = join(project_dir, "lib") + if all([isfile(project_file), isdir(src_dir), isdir(lib_dir)]): raise ProjectInitialized() - for d in ("lib", "src"): - if not isdir(d): - makedirs(d) - if not isfile("platformio.ini"): - copyfile(join(get_source_dir(), "projectconftpl.ini"), - "platformio.ini") - secho("Project has been initialized!\n" - "Please put your source code to `src` directory, " - "external libraries to `lib` and " - "setup environments in `platformio.ini` file.\n" - "Then process project with `platformio run` command.", - fg="green") + + if project_dir == getcwd(): + click.secho("The current working directory", fg="yellow", nl=False) + click.secho(" %s " % project_dir, fg="blue", nl=False) + click.secho( + "will be used for the new project.\n" + "You can specify another project directory via\n" + "`platformio init -d %PATH_TO_PROJECT_DIR%` command.\n", + fg="yellow" + ) + + click.echo("The next files/directories will be created in %s" % + click.style(project_dir, fg="blue")) + click.echo("%s - Project Configuration File" % + click.style("platformio.ini", fg="cyan")) + click.echo("%s - a source directory. Put your source code here" % + click.style("src", fg="cyan")) + click.echo("%s - a directory for the project specific libraries" % + click.style("lib", fg="cyan")) + + if click.confirm("Do you want to continue?"): + for d in (src_dir, lib_dir): + if not isdir(d): + makedirs(d) + if not isfile(project_file): + copyfile(join(get_source_dir(), "projectconftpl.ini"), + project_file) + click.secho( + "Project has been successfully initialized!\n" + "Now you can process it with `platformio run` command.", + fg="green" + ) + else: + click.secho("Aborted by user", fg="red")