Improve "init" command. Resolve #31

This commit is contained in:
Ivan Kravets
2014-11-30 18:14:18 +02:00
parent a82d09e816
commit 3c9e809b58
3 changed files with 87 additions and 28 deletions

View File

@@ -5,6 +5,9 @@ Release History
---------
* Implemented `platformio settings <http://docs.platformio.ikravets.com/en/latest/userguide/cmd_settings.html>`_ command
* Improved `platformio init <http://docs.platformio.ikravets.com/en/latest/userguide/cmd_init.html>`_ command.
Added new option ``--project-dir`` where you can specify another path to
directory where new project will be initialized (`issue #31 <https://github.com/ivankravets/platformio/issues/31>`_)
* Added *Migration Manager* which simplifies process with upgrading to a
major release
* Added *Telemetry Service* which should help us make *PlatformIO* better

View File

@@ -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.

View File

@@ -1,30 +1,58 @@
# Copyright (C) Ivan Kravets <me@ikravets.com>
# 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 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("platformio.ini"):
if not isfile(project_file):
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")
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")