Allow to initialise project with the specified embedded boards // Resolve #21

This commit is contained in:
Ivan Kravets
2014-12-29 20:22:01 +02:00
parent 82744afe58
commit af191834d1
7 changed files with 112 additions and 40 deletions

View File

@ -9,6 +9,9 @@ Release History
* Added support of *Engduino* boards for
`atmelavr <http://docs.platformio.ikravets.com/en/latest/platforms/atmelavr.html#engduino>`__
platform (`issue #38 <https://github.com/ivankravets/platformio/issues/38>`_)
* Added ``--board`` option to `platformio init <http://docs.platformio.ikravets.com/en/latest/userguide/cmd_init.html>`_
command which allows to initialise project with the specified embedded boards
(`issue #21 <https://github.com/ivankravets/platformio/issues/21>`_)
* Added `example with uploading firmware <http://docs.platformio.ikravets.com/en/latest/projectconf.html#examples>`_
via USB programmer (USBasp) for
`atmelavr <http://docs.platformio.ikravets.com/en/latest/platforms/atmelavr.html>`_

View File

@ -58,18 +58,24 @@ For example, ``[env:hello_world]``.
Options
~~~~~~~
.. _projectconf_env_platform:
``platform``
^^^^^^^^^^^^
:ref:`Platform <platforms>` type
.. _projectconf_env_framework:
``framework``
^^^^^^^^^^^^^
See ``framework`` type in *Frameworks* section of :ref:`platforms`
.. _projectconf_env_board:
``board``
^^^^^^^^^

View File

@ -7,33 +7,18 @@ Quickstart
Please read `Get Started <http://platformio.ikravets.com/#!/get-started>`_
article from the official WebSite.
First, :ref:`Install PlatformIO <installation>`.
1. :ref:`Install PlatformIO <installation>`.
Print all available development platforms for installing
2. Find board ``type`` from :ref:`platforms` (you can choose multiple board
types).
3. Initialize new PlatformIO based project with the pre-configured
environments for your boards:
.. code-block:: bash
$ platformio search
[ ... ]
$ platformio init --board=TYPE1 --board=TYPE2
Install new development platform
.. code-block:: bash
$ platformio install PLATFORM
Downloading [####################################] 100%
Unpacking [####################################] 100%
Installing .....
[ ... ]
The platform 'PLATFORM' has been successfully installed!
Initialize new PlatformIO based project
.. code-block:: bash
$ platformio init
The current working directory *** will be used for the new project.
You can specify another project directory via
`platformio init -d %PATH_TO_THE_PROJECT_DIR%` command.
@ -46,29 +31,23 @@ Initialize new PlatformIO based project
Project has been successfully initialized!
Now you can process it with `platformio run` command.
More detailed information about this command is here :ref:`cmd_init`.
Setup environments in ``platformio.ini``. For more examples go to
:ref:`Project Configuration File <projectconf_examples>`
.. code-block:: ini
# Simple and base environment
[env:mybaseenv]
platform = %INSTALLED_PLATFORM_NAME_HERE%
Process the project's environments
4. Process the project's environments.
.. code-block:: bash
$ platformio run
# if embedded project then upload firmware
# if you don't have specified `targets = upload` option for environment,
# then you can upload firmware manually with this command:
$ platformio run --target upload
# clean project
$ platformio run --target clean
If you don't have installed required platforms, then *PlatformIO* will propose
you to install them automatically.
Further examples can be found in the ``examples/`` directory in the source
distribution or `on the web <https://github.com/ivankravets/platformio/tree/develop/examples>`_.

View File

@ -32,8 +32,27 @@ Options
.. option::
--project-dir, -d
Specified path to the directory where *PlatformIO* will initialize new project.
A path to the directory where *PlatformIO* will initialise new project.
.. option::
--board, -b
If you specify board ``type`` (you can pass multiple ``--board`` options), then
*PlatformIO* will automatically generate environment for :ref:`projectconf` and
pre-fill these data:
* :ref:`projectconf_env_platform`
* :ref:`projectconf_env_framework`
* :ref:`projectconf_env_board`
The full list with pre-configured boards is available here :ref:`platforms`.
.. option::
--disable-auto-uploading
If you initialise project with the specified ``--board``, then *PlatformIO*
will create environment with enabled firmware auto-uploading. This option
allows you to disable firmware auto-uploading by default.
Examples
--------
@ -43,6 +62,7 @@ Examples
.. code-block:: bash
$ platformio init
The current working directory *** will be used for the new project.
You can specify another project directory via
`platformio init -d %PATH_TO_THE_PROJECT_DIR%` command.
@ -61,6 +81,21 @@ Examples
.. 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.
3. Initialise project for Arduino Uno
.. code-block:: bash
$ platformio init --board uno
The next files/directories will be created in ***
platformio.ini - Project Configuration File
src - a source directory. Put your source code here

View File

@ -8,15 +8,17 @@ from shutil import copyfile
import click
from platformio import app
from platformio.exception import ProjectInitialized
from platformio.util import get_source_dir
from platformio.exception import ProjectInitialized, UnknownBoard
from platformio.util import get_boards, get_source_dir
@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):
@click.option("--board", "-b", multiple=True, metavar="TYPE")
@click.option('--disable-auto-uploading', is_flag=True)
def cli(project_dir, board, disable_auto_uploading):
project_file = join(project_dir, "platformio.ini")
src_dir = join(project_dir, "src")
@ -24,6 +26,10 @@ def cli(project_dir):
if all([isfile(project_file), isdir(src_dir), isdir(lib_dir)]):
raise ProjectInitialized()
builtin_boards = set(get_boards().keys())
if board and not set(board).issubset(builtin_boards):
raise UnknownBoard(", ".join(set(board).difference(builtin_boards)))
if project_dir == getcwd():
click.secho("The current working directory", fg="yellow", nl=False)
click.secho(" %s " % project_dir, fg="cyan", nl=False)
@ -51,6 +57,8 @@ def cli(project_dir):
if not isfile(project_file):
copyfile(join(get_source_dir(), "projectconftpl.ini"),
project_file)
if board:
fill_project_envs(project_file, board, disable_auto_uploading)
click.secho(
"Project has been successfully initialized!\n"
"Now you can process it with `platformio run` command.",
@ -58,3 +66,31 @@ def cli(project_dir):
)
else:
click.secho("Aborted by user", fg="red")
def fill_project_envs(project_file, board_types, disable_auto_uploading):
builtin_boards = get_boards()
content = []
for type_ in board_types:
if type_ not in builtin_boards:
continue
else:
content.append("")
data = builtin_boards[type_]
framework = data.get("build", {}).get("core", None)
if framework in ("msp430", "lm4f"):
framework = "energia"
content.append("[env:autogen_%s]" % type_)
content.append("platform = %s" % data['platform'])
if framework:
content.append("framework = %s" % framework)
content.append("board = %s" % type_)
content.append("%stargets = upload" % "# " if disable_auto_uploading
else "")
with open(project_file, "a") as f:
f.write("\n".join(content))

View File

@ -30,6 +30,11 @@ class UnknownCLICommand(PlatformioException):
" to see all available commands")
class UnknownBoard(PlatformioException):
MESSAGE = "Unknown board type '%s'"
class UnknownPackage(PlatformioException):
MESSAGE = "Detected unknown package '%s'"

View File

@ -5,6 +5,14 @@
# http://docs.platformio.ikravets.com/en/latest/projectconf.html
#
# A sign `#` at the beginning of the line indicate a comment
# Comment lines are ignored.
# Simple and base environment
#[env:mybaseenv]
#platform = %INSTALLED_PLATFORM_NAME_HERE%
# [env:mybaseenv]
# platform = %INSTALLED_PLATFORM_NAME_HERE%
# framework =
# board =
#
# Automatic targets - enable auto-uploading
# targets = upload