Keep user changes for ".gitignore" file when re-generate/update project data

This commit is contained in:
Ivan Kravets
2016-07-05 13:18:31 +03:00
parent 9160e61ac7
commit 5cd3f9d84d
2 changed files with 30 additions and 11 deletions

View File

@ -11,6 +11,7 @@ PlatformIO 2.0
(from EEMEM directive)
* Improved project generator for `CLion IDE <http://docs.platformio.org/en/latest/ide/clion.html>`__
* Auto-remove project cache when PlatformIO is upgraded
* Keep user changes for ``.gitignore`` file when re-generate/update project data
* Fixed missed ``--boot`` flag for the firmware uploader for ATSAM3X8E
Cortex-M3 MCU based boards (Arduino Due, etc)
(`issue #710 <https://github.com/platformio/platformio/issues/710>`_)

View File

@ -16,8 +16,8 @@ import json
import os
import re
import sys
from os.path import (abspath, basename, expanduser, isdir, join, normpath,
relpath)
from os.path import (abspath, basename, expanduser, isdir, isfile, join,
normpath, relpath)
import bottle
@ -113,16 +113,18 @@ class ProjectGenerator(object):
return tpls
def generate(self):
for _relpath, _path in self.get_tpls():
tpl_dir = self.project_dir
if _relpath:
tpl_dir = join(self.project_dir, _relpath)
if not isdir(tpl_dir):
os.makedirs(tpl_dir)
for tpl_relpath, tpl_path in self.get_tpls():
dst_dir = self.project_dir
if tpl_relpath:
dst_dir = join(self.project_dir, tpl_relpath)
if not isdir(dst_dir):
os.makedirs(dst_dir)
file_name = basename(_path)[:-4]
with open(join(tpl_dir, file_name), "w") as f:
f.write(self._render_tpl(_path).encode("utf8"))
file_name = basename(tpl_path)[:-4]
self._merge_contents(
join(dst_dir, file_name),
self._render_tpl(tpl_path).encode("utf8")
)
def _render_tpl(self, tpl_path):
content = ""
@ -130,6 +132,22 @@ class ProjectGenerator(object):
content = f.read()
return bottle.template(content, **self._tplvars)
def _merge_contents(self, dst_path, contents):
file_name = basename(dst_path)
# merge .gitignore
if file_name == ".gitignore" and isfile(dst_path):
contents = [l.strip() for l in contents.split("\n") if l.strip()]
with open(dst_path) as f:
for line in f.readlines():
line = line.strip()
if line and line not in contents:
contents.append(line)
contents = "\n".join(contents)
with open(dst_path, "w") as f:
f.write(contents)
def _gather_tplvars(self):
self._tplvars.update(self.get_project_env())
self._tplvars.update(self.get_project_build_data())